Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/large-pants-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Fix cache-control header set in middleware being overriden for ISR route
6 changes: 5 additions & 1 deletion packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ export async function revalidateIfRequired(
* @__PURE__
*/
export function fixISRHeaders(headers: OutgoingHttpHeaders) {
const regex = /s-maxage=(\d+)/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do not capture if there is no need (it is slower)

Suggested change
const regex = /s-maxage=(\d+)/;
const regex = /s-maxage=\d+/;

// We only apply the fix if the cache-control header contains s-maxage
if (!headers[CommonHeaders.CACHE_CONTROL]?.match(regex)) {
return;
}
if (headers[CommonHeaders.NEXT_CACHE] === "REVALIDATED") {
headers[CommonHeaders.CACHE_CONTROL] =
"private, no-cache, no-store, max-age=0, must-revalidate";
Expand All @@ -366,7 +371,6 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
// calculate age
const age = Math.round((Date.now() - _lastModified) / 1000);
// extract s-maxage from cache-control
const regex = /s-maxage=(\d+)/;
const cacheControl = headers[CommonHeaders.CACHE_CONTROL];
debug("cache-control", cacheControl, _lastModified, Date.now());
if (typeof cacheControl !== "string") return;
Expand Down
11 changes: 10 additions & 1 deletion packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
headers: OutgoingHttpHeaders = {};
headersSent = false;
_chunks: Buffer[] = [];
headersAlreadyFixed = false;

private _cookies: string[] = [];
private responseStream?: Writable;
Expand Down Expand Up @@ -69,7 +70,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
}

constructor(
private fixHeaders: (headers: OutgoingHttpHeaders) => void,
private fixHeadersFn: (headers: OutgoingHttpHeaders) => void,
private onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
private streamCreator?: StreamCreator,
private initialHeaders?: OutgoingHttpHeaders,
Expand Down Expand Up @@ -271,6 +272,14 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
* OpenNext specific method
*/

fixHeaders(headers: OutgoingHttpHeaders) {
if (this.headersAlreadyFixed) {
return;
}
this.fixHeadersFn(headers);
this.headersAlreadyFixed = true;
}

getFixedHeaders(): OutgoingHttpHeaders {
// Do we want to apply this on writeHead?
this.fixHeaders(this.headers);
Expand Down
15 changes: 14 additions & 1 deletion packages/tests-e2e/tests/appRouter/isr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,26 @@ test("headers", async ({ page }) => {
return response.status() === 200;
});
await page.goto("/isr");
let hasBeenStale = false;
let hasBeenHit = false;

while (true) {
const response = await responsePromise;
const headers = response.headers();
expect(headers["cache-control"]).toBe(
"max-age=10, stale-while-revalidate=999",
);
const cacheHeader =
headers["x-nextjs-cache"] ?? headers["x-opennext-cache"];
if (cacheHeader === "STALE") {
hasBeenStale = true;
}
if (cacheHeader === "HIT") {
hasBeenHit = true;
}

// this was set in middleware
if (headers["cache-control"] === "max-age=10, stale-while-revalidate=999") {
if (hasBeenStale && hasBeenHit) {
break;
}
await page.waitForTimeout(1000);
Expand Down
21 changes: 21 additions & 0 deletions packages/tests-unit/tests/core/routing/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ describe("fixISRHeaders", () => {
it("should set cache-control directive to must-revalidate when x-nextjs-cache is REVALIDATED", () => {
const headers: Record<string, string> = {
"x-nextjs-cache": "REVALIDATED",
"cache-control": "s-maxage=10, stale-while-revalidate=86400",
};
fixISRHeaders(headers);

Expand All @@ -771,13 +772,33 @@ describe("fixISRHeaders", () => {
it("should set cache-control directive to stale-while-revalidate when x-nextjs-cache is STALE", () => {
const headers: Record<string, string> = {
"x-nextjs-cache": "STALE",
"cache-control": "s-maxage=86400", // 1 day
};
fixISRHeaders(headers);

expect(headers["cache-control"]).toBe(
"s-maxage=2, stale-while-revalidate=2592000",
);
});

it("should not modify cache-control when cache-control is missing", () => {
const headers: Record<string, string> = {
"x-nextjs-cache": "HIT",
};
fixISRHeaders(headers);

expect(headers["cache-control"]).toBeUndefined();
});

it("should not modify cache-control when cache-control has no s-maxage", () => {
const headers: Record<string, string> = {
"x-nextjs-cache": "HIT",
"cache-control": "private, max-age=0",
};
fixISRHeaders(headers);

expect(headers["cache-control"]).toBe("private, max-age=0");
});
});

describe("invalidateCDNOnRequest", () => {
Expand Down