Skip to content

Conversation

@fredericDelaporte
Copy link
Contributor

This relates to...

The cacheByDefault states:

The default expiration time to cache responses by if they don't have an explicit expiration. If this isn't present, responses without explicit expiration will not be cached.

That is wrong. If the response has a last-modified header, an heuristic expiry will be used by current Undici code base instead of cacheByDefault. If the response is heuristically cacheable, it will be cached even if cacheByDefault is undefined.

See

https://github.com/nodejs/undici/blob/4608ef157cc75d5ce3d2802e6949cb865d73146c/lib/handler/cache-handler.js#L346C26-L358

https://github.com/nodejs/undici/blob/4608ef157cc75d5ce3d2802e6949cb865d73146c/lib/handler/cache-handler.js#L115C1-L125C6

Changes

  • Update cacheByDefault about heuristic caching.

Features

N/A

Bug Fixes

  • Inaccurate/outdated documentation.

Breaking Changes and Deprecations

N/A

Status

- `store` - The [`CacheStore`](/docs/docs/api/CacheStore.md) to store and retrieve responses from. Default is [`MemoryCacheStore`](/docs/docs/api/CacheStore.md#memorycachestore).
- `methods` - The [**safe** HTTP methods](https://www.rfc-editor.org/rfc/rfc9110#section-9.2.1) to cache the response of.
- `cacheByDefault` - The default expiration time to cache responses by if they don't have an explicit expiration. If this isn't present, responses without explicit expiration will not be cached. Default `undefined`.
- `cacheByDefault` - The default expiration time to cache responses by if they don't have an explicit expiration and cannot have an heuristic expiry computed. If this isn't present, responses neither with an explicit expiration nor heuristically cacheable will not be cached. Default `undefined`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestion of a better wording is welcome. I do not find this easy to understand. But my attempts to make it more straightforward resulted in a lot more sentences, which ends up no better.

@fredericDelaporte
Copy link
Contributor Author

Actually I find it surprising to have heuristic caching enabled by default in a shared cache (shared in most case at least). I was not expecting to have heuristic caching at all in the cache interceptor. And I wonder if that is really a desirable default feature of it.

Also see #4335. Correctly supporting heuristic caching while not disabling more desirable caching (in my opinion at least) like caching of responses with explicit caching directive (but non heuristically cacheable status) is a bit complicated.

@mcollina mcollina merged commit 44fc6d1 into nodejs:main Jul 17, 2025
25 of 28 checks passed
@fredericDelaporte fredericDelaporte deleted the fixes/cache-by-default branch July 17, 2025 15:58
@github-actions github-actions bot mentioned this pull request Jul 18, 2025
@JoshMock JoshMock mentioned this pull request Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants