Skip to content

Commit

Permalink
perf: use must-revalidate cache-control header as common and only cre…
Browse files Browse the repository at this point in the history
…ate 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>
  • Loading branch information
gatsbybot and pieh committed Jan 25, 2024
1 parent b24134d commit d328fd8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ exports[`getRoutesManifest should return routes manifest 2`] = `
Array [
Object {
"headers": Array [
Object {
"key": "cache-control",
"value": "public, max-age=0, must-revalidate",
},
Object {
"key": "x-xss-protection",
"value": "1; mode=block",
Expand Down Expand Up @@ -387,42 +391,6 @@ Array [
],
"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 {
Expand All @@ -432,32 +400,5 @@ Array [
],
"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",
},
]
`;
26 changes: 8 additions & 18 deletions packages/gatsby/src/utils/adapter/__tests__/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ describe(`getRoutesManifest`, () => {

expect(headers).toContainEqual({
headers: [
{
key: `cache-control`,
value: `public, max-age=0, must-revalidate`,
},
{ key: `x-xss-protection`, value: `1; mode=block` },
{ key: `x-content-type-options`, value: `nosniff` },
{ key: `referrer-policy`, value: `same-origin` },
Expand All @@ -172,15 +176,6 @@ describe(`getRoutesManifest`, () => {
],
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: [
{
Expand Down Expand Up @@ -234,6 +229,10 @@ describe(`getRoutesManifest`, () => {

expect(headers).toContainEqual({
headers: [
{
key: `cache-control`,
value: `public, max-age=0, must-revalidate`,
},
{ key: `x-xss-protection`, value: `1; mode=block` },
{ key: `x-content-type-options`, value: `nosniff` },
{ key: `referrer-policy`, value: `same-origin` },
Expand All @@ -250,15 +249,6 @@ describe(`getRoutesManifest`, () => {
],
path: `/prefix/static/*`,
})
expect(headers).toContainEqual({
headers: [
{
key: `cache-control`,
value: `public, max-age=0, must-revalidate`,
},
],
path: `/prefix/page-data/index/page-data.json`,
})
expect(headers).toContainEqual({
headers: [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/adapter/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const headersAreEqual = (a, b): boolean =>
const getDefaultHeaderRoutes = (pathPrefix: string): HeaderRoutes => [
{
path: `${pathPrefix}/*`,
headers: BASE_HEADERS,
headers: MUST_REVALIDATE_HEADERS,
},
{
path: `${pathPrefix}/static/*`,
Expand All @@ -319,7 +319,7 @@ const getDefaultHeaderRoutes = (pathPrefix: string): HeaderRoutes => [
const customHeaderFilter =
(route: Route, pathPrefix: string) =>
(h: IHeader["headers"][0]): boolean => {
for (const baseHeader of BASE_HEADERS) {
for (const baseHeader of MUST_REVALIDATE_HEADERS) {
if (headersAreEqual(baseHeader, h)) {
return false
}
Expand Down

0 comments on commit d328fd8

Please sign in to comment.