From 978c05bb172d161cc4d2cecc0f58d40fec169d21 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 18 Oct 2026 19:03:19 -0400 Subject: [PATCH] perf: Remember empty unfurl results on the client (#14135) * perf: Remember empty unfurl results on the client * fix: Apply unfurl cooldown and request sharing to background refreshes --- app/stores/UnfurlsStore.ts | 46 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/app/stores/UnfurlsStore.ts b/app/stores/UnfurlsStore.ts index 94fb5c68e6..0e2537cf00 100644 --- a/app/stores/UnfurlsStore.ts +++ b/app/stores/UnfurlsStore.ts @@ -44,9 +44,18 @@ class UnfurlsStore extends Store> { return unfurl; } - return this.unfurl({ url, documentId }); + return this.request({ url, documentId }); }; + /** Time that a url last returned no unfurl data, keyed by url. */ + private emptyFetchedAt = new Map(); + + /** Unfurl requests that are in progress, keyed by url. */ + private inflight = new Map< + string, + Promise | undefined> + >(); + private refetch = ({ unfurl, documentId, @@ -57,7 +66,37 @@ class UnfurlsStore extends Store> { const fiveMinutesAgo = subMinutes(new Date(), 5); if (new Date(unfurl.fetchedAt) < fiveMinutesAgo) { - void this.unfurl({ url: unfurl.id, documentId }); + void this.request({ url: unfurl.id, documentId }); + } + }; + + /** + * Requests an unfurl, skipping urls that recently returned no data and + * sharing a request that is already in progress for the same url. + */ + private request = async ({ + url, + documentId, + }: { + url: string; + documentId?: string; + }): Promise | undefined> => { + const emptyAt = this.emptyFetchedAt.get(url); + if (emptyAt && emptyAt > subMinutes(new Date(), 5).getTime()) { + return; + } + + const existing = this.inflight.get(url); + if (existing) { + return existing as Promise | undefined>; + } + + const promise = this.unfurl({ url, documentId }); + this.inflight.set(url, promise); + try { + return await promise; + } finally { + this.inflight.delete(url); } }; @@ -79,9 +118,12 @@ class UnfurlsStore extends Store> { // unfurls can succeed with no data. if (!data) { + this.emptyFetchedAt.set(url, Date.now()); return; } + this.emptyFetchedAt.delete(url); + return this.add({ id: url, type: data.type,