Deduplication
Up to ten callers request the same user at the same moment. With deduplicate on, only the first one goes to the
network and the others wait for its response. Turn it off to see one request per caller.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { createClient } from "@hyper-fetch/core";
export const client = createClient({ url: "https://dummyjson.com" });
export type User = { id: number; firstName: string; lastName: string };
/**
* `deduplicate: true` lets identical requests that start while one is in flight share that call and its response.
* The default HTTP adapter already turns it on for GET requests; it is set here to make it visible.
* `delay` keeps the request in flight for a second, so every caller below overlaps with the first one.
*/
export const getUserRequest = client
.createRequest<{
response: User;
queryParams: { select: string; delay: number };
}>()({
endpoint: "/users/:userId",
deduplicate: true,
})
.setParams({ userId: 1 })
// `select` and `delay` are dummyjson query params: return only these fields, answer after `delay` ms.
.setQueryParams({ select: "firstName,lastName", delay: 1000 });
npx tiged BetterTyped/hyper-fetch/examples/react/deduplication hyper-fetch-react-deduplication && cd hyper-fetch-react-deduplication && npm install && npm run devdeduplicate: identical requests started while one is in flight join it instead of sending their own.- Every caller still gets a response: each
send()resolves with the shared result. - Dispatcher events:
onRequestStartcounts network requests,onDeduplicatedcounts the callers that joined.
How it works
-
getUserRequestis created withdeduplicate: trueand adelayof one second, so all callers overlap with the first request. -
Send from all callers calls
send()once per caller, in the same tick. The switch picksgetUserRequest.setDeduplicate(true)orfalse. -
The dispatcher groups requests by
queryKey. When a deduplicated request arrives and the latest request in its queue is still running, it joins that request instead of adding a new one:const request = getUserRequest.setDeduplicate(deduplicate);request.send(); // x callers -
The effect in
Applistens toclient.requestManager.events.onRequestStartfires only for real network calls,onDeduplicatedfor each caller that joined one. -
deduplicateTimelimits how long a running request can be joined, counted from when it was added. By default there is no limit: any request still in flight can be joined.
The default HTTP adapter turns deduplicate on for GET and QUERY requests, so most reads are already deduplicated.
On mount, useFetch also skips fetching when a request for the same cache key is already running.
- Several components that need the same data when a page opens, such as the current user.
- Code outside React (
send()in loaders, services or event handlers) that can ask for the same data at once. - Keep it off for mutations: two identical
POSTs are usually two separate actions.
Related
deduplicate lets identical requests share one call while it is in flight. Every caller gets the response, and the
server sees one request.
