Spoosh
Plugins

Deduplication

Prevent duplicate concurrent requests

The deduplication plugin prevents making the same request multiple times concurrently. If a request is already in flight, subsequent calls reuse the same promise.

This is especially important when using invalidationPlugin - when a mutation invalidates multiple queries that share the same endpoint, deduplication ensures only one network request is made.

Installation

npm install @spoosh/plugin-deduplication

Usage

import { Spoosh } from "@spoosh/core";
import { cachePlugin } from "@spoosh/plugin-cache";
import { deduplicationPlugin } from "@spoosh/plugin-deduplication";
import { invalidationPlugin } from "@spoosh/plugin-invalidation";

const client = new Spoosh<ApiSchema, Error>("/api").use([
  cachePlugin({ staleTime: 5000 }),
  deduplicationPlugin(),
  invalidationPlugin(),
]);

Why It Matters

Without deduplication, invalidating queries can cause duplicate requests:

// Multiple components using the same query
function ComponentA() {
  const { data } = useRead((api) => api("users").GET());
}

function ComponentB() {
  const { data } = useRead((api) => api("users").GET());
}

// When a mutation invalidates "users", both components would refetch
// With deduplicationPlugin, only ONE network request is made

Per-Request Override

// Disable deduplication for this request
useRead((api) => api("posts").GET(), { dedupe: false });

Options

Plugin Config

OptionTypeDefaultDescription
read"in-flight" | false"in-flight"Deduplication mode for reads
write"in-flight" | falsefalseDeduplication mode for writes

Per-Request Options

OptionTypeDescription
dedupe"in-flight" | falseOverride deduplication for this request

Modes

ModeDescription
"in-flight"Reuse existing in-flight request promise if one exists
falseAlways make a new request

Write Deduplication

By default, writes are not deduplicated to avoid unintended side effects. Enable with caution:

import { Spoosh } from "@spoosh/core";
import { deduplicationPlugin } from "@spoosh/plugin-deduplication";

const client = new Spoosh<ApiSchema, Error>("/api").use([
  deduplicationPlugin({ write: "in-flight" }),
]);

On this page