Skip to content

Commit af8a453

Browse files
bjohansebasalexander-akait
authored andcommitted
feat: enable cacheImmutable by default (#2247)
* feat: enable cacheImmutable by default * fix: thoses files shouldn't immutable * fix: update cache control logic becauses undefined !== false (true) * test: add tests for cacheControl option behavior with cacheImmutable set to false * refactor: normalize cacheControl handling in response headers * refactor: simplify maxAge calculation in cacheControl handling
1 parent aa23684 commit af8a453

2 files changed

Lines changed: 354 additions & 33 deletions

File tree

src/middleware.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -571,38 +571,34 @@ function wrapper(context) {
571571
}
572572

573573
if (!getResponseHeader(res, "Cache-Control")) {
574-
// TODO enable the `cacheImmutable` by default for the next major release
575-
const cacheControl =
576-
context.options.cacheImmutable && extra.immutable
577-
? { immutable: true }
578-
: context.options.cacheControl;
579-
580-
if (cacheControl) {
581-
let cacheControlValue;
582-
583-
if (typeof cacheControl === "boolean") {
584-
cacheControlValue = "public, max-age=31536000";
585-
} else if (typeof cacheControl === "number") {
586-
const maxAge = Math.floor(
587-
Math.min(Math.max(0, cacheControl), MAX_MAX_AGE) / 1000,
588-
);
574+
const hasCacheImmutable =
575+
context.options.cacheImmutable === undefined
576+
? true
577+
: context.options.cacheImmutable;
578+
579+
let { cacheControl } = context.options;
580+
581+
// Normalize cacheControl to object
582+
if (typeof cacheControl === "string") {
583+
setResponseHeader(res, "Cache-Control", cacheControl);
584+
} else if (hasCacheImmutable && extra.immutable) {
585+
cacheControl = { immutable: true };
586+
} else if (typeof cacheControl === "boolean") {
587+
cacheControl = { maxAge: MAX_MAX_AGE };
588+
} else if (typeof cacheControl === "number") {
589+
cacheControl = { maxAge: cacheControl };
590+
}
589591

590-
cacheControlValue = `public, max-age=${maxAge}`;
591-
} else if (typeof cacheControl === "string") {
592-
cacheControlValue = cacheControl;
593-
} else {
594-
const maxAge = cacheControl.maxAge
595-
? Math.floor(
596-
Math.min(Math.max(0, cacheControl.maxAge), MAX_MAX_AGE) /
597-
1000,
598-
)
599-
: MAX_MAX_AGE / 1000;
600-
601-
cacheControlValue = `public, max-age=${maxAge}`;
602-
603-
if (cacheControl.immutable) {
604-
cacheControlValue += ", immutable";
605-
}
592+
if (cacheControl && typeof cacheControl === "object") {
593+
const maxAge =
594+
cacheControl.maxAge !== undefined
595+
? Math.min(Math.max(0, cacheControl.maxAge), MAX_MAX_AGE)
596+
: MAX_MAX_AGE;
597+
598+
let cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`;
599+
600+
if (cacheControl.immutable && hasCacheImmutable) {
601+
cacheControlValue += ", immutable";
606602
}
607603

608604
setResponseHeader(res, "Cache-Control", cacheControlValue);

0 commit comments

Comments
 (0)