Skip to content

Commit 56f8e6b

Browse files
committed
fix cache control for fully-static pages router page
1 parent 579ed15 commit 56f8e6b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/open-next/src/core/routing/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ export function fixCacheHeaderForHtmlPages(
243243
const localizedPath = localizePath(internalEvent);
244244
// WORKAROUND: `NextServer` does not set cache headers for HTML pages
245245
// https://opennext.js.org/aws/v2/advanced/workaround#workaround-nextserver-does-not-set-cache-headers-for-html-pages
246-
if (HtmlPages.includes(localizedPath)) {
246+
// We need to not cache if the request contains an `x-middleware-prefetch` header
247+
if (
248+
HtmlPages.includes(localizedPath) &&
249+
!internalEvent.headers["x-middleware-prefetch"]
250+
) {
247251
headers[CommonHeaders.CACHE_CONTROL] =
248252
"public, max-age=0, s-maxage=31536000, must-revalidate";
249253
}

packages/tests-unit/tests/core/routing/util.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ describe("fixCacheHeaderForHtmlPages", () => {
487487
fixCacheHeaderForHtmlPages(
488488
{
489489
rawPath: "/my-html-page",
490+
headers: {},
490491
},
491492
headers,
492493
);
@@ -496,6 +497,23 @@ describe("fixCacheHeaderForHtmlPages", () => {
496497
);
497498
});
498499

500+
it("should not add cache-control header for html page but with an `x-middleware-prefetch` header", () => {
501+
const headers: Record<string, string> = {};
502+
config.HtmlPages.push("/my-html-page");
503+
504+
fixCacheHeaderForHtmlPages(
505+
{
506+
rawPath: "/my-html-page",
507+
headers: {
508+
"x-middleware-prefetch": "1",
509+
},
510+
},
511+
headers,
512+
);
513+
514+
expect(headers).not.toHaveProperty("cache-control");
515+
});
516+
499517
it("should not add cache-control header for non html page", () => {
500518
const headers: Record<string, string> = {};
501519

0 commit comments

Comments
 (0)