Skip to content

Commit 247c26c

Browse files
authored
perf: disable cacheImmutable by default (#39)
1 parent b4c7dfc commit 247c26c

5 files changed

Lines changed: 18 additions & 14 deletions

File tree

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ See [below](#other-servers) for examples of use with other servers.
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` | `true` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. |
75+
| **[`cacheImmutable`](#cacheimmutable)** | `boolean` | `false` | 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. |
@@ -208,13 +208,16 @@ Enable or disable setting `Cache-Control` response header.
208208
### cacheImmutable
209209

210210
Type: `Boolean`
211-
Default: `true`
211+
Default: `false`
212212

213213
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`).
214+
214215
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.
215-
When omitted, immutable assets use this header by default.
216-
Set `cacheImmutable: false` to fall back to the `cacheControl` option even for immutable assets.
217-
This takes precedence over the `cacheControl` option only when the asset was defined as immutable and `cacheImmutable` is not `false`.
216+
217+
When omitted, immutable assets fall back to the `cacheControl` option.
218+
219+
Set `cacheImmutable: true` to opt into the immutable cache header for hashed assets.
220+
This takes precedence over the `cacheControl` option only when the asset was defined as immutable and `cacheImmutable` is `true`.
218221

219222
### publicPath
220223

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const noop = () => {};
126126
* @property {"weak" | "strong"=} etag options to generate etag header
127127
* @property {boolean=} lastModified options to generate last modified header
128128
* @property {(boolean | number | string | { maxAge?: number, immutable?: boolean })=} cacheControl options to generate cache headers
129-
* @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to true when omitted)
129+
* @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to false when omitted)
130130
*/
131131

132132
/**

src/middleware.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,7 @@ function wrapper(context) {
621621

622622
if (!getResponseHeader(res, "Cache-Control")) {
623623
const { cacheControl, cacheImmutable } = context.options;
624-
const useImmutableCache =
625-
(cacheImmutable === undefined || cacheImmutable) && extra.immutable;
624+
const useImmutableCache = cacheImmutable === true && extra.immutable;
626625

627626
let cacheControlValue;
628627

test/middleware.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6262,6 +6262,7 @@ describe.each([
62626262
name,
62636263
framework,
62646264
compiler,
6265+
{ cacheImmutable: true },
62656266
);
62666267
});
62676268

@@ -6306,6 +6307,7 @@ describe.each([
63066307
name,
63076308
framework,
63086309
compiler,
6310+
{ cacheImmutable: true },
63096311
);
63106312
});
63116313

@@ -6350,7 +6352,7 @@ describe.each([
63506352
name,
63516353
framework,
63526354
compiler,
6353-
{ cacheControl: 1000000 },
6355+
{ cacheImmutable: true, cacheControl: 1000000 },
63546356
);
63556357
});
63566358

@@ -6603,7 +6605,7 @@ describe.each([
66036605
});
66046606
});
66056607

6606-
describe("should use cacheControl object option with explicit immutable false", () => {
6608+
describe("should use cacheControl object option with explicit immutable false when cacheImmutable is not enabled", () => {
66076609
beforeEach(async () => {
66086610
const compiler = getCompiler({
66096611
...webpackConfigImmutable,
@@ -6634,7 +6636,7 @@ describe.each([
66346636
);
66356637
});
66366638

6637-
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 () => {
6639+
it('should return the "200" code for the "GET" request to the immutable asset and generate `Cache-Control` header from cacheControl without immutable', async () => {
66386640
await req.get("/main.js");
66396641

66406642
const response = await req.get(
@@ -6644,7 +6646,7 @@ describe.each([
66446646
expect(response.statusCode).toBe(200);
66456647
expect(response.headers["cache-control"]).toBeDefined();
66466648
expect(response.headers["cache-control"]).toBe(
6647-
"public, max-age=31536000, immutable",
6649+
"public, max-age=3000",
66486650
);
66496651
});
66506652
});

types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
* @property {"weak" | "strong"=} etag options to generate etag header
9696
* @property {boolean=} lastModified options to generate last modified header
9797
* @property {(boolean | number | string | { maxAge?: number, immutable?: boolean })=} cacheControl options to generate cache headers
98-
* @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to true when omitted)
98+
* @property {boolean=} cacheImmutable enable immutable cache headers for immutable assets (defaults to false when omitted)
9999
*/
100100
/**
101101
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
@@ -363,7 +363,7 @@ export type Options<
363363
)
364364
| undefined;
365365
/**
366-
* enable immutable cache headers for immutable assets (defaults to true when omitted)
366+
* enable immutable cache headers for immutable assets (defaults to false when omitted)
367367
*/
368368
cacheImmutable?: boolean | undefined;
369369
};

0 commit comments

Comments
 (0)