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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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`,

0 commit comments

Comments
 (0)