-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-adapter-netlify): adapter use headerRoutes (#38652)
* simplified header rules * lint * lint * update test/snapshot * update snapshot * add snapshot for headerRoutes * adapter use headerRoutes * export type * first pass at headers tests * merge conflict fix * lint error * remove accidental nesting * tests * tests * static assets todo * example of permanent caching header assertion * ensure getServerData header has priority over config header for SSR pages * normalize header values before assertions * add page- and app-data header checks * tmp: skip deleting deploys for easier iteration * refactor test a bit so it's easier to assert same thing in multiple tests + add assertions for js assets * add slice-data headers check * add static query result header test --------- Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
- Loading branch information
Showing
9 changed files
with
294 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { WorkaroundCachedResponse } from "../utils/dont-cache-responses-in-browser" | ||
|
||
describe("Headers", () => { | ||
const defaultHeaders = { | ||
"x-xss-protection": "1; mode=block", | ||
"x-content-type-options": "nosniff", | ||
"referrer-policy": "same-origin", | ||
"x-frame-options": "DENY", | ||
} | ||
|
||
// DRY for repeated assertions in multple tests | ||
const expectedHeadersByRouteAlias = { | ||
"@app-data": { | ||
...defaultHeaders, | ||
"cache-control": "public,max-age=0,must-revalidate", | ||
}, | ||
"@page-data": { | ||
...defaultHeaders, | ||
"cache-control": "public,max-age=0,must-revalidate", | ||
}, | ||
"@slice-data": { | ||
...defaultHeaders, | ||
"cache-control": "public,max-age=0,must-revalidate", | ||
}, | ||
"@static-query-result": { | ||
...defaultHeaders, | ||
"cache-control": "public,max-age=0,must-revalidate", | ||
}, | ||
"@img-webpack-import": { | ||
...defaultHeaders, | ||
"cache-control": "public,max-age=31536000,immutable", | ||
}, | ||
"@js": { | ||
...defaultHeaders, | ||
"cache-control": "public,max-age=31536000,immutable", | ||
}, | ||
} | ||
|
||
// `ntl serve` and actual deploy seem to have possible slight differences around header value formatting | ||
// so this just remove spaces around commas to make it easier to compare | ||
function normalizeHeaderValue(value: string | undefined): string | undefined { | ||
if (typeof value === "undefined") { | ||
return value | ||
} | ||
// Remove spaces around commas | ||
return value.replace(/\s*,\s*/gm, `,`) | ||
} | ||
function checkHeaders( | ||
routeAlias: string, | ||
expectedHeaders?: Record<string, string> | ||
) { | ||
if (!expectedHeaders) { | ||
expectedHeaders = expectedHeadersByRouteAlias[routeAlias] | ||
} | ||
|
||
if (!expectedHeaders) { | ||
throw new Error(`No expected headers provided for "${routeAlias}`) | ||
} | ||
|
||
cy.wait(routeAlias).then(interception => { | ||
Object.keys(expectedHeaders).forEach(headerKey => { | ||
const headers = interception.response.headers[headerKey] | ||
|
||
const firstHeader: string = Array.isArray(headers) | ||
? headers[0] | ||
: headers | ||
|
||
expect(normalizeHeaderValue(firstHeader)).to.eq( | ||
normalizeHeaderValue(expectedHeaders[headerKey]) | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
beforeEach(() => { | ||
cy.intercept("/", WorkaroundCachedResponse).as("index") | ||
cy.intercept("routes/ssr/static", WorkaroundCachedResponse).as("ssr") | ||
cy.intercept("routes/dsg/static", WorkaroundCachedResponse).as("dsg") | ||
|
||
cy.intercept("**/page-data.json", WorkaroundCachedResponse).as("page-data") | ||
cy.intercept("**/app-data.json", WorkaroundCachedResponse).as("app-data") | ||
cy.intercept("**/slice-data/*.json", WorkaroundCachedResponse).as( | ||
"slice-data" | ||
) | ||
cy.intercept("**/page-data/sq/d/*.json", WorkaroundCachedResponse).as( | ||
"static-query-result" | ||
) | ||
|
||
cy.intercept("/static/astro-**.png", WorkaroundCachedResponse).as( | ||
"img-webpack-import" | ||
) | ||
cy.intercept("*.js", WorkaroundCachedResponse).as("js") | ||
}) | ||
|
||
it("should contain correct headers for index page", () => { | ||
cy.visit("/").waitForRouteChange() | ||
|
||
checkHeaders("@index", { | ||
...defaultHeaders, | ||
"x-custom-header": "my custom header value", | ||
"cache-control": "public,max-age=0,must-revalidate", | ||
}) | ||
|
||
checkHeaders("@app-data") | ||
checkHeaders("@page-data") | ||
checkHeaders("@slice-data") | ||
checkHeaders("@static-query-result") | ||
|
||
// index page is only one showing webpack imported image | ||
checkHeaders("@img-webpack-import") | ||
checkHeaders("@js") | ||
}) | ||
|
||
it("should contain correct headers for ssr page", () => { | ||
cy.visit("routes/ssr/static").waitForRouteChange() | ||
|
||
checkHeaders("@ssr", { | ||
...defaultHeaders, | ||
"x-custom-header": "my custom header value", | ||
"x-ssr-header": "my custom header value from config", | ||
"x-ssr-header-getserverdata": "my custom header value from getServerData", | ||
"x-ssr-header-overwrite": "getServerData wins", | ||
}) | ||
|
||
checkHeaders("@app-data") | ||
// page-data is baked into SSR page so it's not fetched and we don't assert it | ||
checkHeaders("@slice-data") | ||
checkHeaders("@static-query-result") | ||
checkHeaders("@js") | ||
}) | ||
|
||
it("should contain correct headers for dsg page", () => { | ||
cy.visit("routes/dsg/static").waitForRouteChange() | ||
|
||
checkHeaders("@dsg", { | ||
...defaultHeaders, | ||
"x-custom-header": "my custom header value", | ||
"x-dsg-header": "my custom header value", | ||
}) | ||
|
||
checkHeaders("@app-data") | ||
checkHeaders("@page-data") | ||
checkHeaders("@slice-data") | ||
checkHeaders("@static-query-result") | ||
checkHeaders("@js") | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
e2e-tests/adapters/cypress/utils/dont-cache-responses-in-browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { CyHttpMessages } from "cypress/types/net-stubbing" | ||
|
||
/** | ||
* https://docs.cypress.io/api/commands/intercept#cyintercept-and-request-caching | ||
* | ||
* For responses that are to be cached we need to use a trick so browser doesn't cache them | ||
* So this enforces `no-store` cache-control header before response hits the browser | ||
* and then restore original cache-control value for assertions. | ||
*/ | ||
export const WorkaroundCachedResponse = ( | ||
req: CyHttpMessages.IncomingHttpRequest | ||
): void | Promise<void> => { | ||
req.on("before:response", res => { | ||
res.headers["x-original-cache-control"] = res.headers["cache-control"] | ||
res.headers["cache-control"] = "no-store" | ||
}) | ||
req.on("after:response", res => { | ||
res.headers["cache-control"] = res.headers["x-original-cache-control"] | ||
delete res.headers["x-original-cache-control"] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.