Skip to content
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 64 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
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 Apr 8, 2022
0c2cb3b
test(gatsby-script): Unit tests
tyhopp Apr 8, 2022
a305190
chore(gatsby-script): Adjust build scripts, typegen
tyhopp Apr 11, 2022
a3c15e6
chore(gatsby-script): Introduce example site
tyhopp Apr 11, 2022
99a4e70
chore(gatsby-script): Update yarn lock so CI runs
tyhopp Apr 12, 2022
4863c1c
chore(gatsby-script): Remove unnecessary config files
tyhopp Apr 12, 2022
04b4dec
test(gatsby-script): e2e tests (#35416)
tyhopp Apr 15, 2022
937fdae
chore(gatsby-script): Remove example site
tyhopp Apr 15, 2022
62e7c14
feat(gatsby-script): Inline scripts (#35443)
tyhopp Apr 19, 2022
fef9d3b
feat(gatsby-script): Normal attributes (#35445)
tyhopp Apr 19, 2022
baa1b6e
feat(gatsby-script): On load callback, cache (#35464)
tyhopp Apr 21, 2022
14fea1f
refactor(gatsby-script): Pre-hydrate scripts with sources will not su…
tyhopp Apr 21, 2022
a8f8ded
feat(gatsby-script): On error callback (#35466)
tyhopp Apr 21, 2022
62953a0
refactor(gatsby-script): Remove pre-hydrate strategy
tyhopp Apr 22, 2022
48cf273
refactor(gatsby-script): Return null instead of fragment
tyhopp Apr 22, 2022
dc1ded4
chore(gatsby-script): Fix typegen
tyhopp Apr 22, 2022
d715b23
feat(gatsby-script): Allow enum or string strategy type
tyhopp Apr 22, 2022
e9c498e
chore(gatsby-script): Install latest canary on e2e test site
tyhopp Apr 22, 2022
450efa5
test(gatsby-script): Fix unit tests
tyhopp Apr 22, 2022
a0c245c
feat(gatsby-script): Off main thread implementation (#35490)
tyhopp May 9, 2022
0751d79
chore(docs): Gatsby script docs (#35509)
tyhopp May 9, 2022
8e4053f
Bump babel-preset-gatsby-package
tyhopp May 10, 2022
52f07f9
Publish gatsby-script next version bump to fix bootstrap
tyhopp May 10, 2022
3d705c3
Update load plugins snapshots
tyhopp May 10, 2022
31a6923
Fix lockfile
tyhopp May 10, 2022
fe2f141
Update load plugins snapshots once more
tyhopp May 10, 2022
a1952de
Fix strange Cypress ts issue by not importing from gatsby
tyhopp May 10, 2022
6541f67
Remove docs to be added in separate PR prior to release
tyhopp May 12, 2022
d3add7b
Fix test description
tyhopp May 12, 2022
6e5ac42
Fix incorrect escaping of inline off-main-thread scripts
tyhopp May 12, 2022
7ad4d0a
Refactor forward collection logic
tyhopp May 12, 2022
ef69210
Bump gatsby-script
tyhopp May 12, 2022
f4ea320
feat(gatsby-script): Default proxy safelist
tyhopp May 11, 2022
b93e543
Gatsby serve support
tyhopp May 11, 2022
bf0cf52
Remove wildcard
tyhopp May 11, 2022
08fb41e
Try colon syntax for rest of url
tyhopp May 11, 2022
97bedb1
Pivot to absolute URLs
tyhopp May 11, 2022
8ccccb9
Rename config key to match absolute url approach
tyhopp May 11, 2022
957f56d
Remove unused import
tyhopp May 11, 2022
a1d43f1
Remove comment
tyhopp May 11, 2022
2e4e189
Remove CORS header from proxy
tyhopp May 12, 2022
3c7d7d3
Strict URL matching in gatsby serve
tyhopp May 12, 2022
5db414a
make sure partytown's wrapRootElement runs as last one, to be able to…
pieh May 12, 2022
9a14b96
Bump gatsby-script
tyhopp May 13, 2022
4575a3b
Update load plugins snapshots
tyhopp May 13, 2022
a8ab994
feat(gatsby): Support for off-main-thread in develop (#35648)
tyhopp May 16, 2022
6919d9e
test(gatsby-script): Migrate e2e test to production and develop runti…
tyhopp May 16, 2022
6420b54
test(gatsby-script): SSR and browser APIs (#35664)
tyhopp May 16, 2022
302eb4e
update snapshots
pieh May 16, 2022
caff1f4
test(gatsby-script): Off main thread e2e (#35665)
tyhopp May 17, 2022
182b6c3
Make partytown a dev dep in gatsby-script
tyhopp May 17, 2022
3cf979f
Remove gatsby-script e2e test suite that is now in dev/prod runtime
tyhopp May 17, 2022
bcfef18
Resolve conflicts
tyhopp May 17, 2022
bfb2178
Merge branch 'master' into feat-script-component
tyhopp May 17, 2022
cfd280f
feat(gatby-script): Shim requestIdleCallback (#35611)
marvinjude May 17, 2022
a8711ab
Remove job
tyhopp May 17, 2022
b18cb0c
Remove incorrect type
tyhopp May 17, 2022
d099953
Request idle callback shim test timeout
tyhopp May 17, 2022
4d7d8a1
Add only Partytown key we handle to type
tyhopp May 17, 2022
8e55e3a
Add warning for no collect script func available
tyhopp May 17, 2022
3d246dd
Use shortened URL
tyhopp May 17, 2022
38e3707
Update gatsby-script readme
tyhopp May 17, 2022
2ff6d42
Fix offline tests
tyhopp May 18, 2022
a57cb15
feat(gatsby-script): Apply crossorigin anonymous to off-main-thread s…
tyhopp May 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions e2e-tests/development-runtime/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ cypress/videos
__history__.json

src/gatsby-types.d.ts
/test-results/
/playwright-report/
/playwright/.cache/
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)
Comment on lines +82 to +84
Copy link
Contributor

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?

Copy link
Contributor

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

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`
)
})
})
})
}
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`)
})
})
})
Loading