Skip to content

Commit d328fd8

Browse files
gatsbybotpieh
andauthoredJan 25, 2024
perf: use must-revalidate cache-control header as common and only create header routes for routes with different cache-control (#38820) (#38824)
* perf: use must-revalidate cache-control header as common and only create header routes for routes with different cache-control * test: update headerRoutes unit tests (cherry picked from commit fb77fe5) Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>

File tree

3 files changed

+14
-83
lines changed

3 files changed

+14
-83
lines changed
 

‎packages/gatsby/src/utils/adapter/__tests__/__snapshots__/manager.ts.snap

+4-63
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ exports[`getRoutesManifest should return routes manifest 2`] = `
359359
Array [
360360
Object {
361361
"headers": Array [
362+
Object {
363+
"key": "cache-control",
364+
"value": "public, max-age=0, must-revalidate",
365+
},
362366
Object {
363367
"key": "x-xss-protection",
364368
"value": "1; mode=block",
@@ -387,42 +391,6 @@ Array [
387391
],
388392
"path": "/static/*",
389393
},
390-
Object {
391-
"headers": Array [
392-
Object {
393-
"key": "cache-control",
394-
"value": "public, max-age=0, must-revalidate",
395-
},
396-
],
397-
"path": "/",
398-
},
399-
Object {
400-
"headers": Array [
401-
Object {
402-
"key": "cache-control",
403-
"value": "public, max-age=0, must-revalidate",
404-
},
405-
],
406-
"path": "/page-data/index/page-data.json",
407-
},
408-
Object {
409-
"headers": Array [
410-
Object {
411-
"key": "cache-control",
412-
"value": "public, max-age=0, must-revalidate",
413-
},
414-
],
415-
"path": "/page-data/sq/d/1.json",
416-
},
417-
Object {
418-
"headers": Array [
419-
Object {
420-
"key": "cache-control",
421-
"value": "public, max-age=0, must-revalidate",
422-
},
423-
],
424-
"path": "/page-data/app-data.json",
425-
},
426394
Object {
427395
"headers": Array [
428396
Object {
@@ -432,32 +400,5 @@ Array [
432400
],
433401
"path": "/app-123.js",
434402
},
435-
Object {
436-
"headers": Array [
437-
Object {
438-
"key": "cache-control",
439-
"value": "public, max-age=0, must-revalidate",
440-
},
441-
],
442-
"path": "/chunk-map.json",
443-
},
444-
Object {
445-
"headers": Array [
446-
Object {
447-
"key": "cache-control",
448-
"value": "public, max-age=0, must-revalidate",
449-
},
450-
],
451-
"path": "/webpack.stats.json",
452-
},
453-
Object {
454-
"headers": Array [
455-
Object {
456-
"key": "cache-control",
457-
"value": "public, max-age=0, must-revalidate",
458-
},
459-
],
460-
"path": "/_gatsby/slices/_gatsby-scripts-1.html",
461-
},
462403
]
463404
`;

‎packages/gatsby/src/utils/adapter/__tests__/manager.ts

+8-18
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ describe(`getRoutesManifest`, () => {
156156

157157
expect(headers).toContainEqual({
158158
headers: [
159+
{
160+
key: `cache-control`,
161+
value: `public, max-age=0, must-revalidate`,
162+
},
159163
{ key: `x-xss-protection`, value: `1; mode=block` },
160164
{ key: `x-content-type-options`, value: `nosniff` },
161165
{ key: `referrer-policy`, value: `same-origin` },
@@ -172,15 +176,6 @@ describe(`getRoutesManifest`, () => {
172176
],
173177
path: `/static/*`,
174178
})
175-
expect(headers).toContainEqual({
176-
headers: [
177-
{
178-
key: `cache-control`,
179-
value: `public, max-age=0, must-revalidate`,
180-
},
181-
],
182-
path: `/page-data/index/page-data.json`,
183-
})
184179
expect(headers).toContainEqual({
185180
headers: [
186181
{
@@ -234,6 +229,10 @@ describe(`getRoutesManifest`, () => {
234229

235230
expect(headers).toContainEqual({
236231
headers: [
232+
{
233+
key: `cache-control`,
234+
value: `public, max-age=0, must-revalidate`,
235+
},
237236
{ key: `x-xss-protection`, value: `1; mode=block` },
238237
{ key: `x-content-type-options`, value: `nosniff` },
239238
{ key: `referrer-policy`, value: `same-origin` },
@@ -250,15 +249,6 @@ describe(`getRoutesManifest`, () => {
250249
],
251250
path: `/prefix/static/*`,
252251
})
253-
expect(headers).toContainEqual({
254-
headers: [
255-
{
256-
key: `cache-control`,
257-
value: `public, max-age=0, must-revalidate`,
258-
},
259-
],
260-
path: `/prefix/page-data/index/page-data.json`,
261-
})
262252
expect(headers).toContainEqual({
263253
headers: [
264254
{

‎packages/gatsby/src/utils/adapter/manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ const headersAreEqual = (a, b): boolean =>
308308
const getDefaultHeaderRoutes = (pathPrefix: string): HeaderRoutes => [
309309
{
310310
path: `${pathPrefix}/*`,
311-
headers: BASE_HEADERS,
311+
headers: MUST_REVALIDATE_HEADERS,
312312
},
313313
{
314314
path: `${pathPrefix}/static/*`,
@@ -319,7 +319,7 @@ const getDefaultHeaderRoutes = (pathPrefix: string): HeaderRoutes => [
319319
const customHeaderFilter =
320320
(route: Route, pathPrefix: string) =>
321321
(h: IHeader["headers"][0]): boolean => {
322-
for (const baseHeader of BASE_HEADERS) {
322+
for (const baseHeader of MUST_REVALIDATE_HEADERS) {
323323
if (headersAreEqual(baseHeader, h)) {
324324
return false
325325
}

0 commit comments

Comments
 (0)
Please sign in to comment.