Skip to content

fix for fully-static pages router page #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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)) {
// 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";
}
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