Skip to content

Commit 6869562

Browse files
gatsbybotkathmbeck
andauthored
fix(gatsby): Adapter header rules (#38644) (#38661)
* simplified header rules * lint * lint * update test/snapshot * update snapshot * add snapshot for headerRoutes * export type (cherry picked from commit dc21604) Co-authored-by: Katherine Beck <[email protected]>
1 parent 4a76878 commit 6869562

File tree

6 files changed

+281
-28
lines changed

6 files changed

+281
-28
lines changed

packages/gatsby/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export {
4242
IRedirectRoute,
4343
IFunctionDefinition,
4444
RoutesManifest,
45+
HeaderRoutes,
4546
FunctionsManifest,
4647
IAdapterConfig,
4748
} from "./dist/utils/adapter/types"

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,110 @@ Array [
322322
},
323323
]
324324
`;
325+
326+
exports[`getRoutesManifest should return routes manifest 2`] = `
327+
Array [
328+
Object {
329+
"headers": Array [
330+
Object {
331+
"key": "x-xss-protection",
332+
"value": "1; mode=block",
333+
},
334+
Object {
335+
"key": "x-content-type-options",
336+
"value": "nosniff",
337+
},
338+
Object {
339+
"key": "referrer-policy",
340+
"value": "same-origin",
341+
},
342+
Object {
343+
"key": "x-frame-options",
344+
"value": "DENY",
345+
},
346+
],
347+
"path": "/*",
348+
},
349+
Object {
350+
"headers": Array [
351+
Object {
352+
"key": "cache-control",
353+
"value": "public, max-age=31536000, immutable",
354+
},
355+
],
356+
"path": "/static/*",
357+
},
358+
Object {
359+
"headers": Array [
360+
Object {
361+
"key": "cache-control",
362+
"value": "public, max-age=0, must-revalidate",
363+
},
364+
],
365+
"path": "/",
366+
},
367+
Object {
368+
"headers": Array [
369+
Object {
370+
"key": "cache-control",
371+
"value": "public, max-age=0, must-revalidate",
372+
},
373+
],
374+
"path": "/page-data/index/page-data.json",
375+
},
376+
Object {
377+
"headers": Array [
378+
Object {
379+
"key": "cache-control",
380+
"value": "public, max-age=0, must-revalidate",
381+
},
382+
],
383+
"path": "/page-data/sq/d/1.json",
384+
},
385+
Object {
386+
"headers": Array [
387+
Object {
388+
"key": "cache-control",
389+
"value": "public, max-age=0, must-revalidate",
390+
},
391+
],
392+
"path": "/page-data/app-data.json",
393+
},
394+
Object {
395+
"headers": Array [
396+
Object {
397+
"key": "cache-control",
398+
"value": "public, max-age=31536000, immutable",
399+
},
400+
],
401+
"path": "/app-123.js",
402+
},
403+
Object {
404+
"headers": Array [
405+
Object {
406+
"key": "cache-control",
407+
"value": "public, max-age=0, must-revalidate",
408+
},
409+
],
410+
"path": "/chunk-map.json",
411+
},
412+
Object {
413+
"headers": Array [
414+
Object {
415+
"key": "cache-control",
416+
"value": "public, max-age=0, must-revalidate",
417+
},
418+
],
419+
"path": "/webpack.stats.json",
420+
},
421+
Object {
422+
"headers": Array [
423+
Object {
424+
"key": "cache-control",
425+
"value": "public, max-age=0, must-revalidate",
426+
},
427+
],
428+
"path": "/_gatsby/slices/_gatsby-scripts-1.html",
429+
},
430+
]
431+
`;

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

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ describe(`getRoutesManifest`, () => {
5050
process.chdir(fixturesDir)
5151
setWebpackAssets(new Set([`app-123.js`]))
5252

53-
const routesManifest = getRoutesManifest()
53+
const { routes: routesManifest, headers: headerRoutes } =
54+
getRoutesManifest()
5455

5556
expect(routesManifest).toMatchSnapshot()
57+
expect(headerRoutes).toMatchSnapshot()
5658
})
5759

5860
it(`should respect "never" trailingSlash config option`, () => {
@@ -62,7 +64,7 @@ describe(`getRoutesManifest`, () => {
6264
process.chdir(fixturesDir)
6365
setWebpackAssets(new Set([`app-123.js`]))
6466

65-
const routesManifest = getRoutesManifest()
67+
const { routes: routesManifest } = getRoutesManifest()
6668

6769
expect(routesManifest).toEqual(
6870
expect.arrayContaining([
@@ -81,7 +83,7 @@ describe(`getRoutesManifest`, () => {
8183
process.chdir(fixturesDir)
8284
setWebpackAssets(new Set([`app-123.js`]))
8385

84-
const routesManifest = getRoutesManifest()
86+
const { routes: routesManifest } = getRoutesManifest()
8587

8688
expect(routesManifest).toEqual(
8789
expect.arrayContaining([
@@ -98,14 +100,87 @@ describe(`getRoutesManifest`, () => {
98100
process.chdir(fixturesDir)
99101
setWebpackAssets(new Set([`app-123.js`]))
100102

101-
const routesManifest = getRoutesManifest()
102-
expect(routesManifest).toEqual(
103+
const { routes } = getRoutesManifest()
104+
expect(routes).toEqual(
103105
expect.arrayContaining([
104106
expect.objectContaining({ path: `https://old-url` }),
105107
expect.objectContaining({ path: `http://old-url` }),
106108
])
107109
)
108110
})
111+
112+
it(`should return header rules`, () => {
113+
mockStoreState(stateDefault, {
114+
config: {
115+
...stateDefault.config,
116+
headers: [
117+
{
118+
source: `/ssr/*`,
119+
headers: [
120+
{
121+
key: `x-ssr-header`,
122+
value: `my custom header value from config`,
123+
},
124+
],
125+
},
126+
],
127+
},
128+
})
129+
process.chdir(fixturesDir)
130+
setWebpackAssets(new Set([`app-123.js`, `static/app-456.js`]))
131+
132+
const { headers } = getRoutesManifest()
133+
134+
expect(headers).toContainEqual({
135+
headers: [
136+
{ key: `x-xss-protection`, value: `1; mode=block` },
137+
{ key: `x-content-type-options`, value: `nosniff` },
138+
{ key: `referrer-policy`, value: `same-origin` },
139+
{ key: `x-frame-options`, value: `DENY` },
140+
],
141+
path: `/*`,
142+
})
143+
expect(headers).toContainEqual({
144+
headers: [
145+
{
146+
key: `cache-control`,
147+
value: `public, max-age=31536000, immutable`,
148+
},
149+
],
150+
path: `/static/*`,
151+
})
152+
expect(headers).toContainEqual({
153+
headers: [
154+
{
155+
key: `cache-control`,
156+
value: `public, max-age=0, must-revalidate`,
157+
},
158+
],
159+
path: `/page-data/index/page-data.json`,
160+
})
161+
expect(headers).toContainEqual({
162+
headers: [
163+
{
164+
key: `cache-control`,
165+
value: `public, max-age=31536000, immutable`,
166+
},
167+
],
168+
path: `/app-123.js`,
169+
})
170+
expect(headers).not.toContainEqual({
171+
headers: [
172+
{ key: `x-xss-protection`, value: `1; mode=block` },
173+
{ key: `x-content-type-options`, value: `nosniff` },
174+
{ key: `referrer-policy`, value: `same-origin` },
175+
{ key: `x-frame-options`, value: `DENY` },
176+
],
177+
path: `/ssr/*`,
178+
})
179+
180+
expect(headers).not.toContain(
181+
expect.objectContaining({ path: `/static/app-456.js` })
182+
)
183+
})
109184
})
110185

111186
describe(`getFunctionsManifest`, () => {

packages/gatsby/src/utils/adapter/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ export const MUST_REVALIDATE_HEADERS: IHeader["headers"] = [
2727
...BASE_HEADERS,
2828
]
2929

30-
export const PERMAMENT_CACHING_HEADERS: IHeader["headers"] = [
30+
export const PERMANENT_CACHE_CONTROL_HEADER: IHeader["headers"] = [
3131
{
3232
key: `cache-control`,
3333
value: `public, max-age=31536000, immutable`,
3434
},
35+
]
36+
37+
export const PERMAMENT_CACHING_HEADERS: IHeader["headers"] = [
38+
...PERMANENT_CACHE_CONTROL_HEADER,
3539
...BASE_HEADERS,
3640
]

0 commit comments

Comments
 (0)