Skip to content

Commit 9f6eeab

Browse files
authored
fix 500 error page being cached (#590)
* fix 500 error page cached * fix issue and add a bypass * fix linting
1 parent 4d2ea31 commit 9f6eeab

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/open-next/src/core/routing/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ export function fixCacheHeaderForHtmlPages(
288288
) {
289289
// We don't want to cache error pages
290290
if (rawPath === "/404" || rawPath === "/500") {
291+
if (process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === "true") {
292+
return;
293+
}
291294
headers[CommonHeaders.CACHE_CONTROL] =
292295
"private, no-cache, no-store, max-age=0, must-revalidate";
293296
return;

packages/open-next/src/http/openNextResponse.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
182182
: [...initialCookies, ...this._cookies];
183183
}
184184
this.fixHeaders(this.headers);
185+
this.fixHeadersForError();
185186

186187
// We need to fix the set-cookie header here
187188
this.headers[SET_COOKIE_HEADER] = this._cookies;
@@ -275,6 +276,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
275276
getFixedHeaders(): OutgoingHttpHeaders {
276277
// Do we want to apply this on writeHead?
277278
this.fixHeaders(this.headers);
279+
this.fixHeadersForError();
278280
// This way we ensure that the cookies are correct
279281
this.headers[SET_COOKIE_HEADER] = this._cookies;
280282
return this.headers;
@@ -384,4 +386,20 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
384386
//TODO: test to see if we need to call end here
385387
return this;
386388
}
389+
390+
// For some reason, next returns the 500 error page with some cache-control headers
391+
// We need to fix that
392+
private fixHeadersForError() {
393+
if (process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === "true") {
394+
return;
395+
}
396+
// We only check for 404 and 500 errors
397+
// The rest should be errors that are handled by the user and they should set the cache headers themselves
398+
if (this.statusCode === 404 || this.statusCode === 500) {
399+
// For some reason calling this.setHeader("Cache-Control", "no-cache, no-store, must-revalidate") does not work here
400+
// The function is not even called, i'm probably missing something obvious
401+
this.headers["cache-control"] =
402+
"private, no-cache, no-store, max-age=0, must-revalidate";
403+
}
404+
}
387405
}

0 commit comments

Comments
 (0)