Skip to content

Commit 5e58db0

Browse files
marvinjudeLekoArts
authored andcommitted
fix(gatsby): prevent escaping textContent for scripts (#36174)
* prevent escaping textContent for <script> data blocks * fix typo * touch up * spread attributes * add gatsby-head attribute * unescape for all script tags (cherry picked from commit d42243c)
1 parent 53bedf1 commit 5e58db0

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

packages/gatsby/cache-dir/head/head-export-handler-for-ssr.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,31 @@ export function headHandlerForSSR({
5656

5757
const seenIds = new Map()
5858
for (const node of headNodes) {
59-
const { rawTagName, attributes } = node
60-
const id = attributes.id
59+
const { rawTagName } = node
60+
const id = node.attributes.id
6161

6262
if (!VALID_NODE_NAMES.includes(rawTagName)) {
6363
warnForInvalidTags(rawTagName)
6464
} else {
65-
const element = createElement(
66-
rawTagName,
67-
{
68-
...attributes,
69-
"data-gatsby-head": true,
70-
},
71-
node.childNodes[0]?.textContent
72-
)
65+
let element
66+
const attributes = { ...node.attributes, "data-gatsby-head": true }
67+
if (rawTagName === `script`) {
68+
element = (
69+
<script
70+
{...attributes}
71+
dangerouslySetInnerHTML={{
72+
__html: node.text,
73+
}}
74+
/>
75+
)
76+
} else {
77+
element = (
78+
<node.rawTagName {...attributes}>
79+
{node.childNodes[0]?.textContent}
80+
</node.rawTagName>
81+
)
82+
}
83+
7384
if (id) {
7485
if (!seenIds.has(id)) {
7586
validHeadNodes.push(element)

0 commit comments

Comments
 (0)