Skip to content

Commit

Permalink
fix: prevent clearing existing title (#259)
Browse files Browse the repository at this point in the history
* test: added test to exemplify bug

* fix: prevent clearing existing title

* test: check for empty title

* Bump version
  • Loading branch information
cwelch5 authored Mar 28, 2017
1 parent 2cc70de commit 549b603
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-helmet",
"description": "A document head manager for React",
"version": "5.0.1",
"version": "5.0.2",
"main": "./lib/Helmet.js",
"author": "NFL <[email protected]>",
"contributors": [
Expand Down
6 changes: 3 additions & 3 deletions src/HelmetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const getTitleFromPropsList = (propsList) => {

const innermostDefaultTitle = getInnermostProperty(propsList, HELMET_PROPS.DEFAULT_TITLE);

return innermostTitle || innermostDefaultTitle || "";
return innermostTitle || innermostDefaultTitle || undefined;
};

const getOnChangeClientState = (propsList) => {
Expand Down Expand Up @@ -301,7 +301,7 @@ const handleClientStateChange = (newState) => {
};

const updateTitle = (title, attributes) => {
if (document.title !== title) {
if (typeof title === "string" && document.title !== title) {
document.title = title;
}

Expand Down Expand Up @@ -508,7 +508,7 @@ const mapStateOnServer = ({
noscriptTags,
scriptTags,
styleTags,
title,
title = "",
titleAttributes
}) => ({
base: getMethodsForTag(TAG_NAMES.BASE, baseTag, encode),
Expand Down
50 changes: 47 additions & 3 deletions test/HelmetDeclarativeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,50 @@ describe("Helmet - Declarative API", () => {
done();
});
});

it("retains existing title tag when no title tag is defined", (done) => {
headElement.innerHTML = `<title>Existing Title</title>`;

ReactDOM.render(
<Helmet>
<meta name="keywords" content="stuff" />
</Helmet>,
container
);

requestIdleCallback(() => {
expect(document.title).to.equal("Existing Title");

done();
});
});

it("clears title tag if empty title is defined", (done) => {
ReactDOM.render(
<Helmet>
<title>Existing Title</title>
<meta name="keywords" content="stuff" />
</Helmet>,
container
);

requestIdleCallback(() => {
expect(document.title).to.equal("Existing Title");

ReactDOM.render(
<Helmet>
<title>{" "}</title>
<meta name="keywords" content="stuff" />
</Helmet>,
container
);

requestIdleCallback(() => {
expect(document.title).to.equal("");
done();
});
});
});
});

describe("title attributes", () => {
Expand Down Expand Up @@ -3076,7 +3120,7 @@ describe("Helmet - Declarative API", () => {
<Helmet>
<title>Test Title</title>
<Helmet>
<title>Title you'll never see</title>
<title>Title you will never see</title>
</Helmet>
</Helmet>,
container
Expand All @@ -3102,7 +3146,7 @@ describe("Helmet - Declarative API", () => {
<Helmet>
<title>Test Title</title>
<div>
<title>Title you'll never see</title>
<title>Title you will never see</title>
</div>
</Helmet>,
container
Expand Down Expand Up @@ -3163,7 +3207,7 @@ describe("Helmet - Declarative API", () => {
<Helmet>
<title>Test Title</title>
<script>
<title>Title you'll never see</title>
<title>Title you will never see</title>
</script>
</Helmet>,
container
Expand Down

0 comments on commit 549b603

Please sign in to comment.