bestfetch's request deduplication and response caching features work by determining which requests are identical. This guide explains when requests are identical, and it also covers how you can change this behavior.
This library looks at the following pieces of information about a request:
GET
)responseType
bestfetch combines these pieces of information into a string called a requestKey
. Requests with the same requestKey
are identical.
This algorithm is exported from this library as getRequestKey
.
requestKey
In rare situations, you may wish to have control over when two requests are considered to be identical. You can do this by specifying the requestKey
when calling bestfetch
.
By default, a requestKey
is generated for you, but you may pass your own to override this behavior.
bestfetch('/api/books/2', { requestKey: 'my-custom-key' })
.then(res => {
console.log('Received the book:', res);
});
Be careful when specifying your own request keys – it will affect the behavior of both request deduplication and response caching!