Skip to content

Commit 6660144

Browse files
imjoshinLekoArtstyhopppiehmarvinjude
authoredOct 7, 2022
feat(gatsby): Slices API (#36489)
Co-authored-by: Lennart <lekoarts@gmail.com> Co-authored-by: Ty Hopp <tyhopp@users.noreply.github.com> Co-authored-by: pieh <misiek.piechowiak@gmail.com> Co-authored-by: Jude Agboola <marvinjudehk@gmail.com>

File tree

83 files changed

+3342
-478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+3342
-478
lines changed
 

‎integration-tests/artifacts/__tests__/index.js

+136-74
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ const gatsbyBin = path.join(`node_modules`, `gatsby`, `cli.js`)
1414
const manifest = {}
1515
const filesToRevert = {}
1616

17+
let _CFLAGS_ = {
18+
GATSBY_MAJOR: `4`,
19+
}
20+
if (process.env.COMPILER_OPTIONS) {
21+
// COMPILER_OPTIONS syntax is key=value,key2=value2
22+
_CFLAGS_ = process.env.COMPILER_OPTIONS.split(`,`).reduce((acc, curr) => {
23+
const [key, value] = curr.split(`=`)
24+
25+
if (key) {
26+
acc[key] = value
27+
}
28+
29+
return acc
30+
}, _CFLAGS_)
31+
}
32+
33+
let SLICES_ENABLED = _CFLAGS_.GATSBY_MAJOR === `5` && process.env.GATSBY_SLICES
1734
let exitCode
1835

1936
function runGatsbyWithRunTestSetup(runNumber = 1) {
@@ -694,101 +711,146 @@ describe(`Second run (different pages created, data changed)`, () => {
694711
assertNodeCorrectness(runNumber)
695712
})
696713

697-
describe(`Third run (js change, all pages are recreated)`, () => {
698-
const runNumber = 3
699-
700-
const expectedPages = [
701-
`/stale-pages/only-not-in-first`,
702-
`/page-query-dynamic-3/`,
703-
`/page-that-will-have-trailing-slash-removed`,
704-
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont`,
705-
]
706-
707-
const unexpectedPages = [
708-
`/stale-pages/only-in-first/`,
709-
`/page-query-dynamic-1/`,
710-
`/page-query-dynamic-2/`,
711-
`/stateful-page-not-recreated-in-third-run/`,
712-
]
713-
714-
let changedFileOriginalContent
715-
const changedFileAbspath = path.join(
716-
process.cwd(),
717-
`src`,
718-
`pages`,
719-
`gatsby-browser.js`
720-
)
714+
describe(
715+
SLICES_ENABLED
716+
? `Third run (js template change, just pages of that template are recreated, all pages are stitched)`
717+
: `Third run (js change, all pages are recreated)`,
718+
719+
() => {
720+
const runNumber = 3
721+
722+
const expectedPagesToRemainFromPreviousBuild = [
723+
`/stale-pages/stable/`,
724+
`/page-query-stable/`,
725+
`/page-query-changing-but-not-invalidating-html/`,
726+
`/static-query-result-tracking/stable/`,
727+
`/static-query-result-tracking/rerun-query-but-dont-recreate-html/`,
728+
`/page-that-will-have-trailing-slash-removed`,
729+
]
730+
731+
const expectedPagesToBeGenerated = [
732+
// this is page that gets template change
733+
`/gatsby-browser/`,
734+
// those change happen on every build
735+
`/page-query-dynamic-3/`,
736+
`/stale-pages/sometimes-i-have-trailing-slash-sometimes-i-dont`,
737+
`/changing-context/`,
738+
]
739+
740+
const expectedPages = [
741+
// this page should remain from first build
742+
...expectedPagesToRemainFromPreviousBuild,
743+
// those pages should have been (re)created
744+
...expectedPagesToBeGenerated,
745+
]
746+
747+
const unexpectedPages = [
748+
`/stale-pages/only-in-first/`,
749+
`/page-query-dynamic-1/`,
750+
`/page-query-dynamic-2/`,
751+
`/stateful-page-not-recreated-in-third-run/`,
752+
]
753+
754+
let changedFileOriginalContent
755+
const changedFileAbspath = path.join(
756+
process.cwd(),
757+
`src`,
758+
`pages`,
759+
`gatsby-browser.js`
760+
)
721761

722-
beforeAll(async () => {
723-
// make change to some .js
724-
changedFileOriginalContent = fs.readFileSync(changedFileAbspath, `utf-8`)
725-
filesToRevert[changedFileAbspath] = changedFileOriginalContent
762+
beforeAll(async () => {
763+
// make change to some .js
764+
changedFileOriginalContent = fs.readFileSync(changedFileAbspath, `utf-8`)
765+
filesToRevert[changedFileAbspath] = changedFileOriginalContent
726766

727-
const newContent = changedFileOriginalContent.replace(/sad/g, `not happy`)
767+
const newContent = changedFileOriginalContent.replace(/sad/g, `not happy`)
728768

729-
if (newContent === changedFileOriginalContent) {
730-
throw new Error(`Test setup failed`)
731-
}
769+
if (newContent === changedFileOriginalContent) {
770+
throw new Error(`Test setup failed`)
771+
}
732772

733-
fs.writeFileSync(changedFileAbspath, newContent)
734-
await runGatsbyWithRunTestSetup(runNumber)()
735-
})
773+
fs.writeFileSync(changedFileAbspath, newContent)
774+
await runGatsbyWithRunTestSetup(runNumber)()
775+
})
736776

737-
assertExitCode(runNumber)
777+
assertExitCode(runNumber)
738778

739-
describe(`html files`, () => {
740-
const type = `html`
779+
describe(`html files`, () => {
780+
const type = `html`
741781

742-
describe(`should have expected html files`, () => {
743-
assertFileExistenceForPagePaths({
744-
pagePaths: expectedPages,
745-
type,
746-
shouldExist: true,
782+
describe(`should have expected html files`, () => {
783+
assertFileExistenceForPagePaths({
784+
pagePaths: expectedPages,
785+
type,
786+
shouldExist: true,
787+
})
747788
})
748-
})
749789

750-
describe(`shouldn't have unexpected html files`, () => {
751-
assertFileExistenceForPagePaths({
752-
pagePaths: unexpectedPages,
753-
type,
754-
shouldExist: false,
790+
describe(`shouldn't have unexpected html files`, () => {
791+
assertFileExistenceForPagePaths({
792+
pagePaths: unexpectedPages,
793+
type,
794+
shouldExist: false,
795+
})
755796
})
756-
})
757797

758-
it(`should recreate all html files`, () => {
759-
expect(manifest[runNumber].generated.sort()).toEqual(
760-
manifest[runNumber].allPages.sort()
761-
)
798+
if (SLICES_ENABLED) {
799+
it(`should recreate only some html files`, () => {
800+
expect(manifest[runNumber].generated.sort()).toEqual(
801+
expectedPagesToBeGenerated.sort()
802+
)
803+
})
804+
805+
it(`should stitch fragments back in all html files (browser bundle changed)`, () => {
806+
expect(manifest[runNumber].stitched.sort()).toEqual(
807+
manifest[runNumber].allPages.sort()
808+
)
809+
})
810+
} else {
811+
it(`should recreate all html files`, () => {
812+
expect(manifest[runNumber].generated.sort()).toEqual(
813+
manifest[runNumber].allPages.sort()
814+
)
815+
})
816+
}
762817
})
763-
})
764818

765-
describe(`page-data files`, () => {
766-
const type = `page-data`
819+
describe(`page-data files`, () => {
820+
const type = `page-data`
767821

768-
describe(`should have expected page-data files`, () => {
769-
assertFileExistenceForPagePaths({
770-
pagePaths: expectedPages,
771-
type,
772-
shouldExist: true,
822+
describe(`should have expected page-data files`, () => {
823+
assertFileExistenceForPagePaths({
824+
pagePaths: expectedPages,
825+
type,
826+
shouldExist: true,
827+
})
773828
})
774-
})
775829

776-
describe(`shouldn't have unexpected page-data files`, () => {
777-
assertFileExistenceForPagePaths({
778-
pagePaths: unexpectedPages,
779-
type,
780-
shouldExist: false,
830+
describe(`shouldn't have unexpected page-data files`, () => {
831+
assertFileExistenceForPagePaths({
832+
pagePaths: unexpectedPages,
833+
type,
834+
shouldExist: false,
835+
})
781836
})
782837
})
783-
})
784838

785-
// third run - we modify module used by both ssr and browser bundle - both bundles should change
786-
assertWebpackBundleChanges({ browser: true, ssr: true, runNumber })
839+
if (SLICES_ENABLED) {
840+
// third run - we modify template used by both ssr and browser bundle - global, shared SSR won't change
841+
// as the change is localized in just one of templates, which in Gatsby 5 doesn't invalidate all html
842+
// files anymore
843+
assertWebpackBundleChanges({ browser: true, ssr: false, runNumber })
844+
} else {
845+
// third run - we modify module used by both ssr and browser bundle - both bundles should change
846+
assertWebpackBundleChanges({ browser: true, ssr: true, runNumber })
847+
}
787848

788-
assertHTMLCorrectness(runNumber)
849+
assertHTMLCorrectness(runNumber)
789850

790-
assertNodeCorrectness(runNumber)
791-
})
851+
assertNodeCorrectness(runNumber)
852+
}
853+
)
792854

793855
describe(`Fourth run (gatsby-browser change - cache get invalidated)`, () => {
794856
const runNumber = 4

‎integration-tests/artifacts/gatsby-node.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ let changedBrowserCompilationHash
99
let changedSsrCompilationHash
1010
let regeneratedPages = []
1111
let deletedPages = []
12+
let stitchedPages = []
1213

13-
exports.onPreInit = ({ emitter }) => {
14+
exports.onPreInit = ({ emitter, store }) => {
1415
emitter.on(`SET_WEBPACK_COMPILATION_HASH`, action => {
1516
changedBrowserCompilationHash = action.payload
1617
})
@@ -28,6 +29,17 @@ exports.onPreInit = ({ emitter }) => {
2829
emitter.on(`HTML_REMOVED`, action => {
2930
deletedPages.push(action.payload)
3031
})
32+
33+
// this is last step before stitching slice html into page html
34+
// we don't have action specific for stitching, so we just use this one
35+
// to read state that determine which page htmls will be stitched
36+
emitter.on(`SLICES_PROPS_REMOVE_STALE`, () => {
37+
stitchedPages = []
38+
39+
for (const path of store.getState().html.pagesThatNeedToStitchSlices) {
40+
stitchedPages.push(path)
41+
}
42+
})
3143
}
3244

3345
let previouslyCreatedNodes = new Map()
@@ -215,6 +227,7 @@ exports.onPreBuild = () => {
215227
changedSsrCompilationHash = `not-changed`
216228
regeneratedPages = []
217229
deletedPages = []
230+
stitchedPages = []
218231
}
219232

220233
let counter = 1
@@ -250,6 +263,7 @@ exports.onPostBuild = async ({ graphql, getNodes }) => {
250263
changedSsrCompilationHash,
251264
generated: regeneratedPages,
252265
removed: deletedPages,
266+
stitched: stitchedPages,
253267
}
254268
)
255269
}

0 commit comments

Comments
 (0)
Please sign in to comment.