Prefetch all the things!

This commit is contained in:
Jack Kingsman
2026-02-13 00:48:37 -08:00
parent 908a479fa6
commit 1c4d6c07a8
4 changed files with 43 additions and 14 deletions
+26
View File
@@ -0,0 +1,26 @@
/**
* Consume prefetched API promises started in index.html before React loaded.
*
* Each key is consumed at most once — the first caller gets the promise,
* subsequent callers get undefined and should fall back to a normal fetch.
*/
import type { AppSettings, Channel, Contact, RadioConfig, UnreadCounts } from './types';
interface PrefetchMap {
config?: Promise<RadioConfig>;
settings?: Promise<AppSettings>;
channels?: Promise<Channel[]>;
contacts?: Promise<Contact[]>;
unreads?: Promise<UnreadCounts>;
undecryptedCount?: Promise<{ count: number }>;
}
const store: PrefetchMap = (window as unknown as { __prefetch?: PrefetchMap }).__prefetch ?? {};
/** Take a prefetched promise (consumed once, then gone). */
export function takePrefetch<K extends keyof PrefetchMap>(key: K): PrefetchMap[K] {
const p = store[key];
delete store[key];
return p;
}