Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {

import { debug, error } from "../../adapters/logger.js";
import { isBinaryContentType } from "../../utils/binary.js";
import { localizePath } from "./i18n/index.js";

/**
*
Expand Down Expand Up @@ -196,21 +197,22 @@ enum CommonHeaders {
* @__PURE__
*/
export function fixCacheHeaderForHtmlPages(
rawPath: string,
internalEvent: InternalEvent,
headers: OutgoingHttpHeaders,
) {
// We don't want to cache error pages
if (rawPath === "/404" || rawPath === "/500") {
if (internalEvent.rawPath === "/404" || internalEvent.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;
}
const localizedPath = localizePath(internalEvent);
// WORKAROUND: `NextServer` does not set cache headers for HTML pages
// https://opennext.js.org/aws/v2/advanced/workaround#workaround-nextserver-does-not-set-cache-headers-for-html-pages
if (HtmlPages.includes(rawPath)) {
if (HtmlPages.includes(localizedPath)) {
headers[CommonHeaders.CACHE_CONTROL] =
"public, max-age=0, s-maxage=31536000, must-revalidate";
}
Expand Down Expand Up @@ -407,7 +409,7 @@ export function createServerResponse(
) {
return new OpenNextNodeResponse(
(_headers) => {
fixCacheHeaderForHtmlPages(internalEvent.rawPath, _headers);
fixCacheHeaderForHtmlPages(internalEvent, _headers);
fixSWRCacheHeader(_headers);
addOpenNextHeader(_headers);
fixISRHeaders(_headers);
Expand Down
66 changes: 63 additions & 3 deletions packages/tests-unit/tests/core/routing/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,12 @@ describe("fixCacheHeaderForHtmlPages", () => {

it("should set cache-control header for /404 page", () => {
const headers: Record<string, string> = {};
fixCacheHeaderForHtmlPages("/404", headers);
fixCacheHeaderForHtmlPages(
{
rawPath: "/404",
},
headers,
);

expect(headers["cache-control"]).toBe(
"private, no-cache, no-store, max-age=0, must-revalidate",
Expand All @@ -457,7 +462,12 @@ describe("fixCacheHeaderForHtmlPages", () => {

it("should set cache-control header for /500 page", () => {
const headers: Record<string, string> = {};
fixCacheHeaderForHtmlPages("/500", headers);
fixCacheHeaderForHtmlPages(
{
rawPath: "/500",
},
headers,
);

expect(headers["cache-control"]).toBe(
"private, no-cache, no-store, max-age=0, must-revalidate",
Expand All @@ -468,7 +478,12 @@ describe("fixCacheHeaderForHtmlPages", () => {
const headers: Record<string, string> = {};
config.HtmlPages.push("/my-html-page");

fixCacheHeaderForHtmlPages("/my-html-page", headers);
fixCacheHeaderForHtmlPages(
{
rawPath: "/my-html-page",
},
headers,
);

expect(headers["cache-control"]).toBe(
"public, max-age=0, s-maxage=31536000, must-revalidate",
Expand All @@ -482,6 +497,51 @@ describe("fixCacheHeaderForHtmlPages", () => {

expect(headers).not.toHaveProperty("cache-control");
});

it("should add cache-control header for html page with default i18n", () => {
const headers: Record<string, string> = {};
config.HtmlPages.push("/en/my-html-page");
config.NextConfig.i18n = {
defaultLocale: "en",
locales: ["en", "fr"],
};

fixCacheHeaderForHtmlPages(
{
rawPath: "/my-html-page",
cookies: {},
headers: {},
},
headers,
);

expect(headers["cache-control"]).toBe(
"public, max-age=0, s-maxage=31536000, must-revalidate",
);

config.NextConfig.i18n = undefined;
});

it("should add cache-control header for html page with locale", () => {
const headers: Record<string, string> = {};
config.HtmlPages.push("/en/my-html-page");
config.HtmlPages.push("/fr/my-html-page");

fixCacheHeaderForHtmlPages(
{
rawPath: "/en/my-html-page",
cookies: {},
headers: {
"accept-language": "fr",
},
},
headers,
);

expect(headers["cache-control"]).toBe(
"public, max-age=0, s-maxage=31536000, must-revalidate",
);
});
});

describe("fixSWRCacheHeader", () => {
Expand Down
Loading