In-Flight Request Deduplication

Prevent duplicate network flights when multiple React Native components mount and request the same data simultaneously.


How In-Flight Deduplication Works

Multiple parts of an application can request the same resource at nearly the same time. Deduplication can let matching eligible requests share the same in-flight network operation.

With dedup: { enabled: true }, the client builds a request key from the method, normalized URL, selected headers, credentials, and body. A matching eligible request joins the existing in-flight operation:

dedup.ts
typescript
import { createClient } from 'react-native-fetch-client';

const api = createClient({
  baseURL: 'https://api.example.com',
  // Enable globally:
  dedup: {
    enabled: true,
  },
});

// Component A and Component B mount simultaneously and both call /current-user:
const [resA, resB] = await Promise.all([
  api.get('/current-user'),
  api.get('/current-user'),
]);

// Matching concurrent requests share an in-flight network request.
console.log(resA.fromDedup, resB.fromDedup);

Partial Cancellation Safety

If Component A unmounts and aborts its signal, the client checks if other callers are still waiting. If Component B is still waiting, the underlying network flight is NOT aborted, allowing Component B to complete successfully without throwing an unexpected error.

typescript
// You can also enable or disable deduplication per-request:
const res = await api.get('/heavy-report', {
  dedup: {
    enabled: true,
    // Override the client-level setting for this request:
    methods: ['GET', 'HEAD'],
  },
});

DedupConfig Options

DedupConfig Reference
PropertyTypeDefaultDescription
enabledbooleanfalseWhether to coalesce identical in-flight GET requests.
methodsHttpMethod[]['GET', 'HEAD']HTTP methods eligible for deduplication. Configure carefully before including mutation methods.
GET Requests Only
By default, only GET and HEAD requests are eligible. The methods option can change that list, so avoid including mutations unless sharing them is appropriate for your application.