Skip to content

Commit 783b937

Browse files
authored
fix(gatsby): add componentChunkName to components list so don't need 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
1 parent 3457163 commit 783b937

File tree

9 files changed

+85
-10
lines changed

9 files changed

+85
-10
lines changed

packages/gatsby-plugin-gatsby-cloud/src/__tests__/build-headers-program.js

+26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ describe(`build-headers-program`, () => {
2828
)
2929

3030
return {
31+
components: new Map([
32+
[
33+
1,
34+
{
35+
componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`,
36+
},
37+
],
38+
[
39+
2,
40+
{
41+
componentChunkName: `component---src-templates-blog-post-js`,
42+
},
43+
],
44+
[
45+
3,
46+
{
47+
componentChunkName: `component---src-pages-404-js`,
48+
},
49+
],
50+
[
51+
4,
52+
{
53+
componentChunkName: `component---src-pages-index-js`,
54+
},
55+
],
56+
]),
3157
pages: new Map([
3258
[
3359
`/offline-plugin-app-shell-fallback/`,

packages/gatsby-plugin-gatsby-cloud/src/build-headers-program.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,19 @@ const applyCachingHeaders = (
273273
return headers
274274
}
275275

276-
const chunks = Array.from(pluginData.pages.values()).map(
277-
page => page.componentChunkName
278-
)
276+
let chunks = []
277+
// Gatsby v3.5 added componentChunkName to store().components
278+
// So we prefer to pull chunk names off that as it gets very expensive to loop
279+
// over large numbers of pages.
280+
const isComponentChunkSet = !!pluginData.components.entries()?.next()
281+
?.value[1]?.componentChunkName
282+
if (isComponentChunkSet) {
283+
chunks = [...pluginData.components.values()].map(c => c.componentChunkName)
284+
} else {
285+
chunks = Array.from(pluginData.pages.values()).map(
286+
page => page.componentChunkName
287+
)
288+
}
279289

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

packages/gatsby-plugin-gatsby-cloud/src/plugin-data.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function buildPrefixer(prefix, ...paths) {
88
// shape of `static-entry.js`. With it, we can build headers that point to the correct
99
// hashed filenames and ensure we pull in the componentChunkName.
1010
export default function makePluginData(store, assetsManifest, pathPrefix) {
11-
const { program, pages: storePages } = store.getState()
11+
const { program, pages, components } = store.getState()
1212
const publicFolder = buildPrefixer(program.directory, `public`)
1313
const functionsFolder = buildPrefixer(
1414
program.directory,
@@ -18,13 +18,13 @@ export default function makePluginData(store, assetsManifest, pathPrefix) {
1818
const stats = require(publicFolder(`webpack.stats.json`))
1919
// Get all the files, not just the first
2020
const chunkManifest = stats.assetsByChunkName
21-
const pages = storePages
2221

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

2625
return {
2726
pages,
27+
components,
2828
manifest,
2929
program,
3030
pathPrefix,

packages/gatsby-plugin-netlify/src/__tests__/build-headers-program.js

+26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ describe(`build-headers-program`, () => {
2828
)
2929

3030
return {
31+
components: new Map([
32+
[
33+
1,
34+
{
35+
componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`,
36+
},
37+
],
38+
[
39+
2,
40+
{
41+
componentChunkName: `component---src-templates-blog-post-js`,
42+
},
43+
],
44+
[
45+
3,
46+
{
47+
componentChunkName: `component---src-pages-404-js`,
48+
},
49+
],
50+
[
51+
4,
52+
{
53+
componentChunkName: `component---src-pages-index-js`,
54+
},
55+
],
56+
]),
3157
pages: new Map([
3258
[
3359
`/offline-plugin-app-shell-fallback/`,

packages/gatsby-plugin-netlify/src/build-headers-program.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,19 @@ const applyCachingHeaders = (
306306
return headers
307307
}
308308

309-
const chunks = Array.from(pluginData.pages.values()).map(
310-
page => page.componentChunkName
311-
)
309+
let chunks = []
310+
// Gatsby v3.5 added componentChunkName to store().components
311+
// So we prefer to pull chunk names off that as it gets very expensive to loop
312+
// over large numbers of pages.
313+
const isComponentChunkSet = !!pluginData.components.entries()?.next()
314+
?.value[1]?.componentChunkName
315+
if (isComponentChunkSet) {
316+
chunks = [...pluginData.components.values()].map(c => c.componentChunkName)
317+
} else {
318+
chunks = Array.from(pluginData.pages.values()).map(
319+
page => page.componentChunkName
320+
)
321+
}
312322

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

packages/gatsby-plugin-netlify/src/plugin-data.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ export function buildPrefixer(prefix, ...paths) {
88
// shape of `static-entry.js`. With it, we can build headers that point to the correct
99
// hashed filenames and ensure we pull in the componentChunkName.
1010
export default function makePluginData(store, assetsManifest, pathPrefix) {
11-
const { program, pages: storePages } = store.getState()
11+
const { program, pages, components } = store.getState()
1212
const publicFolder = buildPrefixer(program.directory, `public`)
1313
const stats = require(publicFolder(`webpack.stats.json`))
1414
// Get all the files, not just the first
1515
const chunkManifest = stats.assetsByChunkName
16-
const pages = storePages
1716

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

2120
return {
2221
pages,
22+
components,
2323
manifest,
2424
pathPrefix,
2525
publicFolder,

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`redux db should write redux cache to disk 1`] = `
44
Object {
55
"components": Map {
66
"/Users/username/dev/site/src/templates/my-sweet-new-page.js" => Object {
7+
"componentChunkName": "component---users-username-dev-site-src-templates-my-sweet-new-page-js",
78
"componentPath": "/Users/username/dev/site/src/templates/my-sweet-new-page.js",
89
"isInBootstrap": true,
910
"pages": Set {

packages/gatsby/src/redux/reducers/components.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const componentsReducer = (
2222
if (!component) {
2323
component = {
2424
componentPath: action.payload.componentPath,
25+
componentChunkName: action.payload.componentChunkName,
2526
query: ``,
2627
pages: new Set(),
2728
isInBootstrap: true,

packages/gatsby/src/redux/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export interface IGatsbyStaticQueryComponents {
106106

107107
export interface IGatsbyPageComponent {
108108
componentPath: SystemPath
109+
componentChunkName: string
109110
query: string
110111
pages: Set<string>
111112
isInBootstrap: boolean

0 commit comments

Comments
 (0)