Skip to main content

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.

intermediatesenddeduplication
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 });
Run locallynpx tiged BetterTyped/hyper-fetch/examples/react/deduplication hyper-fetch-react-deduplication && cd hyper-fetch-react-deduplication && npm install && npm run dev
What it shows
  1. deduplicate: identical requests started while one is in flight join it instead of sending their own.
  2. Every caller still gets a response: each send() resolves with the shared result.
  3. Dispatcher events: onRequestStart counts network requests, onDeduplicated counts the callers that joined.

How it works​

  1. getUserRequest is created with deduplicate: true and a delay of one second, so all callers overlap with the first request.

  2. Send from all callers calls send() once per caller, in the same tick. The switch picks getUserRequest.setDeduplicate(true) or false.

  3. 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
  4. The effect in App listens to client.requestManager.events. onRequestStart fires only for real network calls, onDeduplicated for each caller that joined one.

  5. deduplicateTime limits 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.

When to use it
  • 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.

Summary

deduplicate lets identical requests share one call while it is in flight. Every caller gets the response, and the server sees one request.