MCPcopy Create free account
hub / github.com/adobe/react-spectrum / prefetchRoute

Function prefetchRoute

packages/dev/s2-docs/src/prefetch.ts:10–46  ·  view source on GitHub ↗
(pathname: string, priority: RequestPriority = 'low')

Source from the content-addressed store, hash-verified

8const prefetchPromises = new Map<string, Promise<ReactElement>>();
9
10export function prefetchRoute(pathname: string, priority: RequestPriority = 'low') {
11 if (process.env.NODE_ENV !== 'production') {
12 return;
13 }
14
15 let url = getRSCUrl(pathname);
16
17 // Skip if already prefetched
18 if (prefetchPromises.has(url)) {
19 return;
20 }
21
22 if (priority === 'low' && 'connection' in navigator) {
23 let conn: any = navigator.connection;
24 if (conn.saveData || /2g/.test(conn.effectiveType)) {
25 return;
26 }
27 }
28
29 // Start prefetch and cache the promise
30 const prefetchPromise = fetchRSC<ReactElement>(url, {priority});
31
32 prefetchPromise
33 .then(res => {
34 // Remove from cache after 30 seconds (rely on browser cache for subsequent requests)
35 // This is required so we can reuse the same promise on click.
36 setTimeout(() => {
37 prefetchPromises.delete(url);
38 }, 30_000);
39 return res;
40 })
41 .catch(() => {
42 prefetchPromises.delete(url);
43 });
44
45 prefetchPromises.set(url, prefetchPromise);
46}
47
48export function getPrefetchedPromise(rscPath: string): Promise<ReactElement> | undefined {
49 return prefetchPromises.get(rscPath);

Callers 2

client.tsxFile · 0.90
prefetch.tsFile · 0.85

Calls 4

getRSCUrlFunction · 0.90
deleteMethod · 0.80
hasMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected