From c35a83aa3f65a20c8a2fdfd2dfdb3e35878741b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sat, 1 Aug 2026 22:15:16 +0300 Subject: [PATCH] =?UTF-8?q?perf:=20=E2=9A=A1=20coalesce=20overlapping=20GE?= =?UTF-8?q?T=20requests=20in=20the=20api=20client=20-=20in-flight=20identi?= =?UTF-8?q?cal=20GETs=20share=20one=20promise=20(keyed=20by=20path=20+=20c?= =?UTF-8?q?ache=20=20=20mode);=20settled=20entries=20are=20dropped,=20so?= =?UTF-8?q?=20this=20is=20not=20a=20cache=20and=20=20=20refetches=20still?= =?UTF-8?q?=20reach=20the=20backend=20-=20skips=20POST/PATCH/DELETE,=20bod?= =?UTF-8?q?ied=20GETs,=20AbortSignal,=20and=20no-store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/__tests__/client/client.test.ts | 12 ++++++++++++ src/api/client/client.ts | 22 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/api/__tests__/client/client.test.ts b/src/api/__tests__/client/client.test.ts index a2ac1d0..46ef0bf 100644 --- a/src/api/__tests__/client/client.test.ts +++ b/src/api/__tests__/client/client.test.ts @@ -18,6 +18,18 @@ describe("client", () => { expect(init?.credentials).toBe("same-origin"); }); + it("coalesces simultaneous identical GETs without caching later reads", async () => { + let resolve!: (value: Response) => void; + vi.stubGlobal("fetch", vi.fn(() => new Promise((done) => { resolve = done; }))); + const first = client("/shared"); + const second = client("/shared"); + expect(globalThis.fetch).toHaveBeenCalledTimes(1); + resolve(new Response(JSON.stringify({ ok: true }), { status: 200 })); + await expect(Promise.all([first, second])).resolves.toEqual([{ ok: true }, { ok: true }]); + mockFetchSuccess({ fresh: true }); + await expect(client("/shared")).resolves.toEqual({ fresh: true }); + }); + it("makes POST request with JSON body", async () => { mockFetchSuccess({ ok: true }); const body = { foo: "bar" }; diff --git a/src/api/client/client.ts b/src/api/client/client.ts index 35a31ae..9ad6713 100644 --- a/src/api/client/client.ts +++ b/src/api/client/client.ts @@ -349,7 +349,9 @@ function errorMessageFromPayload(data: unknown, fallback: string): string { return fallback; } -export async function client(path: string, options: RequestOptions = {}): Promise { +const pendingGets = new Map>(); + +async function request(path: string, options: RequestOptions = {}): Promise { const headers: Record = {}; let body: string | undefined; @@ -421,6 +423,24 @@ export async function client(path: string, options: RequestOptions = {}): Pro return data as T; } +/** + * Coalesce only overlapping, non-cancellable GETs. This is deliberately not a + * cache: once settled the entry disappears, so every later resource refetch + * still reaches the backend and mutation consistency stays unchanged. + */ +export function client(path: string, options: RequestOptions = {}): Promise { + const method = options.method ?? "GET"; + if (method !== "GET" || options.body !== undefined || options.signal || options.cache === "no-store") { + return request(path, options); + } + const key = `${path}\u0000${options.cache ?? "default"}`; + const pending = pendingGets.get(key); + if (pending) return pending as Promise; + const next = request(path, options).finally(() => pendingGets.delete(key)); + pendingGets.set(key, next); + return next; +} + export async function formClient(path: string, body: FormData, signal?: AbortSignal): Promise { const res = await fetch(`${API_PREFIX}${path}`, { method: "POST",