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

Lines changed: 8 additions & 3 deletions
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

Lines changed: 25 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy