From 56f8e6b7d2aa78eba919cf5e079939b494b2101d Mon Sep 17 00:00:00 2001 From: Dorseuil Nicolas Date: Fri, 4 Apr 2025 11:19:08 +0200 Subject: [PATCH 1/3] fix cache control for fully-static pages router page --- packages/open-next/src/core/routing/util.ts | 6 +++++- .../tests-unit/tests/core/routing/util.test.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index b24f896c4..1fafdbc32 100644 --- a/packages/open-next/src/core/routing/util.ts +++ b/packages/open-next/src/core/routing/util.ts @@ -243,7 +243,11 @@ export function fixCacheHeaderForHtmlPages( 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(localizedPath)) { + // We need to not cache if the request contains an `x-middleware-prefetch` header + if ( + HtmlPages.includes(localizedPath) && + !internalEvent.headers["x-middleware-prefetch"] + ) { headers[CommonHeaders.CACHE_CONTROL] = "public, max-age=0, s-maxage=31536000, must-revalidate"; } diff --git a/packages/tests-unit/tests/core/routing/util.test.ts b/packages/tests-unit/tests/core/routing/util.test.ts index 6377371c5..0a2ab151d 100644 --- a/packages/tests-unit/tests/core/routing/util.test.ts +++ b/packages/tests-unit/tests/core/routing/util.test.ts @@ -487,6 +487,7 @@ describe("fixCacheHeaderForHtmlPages", () => { fixCacheHeaderForHtmlPages( { rawPath: "/my-html-page", + headers: {}, }, headers, ); @@ -496,6 +497,23 @@ describe("fixCacheHeaderForHtmlPages", () => { ); }); + it("should not add cache-control header for html page but with an `x-middleware-prefetch` header", () => { + const headers: Record = {}; + config.HtmlPages.push("/my-html-page"); + + fixCacheHeaderForHtmlPages( + { + rawPath: "/my-html-page", + headers: { + "x-middleware-prefetch": "1", + }, + }, + headers, + ); + + expect(headers).not.toHaveProperty("cache-control"); + }); + it("should not add cache-control header for non html page", () => { const headers: Record = {}; From a4bf6d8d1f80a9c180b5ccd3c945926c64ef459b Mon Sep 17 00:00:00 2001 From: Dorseuil Nicolas Date: Fri, 4 Apr 2025 11:40:00 +0200 Subject: [PATCH 2/3] changeset --- .changeset/grumpy-pens-shake.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/grumpy-pens-shake.md diff --git a/.changeset/grumpy-pens-shake.md b/.changeset/grumpy-pens-shake.md new file mode 100644 index 000000000..04536a26e --- /dev/null +++ b/.changeset/grumpy-pens-shake.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +fix cache-control header for fully static page router route From 4fc4f3c7030d845a1aa8c444791b93e7d88ba57e Mon Sep 17 00:00:00 2001 From: conico974 Date: Fri, 4 Apr 2025 11:59:45 +0200 Subject: [PATCH 3/3] Update packages/open-next/src/core/routing/util.ts Co-authored-by: Victor Berchet --- packages/open-next/src/core/routing/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index 1fafdbc32..a97953a33 100644 --- a/packages/open-next/src/core/routing/util.ts +++ b/packages/open-next/src/core/routing/util.ts @@ -243,7 +243,7 @@ export function fixCacheHeaderForHtmlPages( 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 - // We need to not cache if the request contains an `x-middleware-prefetch` header + // Requests containing an `x-middleware-prefetch` header must not be cached if ( HtmlPages.includes(localizedPath) && !internalEvent.headers["x-middleware-prefetch"]