Skip to content
Merged
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
3 changes: 3 additions & 0 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -384,4 +386,20 @@ 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() {
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) {
// 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";
}
}
}