-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): Make <script> in Head behave correctly (#36212)
* use innerHTML since since text is unesacaped * make scripts run * eliminate multiple append calls * dev inline script test * fix problematic multiple appending * test that script run and get called once * diff nodes before reappending * add new elements and remove stale ones * remove element in prev loop * move node diffind to separate function, add unit test * slightly more performant diffing * don't init array if we will bail early anyway Co-authored-by: pieh <[email protected]> (cherry picked from commit 6612f69)
- Loading branch information
1 parent
a6ff9e9
commit 9c6988d
Showing
8 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
e2e-tests/development-runtime/cypress/integration/head-function-export/scripts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { page } from "../../../shared-data/head-function-export.js" | ||
|
||
describe("Scripts", () => { | ||
beforeEach(() => { | ||
cy.visit(page.basic).waitForRouteChange() | ||
}) | ||
|
||
// This tests that we don't append elements to the document head more than once | ||
// A script will get called more than once it that happens | ||
it(`Inline script work and get called only once`, () => { | ||
|
||
// Head export seem to be appending the tags after waitForRouteChange() | ||
// We need to find a way to make waitForRouteChange() catch Head export too | ||
cy.wait(3000) | ||
|
||
cy.window().then(win => { | ||
expect(win.__SOME_GLOBAL_TO_CHECK_CALL_COUNT__).to.equal(1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
e2e-tests/production-runtime/cypress/integration/head-function-export/scripts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { page } from "../../../shared-data/head-function-export.js" | ||
|
||
describe("Scripts", () => { | ||
beforeEach(() => { | ||
cy.visit(page.basic).waitForRouteChange() | ||
}) | ||
|
||
// This tests that we don't append elements to the document head more than once | ||
// A script will get called more than once it that happens | ||
it(`Inline script work and get called only once`, () => { | ||
|
||
// Head export seem to be appending the tags after waitForRouteChange() | ||
// We need to find a way to make waitForRouteChange() catch Head export too | ||
cy.wait(3000) | ||
|
||
cy.window().then(win => { | ||
expect(win.__SOME_GLOBAL_TO_CHECK_CALL_COUNT__).to.equal(1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { diffNodes } from "../utils" | ||
|
||
function createElement( | ||
type: string, | ||
attributes: Record<string, string> | undefined = undefined, | ||
innerHTML: string | undefined = undefined | ||
): Element { | ||
const element: Element = document.createElement(type) | ||
if (attributes) { | ||
for (const [key, value] of Object.entries(attributes)) { | ||
if (value === `string`) { | ||
element.setAttribute(key, value) | ||
} | ||
} | ||
} | ||
if (innerHTML) { | ||
element.innerHTML = innerHTML | ||
} | ||
return element | ||
} | ||
|
||
describe(`diffNodes`, () => { | ||
it(`should keep same nodes, remove nodes that were not re-created, and add new nodes`, () => { | ||
const oldNodes = [ | ||
createElement(`title`, {}, `to remove`), | ||
createElement(`script`, {}, `stable`), | ||
createElement(`script`, {}, `to remove`), | ||
] | ||
|
||
const newNodes = [ | ||
createElement(`title`, {}, `to add`), | ||
createElement(`script`, {}, `stable`), | ||
createElement(`script`, {}, `to add`), | ||
] | ||
|
||
const onStale = jest.fn() | ||
const onNew = jest.fn() | ||
|
||
diffNodes({ oldNodes, newNodes, onStale, onNew }) | ||
|
||
expect(onStale.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
<title> | ||
to remove | ||
</title>, | ||
], | ||
Array [ | ||
<script> | ||
to remove | ||
</script>, | ||
], | ||
] | ||
`) | ||
expect(onNew.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
<title> | ||
to add | ||
</title>, | ||
], | ||
Array [ | ||
<script> | ||
to add | ||
</script>, | ||
], | ||
] | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters