From d61142471b2aefc5a9747a510b8e3040beb900bc Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 15 Apr 2026 13:37:41 +0800 Subject: [PATCH] perf: disable cacheImmutable by default --- README.md | 13 ++++++++----- src/index.js | 2 +- src/middleware.js | 3 +-- test/middleware.test.js | 10 ++++++---- types/index.d.ts | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8963093..6002995 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ See [below](#other-servers) for examples of use with other servers. | **[`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` | `true` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. | +| **[`cacheImmutable`](#cacheimmutable)** | `boolean` | `false` | 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. | @@ -208,13 +208,16 @@ Enable or disable setting `Cache-Control` response header. ### cacheImmutable Type: `Boolean` -Default: `true` +Default: `false` Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets (i.e. asset with a hash like `image.a4c12bde.jpg`). + Immutable assets are assets that have their hash in the file name therefore they can be cached, because if you change their contents the file name will be changed. -When omitted, immutable assets use this header by default. -Set `cacheImmutable: false` to fall back to the `cacheControl` option even for immutable assets. -This takes precedence over the `cacheControl` option only when the asset was defined as immutable and `cacheImmutable` is not `false`. + +When omitted, immutable assets fall back to the `cacheControl` option. + +Set `cacheImmutable: true` to opt into the immutable cache header for hashed assets. +This takes precedence over the `cacheControl` option only when the asset was defined as immutable and `cacheImmutable` is `true`. ### publicPath diff --git a/src/index.js b/src/index.js index edd630b..2fe1d5c 100644 --- a/src/index.js +++ b/src/index.js @@ -126,7 +126,7 @@ const noop = () => {}; * @property {"weak" | "strong"=} etag options to generate etag header * @property {boolean=} lastModified options to generate last modified header * @property {(boolean | number | string | { maxAge?: number, immutable?: boolean })=} cacheControl options to generate cache headers - * @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to true when omitted) + * @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to false when omitted) */ /** diff --git a/src/middleware.js b/src/middleware.js index 14f7246..45cce79 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -621,8 +621,7 @@ function wrapper(context) { if (!getResponseHeader(res, "Cache-Control")) { const { cacheControl, cacheImmutable } = context.options; - const useImmutableCache = - (cacheImmutable === undefined || cacheImmutable) && extra.immutable; + const useImmutableCache = cacheImmutable === true && extra.immutable; let cacheControlValue; diff --git a/test/middleware.test.js b/test/middleware.test.js index f2210a6..05be2fc 100644 --- a/test/middleware.test.js +++ b/test/middleware.test.js @@ -6262,6 +6262,7 @@ describe.each([ name, framework, compiler, + { cacheImmutable: true }, ); }); @@ -6306,6 +6307,7 @@ describe.each([ name, framework, compiler, + { cacheImmutable: true }, ); }); @@ -6350,7 +6352,7 @@ describe.each([ name, framework, compiler, - { cacheControl: 1000000 }, + { cacheImmutable: true, cacheControl: 1000000 }, ); }); @@ -6603,7 +6605,7 @@ describe.each([ }); }); - describe("should use cacheControl object option with explicit immutable false", () => { + describe("should use cacheControl object option with explicit immutable false when cacheImmutable is not enabled", () => { beforeEach(async () => { const compiler = getCompiler({ ...webpackConfigImmutable, @@ -6634,7 +6636,7 @@ describe.each([ ); }); - it('should return the "200" code for the "GET" request to the immutable asset and generate `Cache-Control` header for the immutable asset by default', async () => { + it('should return the "200" code for the "GET" request to the immutable asset and generate `Cache-Control` header from cacheControl without immutable', async () => { await req.get("/main.js"); const response = await req.get( @@ -6644,7 +6646,7 @@ describe.each([ expect(response.statusCode).toBe(200); expect(response.headers["cache-control"]).toBeDefined(); expect(response.headers["cache-control"]).toBe( - "public, max-age=31536000, immutable", + "public, max-age=3000", ); }); }); diff --git a/types/index.d.ts b/types/index.d.ts index c6d271a..805b429 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -95,7 +95,7 @@ * @property {"weak" | "strong"=} etag options to generate etag header * @property {boolean=} lastModified options to generate last modified header * @property {(boolean | number | string | { maxAge?: number, immutable?: boolean })=} cacheControl options to generate cache headers - * @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to true when omitted) + * @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to false when omitted) */ /** * @template {IncomingMessage} [RequestInternal=IncomingMessage] @@ -363,7 +363,7 @@ export type Options< ) | undefined; /** - * enable immutable cache headers for immutable assets (defaults to true when omitted) + * enable immutable cache headers for immutable assets (defaults to false when omitted) */ cacheImmutable?: boolean | undefined; };