From f5ef2f2b30d5b11d3ddc84eff7fa367ef4a674c9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 3 Mar 2026 18:06:48 -0500 Subject: [PATCH] perf: Add request deduping in ApiClient (#11629) --- app/utils/ApiClient.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/utils/ApiClient.ts b/app/utils/ApiClient.ts index 4570ef89e2..0f1889c25a 100644 --- a/app/utils/ApiClient.ts +++ b/app/utils/ApiClient.ts @@ -44,6 +44,9 @@ class ApiClient { shareId?: string; + /** Map of in-flight POST requests for deduplication, keyed by path + body. */ + private inflightRequests = new Map>(); + constructor(options: Options = {}) { this.baseUrl = options.baseUrl || "/api"; } @@ -280,7 +283,23 @@ class ApiClient { path: string, data?: JSONObject | FormData | undefined, options?: FetchOptions - ) => this.fetch(path, "POST", data, options); + ): Promise => { + if (data instanceof FormData) { + return this.fetch(path, "POST", data, options); + } + + const key = `${path}:${JSON.stringify(data)}:${JSON.stringify(options)}`; + const inflight = this.inflightRequests.get(key); + if (inflight) { + return inflight; + } + + const promise = this.fetch(path, "POST", data, options).finally(() => { + this.inflightRequests.delete(key); + }); + this.inflightRequests.set(key, promise); + return promise; + }; } export const client = new ApiClient();