Skip to content

Commit 31e9249

Browse files
piehkodiakhq[bot]
andauthored
fix: apply caching headers to all produced assets (#217)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 7339592 commit 31e9249

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

src/__tests__/__snapshots__/build-headers-program.ts.snap

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ exports[`build-headers-program with manifest['pages-manifest'] 1`] = `
88
X-XSS-Protection: 1; mode=block
99
X-Content-Type-Options: nosniff
1010
Referrer-Policy: same-origin
11+
/webpack-runtime-acaa8994f1f704475e21.js
12+
Cache-Control: public, max-age=31536000, immutable
13+
/styles.1025963f4f2ec7abbad4.css
14+
Cache-Control: public, max-age=31536000, immutable
15+
/styles-565f081c8374bbda155f.js
16+
Cache-Control: public, max-age=31536000, immutable
17+
/app-f33c13590352da20930f.js
18+
Cache-Control: public, max-age=31536000, immutable
1119
/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js
1220
Cache-Control: public, max-age=31536000, immutable
1321
/0-0180cd94ef2497ac7db8.js
@@ -18,15 +26,9 @@ exports[`build-headers-program with manifest['pages-manifest'] 1`] = `
1826
Cache-Control: public, max-age=31536000, immutable
1927
/component---src-pages-index-js-0bdd01c77ee09ef0224c.js
2028
Cache-Control: public, max-age=31536000, immutable
21-
/pages-manifest-ab11f09e0ca7ecd3b43e.js
22-
Cache-Control: public, max-age=31536000, immutable
23-
/webpack-runtime-acaa8994f1f704475e21.js
24-
Cache-Control: public, max-age=31536000, immutable
25-
/styles.1025963f4f2ec7abbad4.css
26-
Cache-Control: public, max-age=31536000, immutable
27-
/styles-565f081c8374bbda155f.js
29+
/711-90491aa56de138c82516.js
2830
Cache-Control: public, max-age=31536000, immutable
29-
/app-f33c13590352da20930f.js
31+
/pages-manifest-ab11f09e0ca7ecd3b43e.js
3032
Cache-Control: public, max-age=31536000, immutable
3133
/static/*
3234
Cache-Control: public, max-age=31536000, immutable
@@ -46,6 +48,14 @@ exports[`build-headers-program with security headers 1`] = `
4648
Content-Security-Policy: frame-ancestors 'self' https://*.storyblok.com/
4749
/hello
4850
X-Frame-Options: SAMEORIGIN
51+
/webpack-runtime-acaa8994f1f704475e21.js
52+
Cache-Control: public, max-age=31536000, immutable
53+
/styles.1025963f4f2ec7abbad4.css
54+
Cache-Control: public, max-age=31536000, immutable
55+
/styles-565f081c8374bbda155f.js
56+
Cache-Control: public, max-age=31536000, immutable
57+
/app-f33c13590352da20930f.js
58+
Cache-Control: public, max-age=31536000, immutable
4959
/component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js
5060
Cache-Control: public, max-age=31536000, immutable
5161
/0-0180cd94ef2497ac7db8.js
@@ -56,13 +66,7 @@ exports[`build-headers-program with security headers 1`] = `
5666
Cache-Control: public, max-age=31536000, immutable
5767
/component---src-pages-index-js-0bdd01c77ee09ef0224c.js
5868
Cache-Control: public, max-age=31536000, immutable
59-
/webpack-runtime-acaa8994f1f704475e21.js
60-
Cache-Control: public, max-age=31536000, immutable
61-
/styles.1025963f4f2ec7abbad4.css
62-
Cache-Control: public, max-age=31536000, immutable
63-
/styles-565f081c8374bbda155f.js
64-
Cache-Control: public, max-age=31536000, immutable
65-
/app-f33c13590352da20930f.js
69+
/711-90491aa56de138c82516.js
6670
Cache-Control: public, max-age=31536000, immutable
6771
/static/*
6872
Cache-Control: public, max-age=31536000, immutable

src/__tests__/helpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ export const createPluginData = async () => {
143143
],
144144
]),
145145
manifest: {
146-
'main.js': `render-page.js`,
147-
'main.js.map': `render-page.js.map`,
148146
app: [
149147
`webpack-runtime-acaa8994f1f704475e21.js`,
150148
`styles.1025963f4f2ec7abbad4.css`,
@@ -166,6 +164,7 @@ export const createPluginData = async () => {
166164
`0-0180cd94ef2497ac7db8.js`,
167165
`component---src-pages-index-js-0bdd01c77ee09ef0224c.js`,
168166
],
167+
'711-90491aa56de138c82516.js': `711-90491aa56de138c82516.js`,
169168
},
170169
pathPrefix: ``,
171170
publicFolder: (...files: any[]) => join(tmpDir, ...files),

src/build-headers-program.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,16 @@ const applyCachingHeaders =
182182
return headers
183183
}
184184

185-
let chunks = []
186-
// Gatsby v3.5 added componentChunkName to store().components
187-
// So we prefer to pull chunk names off that as it gets very expensive to loop
188-
// over large numbers of pages.
189-
const isComponentChunkSet = Boolean(pluginData.components.entries()?.next()?.value[1]?.componentChunkName)
190-
chunks = isComponentChunkSet
191-
? [...pluginData.components.values()].map((c) => c.componentChunkName)
192-
: [...pluginData.pages.values()].map((page) => page.componentChunkName)
193-
194-
chunks.push(`pages-manifest`, `app`)
195-
196-
const files = chunks.flatMap((chunk) => pluginData.manifest[chunk])
185+
const files = new Set()
186+
for (const fileNameOrArrayOfFileNames of Object.values(pluginData.manifest)) {
187+
if (Array.isArray(fileNameOrArrayOfFileNames)) {
188+
for (const filename of fileNameOrArrayOfFileNames) {
189+
files.add(filename)
190+
}
191+
} else if (typeof fileNameOrArrayOfFileNames === `string`) {
192+
files.add(fileNameOrArrayOfFileNames)
193+
}
194+
}
197195

198196
const cachingHeaders = {}
199197

src/constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Gatsby values
2-
export const BUILD_HTML_STAGE = `build-html`
3-
export const BUILD_CSS_STAGE = `build-css`
2+
export const BUILD_BROWSER_BUNDLE_STAGE = `build-javascript`
43

54
// Plugin values
65
export const NETLIFY_HEADERS_FILENAME = `_headers`

src/gatsby-node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { generatePageDataPath } from 'gatsby-core-utils'
66
import WebpackAssetsManifest from 'webpack-assets-manifest'
77

88
import buildHeadersProgram from './build-headers-program'
9-
import { DEFAULT_OPTIONS, BUILD_HTML_STAGE, BUILD_CSS_STAGE, PAGE_COUNT_WARN } from './constants'
9+
import { DEFAULT_OPTIONS, BUILD_BROWSER_BUNDLE_STAGE, PAGE_COUNT_WARN } from './constants'
1010
import createRedirects from './create-redirects'
1111
import makePluginData from './plugin-data'
1212

@@ -44,7 +44,8 @@ export const pluginOptionsSchema = ({ Joi }: any) => {
4444
/** @type {import("gatsby").GatsbyNode["onCreateWebpackConfig"]} */
4545

4646
export const onCreateWebpackConfig = ({ actions, stage }: any) => {
47-
if (stage !== BUILD_HTML_STAGE && stage !== BUILD_CSS_STAGE) {
47+
// We only need to get manifest for production browser bundle
48+
if (stage !== BUILD_BROWSER_BUNDLE_STAGE) {
4849
return
4950
}
5051
actions.setWebpackConfig({

0 commit comments

Comments
 (0)