From 19f9cc6d7fa012cf12a408fcd924fb06747183f9 Mon Sep 17 00:00:00 2001 From: alexander-akait Date: Tue, 27 Jan 2026 15:33:27 +0300 Subject: [PATCH] refactor: logic for cacheImmutable --- README.md | 2 +- src/middleware.js | 38 +++++++++++++++++++------------------- test/middleware.test.js | 6 +++--- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index ce8a7feda..4a7cfbbf2 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ See [below](#other-servers) for an example of use with fastify. | **[`etag`](#tag)** | `boolean\| "weak"\| "strong"` | `undefined` | Enable or disable etag generation. | | **[`lastModified`](#lastmodified)** | `boolean` | `undefined` | Enable or disable `Last-Modified` header. Uses the file system's last modified value. | | **[`cacheControl`](#cachecontrol)** | `boolean\|number\|string\|Object` | `undefined` | Enable or disable setting `Cache-Control` response header. | -| **[`cacheImmutable`](#cacheimmutable)** | `boolean\` | `undefined` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. | +| **[`cacheImmutable`](#cacheimmutable)** | `boolean` | `undefined` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. | | **[`publicPath`](#publicpath)** | `string` | `undefined` | The public path that the middleware is bound to. | | **[`stats`](#stats)** | `boolean\|string\|Object` | `stats` (from a configuration) | Stats options object or preset name. | | **[`serverSideRender`](#serversiderender)** | `boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. | diff --git a/src/middleware.js b/src/middleware.js index ba47666a9..3441f3285 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -571,36 +571,36 @@ function wrapper(context) { } if (!getResponseHeader(res, "Cache-Control")) { - const hasCacheImmutable = - context.options.cacheImmutable === undefined - ? true - : context.options.cacheImmutable; - - let { cacheControl } = context.options; - - // Normalize cacheControl to object - if (typeof cacheControl === "string") { - setResponseHeader(res, "Cache-Control", cacheControl); - } else if (hasCacheImmutable && extra.immutable) { - cacheControl = { immutable: true }; + const { cacheControl, cacheImmutable } = context.options; + + let cacheControlValue; + + if ( + (cacheImmutable === undefined || cacheImmutable) && + extra.immutable + ) { + cacheControlValue = `public, max-age=${Math.floor(MAX_MAX_AGE / 1000)}, immutable`; } else if (typeof cacheControl === "boolean") { - cacheControl = { maxAge: MAX_MAX_AGE }; + cacheControlValue = `public, max-age=${Math.floor(MAX_MAX_AGE / 1000)}`; } else if (typeof cacheControl === "number") { - cacheControl = { maxAge: cacheControl }; - } - - if (cacheControl && typeof cacheControl === "object") { + const maxAge = Math.min(Math.max(0, cacheControl), MAX_MAX_AGE); + cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`; + } else if (typeof cacheControl === "string") { + cacheControlValue = cacheControl; + } else if (cacheControl) { const maxAge = cacheControl.maxAge !== undefined ? Math.min(Math.max(0, cacheControl.maxAge), MAX_MAX_AGE) : MAX_MAX_AGE; - let cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`; + cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`; - if (cacheControl.immutable && hasCacheImmutable) { + if (cacheControl.immutable) { cacheControlValue += ", immutable"; } + } + if (cacheControlValue) { setResponseHeader(res, "Cache-Control", cacheControlValue); } } diff --git a/test/middleware.test.js b/test/middleware.test.js index 7bf3ea687..4e076031b 100644 --- a/test/middleware.test.js +++ b/test/middleware.test.js @@ -6393,7 +6393,7 @@ describe.each([ }); }); - describe("should use cacheControl object option (with only immutable: true) when cacheImmutable is false, and not add 'immutable' to Cache-Control header", () => { + describe("should use cacheControl object option (with only immutable: true) when cacheImmutable is false, and add 'immutable' to Cache-Control header", () => { beforeEach(async () => { const compiler = getCompiler({ ...webpackConfigImmutable, @@ -6420,7 +6420,7 @@ describe.each([ expect(response.statusCode).toBe(200); expect(response.headers["cache-control"]).toBeDefined(); expect(response.headers["cache-control"]).toBe( - "public, max-age=31536000", + "public, max-age=31536000, immutable", ); }); @@ -6430,7 +6430,7 @@ describe.each([ expect(response.statusCode).toBe(200); expect(response.headers["cache-control"]).toBeDefined(); expect(response.headers["cache-control"]).toBe( - "public, max-age=31536000", + "public, max-age=31536000, immutable", ); }); });