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
This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-18 19:03:19 -04:00
1 parent 20cc8e1106
commit 978c05bb17
1 file changed
+44 -2
+44 -2
View File
@@ -44,9 +44,18 @@ class UnfurlsStore extends Store<Unfurl<any>> {
return unfurl;
}
return this.unfurl<UnfurlType>({ url, documentId });
return this.request<UnfurlType>({ url, documentId });
};
/** Time that a url last returned no unfurl data, keyed by url. */
private emptyFetchedAt = new Map<string, number>();
/** Unfurl requests that are in progress, keyed by url. */
private inflight = new Map<
string,
Promise<Unfurl<UnfurlResourceType> | undefined>
>();
private refetch = <UnfurlType extends UnfurlResourceType>({
unfurl,
documentId,
@@ -57,7 +66,37 @@ class UnfurlsStore extends Store<Unfurl<any>> {
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 <UnfurlType extends UnfurlResourceType>({
url,
documentId,
}: {
url: string;
documentId?: string;
}): Promise<Unfurl<UnfurlType> | 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<Unfurl<UnfurlType> | undefined>;
}
const promise = this.unfurl<UnfurlType>({ url, documentId });
this.inflight.set(url, promise);
try {
return await promise;
} finally {
this.inflight.delete(url);
}
};
@@ -79,9 +118,12 @@ class UnfurlsStore extends Store<Unfurl<any>> {
// 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,