Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): Adapter header rules (#38644) #38661

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
]
Loading