Skip to content

Commit 66b8212

Browse files
author
Richard Herrera
committed
fix: Remove unexpected comma in server-rendered title
Fixes #286
1 parent 312229e commit 66b8212

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/HelmetUtils.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,13 @@ const handleClientStateChange = (newState) => {
300300
});
301301
};
302302

303+
const flattenArray = (possibleArray) => {
304+
return Array.isArray(possibleArray) ? possibleArray.join("") : possibleArray;
305+
};
306+
303307
const updateTitle = (title, attributes) => {
304308
if (typeof title !== "undefined" && document.title !== title) {
305-
document.title = Array.isArray(title) ? title.join("") : title;
309+
document.title = flattenArray(title);
306310
}
307311

308312
updateAttributes(TAG_NAMES.TITLE, attributes);
@@ -410,9 +414,10 @@ const generateElementAttributesAsString = (attributes) => Object.keys(attributes
410414

411415
const generateTitleAsString = (type, title, attributes, encode) => {
412416
const attributeString = generateElementAttributesAsString(attributes);
417+
const flattenedTitle = flattenArray(title);
413418
return attributeString
414-
? `<${type} ${HELMET_ATTRIBUTE}="true" ${attributeString}>${encodeSpecialCharacters(title, encode)}</${type}>`
415-
: `<${type} ${HELMET_ATTRIBUTE}="true">${encodeSpecialCharacters(title, encode)}</${type}>`;
419+
? `<${type} ${HELMET_ATTRIBUTE}="true" ${attributeString}>${encodeSpecialCharacters(flattenedTitle, encode)}</${type}>`
420+
: `<${type} ${HELMET_ATTRIBUTE}="true">${encodeSpecialCharacters(flattenedTitle, encode)}</${type}>`;
416421
};
417422

418423
const generateTagsAsString = (type, tags, encode) => tags.reduce((str, tag) => {

test/HelmetDeclarativeTest.js

+25
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,7 @@ describe("Helmet - Declarative API", () => {
21672167
const stringifiedTitle = `<title ${HELMET_ATTRIBUTE}="true">Dangerous &lt;script&gt; include</title>`;
21682168
const unEncodedStringifiedTitle = `<title ${HELMET_ATTRIBUTE}="true">This is text and & and '.</title>`;
21692169
const stringifiedTitleWithItemprop = `<title ${HELMET_ATTRIBUTE}="true" itemprop="name">Title with Itemprop</title>`;
2170+
const stringifiedTitleWithTitleExpression = `<title ${HELMET_ATTRIBUTE}="true">Title: Some Great Title</title>`;
21702171
const stringifiedBaseTag = `<base ${HELMET_ATTRIBUTE}="true" target="_blank" href="http://localhost/"/>`;
21712172

21722173
const stringifiedMetaTags = [
@@ -2568,6 +2569,30 @@ describe("Helmet - Declarative API", () => {
25682569
.that.equals(stringifiedTitle);
25692570
});
25702571

2572+
it("renders title and allows children containing expressions", (done) => {
2573+
const someValue = "Some Great Title";
2574+
2575+
ReactDOM.render(
2576+
<Helmet>
2577+
<title>Title: {someValue}</title>
2578+
</Helmet>,
2579+
container
2580+
);
2581+
2582+
const head = Helmet.rewind();
2583+
2584+
expect(head.title).to.exist;
2585+
expect(head.title).to.respondTo("toString");
2586+
2587+
requestIdleCallback(() => {
2588+
expect(head.title.toString())
2589+
.to.be.a("string")
2590+
.that.equals(stringifiedTitleWithTitleExpression);
2591+
2592+
done();
2593+
});
2594+
});
2595+
25712596
it("renders title with itemprop name as string", () => {
25722597
ReactDOM.render(
25732598
<Helmet>

0 commit comments

Comments
 (0)