Skip to content

Commit c1be825

Browse files
conico974vicb
andauthored
fix for fully-static pages router page (#814)
* fix cache control for fully-static pages router page * changeset * Update packages/open-next/src/core/routing/util.ts Co-authored-by: Victor Berchet <victor@suumit.com> --------- Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 579ed15 commit c1be825

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.changeset/grumpy-pens-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix cache-control header for fully static page router route

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+
// Requests containing an `x-middleware-prefetch` header must not be cached
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)