Skip to content

Commit

Permalink
fix(gatsby): Fix truncation of childNode during static HTML generation (
Browse files Browse the repository at this point in the history
#36704) (#36717)

* use innertext of node and not first node text content

* use node "text"

* add test

* remove logs

* use textcontext

(cherry picked from commit ad1148e)

Co-authored-by: Jude Agboola <marvinjudehk@gmail.com>
  • Loading branch information
gatsbybot and marvinjude committed Sep 30, 2022
1 parent 6eefacf commit f43c6bb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe(`Head function export SSR'ed HTML output`, () => {
const { base, title, meta, noscript, style, link, jsonLD } = getNodes(dom)

expect(base.attributes.href).toEqual(data.static.base)
expect(title.text).toEqual(data.static.title)
//Intentionally duplicate the title to test that we don't strip out multiple text nodes
expect(title.text).toEqual(`${data.static.title} ${data.static.title}`)
expect(meta.attributes.content).toEqual(data.static.meta)
expect(noscript.text).toEqual(data.static.noscript)
expect(style.text).toContain(data.static.style)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function Head() {
return (
<>
<base data-testid="base" href={base} />
<title data-testid="title">{title}</title>
<title data-testid="title">{title} {title}</title>
<meta data-testid="meta" name="author" content={meta} />
<noscript data-testid="noscript">{noscript}</noscript>
<style data-testid="style">
Expand Down
13 changes: 8 additions & 5 deletions packages/gatsby/cache-dir/head/head-export-handler-for-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ export function headHandlerForSSR({
/>
)
} else {
element = (
<node.rawTagName {...attributes}>
{node.childNodes[0]?.textContent}
</node.rawTagName>
)
element =
node.textContent.length > 0 ? (
<node.rawTagName {...attributes}>
{node.textContent}
</node.rawTagName>
) : (
<node.rawTagName {...attributes} />
)
}

if (id) {
Expand Down

0 comments on commit f43c6bb

Please sign in to comment.