-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gatsby-script): Script component #35403
Merged
Merged
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
3442bb4
feat(gatsby-script): Package scaffolding
tyhopp 0c2cb3b
test(gatsby-script): Unit tests
tyhopp a305190
chore(gatsby-script): Adjust build scripts, typegen
tyhopp a3c15e6
chore(gatsby-script): Introduce example site
tyhopp 99a4e70
chore(gatsby-script): Update yarn lock so CI runs
tyhopp 4863c1c
chore(gatsby-script): Remove unnecessary config files
tyhopp 04b4dec
test(gatsby-script): e2e tests (#35416)
tyhopp 937fdae
chore(gatsby-script): Remove example site
tyhopp 62e7c14
feat(gatsby-script): Inline scripts (#35443)
tyhopp fef9d3b
feat(gatsby-script): Normal attributes (#35445)
tyhopp baa1b6e
feat(gatsby-script): On load callback, cache (#35464)
tyhopp 14fea1f
refactor(gatsby-script): Pre-hydrate scripts with sources will not su…
tyhopp a8f8ded
feat(gatsby-script): On error callback (#35466)
tyhopp 62953a0
refactor(gatsby-script): Remove pre-hydrate strategy
tyhopp 48cf273
refactor(gatsby-script): Return null instead of fragment
tyhopp dc1ded4
chore(gatsby-script): Fix typegen
tyhopp d715b23
feat(gatsby-script): Allow enum or string strategy type
tyhopp e9c498e
chore(gatsby-script): Install latest canary on e2e test site
tyhopp 450efa5
test(gatsby-script): Fix unit tests
tyhopp a0c245c
feat(gatsby-script): Off main thread implementation (#35490)
tyhopp 0751d79
chore(docs): Gatsby script docs (#35509)
tyhopp 8e4053f
Bump babel-preset-gatsby-package
tyhopp 52f07f9
Publish gatsby-script next version bump to fix bootstrap
tyhopp 3d705c3
Update load plugins snapshots
tyhopp 31a6923
Fix lockfile
tyhopp fe2f141
Update load plugins snapshots once more
tyhopp a1952de
Fix strange Cypress ts issue by not importing from gatsby
tyhopp 6541f67
Remove docs to be added in separate PR prior to release
tyhopp d3add7b
Fix test description
tyhopp 6e5ac42
Fix incorrect escaping of inline off-main-thread scripts
tyhopp 7ad4d0a
Refactor forward collection logic
tyhopp ef69210
Bump gatsby-script
tyhopp f4ea320
feat(gatsby-script): Default proxy safelist
tyhopp b93e543
Gatsby serve support
tyhopp bf0cf52
Remove wildcard
tyhopp 08fb41e
Try colon syntax for rest of url
tyhopp 97bedb1
Pivot to absolute URLs
tyhopp 8ccccb9
Rename config key to match absolute url approach
tyhopp 957f56d
Remove unused import
tyhopp a1d43f1
Remove comment
tyhopp 2e4e189
Remove CORS header from proxy
tyhopp 3c7d7d3
Strict URL matching in gatsby serve
tyhopp 5db414a
make sure partytown's wrapRootElement runs as last one, to be able to…
pieh 9a14b96
Bump gatsby-script
tyhopp 4575a3b
Update load plugins snapshots
tyhopp a8ab994
feat(gatsby): Support for off-main-thread in develop (#35648)
tyhopp 6919d9e
test(gatsby-script): Migrate e2e test to production and develop runti…
tyhopp 6420b54
test(gatsby-script): SSR and browser APIs (#35664)
tyhopp 302eb4e
update snapshots
pieh caff1f4
test(gatsby-script): Off main thread e2e (#35665)
tyhopp 182b6c3
Make partytown a dev dep in gatsby-script
tyhopp 3cf979f
Remove gatsby-script e2e test suite that is now in dev/prod runtime
tyhopp bcfef18
Resolve conflicts
tyhopp bfb2178
Merge branch 'master' into feat-script-component
tyhopp cfd280f
feat(gatby-script): Shim requestIdleCallback (#35611)
marvinjude a8711ab
Remove job
tyhopp b18cb0c
Remove incorrect type
tyhopp d099953
Request idle callback shim test timeout
tyhopp 4d7d8a1
Add only Partytown key we handle to type
tyhopp 8e55e3a
Add warning for no collect script func available
tyhopp 3d246dd
Use shortened URL
tyhopp 38e3707
Update gatsby-script readme
tyhopp 2ff6d42
Fix offline tests
tyhopp a57cb15
feat(gatsby-script): Apply crossorigin anonymous to off-main-thread s…
tyhopp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -76,3 +76,6 @@ cypress/videos | |
__history__.json | ||
|
||
src/gatsby-types.d.ts | ||
/test-results/ | ||
/playwright-report/ | ||
/playwright/.cache/ |
195 changes: 195 additions & 0 deletions
195
...sts/development-runtime/cypress/integration/gatsby-script/gatsby-script-inline-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,195 @@ | ||
import { inlineScript } from "../../../gatsby-script-scripts" | ||
import { resourceRecord, markRecord } from "../../../gatsby-script-records" | ||
|
||
const page = { | ||
target: `/gatsby-script-inline-scripts/`, | ||
navigation: `/gatsby-script-navigation/`, | ||
} | ||
|
||
const typesOfInlineScripts = [ | ||
{ | ||
descriptor: `dangerouslySetInnerHTML`, | ||
inlineScriptType: inlineScript.dangerouslySet, | ||
}, | ||
{ | ||
descriptor: `template literals`, | ||
inlineScriptType: inlineScript.templateLiteral, | ||
}, | ||
] | ||
|
||
/** | ||
* Normally we would duplicate the tests so they're flatter and easier to debug, | ||
* but since the test count grew and the cases are exactly the same we'll iterate. | ||
*/ | ||
|
||
for (const { descriptor, inlineScriptType } of typesOfInlineScripts) { | ||
describe(`inline scripts set via ${descriptor}`, () => { | ||
describe(`using the post-hydrate strategy`, () => { | ||
it(`should execute successfully`, () => { | ||
cy.visit(page.target) | ||
|
||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`success`, | ||
true | ||
).should(`equal`, `true`) | ||
}) | ||
|
||
it(`should load after the framework bundle has loaded`, () => { | ||
cy.visit(page.target) | ||
|
||
// Assert framework is loaded before inline script is executed | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
markRecord.executeStart | ||
).then(dangerouslySetExecuteStart => { | ||
cy.getRecord(`framework`, resourceRecord.responseEnd).should( | ||
`be.lessThan`, | ||
dangerouslySetExecuteStart | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`using the idle strategy`, () => { | ||
it(`should execute successfully`, () => { | ||
cy.visit(page.target) | ||
|
||
cy.getRecord(`idle-${inlineScriptType}`, `success`, true).should( | ||
`equal`, | ||
`true` | ||
) | ||
}) | ||
|
||
it(`should load after other strategies`, () => { | ||
cy.visit(page.target) | ||
|
||
cy.getRecord(`idle-${inlineScriptType}`, markRecord.executeStart).then( | ||
dangerouslySetExecuteStart => { | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
markRecord.executeStart | ||
).should(`be.lessThan`, dangerouslySetExecuteStart) | ||
} | ||
) | ||
}) | ||
}) | ||
|
||
describe(`when navigation occurs`, () => { | ||
it(`should load only once on initial page load`, () => { | ||
cy.visit(page.target) | ||
|
||
cy.get(`table[id=script-mark-records] tbody`) | ||
.children() | ||
.should(`have.length`, 4) | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`strategy`, | ||
true | ||
).should(`equal`, `post-hydrate`) | ||
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should( | ||
`equal`, | ||
`idle` | ||
) | ||
}) | ||
|
||
it(`should load only once after the page is refreshed`, () => { | ||
cy.visit(page.target).waitForRouteChange() | ||
cy.reload().url().should(`contain`, page.target) | ||
|
||
cy.get(`table[id=script-mark-records] tbody`) | ||
.children() | ||
.should(`have.length`, 4) | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`strategy`, | ||
true | ||
).should(`equal`, `post-hydrate`) | ||
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should( | ||
`equal`, | ||
`idle` | ||
) | ||
}) | ||
|
||
it(`should load only once after anchor link navigation`, () => { | ||
cy.visit(page.target) | ||
cy.get(`a[id=anchor-link-back-to-index]`).click() | ||
cy.url().should(`contain`, page.navigation) | ||
cy.get(`a[href="${page.target}"][id=anchor-link]`).click() | ||
|
||
cy.get(`table[id=script-mark-records] tbody`) | ||
.children() | ||
.should(`have.length`, 4) | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`strategy`, | ||
true | ||
).should(`equal`, `post-hydrate`) | ||
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should( | ||
`equal`, | ||
`idle` | ||
) | ||
}) | ||
|
||
it(`should load only once if the page is revisited via browser back/forward buttons after anchor link navigation`, () => { | ||
cy.visit(page.navigation).waitForRouteChange() | ||
cy.get(`a[href="${page.target}"][id=anchor-link]`).click() | ||
cy.get(`table[id=script-mark-records] tbody`) // Make sure history has time to change | ||
cy.go(`back`) | ||
cy.go(`forward`) | ||
|
||
cy.get(`table[id=script-mark-records] tbody`) | ||
.children() | ||
.should(`have.length`, 4) | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`strategy`, | ||
true | ||
).should(`equal`, `post-hydrate`) | ||
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should( | ||
`equal`, | ||
`idle` | ||
) | ||
}) | ||
|
||
it(`should load only once after Gatsby link navigation`, () => { | ||
cy.visit(page.target) | ||
cy.get(`a[id=gatsby-link-back-to-index]`).click() | ||
cy.get(`a[href="${page.target}"][id=gatsby-link]`).click() | ||
|
||
cy.get(`table[id=script-mark-records] tbody`) | ||
.children() | ||
.should(`have.length`, 4) | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`strategy`, | ||
true | ||
).should(`equal`, `post-hydrate`) | ||
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should( | ||
`equal`, | ||
`idle` | ||
) | ||
}) | ||
|
||
it(`should load only once if the page is revisited via browser back/forward buttons after Gatsby link navigation`, () => { | ||
cy.visit(page.navigation) | ||
cy.get(`a[href="${page.target}"][id=gatsby-link]`).click() | ||
cy.go(`back`) | ||
cy.go(`forward`) | ||
|
||
cy.get(`table[id=script-mark-records] tbody`) | ||
.children() | ||
.should(`have.length`, 4) | ||
cy.getRecord( | ||
`post-hydrate-${inlineScriptType}`, | ||
`strategy`, | ||
true | ||
).should(`equal`, `post-hydrate`) | ||
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should( | ||
`equal`, | ||
`idle` | ||
) | ||
}) | ||
}) | ||
}) | ||
} |
166 changes: 166 additions & 0 deletions
166
...velopment-runtime/cypress/integration/gatsby-script/gatsby-script-scripts-with-sources.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,166 @@ | ||
import { script } from "../../../gatsby-script-scripts" | ||
import { resourceRecord } from "../../../gatsby-script-records" | ||
|
||
const page = { | ||
target: `/gatsby-script-scripts-with-sources/`, | ||
navigation: `/gatsby-script-navigation/`, | ||
} | ||
|
||
describe(`scripts with sources`, () => { | ||
describe(`using the post-hydrate strategy`, () => { | ||
it(`should load successfully`, () => { | ||
cy.visit(page.target) | ||
cy.getRecord(script.three, `success`, true).should(`equal`, `true`) | ||
}) | ||
|
||
it(`should load after the framework bundle has loaded`, () => { | ||
cy.visit(page.target) | ||
|
||
// Assert framework is loaded before three starts loading | ||
cy.getRecord(script.three, resourceRecord.fetchStart).then( | ||
threeFetchStart => { | ||
cy.getRecord(`framework`, resourceRecord.responseEnd).should( | ||
`be.lessThan`, | ||
threeFetchStart | ||
) | ||
} | ||
) | ||
}) | ||
|
||
it(`should call an on load callback once the script has loaded`, () => { | ||
cy.visit(page.target) | ||
cy.getRecord(script.three, resourceRecord.responseEnd).then(() => { | ||
cy.get(`[data-on-load-result=post-hydrate]`) | ||
}) | ||
}) | ||
|
||
it(`should call an on error callback if an error occurred`, () => { | ||
cy.visit(page.target) | ||
cy.get(`[data-on-error-result=post-hydrate]`) | ||
}) | ||
}) | ||
|
||
describe(`using the idle strategy`, () => { | ||
it(`should load successfully`, () => { | ||
cy.visit(page.target) | ||
cy.getRecord(script.marked, `success`, true).should(`equal`, `true`) | ||
}) | ||
|
||
it(`should load after other strategies`, () => { | ||
cy.visit(page.target) | ||
|
||
cy.getRecord(script.marked, resourceRecord.fetchStart).then( | ||
markedFetchStart => { | ||
cy.getRecord(script.three, resourceRecord.fetchStart).should( | ||
`be.lessThan`, | ||
markedFetchStart | ||
) | ||
} | ||
) | ||
}) | ||
|
||
it(`should call an on load callback once the script has loaded`, () => { | ||
cy.visit(page.target) | ||
cy.getRecord(script.marked, resourceRecord.responseEnd).then(() => { | ||
cy.get(`[data-on-load-result=idle]`) | ||
}) | ||
}) | ||
|
||
it(`should call an on error callback if an error occurred`, () => { | ||
cy.visit(page.target) | ||
cy.get(`[data-on-error-result=idle]`) | ||
}) | ||
}) | ||
|
||
describe(`when navigation occurs`, () => { | ||
it(`should load only once on initial page load`, () => { | ||
cy.visit(page.target) | ||
|
||
cy.get(`table[id=script-resource-records] tbody`) | ||
.children() | ||
.should(`have.length`, 5) | ||
cy.getRecord(script.three, `strategy`, true).should( | ||
`equal`, | ||
`post-hydrate` | ||
) | ||
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`) | ||
}) | ||
|
||
it(`should load only once after the page is refreshed`, () => { | ||
cy.visit(page.target) | ||
cy.reload() | ||
|
||
cy.get(`table[id=script-resource-records] tbody`) | ||
.children() | ||
.should(`have.length`, 5) | ||
cy.getRecord(script.three, `strategy`, true).should( | ||
`equal`, | ||
`post-hydrate` | ||
) | ||
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`) | ||
}) | ||
|
||
it(`should load only once after anchor link navigation`, () => { | ||
cy.visit(page.target) | ||
cy.get(`a[id=anchor-link-back-to-index]`).click() | ||
cy.url().should(`contain`, page.navigation) | ||
cy.get(`a[href="${page.target}"][id=anchor-link]`).click() | ||
|
||
cy.get(`table[id=script-resource-records] tbody`) | ||
.children() | ||
.should(`have.length`, 5) | ||
cy.getRecord(script.three, `strategy`, true).should( | ||
`equal`, | ||
`post-hydrate` | ||
) | ||
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`) | ||
}) | ||
|
||
it(`should load only once if the page is revisited via browser back/forward buttons after anchor link navigation`, () => { | ||
cy.visit(page.navigation) | ||
cy.get(`a[href="${page.target}"][id=anchor-link]`).click() | ||
cy.go(`back`) | ||
cy.go(`forward`) | ||
|
||
cy.get(`table[id=script-resource-records] tbody`) | ||
.children() | ||
.should(`have.length`, 5) | ||
cy.getRecord(script.three, `strategy`, true).should( | ||
`equal`, | ||
`post-hydrate` | ||
) | ||
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`) | ||
}) | ||
|
||
it(`should load only once after Gatsby link navigation`, () => { | ||
cy.visit(page.target) | ||
cy.get(`a[id=gatsby-link-back-to-index]`).click() | ||
cy.get(`a[href="${page.target}"][id=gatsby-link]`).click() | ||
|
||
cy.get(`table[id=script-resource-records] tbody`) | ||
.children() | ||
.should(`have.length`, 5) | ||
cy.getRecord(script.three, `strategy`, true).should( | ||
`equal`, | ||
`post-hydrate` | ||
) | ||
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`) | ||
}) | ||
|
||
it(`should load only once if the page is revisited via browser back/forward buttons after Gatsby link navigation`, () => { | ||
cy.visit(page.navigation) | ||
cy.get(`a[href="${page.target}"][id=gatsby-link]`).click() | ||
cy.go(`back`) | ||
cy.go(`forward`) | ||
|
||
cy.get(`table[id=script-resource-records] tbody`) | ||
.children() | ||
.should(`have.length`, 5) | ||
cy.getRecord(script.three, `strategy`, true).should( | ||
`equal`, | ||
`post-hydrate` | ||
) | ||
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This counts the rows of the table, and this is how you know how many scripts got loaded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part assertion and I think part flake potential removal as this will (potentially?) wait for expected amount of scripts to report their status before doing assertion on those individual ones