Dedupe Plugin
Prevent redundant requests by deduplicating similar in-flight requests, reducing the number of requests sent to the server.
Overview
import { DedupeLinkPlugin } from '@orpc/client/plugins'
const link = new RPCLink({
plugins: [
new DedupeLinkPlugin({
groups: [
{
condition: () => true,
context: {}, // Context used for the rest of the request lifecycle
},
],
}),
],
})
Filter
By default, the plugin deduplicates GET and QUERY requests. You can customize this behavior by providing a filter function.
const link = new RPCLink({
plugins: [
new DedupeLinkPlugin({
filter: ({ request }) => request.method === 'GET',
groups: [
{
condition: () => true,
context: {},
},
],
}),
],
})
Groups
Only requests in the same group are deduplicated together. Each group also defines a context, as described in client context.
The following example deduplicates requests by cache policy:
interface ClientContext {
cache?: RequestCache
}
const link = new RPCLink<ClientContext>({
method: ({ context }) => {
if (context?.cache) {
return 'GET'
}
return 'POST'
},
plugins: [
new DedupeLinkPlugin({
groups: [
{
condition: ({ context }) => context?.cache === 'force-cache',
context: { // used for the rest of the request lifecycle
cache: 'force-cache',
},
},
{ // Fallback for all other requests. Keep this last.
condition: () => true,
context: {},
},
],
}),
],
fetch: (url, init, { context }) => globalThis.fetch(url, {
...init,
cache: context?.cache,
}),
})
Now, calls made with cache = 'force-cache' use that cache setting whether they are deduplicated or sent individually.
Wait Time
By default, only identical requests made in the same event loop tick are deduplicated. Set wait to hold requests for a number of milliseconds, so identical requests made shortly after share one response.
const link = new RPCLink({
plugins: [
new DedupeLinkPlugin({
wait: 10, // ms
groups: [
{
condition: () => true,
context: {},
},
],
}),
],
})
The wait starts when the first request is queued, so no request waits longer than wait, even while more keep arriving.
Learn More
For implementation details, see the source code.