Skip to content

Commit 62d2a49

Browse files
gatsbybotLekoArts
andauthored
fix(gatsby): Allow script in Gatsby Head (#36136) (#36137)
Co-authored-by: Lennart <[email protected]>
1 parent 41f88f1 commit 62d2a49

File tree

18 files changed

+45
-25
lines changed

18 files changed

+45
-25
lines changed

e2e-tests/development-runtime/cypress/integration/head-function-export/html-insertion.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe(`Head function export html insertion`, () => {
1515
cy.getTestElement(`link`)
1616
.invoke(`attr`, `href`)
1717
.should(`equal`, data.static.link)
18+
cy.getTestElement(`jsonLD`).should(`have.text`, data.static.jsonLD)
1819
})
1920

2021
it(`should work with data from a page query`, () => {
@@ -47,6 +48,7 @@ describe(`Head function export html insertion`, () => {
4748
cy.getTestElement(`link`)
4849
.invoke(`attr`, `href`)
4950
.should(`equal`, data.static.link)
51+
cy.getTestElement(`jsonLD`).should(`have.text`, data.static.jsonLD)
5052
})
5153

5254
it(`should work when an imported Head component with queried data is used`, () => {

e2e-tests/development-runtime/cypress/integration/head-function-export/navigation.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe(`Head function export behavior during CSR navigation (Gatsby Link)`, ()
4242
cy.getTestElement(`link`)
4343
.invoke(`attr`, `href`)
4444
.should(`equal`, data.static.link)
45+
cy.getTestElement(`jsonLD`).should(`have.text`, data.static.jsonLD)
4546

4647
cy.getTestElement(`navigate-to-page-without-head-export`)
4748
.click()
@@ -53,6 +54,7 @@ describe(`Head function export behavior during CSR navigation (Gatsby Link)`, ()
5354
cy.getTestElement(`noscript`).should(`not.exist`)
5455
cy.getTestElement(`style`).should(`not.exist`)
5556
cy.getTestElement(`link`).should(`not.exist`)
57+
cy.getTestElement(`jsonLD`).should(`not.exist`)
5658
})
5759

5860
/**

e2e-tests/development-runtime/cypress/integration/head-function-export/warnings.js

-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,4 @@ describe(`Head function export should warn`, () => {
1818
)}`
1919
)
2020
})
21-
22-
it(`for scripts that could use the script component`, () => {
23-
cy.get(`@consoleWarn`).should(
24-
`be.calledWith`,
25-
`Do not add scripts here. Please use the <Script> component in your page template instead. For more info see: https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-script/`
26-
)
27-
})
2821
})

e2e-tests/development-runtime/shared-data/head-function-export.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const data = {
2222
style: `rebeccapurple`,
2323
link: `/used-by-head-function-export-basic.css`,
2424
extraMeta: `Extra meta tag that should be removed during navigation`,
25+
jsonLD: `{"@context":"https://schema.org","@type":"Organization","url":"https://www.spookytech.com","name":"Spookytechnologies","contactPoint":{"@type":"ContactPoint","telephone":"+5-601-785-8543","contactType":"CustomerSupport"}}`
2526
},
2627
queried: {
2728
base: `http://localhost:8000`,

e2e-tests/development-runtime/src/components/head-function-export.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function HeadComponent({ children }) {
4343
}
4444

4545
function Head() {
46-
const { base, title, meta, noscript, style, link } = data.static
46+
const { base, title, meta, noscript, style, link, jsonLD } = data.static
4747

4848
return (
4949
<>
@@ -59,6 +59,9 @@ function Head() {
5959
`}
6060
</style>
6161
<link data-testid="link" href={link} rel="stylesheet" />
62+
<script data-testid="jsonLD" type="application/ld+json">
63+
{jsonLD}
64+
</script>
6265
</>
6366
)
6467
}

e2e-tests/development-runtime/src/pages/head-function-export/basic.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function Head() {
2828
style,
2929
link,
3030
extraMeta,
31+
jsonLD
3132
} = data.static
3233

3334
return (
@@ -50,6 +51,9 @@ export function Head() {
5051
name="extra-meta-for-hot-reloading"
5152
content="%SOME_EXTRA_META%"
5253
/>
54+
<script data-testid="jsonLD" type="application/ld+json">
55+
{jsonLD}
56+
</script>
5357
</>
5458
)
5559
}

e2e-tests/development-runtime/src/pages/head-function-export/warnings.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function Head() {
1414
return (
1515
<>
1616
<h1 data-testid="h1">hello</h1>
17-
<script data-testid="script">{`console.log('hello')`}</script>
1817
</>
1918
)
2019
}

e2e-tests/production-runtime/cypress/integration/head-function-export/html-insertion.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe(`Head function export html insertion`, () => {
1515
cy.getTestElement(`link`)
1616
.invoke(`attr`, `href`)
1717
.should(`equal`, data.static.link)
18+
cy.getTestElement(`jsonLD`).should(`have.text`, data.static.jsonLD)
1819
})
1920

2021
it(`should work with data from a page query`, () => {
@@ -47,6 +48,7 @@ describe(`Head function export html insertion`, () => {
4748
cy.getTestElement(`link`)
4849
.invoke(`attr`, `href`)
4950
.should(`equal`, data.static.link)
51+
cy.getTestElement(`jsonLD`).should(`have.text`, data.static.jsonLD)
5052
})
5153

5254
it(`should work when an imported Head component with queried data is used`, () => {

e2e-tests/production-runtime/cypress/integration/head-function-export/navigation.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe(`Head function export behavior during CSR navigation (Gatsby Link)`, ()
4242
cy.getTestElement(`link`)
4343
.invoke(`attr`, `href`)
4444
.should(`equal`, data.static.link)
45+
cy.getTestElement(`jsonLD`).should(`have.text`, data.static.jsonLD)
4546

4647
cy.getTestElement(`navigate-to-page-without-head-export`)
4748
.click()
@@ -53,6 +54,7 @@ describe(`Head function export behavior during CSR navigation (Gatsby Link)`, ()
5354
cy.getTestElement(`noscript`).should(`not.exist`)
5455
cy.getTestElement(`style`).should(`not.exist`)
5556
cy.getTestElement(`link`).should(`not.exist`)
57+
cy.getTestElement(`jsonLD`).should(`not.exist`)
5658
})
5759

5860
/**

e2e-tests/production-runtime/shared-data/head-function-export.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const data = {
2222
style: `rebeccapurple`,
2323
link: `/used-by-head-function-export-basic.css`,
2424
extraMeta: `Extra meta tag that should be removed during navigation`,
25+
jsonLD: `{"@context":"https://schema.org","@type":"Organization","url":"https://www.spookytech.com","name":"Spookytechnologies","contactPoint":{"@type":"ContactPoint","telephone":"+5-601-785-8543","contactType":"CustomerSupport"}}`
2526
},
2627
queried: {
2728
base: `http://localhost:9000`,

e2e-tests/production-runtime/src/components/head-function-export.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function HeadComponent({ children }) {
4343
}
4444

4545
function Head() {
46-
const { base, title, meta, noscript, style, link } = data.static
46+
const { base, title, meta, noscript, style, link, jsonLD } = data.static
4747

4848
return (
4949
<>
@@ -59,6 +59,9 @@ function Head() {
5959
`}
6060
</style>
6161
<link data-testid="link" href={link} rel="stylesheet" />
62+
<script data-testid="jsonLD" type="application/ld+json">
63+
{jsonLD}
64+
</script>
6265
</>
6366
)
6467
}

e2e-tests/production-runtime/src/pages/head-function-export/basic.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function HeadFunctionExportBasic() {
1818
}
1919

2020
export function Head() {
21-
const { base, title, meta, noscript, style, link, extraMeta } = data.static
21+
const { base, title, meta, noscript, style, link, extraMeta, jsonLD } = data.static
2222

2323
return (
2424
<>
@@ -34,8 +34,10 @@ export function Head() {
3434
`}
3535
</style>
3636
<link data-testid="link" href={link} rel="stylesheet" />
37-
3837
<meta data-testid="extra-meta" name="extra-meta" content={extraMeta} />
38+
<script data-testid="jsonLD" type="application/ld+json">
39+
{jsonLD}
40+
</script>
3941
</>
4042
)
4143
}

integration-tests/head-function-export/__tests__/ssr-html-output.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ function getNodes(dom) {
1818
const noscript = dom.querySelector(`[data-testid=noscript]`)
1919
const style = dom.querySelector(`[data-testid=style]`)
2020
const link = dom.querySelector(`[data-testid=link]`)
21-
return { base, title, meta, noscript, style, link }
21+
const jsonLD = dom.querySelector(`[data-testid=jsonLD]`)
22+
return { base, title, meta, noscript, style, link, jsonLD }
2223
}
2324

2425
describe(`Head function export SSR'ed HTML output`, () => {
2526
it(`should work with static data`, () => {
2627
const html = readFileSync(`${publicDir}${page.basic}/index.html`)
2728
const dom = parse(html)
28-
const { base, title, meta, noscript, style, link } = getNodes(dom)
29+
const { base, title, meta, noscript, style, link, jsonLD } = getNodes(dom)
2930

3031
expect(base.attributes.href).toEqual(data.static.base)
3132
expect(title.text).toEqual(data.static.title)
3233
expect(meta.attributes.content).toEqual(data.static.meta)
3334
expect(noscript.text).toEqual(data.static.noscript)
3435
expect(style.text).toContain(data.static.style)
3536
expect(link.attributes.href).toEqual(data.static.link)
37+
expect(jsonLD.text).toEqual(data.static.jsonLD)
3638
})
3739

3840
it(`should work with data from a page query`, () => {
@@ -51,14 +53,15 @@ describe(`Head function export SSR'ed HTML output`, () => {
5153
it(`should work when a Head function with static data is re-exported from the page`, () => {
5254
const html = readFileSync(`${publicDir}${page.reExport}/index.html`)
5355
const dom = parse(html)
54-
const { base, title, meta, noscript, style, link } = getNodes(dom)
56+
const { base, title, meta, noscript, style, link, jsonLD } = getNodes(dom)
5557

5658
expect(base.attributes.href).toEqual(data.static.base)
5759
expect(title.text).toEqual(data.static.title)
5860
expect(meta.attributes.content).toEqual(data.static.meta)
5961
expect(noscript.text).toEqual(data.static.noscript)
6062
expect(style.text).toContain(data.static.style)
6163
expect(link.attributes.href).toEqual(data.static.link)
64+
expect(jsonLD.text).toEqual(data.static.jsonLD)
6265
})
6366

6467
it(`should work when an imported Head component with queried data is used`, () => {

integration-tests/head-function-export/shared-data/head-function-export.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const data = {
1717
noscript: `You take romance - I will take Jell-O!`,
1818
style: `rebeccapurple`,
1919
link: `/used-by-head-function-export-basic.css`,
20+
jsonLD: `{"@context":"https://schema.org","@type":"Organization","url":"https://www.spookytech.com","name":"Spookytechnologies","contactPoint":{"@type":"ContactPoint","telephone":"+5-601-785-8543","contactType":"CustomerSupport"}}`
2021
},
2122
queried: {
2223
base: `http://localhost:8000`,

integration-tests/head-function-export/src/components/head-function-export.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function HeadComponent({ children }) {
4343
}
4444

4545
function Head() {
46-
const { base, title, meta, noscript, style, link } = data.static
46+
const { base, title, meta, noscript, style, link, jsonLD } = data.static
4747

4848
return (
4949
<>
@@ -59,6 +59,9 @@ function Head() {
5959
`}
6060
</style>
6161
<link data-testid="link" href={link} rel="stylesheet" />
62+
<script data-testid="jsonLD" type="application/ld+json">
63+
{jsonLD}
64+
</script>
6265
</>
6366
)
6467
}

integration-tests/head-function-export/src/pages/head-function-export/basic.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function HeadFunctionExportBasic() {
1515
}
1616

1717
export function Head() {
18-
const { base, title, meta, noscript, style, link } = data.static
18+
const { base, title, meta, noscript, style, link, jsonLD } = data.static
1919

2020
return (
2121
<>
@@ -31,6 +31,9 @@ export function Head() {
3131
`}
3232
</style>
3333
<link data-testid="link" href={link} rel="stylesheet" />
34+
<script data-testid="jsonLD" type="application/ld+json">
35+
{jsonLD}
36+
</script>
3437
</>
3538
)
3639
}

packages/gatsby/cache-dir/head/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export const VALID_NODE_NAMES = [
55
`title`,
66
`base`,
77
`noscript`,
8+
`script`,
89
]

packages/gatsby/cache-dir/head/utils.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,15 @@ if (process.env.NODE_ENV !== `production`) {
3939
}
4040
}
4141

42-
export { warnOnce }
43-
4442
/**
4543
* Warn for invalid tags in head.
4644
* @param {string} tagName
4745
*/
4846
export function warnForInvalidTags(tagName) {
4947
if (process.env.NODE_ENV !== `production`) {
50-
const warning =
51-
tagName !== `script`
52-
? `<${tagName}> is not a valid head element. Please use one of the following: ${VALID_NODE_NAMES.join(
53-
`, `
54-
)}`
55-
: `Do not add scripts here. Please use the <Script> component in your page template instead. For more info see: https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-script/`
48+
const warning = `<${tagName}> is not a valid head element. Please use one of the following: ${VALID_NODE_NAMES.join(
49+
`, `
50+
)}`
5651

5752
warnOnce(warning)
5853
}

0 commit comments

Comments
 (0)