Skip to content

Commit

Permalink
fix(gatsby): Adapter header rules (#38644)
Browse files Browse the repository at this point in the history
* simplified header rules

* lint

* lint

* update test/snapshot

* update snapshot

* add snapshot for headerRoutes

* export type
  • Loading branch information
kathmbeck committed Oct 24, 2023
1 parent a6cc92f commit dc21604
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
IRedirectRoute,
IFunctionDefinition,
RoutesManifest,
HeaderRoutes,
FunctionsManifest,
IAdapterConfig,
} from "./dist/utils/adapter/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,110 @@ Array [
},
]
`;

exports[`getRoutesManifest should return routes manifest 2`] = `
Array [
Object {
"headers": Array [
Object {
"key": "x-xss-protection",
"value": "1; mode=block",
},
Object {
"key": "x-content-type-options",
"value": "nosniff",
},
Object {
"key": "referrer-policy",
"value": "same-origin",
},
Object {
"key": "x-frame-options",
"value": "DENY",
},
],
"path": "/*",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=31536000, immutable",
},
],
"path": "/static/*",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/page-data/index/page-data.json",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/page-data/sq/d/1.json",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/page-data/app-data.json",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=31536000, immutable",
},
],
"path": "/app-123.js",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/chunk-map.json",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/webpack.stats.json",
},
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
],
"path": "/_gatsby/slices/_gatsby-scripts-1.html",
},
]
`;
85 changes: 80 additions & 5 deletions packages/gatsby/src/utils/adapter/__tests__/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ describe(`getRoutesManifest`, () => {
process.chdir(fixturesDir)
setWebpackAssets(new Set([`app-123.js`]))

const routesManifest = getRoutesManifest()
const { routes: routesManifest, headers: headerRoutes } =
getRoutesManifest()

expect(routesManifest).toMatchSnapshot()
expect(headerRoutes).toMatchSnapshot()
})

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

const routesManifest = getRoutesManifest()
const { routes: routesManifest } = getRoutesManifest()

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

const routesManifest = getRoutesManifest()
const { routes: routesManifest } = getRoutesManifest()

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

const routesManifest = getRoutesManifest()
expect(routesManifest).toEqual(
const { routes } = getRoutesManifest()
expect(routes).toEqual(
expect.arrayContaining([
expect.objectContaining({ path: `https://old-url` }),
expect.objectContaining({ path: `http://old-url` }),
])
)
})

it(`should return header rules`, () => {
mockStoreState(stateDefault, {
config: {
...stateDefault.config,
headers: [
{
source: `/ssr/*`,
headers: [
{
key: `x-ssr-header`,
value: `my custom header value from config`,
},
],
},
],
},
})
process.chdir(fixturesDir)
setWebpackAssets(new Set([`app-123.js`, `static/app-456.js`]))

const { headers } = getRoutesManifest()

expect(headers).toContainEqual({
headers: [
{ key: `x-xss-protection`, value: `1; mode=block` },
{ key: `x-content-type-options`, value: `nosniff` },
{ key: `referrer-policy`, value: `same-origin` },
{ key: `x-frame-options`, value: `DENY` },
],
path: `/*`,
})
expect(headers).toContainEqual({
headers: [
{
key: `cache-control`,
value: `public, max-age=31536000, immutable`,
},
],
path: `/static/*`,
})
expect(headers).toContainEqual({
headers: [
{
key: `cache-control`,
value: `public, max-age=0, must-revalidate`,
},
],
path: `/page-data/index/page-data.json`,
})
expect(headers).toContainEqual({
headers: [
{
key: `cache-control`,
value: `public, max-age=31536000, immutable`,
},
],
path: `/app-123.js`,
})
expect(headers).not.toContainEqual({
headers: [
{ key: `x-xss-protection`, value: `1; mode=block` },
{ key: `x-content-type-options`, value: `nosniff` },
{ key: `referrer-policy`, value: `same-origin` },
{ key: `x-frame-options`, value: `DENY` },
],
path: `/ssr/*`,
})

expect(headers).not.toContain(
expect.objectContaining({ path: `/static/app-456.js` })
)
})
})

describe(`getFunctionsManifest`, () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/src/utils/adapter/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export const MUST_REVALIDATE_HEADERS: IHeader["headers"] = [
...BASE_HEADERS,
]

export const PERMAMENT_CACHING_HEADERS: IHeader["headers"] = [
export const PERMANENT_CACHE_CONTROL_HEADER: IHeader["headers"] = [
{
key: `cache-control`,
value: `public, max-age=31536000, immutable`,
},
]

export const PERMAMENT_CACHING_HEADERS: IHeader["headers"] = [
...PERMANENT_CACHE_CONTROL_HEADER,
...BASE_HEADERS,
]

0 comments on commit dc21604

Please sign in to comment.