[question] force cache #1701
-
What would you like to discuss?How do I force cache requests with hooks? I have tried this and it doesn't work. const instance = got.extend({
cache: new Map(),
hooks: {
afterResponse: [
(response) => {
response.headers['cache-control'] = 'max-age=600';
return response;
},
],
},
});Checklist
|
Beta Was this translation helpful? Give feedback.
Answered by
sindresorhus
Oct 11, 2025
Replies: 1 comment 1 reply
-
|
The issue is that modifying the To force caching, you can use the import got from 'got';
import Keyv from 'keyv';
const cache = new Keyv();
const instance = got.extend({
cache,
cacheOptions: {
shared: false,
cacheHeuristic: 1, // Force heuristic caching
immutableMinTimeToLive: 600000, // 10 minutes
ignoreCargoCult: false
}
});Alternatively, if you want to cache everything regardless of headers, you can override the cache key logic: const instance = got.extend({
cache: new Map(),
hooks: {
beforeRequest: [
(options) => {
// Force cache-friendly headers on the request
options.headers['cache-control'] = 'public, max-age=600';
}
]
}
});This way Got will treat all responses as cacheable for 10 minutes. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
sindresorhus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The issue is that modifying the
cache-controlheader inafterResponsedoesn't affect caching because Got has already decided wether to cache the response by that point.To force caching, you can use the
cacheOptionsto customize the cache behavior:Alternatively, if you want to cache everything regardless of headers, you can override the cache key logic: