⚡ coalesce overlapping GET requests in the api client
Browse files at this commitc35a83aa3f65a20c8a2fdfd2dfdb3e35878741b5Parent: 8bc6520- perf
- ⚡ coalesce overlapping GET requests in the api client
2 changed files +33 -1
src/api/__tests__/client/client.test.tsmodified+12 -0
| @@ -18,6 +18,18 @@ describe("client", () => { | |||
| 18 | expect(init?.credentials).toBe("same-origin"); | 18 | expect(init?.credentials).toBe("same-origin"); |
| 19 | }); | 19 | }); |
| 20 | 20 | ||
| 21 | + it("coalesces simultaneous identical GETs without caching later reads", async () => { | ||
| 22 | + let resolve!: (value: Response) => void; | ||
| 23 | + vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>((done) => { resolve = done; }))); | ||
| 24 | + const first = client("/shared"); | ||
| 25 | + const second = client("/shared"); | ||
| 26 | + expect(globalThis.fetch).toHaveBeenCalledTimes(1); | ||
| 27 | + resolve(new Response(JSON.stringify({ ok: true }), { status: 200 })); | ||
| 28 | + await expect(Promise.all([first, second])).resolves.toEqual([{ ok: true }, { ok: true }]); | ||
| 29 | + mockFetchSuccess({ fresh: true }); | ||
| 30 | + await expect(client("/shared")).resolves.toEqual({ fresh: true }); | ||
| 31 | + }); | ||
| 32 | + | ||
| 21 | it("makes POST request with JSON body", async () => { | 33 | it("makes POST request with JSON body", async () => { |
| 22 | mockFetchSuccess({ ok: true }); | 34 | mockFetchSuccess({ ok: true }); |
| 23 | const body = { foo: "bar" }; | 35 | const body = { foo: "bar" }; |
src/api/client/client.tsmodified+21 -1
| @@ -349,7 +349,9 @@ function errorMessageFromPayload(data: unknown, fallback: string): string { | |||
| 349 | return fallback; | 349 | return fallback; |
| 350 | } | 350 | } |
| 351 | 351 | ||
| 352 | -export async function client<T>(path: string, options: RequestOptions = {}): Promise<T> { | 352 | +const pendingGets = new Map<string, Promise<unknown>>(); |
| 353 | + | ||
| 354 | +async function request<T>(path: string, options: RequestOptions = {}): Promise<T> { | ||
| 353 | const headers: Record<string, string> = {}; | 355 | const headers: Record<string, string> = {}; |
| 354 | let body: string | undefined; | 356 | let body: string | undefined; |
| 355 | 357 | ||
| @@ -421,6 +423,24 @@ export async function client<T>(path: string, options: RequestOptions = {}): Pro | |||
| 421 | return data as T; | 423 | return data as T; |
| 422 | } | 424 | } |
| 423 | 425 | ||
| 426 | +/** | ||
| 427 | + * Coalesce only overlapping, non-cancellable GETs. This is deliberately not a | ||
| 428 | + * cache: once settled the entry disappears, so every later resource refetch | ||
| 429 | + * still reaches the backend and mutation consistency stays unchanged. | ||
| 430 | + */ | ||
| 431 | +export function client<T>(path: string, options: RequestOptions = {}): Promise<T> { | ||
| 432 | + const method = options.method ?? "GET"; | ||
| 433 | + if (method !== "GET" || options.body !== undefined || options.signal || options.cache === "no-store") { | ||
| 434 | + return request<T>(path, options); | ||
| 435 | + } | ||
| 436 | + const key = `${path}\u0000${options.cache ?? "default"}`; | ||
| 437 | + const pending = pendingGets.get(key); | ||
| 438 | + if (pending) return pending as Promise<T>; | ||
| 439 | + const next = request<T>(path, options).finally(() => pendingGets.delete(key)); | ||
| 440 | + pendingGets.set(key, next); | ||
| 441 | + return next; | ||
| 442 | +} | ||
| 443 | + | ||
| 424 | export async function formClient<T>(path: string, body: FormData, signal?: AbortSignal): Promise<T> { | 444 | export async function formClient<T>(path: string, body: FormData, signal?: AbortSignal): Promise<T> { |
| 425 | const res = await fetch(`${API_PREFIX}${path}`, { | 445 | const res = await fetch(`${API_PREFIX}${path}`, { |
| 426 | method: "POST", | 446 | method: "POST", |