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 diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index b24f896c4..a97953a33 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)) { + // Requests containing an `x-middleware-prefetch` header must not be cached + 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 = {};