From ce5aa9190e7b9ee83cfa36f183c9d27b531a3f55 Mon Sep 17 00:00:00 2001 From: conico974 Date: Fri, 27 Dec 2024 14:33:11 +0100 Subject: [PATCH 1/2] fix html cache headers with i18n --- packages/open-next/src/core/routing/util.ts | 10 +-- .../tests/core/routing/util.test.ts | 66 ++++++++++++++++++- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index 947d58b36..622bf311f 100644 --- a/packages/open-next/src/core/routing/util.ts +++ b/packages/open-next/src/core/routing/util.ts @@ -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"; /** * @@ -196,11 +197,11 @@ 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; } @@ -208,9 +209,10 @@ export function fixCacheHeaderForHtmlPages( "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"; } @@ -407,7 +409,7 @@ export function createServerResponse( ) { return new OpenNextNodeResponse( (_headers) => { - fixCacheHeaderForHtmlPages(internalEvent.rawPath, _headers); + fixCacheHeaderForHtmlPages(internalEvent, _headers); fixSWRCacheHeader(_headers); addOpenNextHeader(_headers); fixISRHeaders(_headers); diff --git a/packages/tests-unit/tests/core/routing/util.test.ts b/packages/tests-unit/tests/core/routing/util.test.ts index bb3f425a3..f77107484 100644 --- a/packages/tests-unit/tests/core/routing/util.test.ts +++ b/packages/tests-unit/tests/core/routing/util.test.ts @@ -448,7 +448,12 @@ describe("fixCacheHeaderForHtmlPages", () => { it("should set cache-control header for /404 page", () => { const headers: Record = {}; - fixCacheHeaderForHtmlPages("/404", headers); + fixCacheHeaderForHtmlPages( + { + rawPath: "/404", + }, + headers, + ); expect(headers["cache-control"]).toBe( "private, no-cache, no-store, max-age=0, must-revalidate", @@ -457,7 +462,12 @@ describe("fixCacheHeaderForHtmlPages", () => { it("should set cache-control header for /500 page", () => { const headers: Record = {}; - fixCacheHeaderForHtmlPages("/500", headers); + fixCacheHeaderForHtmlPages( + { + rawPath: "/500", + }, + headers, + ); expect(headers["cache-control"]).toBe( "private, no-cache, no-store, max-age=0, must-revalidate", @@ -468,7 +478,12 @@ describe("fixCacheHeaderForHtmlPages", () => { const headers: Record = {}; 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", @@ -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 = {}; + 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 = {}; + 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", () => { From dfda974f6cfcf4bad36df0e77ea73495771f261c Mon Sep 17 00:00:00 2001 From: conico974 Date: Sat, 28 Dec 2024 17:02:40 +0100 Subject: [PATCH 2/2] Create moody-coins-build.md --- .changeset/moody-coins-build.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/moody-coins-build.md diff --git a/.changeset/moody-coins-build.md b/.changeset/moody-coins-build.md new file mode 100644 index 000000000..09a3b3c55 --- /dev/null +++ b/.changeset/moody-coins-build.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +fix html cache headers with i18n