⭐️ bestfetch

Identical Requests

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.

Determining Identical Requests

This library looks at the following pieces of information about a request:

  • The URL
  • The request body
  • The request method (i.e.; GET)
  • The 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.

Specifying a requestKey

💁‍♀️ Heads up! This is an advanced API that few applications should ever need to use.

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!