Skip to content

Commit 4e88b47

Browse files
authored
fix html cache headers with i18n (#685)
* fix html cache headers with i18n * Create moody-coins-build.md
1 parent 9595714 commit 4e88b47

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

Diff for: .changeset/moody-coins-build.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix html cache headers with i18n

Diff for: packages/open-next/src/core/routing/util.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515

1616
import { debug, error } from "../../adapters/logger.js";
1717
import { isBinaryContentType } from "../../utils/binary.js";
18+
import { localizePath } from "./i18n/index.js";
1819

1920
/**
2021
*
@@ -196,21 +197,22 @@ enum CommonHeaders {
196197
* @__PURE__
197198
*/
198199
export function fixCacheHeaderForHtmlPages(
199-
rawPath: string,
200+
internalEvent: InternalEvent,
200201
headers: OutgoingHttpHeaders,
201202
) {
202203
// We don't want to cache error pages
203-
if (rawPath === "/404" || rawPath === "/500") {
204+
if (internalEvent.rawPath === "/404" || internalEvent.rawPath === "/500") {
204205
if (process.env.OPEN_NEXT_DANGEROUSLY_SET_ERROR_HEADERS === "true") {
205206
return;
206207
}
207208
headers[CommonHeaders.CACHE_CONTROL] =
208209
"private, no-cache, no-store, max-age=0, must-revalidate";
209210
return;
210211
}
212+
const localizedPath = localizePath(internalEvent);
211213
// WORKAROUND: `NextServer` does not set cache headers for HTML pages
212214
// https://opennext.js.org/aws/v2/advanced/workaround#workaround-nextserver-does-not-set-cache-headers-for-html-pages
213-
if (HtmlPages.includes(rawPath)) {
215+
if (HtmlPages.includes(localizedPath)) {
214216
headers[CommonHeaders.CACHE_CONTROL] =
215217
"public, max-age=0, s-maxage=31536000, must-revalidate";
216218
}
@@ -407,7 +409,7 @@ export function createServerResponse(
407409
) {
408410
return new OpenNextNodeResponse(
409411
(_headers) => {
410-
fixCacheHeaderForHtmlPages(internalEvent.rawPath, _headers);
412+
fixCacheHeaderForHtmlPages(internalEvent, _headers);
411413
fixSWRCacheHeader(_headers);
412414
addOpenNextHeader(_headers);
413415
fixISRHeaders(_headers);

Diff for: packages/tests-unit/tests/core/routing/util.test.ts

+63-3
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ describe("fixCacheHeaderForHtmlPages", () => {
448448

449449
it("should set cache-control header for /404 page", () => {
450450
const headers: Record<string, string> = {};
451-
fixCacheHeaderForHtmlPages("/404", headers);
451+
fixCacheHeaderForHtmlPages(
452+
{
453+
rawPath: "/404",
454+
},
455+
headers,
456+
);
452457

453458
expect(headers["cache-control"]).toBe(
454459
"private, no-cache, no-store, max-age=0, must-revalidate",
@@ -457,7 +462,12 @@ describe("fixCacheHeaderForHtmlPages", () => {
457462

458463
it("should set cache-control header for /500 page", () => {
459464
const headers: Record<string, string> = {};
460-
fixCacheHeaderForHtmlPages("/500", headers);
465+
fixCacheHeaderForHtmlPages(
466+
{
467+
rawPath: "/500",
468+
},
469+
headers,
470+
);
461471

462472
expect(headers["cache-control"]).toBe(
463473
"private, no-cache, no-store, max-age=0, must-revalidate",
@@ -468,7 +478,12 @@ describe("fixCacheHeaderForHtmlPages", () => {
468478
const headers: Record<string, string> = {};
469479
config.HtmlPages.push("/my-html-page");
470480

471-
fixCacheHeaderForHtmlPages("/my-html-page", headers);
481+
fixCacheHeaderForHtmlPages(
482+
{
483+
rawPath: "/my-html-page",
484+
},
485+
headers,
486+
);
472487

473488
expect(headers["cache-control"]).toBe(
474489
"public, max-age=0, s-maxage=31536000, must-revalidate",
@@ -482,6 +497,51 @@ describe("fixCacheHeaderForHtmlPages", () => {
482497

483498
expect(headers).not.toHaveProperty("cache-control");
484499
});
500+
501+
it("should add cache-control header for html page with default i18n", () => {
502+
const headers: Record<string, string> = {};
503+
config.HtmlPages.push("/en/my-html-page");
504+
config.NextConfig.i18n = {
505+
defaultLocale: "en",
506+
locales: ["en", "fr"],
507+
};
508+
509+
fixCacheHeaderForHtmlPages(
510+
{
511+
rawPath: "/my-html-page",
512+
cookies: {},
513+
headers: {},
514+
},
515+
headers,
516+
);
517+
518+
expect(headers["cache-control"]).toBe(
519+
"public, max-age=0, s-maxage=31536000, must-revalidate",
520+
);
521+
522+
config.NextConfig.i18n = undefined;
523+
});
524+
525+
it("should add cache-control header for html page with locale", () => {
526+
const headers: Record<string, string> = {};
527+
config.HtmlPages.push("/en/my-html-page");
528+
config.HtmlPages.push("/fr/my-html-page");
529+
530+
fixCacheHeaderForHtmlPages(
531+
{
532+
rawPath: "/en/my-html-page",
533+
cookies: {},
534+
headers: {
535+
"accept-language": "fr",
536+
},
537+
},
538+
headers,
539+
);
540+
541+
expect(headers["cache-control"]).toBe(
542+
"public, max-age=0, s-maxage=31536000, must-revalidate",
543+
);
544+
});
485545
});
486546

487547
describe("fixSWRCacheHeader", () => {

0 commit comments

Comments
 (0)