Skip to content

Commit

Permalink
feat(gatsby): split up head & page component loading (#36545)
Browse files Browse the repository at this point in the history
* separate chunk for head, remove default

* use partial hydration env in loader

Co-authored-by: Jude Agboola <marvinjudehk@gmail.com>
  • Loading branch information
wardpeet and marvinjude authored Sep 7, 2022
1 parent dc9aa9a commit 42e241c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 8 additions & 4 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,19 @@ const createComponentUrls = componentChunkName =>

export class ProdLoader extends BaseLoader {
constructor(asyncRequires, matchPaths, pageData) {
const loadComponent = chunkName => {
if (!asyncRequires.components[chunkName]) {
const loadComponent = (chunkName, exportType = `components`) => {
if (!global.hasPartialHydration) {
exportType = `components`
}

if (!asyncRequires[exportType][chunkName]) {
throw new Error(
`We couldn't find the correct component chunk with the name ${chunkName}`
`We couldn't find the correct component chunk with the name "${chunkName}"`
)
}

return (
asyncRequires.components[chunkName]()
asyncRequires[exportType][chunkName]()
// loader will handle the case when component is error
.catch(err => err)
)
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby/src/bootstrap/requires-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ const preferDefault = m => (m && m.default) || m
// Create file with async requires of components/json files.
let asyncRequires = ``

if (process.env.gatsby_executing_command === `develop`) {
if (
process.env.gatsby_executing_command === `develop` ||
(_CFLAGS_.GATSBY_MAJOR === `5` && process.env.GATSBY_PARTIAL_HYDRATION)
) {
asyncRequires = `exports.components = {\n${components
.map((c: IGatsbyPageComponent): string => {
// we need a relative import path to keep contenthash the same if directory changes
Expand Down

0 comments on commit 42e241c

Please sign in to comment.