Skip to content

Commit a0b31bc

Browse files
piehReda Bacha
and
Reda Bacha
authored
fix(gatsby): persist pages between runs (#28590)
* fix(gatsby): add pages to saved redux state * chore: update test snapshots * unskip context change scenario for data tracking tests * mock Date.now so persistance tests don't rely on actual time (needed for updatedAt and snapshot testing) * refactor persistance tests a bit to allow for creating different pages per test scenario * shard pages state (on top of existing sharding for nodes) * drop page count check (there actually might be valid cases for 0 pages) * only show page context size warning for pages (and not nodes) * fix lint * adjust snapshot * test(artifacts): add case for changing page context * fix(gatsby): garbage collect stateful pages (#28760) * add stateful page to artifacts tests * fix(gatsby): garbage collect stateful pages * Revert "fix: clear tracked queries when deleting stale page-data files (#29431)" (#30848) This reverts commit 478cf68. Co-authored-by: Reda Bacha <[email protected]>
1 parent 7494745 commit a0b31bc

File tree

20 files changed

+718
-117
lines changed

20 files changed

+718
-117
lines changed

integration-tests/artifacts/__tests__/index.js

+38
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,40 @@ function assertHTMLCorrectness(runNumber) {
230230
})
231231
})
232232
})
233+
234+
describe(`/changing-context/`, () => {
235+
let pageDataContent
236+
let htmlContent
237+
beforeAll(() => {
238+
pageDataContent = fs.readJsonSync(
239+
path.join(
240+
process.cwd(),
241+
`public`,
242+
`page-data`,
243+
`changing-context`,
244+
`page-data.json`
245+
)
246+
)
247+
248+
htmlContent = fs.readFileSync(
249+
path.join(process.cwd(), `public`, `changing-context`, `index.html`),
250+
`utf-8`
251+
)
252+
})
253+
254+
it(`html is correctly generated using fresh page context`, () => {
255+
// remove <!-- --> from html content string as that's impl details of react ssr
256+
expect(htmlContent.replace(/<!-- -->/g, ``)).toContain(
257+
`Dummy page for runNumber: ${runNumber}`
258+
)
259+
})
260+
261+
it(`page-data is correctly generated using fresh page context`, () => {
262+
expect(pageDataContent.result.pageContext).toEqual({
263+
dummyId: `runNumber: ${runNumber}`,
264+
})
265+
})
266+
})
233267
}
234268

235269
function assertNodeCorrectness(runNumber) {
@@ -486,6 +520,7 @@ describe(`Second run (different pages created, data changed)`, () => {
486520
`/static-query-result-tracking/should-invalidate/`,
487521
`/page-query-template-change/`,
488522
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont/`,
523+
`/changing-context/`,
489524
]
490525

491526
const expectedPagesToRemainFromPreviousBuild = [
@@ -495,6 +530,7 @@ describe(`Second run (different pages created, data changed)`, () => {
495530
`/static-query-result-tracking/stable/`,
496531
`/static-query-result-tracking/rerun-query-but-dont-recreate-html/`,
497532
`/page-that-will-have-trailing-slash-removed`,
533+
`/stateful-page-not-recreated-in-third-run/`,
498534
]
499535

500536
const expectedPages = [
@@ -579,6 +615,7 @@ describe(`Third run (js change, all pages are recreated)`, () => {
579615
`/stale-pages/only-in-first/`,
580616
`/page-query-dynamic-1/`,
581617
`/page-query-dynamic-2/`,
618+
`/stateful-page-not-recreated-in-third-run/`,
582619
]
583620

584621
let changedFileOriginalContent
@@ -664,6 +701,7 @@ describe(`Fourth run (gatsby-browser change - cache get invalidated)`, () => {
664701
const expectedPages = [
665702
`/stale-pages/only-not-in-first`,
666703
`/page-query-dynamic-4/`,
704+
`/stateful-page-not-recreated-in-third-run/`,
667705
]
668706

669707
const unexpectedPages = [

integration-tests/artifacts/gatsby-node.js

+20
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ exports.createPages = async ({ actions, graphql }) => {
164164
}`
165165
)
166166

167+
actions.createPage({
168+
path: `/changing-context/`,
169+
component: require.resolve(`./src/templates/dummy`),
170+
context: {
171+
dummyId: `runNumber: ${runNumber}`,
172+
},
173+
})
174+
167175
const { data } = await graphql(`
168176
{
169177
allDepPageQuery {
@@ -189,6 +197,18 @@ exports.createPages = async ({ actions, graphql }) => {
189197
}
190198
}
191199

200+
exports.createPagesStatefully = async ({ actions }) => {
201+
if (runNumber !== 3) {
202+
actions.createPage({
203+
path: `/stateful-page-not-recreated-in-third-run/`,
204+
component: require.resolve(`./src/templates/dummy`),
205+
context: {
206+
dummyId: `stateful-page`,
207+
},
208+
})
209+
}
210+
}
211+
192212
exports.onPreBuild = () => {
193213
console.log(`[test] onPreBuild`)
194214
changedBrowserCompilationHash = `not-changed`

packages/gatsby/src/bootstrap/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
sourceNodes,
77
buildSchema,
88
createPages,
9-
createPagesStatefully,
109
extractQueries,
1110
writeOutRedirects,
1211
postBootstrap,
@@ -32,9 +31,12 @@ export async function bootstrap(
3231

3332
const parentSpan = tracer.startSpan(`bootstrap`, spanArgs)
3433

35-
const bootstrapContext: IBuildContext = {
34+
const bootstrapContext: IBuildContext & {
35+
shouldRunCreatePagesStatefully: boolean
36+
} = {
3637
...initialContext,
3738
parentSpan,
39+
shouldRunCreatePagesStatefully: true,
3840
}
3941

4042
const context = {
@@ -54,8 +56,6 @@ export async function bootstrap(
5456

5557
await createPages(context)
5658

57-
await createPagesStatefully(context)
58-
5959
await handleStalePageData()
6060

6161
await rebuildSchemaWithSitePage(context)

packages/gatsby/src/query/__tests__/data-tracking.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ describe(`query caching between builds`, () => {
879879
}, 99999)
880880
})
881881

882-
describe.skip(`Changing page context invalidates page queries`, () => {
882+
describe(`Changing page context invalidates page queries`, () => {
883883
beforeAll(() => {
884884
let pageChangeCounter = 1
885885
let nodeChangeCounter = 1

packages/gatsby/src/redux/__tests__/__snapshots__/index.js.snap

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ Object {
6262
},
6363
},
6464
"pageDataStats": Map {},
65+
"pages": Map {
66+
"/my-sweet-new-page/" => Object {
67+
"component": "/Users/username/dev/site/src/templates/my-sweet-new-page.js",
68+
"componentChunkName": "component---users-username-dev-site-src-templates-my-sweet-new-page-js",
69+
"componentPath": "/Users/username/dev/site/src/templates/my-sweet-new-page.js",
70+
"context": Object {
71+
"id": "123456",
72+
},
73+
"internalComponentName": "Component/my-sweet-new-page/",
74+
"isCreatedByStatefulCreatePages": false,
75+
"matchPath": undefined,
76+
"path": "/my-sweet-new-page/",
77+
"pluginCreatorId": "",
78+
"pluginCreator___NODE": "",
79+
"updatedAt": 1,
80+
},
81+
},
6582
"pendingPageDataWrites": Object {
6683
"pagePaths": Set {},
6784
},

0 commit comments

Comments
 (0)