Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-pens-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix cache-control header for fully static page router route
6 changes: 5 additions & 1 deletion packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
18 changes: 18 additions & 0 deletions packages/tests-unit/tests/core/routing/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ describe("fixCacheHeaderForHtmlPages", () => {
fixCacheHeaderForHtmlPages(
{
rawPath: "/my-html-page",
headers: {},
},
headers,
);
Expand All @@ -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<string, string> = {};
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<string, string> = {};

Expand Down
Loading