Skip to content
Discussion options

You must be logged in to vote

The issue is that modifying the cache-control header in afterResponse doesn't affect caching because Got has already decided wether to cache the response by that point.

To force caching, you can use the cacheOptions to customize the cache behavior:

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: {
		bef…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@devadathanmb
Comment options

Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1700 on April 27, 2021 12:23.