Skip to content

Commit

Permalink
fix(gatsby): add componentChunkName to components list so don't need …
Browse files Browse the repository at this point in the history
…to loop over pages (#31547)

* fix(gatsby): add componentChunkName to components list so don't need to loop over pages

* fix type

* fix tests

* more fixes

* Guard against null values
  • Loading branch information
KyleAMathews authored May 25, 2021
1 parent 3457163 commit 783b937
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ describe(`build-headers-program`, () => {
)

return {
components: new Map([
[
1,
{
componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`,
},
],
[
2,
{
componentChunkName: `component---src-templates-blog-post-js`,
},
],
[
3,
{
componentChunkName: `component---src-pages-404-js`,
},
],
[
4,
{
componentChunkName: `component---src-pages-index-js`,
},
],
]),
pages: new Map([
[
`/offline-plugin-app-shell-fallback/`,
Expand Down
16 changes: 13 additions & 3 deletions packages/gatsby-plugin-gatsby-cloud/src/build-headers-program.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,19 @@ const applyCachingHeaders = (
return headers
}

const chunks = Array.from(pluginData.pages.values()).map(
page => page.componentChunkName
)
let chunks = []
// Gatsby v3.5 added componentChunkName to store().components
// So we prefer to pull chunk names off that as it gets very expensive to loop
// over large numbers of pages.
const isComponentChunkSet = !!pluginData.components.entries()?.next()
?.value[1]?.componentChunkName
if (isComponentChunkSet) {
chunks = [...pluginData.components.values()].map(c => c.componentChunkName)
} else {
chunks = Array.from(pluginData.pages.values()).map(
page => page.componentChunkName
)
}

chunks.push(`pages-manifest`, `app`)

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-gatsby-cloud/src/plugin-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function buildPrefixer(prefix, ...paths) {
// shape of `static-entry.js`. With it, we can build headers that point to the correct
// hashed filenames and ensure we pull in the componentChunkName.
export default function makePluginData(store, assetsManifest, pathPrefix) {
const { program, pages: storePages } = store.getState()
const { program, pages, components } = store.getState()
const publicFolder = buildPrefixer(program.directory, `public`)
const functionsFolder = buildPrefixer(
program.directory,
Expand All @@ -18,13 +18,13 @@ export default function makePluginData(store, assetsManifest, pathPrefix) {
const stats = require(publicFolder(`webpack.stats.json`))
// Get all the files, not just the first
const chunkManifest = stats.assetsByChunkName
const pages = storePages

// We combine the manifest of JS and the manifest of assets to make a lookup table.
const manifest = { ...assetsManifest, ...chunkManifest }

return {
pages,
components,
manifest,
program,
pathPrefix,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ describe(`build-headers-program`, () => {
)

return {
components: new Map([
[
1,
{
componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`,
},
],
[
2,
{
componentChunkName: `component---src-templates-blog-post-js`,
},
],
[
3,
{
componentChunkName: `component---src-pages-404-js`,
},
],
[
4,
{
componentChunkName: `component---src-pages-index-js`,
},
],
]),
pages: new Map([
[
`/offline-plugin-app-shell-fallback/`,
Expand Down
16 changes: 13 additions & 3 deletions packages/gatsby-plugin-netlify/src/build-headers-program.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,19 @@ const applyCachingHeaders = (
return headers
}

const chunks = Array.from(pluginData.pages.values()).map(
page => page.componentChunkName
)
let chunks = []
// Gatsby v3.5 added componentChunkName to store().components
// So we prefer to pull chunk names off that as it gets very expensive to loop
// over large numbers of pages.
const isComponentChunkSet = !!pluginData.components.entries()?.next()
?.value[1]?.componentChunkName
if (isComponentChunkSet) {
chunks = [...pluginData.components.values()].map(c => c.componentChunkName)
} else {
chunks = Array.from(pluginData.pages.values()).map(
page => page.componentChunkName
)
}

chunks.push(`pages-manifest`, `app`)

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-netlify/src/plugin-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ export function buildPrefixer(prefix, ...paths) {
// shape of `static-entry.js`. With it, we can build headers that point to the correct
// hashed filenames and ensure we pull in the componentChunkName.
export default function makePluginData(store, assetsManifest, pathPrefix) {
const { program, pages: storePages } = store.getState()
const { program, pages, components } = store.getState()
const publicFolder = buildPrefixer(program.directory, `public`)
const stats = require(publicFolder(`webpack.stats.json`))
// Get all the files, not just the first
const chunkManifest = stats.assetsByChunkName
const pages = storePages

// We combine the manifest of JS and the manifest of assets to make a lookup table.
const manifest = { ...assetsManifest, ...chunkManifest }

return {
pages,
components,
manifest,
pathPrefix,
publicFolder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`redux db should write redux cache to disk 1`] = `
Object {
"components": Map {
"/Users/username/dev/site/src/templates/my-sweet-new-page.js" => Object {
"componentChunkName": "component---users-username-dev-site-src-templates-my-sweet-new-page-js",
"componentPath": "/Users/username/dev/site/src/templates/my-sweet-new-page.js",
"isInBootstrap": true,
"pages": Set {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/redux/reducers/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const componentsReducer = (
if (!component) {
component = {
componentPath: action.payload.componentPath,
componentChunkName: action.payload.componentChunkName,
query: ``,
pages: new Set(),
isInBootstrap: true,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface IGatsbyStaticQueryComponents {

export interface IGatsbyPageComponent {
componentPath: SystemPath
componentChunkName: string
query: string
pages: Set<string>
isInBootstrap: boolean
Expand Down

0 comments on commit 783b937

Please sign in to comment.