Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/

/**
Expand Down
3 changes: 1 addition & 2 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 6 additions & 4 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6262,6 +6262,7 @@ describe.each([
name,
framework,
compiler,
{ cacheImmutable: true },
);
});

Expand Down Expand Up @@ -6306,6 +6307,7 @@ describe.each([
name,
framework,
compiler,
{ cacheImmutable: true },
);
});

Expand Down Expand Up @@ -6350,7 +6352,7 @@ describe.each([
name,
framework,
compiler,
{ cacheControl: 1000000 },
{ cacheImmutable: true, cacheControl: 1000000 },
);
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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",
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
};
Expand Down