From 56313082a86cb92512a6219bc54976fc171ced4e Mon Sep 17 00:00:00 2001 From: Dorseuil Nicolas Date: Tue, 22 Oct 2024 22:08:54 +0200 Subject: [PATCH 1/3] fix 500 error page cached --- packages/open-next/src/http/openNextResponse.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts index d6d457a18..40ef47a45 100644 --- a/packages/open-next/src/http/openNextResponse.ts +++ b/packages/open-next/src/http/openNextResponse.ts @@ -182,6 +182,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { : [...initialCookies, ...this._cookies]; } this.fixHeaders(this.headers); + this.fixHeadersForError(); // We need to fix the set-cookie header here this.headers[SET_COOKIE_HEADER] = this._cookies; @@ -275,6 +276,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { getFixedHeaders(): OutgoingHttpHeaders { // Do we want to apply this on writeHead? this.fixHeaders(this.headers); + this.fixHeadersForError(); // This way we ensure that the cookies are correct this.headers[SET_COOKIE_HEADER] = this._cookies; return this.headers; @@ -384,4 +386,16 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { //TODO: test to see if we need to call end here return this; } + + // For some reason, next returns the 500 error page with some cache-control headers + // We need to fix that + private fixHeadersForError() { + // We only check for 404 and 500 errors + // The rest should be errors that are handled by the user and they should set the cache headers themselves + if (this.statusCode === 404 || this.statusCode === 500) { + this.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + this.setHeader("Pragma", "no-cache"); + this.setHeader("Expires", "0"); + } + } } From 3d253bb81f21132c349ec57ad407e05ac742499a Mon Sep 17 00:00:00 2001 From: Dorseuil Nicolas Date: Fri, 25 Oct 2024 13:19:51 +0200 Subject: [PATCH 2/3] fix issue and add a bypass --- packages/open-next/src/core/routing/util.ts | 3 +++ packages/open-next/src/http/openNextResponse.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index 31e3842f8..a29b96efd 100644 --- a/packages/open-next/src/core/routing/util.ts +++ b/packages/open-next/src/core/routing/util.ts @@ -288,6 +288,9 @@ export function fixCacheHeaderForHtmlPages( ) { // We don't want to cache error pages if (rawPath === "/404" || rawPath === "/500") { + if (process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === "true") { + return; + } headers[CommonHeaders.CACHE_CONTROL] = "private, no-cache, no-store, max-age=0, must-revalidate"; return; diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts index 40ef47a45..9124e431f 100644 --- a/packages/open-next/src/http/openNextResponse.ts +++ b/packages/open-next/src/http/openNextResponse.ts @@ -390,12 +390,15 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { // For some reason, next returns the 500 error page with some cache-control headers // We need to fix that private fixHeadersForError() { + if(process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === 'true') { + return; + } // We only check for 404 and 500 errors // The rest should be errors that are handled by the user and they should set the cache headers themselves if (this.statusCode === 404 || this.statusCode === 500) { - this.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - this.setHeader("Pragma", "no-cache"); - this.setHeader("Expires", "0"); + // For some reason calling this.setHeader("Cache-Control", "no-cache, no-store, must-revalidate") does not work here + // The function is not even called, i'm probably missing something obvious + this.headers["cache-control"] = "private, no-cache, no-store, max-age=0, must-revalidate"; } } } From 09e5bd9df58d517cdb9ffd63a7052c29cda46119 Mon Sep 17 00:00:00 2001 From: Dorseuil Nicolas Date: Fri, 25 Oct 2024 13:24:16 +0200 Subject: [PATCH 3/3] fix linting --- packages/open-next/src/http/openNextResponse.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts index 9124e431f..f062eeb10 100644 --- a/packages/open-next/src/http/openNextResponse.ts +++ b/packages/open-next/src/http/openNextResponse.ts @@ -390,7 +390,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { // For some reason, next returns the 500 error page with some cache-control headers // We need to fix that private fixHeadersForError() { - if(process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === 'true') { + if (process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === "true") { return; } // We only check for 404 and 500 errors @@ -398,7 +398,8 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { if (this.statusCode === 404 || this.statusCode === 500) { // For some reason calling this.setHeader("Cache-Control", "no-cache, no-store, must-revalidate") does not work here // The function is not even called, i'm probably missing something obvious - this.headers["cache-control"] = "private, no-cache, no-store, max-age=0, must-revalidate"; + this.headers["cache-control"] = + "private, no-cache, no-store, max-age=0, must-revalidate"; } } }