Skip to content

Commit b18af24

Browse files
refactor: logic for cacheControl and cacheImmutable (#2254)
1 parent af8a453 commit b18af24

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ See [below](#other-servers) for an example of use with fastify.
7272
| **[`etag`](#tag)** | `boolean\| "weak"\| "strong"` | `undefined` | Enable or disable etag generation. |
7373
| **[`lastModified`](#lastmodified)** | `boolean` | `undefined` | Enable or disable `Last-Modified` header. Uses the file system's last modified value. |
7474
| **[`cacheControl`](#cachecontrol)** | `boolean\|number\|string\|Object` | `undefined` | Enable or disable setting `Cache-Control` response header. |
75-
| **[`cacheImmutable`](#cacheimmutable)** | `boolean\` | `undefined` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. |
75+
| **[`cacheImmutable`](#cacheimmutable)** | `boolean` | `undefined` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. |
7676
| **[`publicPath`](#publicpath)** | `string` | `undefined` | The public path that the middleware is bound to. |
7777
| **[`stats`](#stats)** | `boolean\|string\|Object` | `stats` (from a configuration) | Stats options object or preset name. |
7878
| **[`serverSideRender`](#serversiderender)** | `boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. |

src/middleware.js

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

573573
if (!getResponseHeader(res, "Cache-Control")) {
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 };
574+
const { cacheControl, cacheImmutable } = context.options;
575+
576+
let cacheControlValue;
577+
578+
if (
579+
(cacheImmutable === undefined || cacheImmutable) &&
580+
extra.immutable
581+
) {
582+
cacheControlValue = `public, max-age=${Math.floor(MAX_MAX_AGE / 1000)}, immutable`;
586583
} else if (typeof cacheControl === "boolean") {
587-
cacheControl = { maxAge: MAX_MAX_AGE };
584+
cacheControlValue = `public, max-age=${Math.floor(MAX_MAX_AGE / 1000)}`;
588585
} else if (typeof cacheControl === "number") {
589-
cacheControl = { maxAge: cacheControl };
590-
}
591-
592-
if (cacheControl && typeof cacheControl === "object") {
586+
const maxAge = Math.min(Math.max(0, cacheControl), MAX_MAX_AGE);
587+
cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`;
588+
} else if (typeof cacheControl === "string") {
589+
cacheControlValue = cacheControl;
590+
} else if (cacheControl) {
593591
const maxAge =
594592
cacheControl.maxAge !== undefined
595593
? Math.min(Math.max(0, cacheControl.maxAge), MAX_MAX_AGE)
596594
: MAX_MAX_AGE;
597595

598-
let cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`;
596+
cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`;
599597

600-
if (cacheControl.immutable && hasCacheImmutable) {
598+
if (cacheControl.immutable) {
601599
cacheControlValue += ", immutable";
602600
}
601+
}
603602

603+
if (cacheControlValue) {
604604
setResponseHeader(res, "Cache-Control", cacheControlValue);
605605
}
606606
}

test/middleware.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6393,7 +6393,7 @@ describe.each([
63936393
});
63946394
});
63956395

6396-
describe("should use cacheControl object option (with only immutable: true) when cacheImmutable is false, and not add 'immutable' to Cache-Control header", () => {
6396+
describe("should use cacheControl object option (with only immutable: true) when cacheImmutable is false, and add 'immutable' to Cache-Control header", () => {
63976397
beforeEach(async () => {
63986398
const compiler = getCompiler({
63996399
...webpackConfigImmutable,
@@ -6420,7 +6420,7 @@ describe.each([
64206420
expect(response.statusCode).toBe(200);
64216421
expect(response.headers["cache-control"]).toBeDefined();
64226422
expect(response.headers["cache-control"]).toBe(
6423-
"public, max-age=31536000",
6423+
"public, max-age=31536000, immutable",
64246424
);
64256425
});
64266426

@@ -6430,7 +6430,7 @@ describe.each([
64306430
expect(response.statusCode).toBe(200);
64316431
expect(response.headers["cache-control"]).toBeDefined();
64326432
expect(response.headers["cache-control"]).toBe(
6433-
"public, max-age=31536000",
6433+
"public, max-age=31536000, immutable",
64346434
);
64356435
});
64366436
});

0 commit comments

Comments
 (0)