Skip to content

Commit a711d61

Browse files
docs: add route cache JSDoc
1 parent a54dae6 commit a711d61

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

src/contexts/router-cache.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,66 @@ import {
1111
} from "../route-cache-static-data";
1212

1313
type RouterCacheProviderProps = {
14+
/**
15+
* Clears all cached routes when the key changes.
16+
*
17+
* Use this for user, tenant, workspace, locale, or environment boundaries.
18+
*
19+
* @defaultValue `"__default__"`
20+
*/
1421
cacheScopeKey?: string | number | null;
22+
/** Outlet and surrounding UI that can access the route cache. */
1523
children: ReactNode;
24+
/**
25+
* Initial cached route entries.
26+
*
27+
* Most apps do not need this. Entries without enabled `staticData.routeCache`
28+
* or entries older than their `maxAge` are ignored.
29+
*
30+
* @defaultValue `{}`
31+
*/
1632
defaultCachedRoutes?: CachedRoutes;
33+
/**
34+
* Maximum cached route entries across this provider.
35+
*
36+
* Set to `0` to disable caching.
37+
*
38+
* @defaultValue `Infinity`
39+
*/
1740
maxEntries?: number;
41+
/**
42+
* Maximum cached entries for the same TanStack route id.
43+
*
44+
* Useful for dynamic routes where each pathname can create a separate cached
45+
* view.
46+
*
47+
* @defaultValue `Infinity`
48+
*/
1849
maxEntriesPerRouteId?: number;
1950
};
2051

2152
type RouterSnapshot = ComponentProps<typeof RouterContextProvider>["router"];
2253

2354
export type CachedRouteData = {
55+
/** First time this cache entry was stored, as `Date.now()`. */
2456
createdAt?: number;
57+
/** Full cached href, including search and hash when available. */
2558
href?: string;
59+
/** Last time this cached route became visible, as `Date.now()`. */
2660
lastVisibleAt?: number;
61+
/** TanStack Router route id used for per-route entry limits. */
2762
routeId?: string;
63+
/** Static data from the deepest cache-enabled route match. */
2864
staticData: StaticDataRouteOption;
65+
/** TanStack Router match id rendered by the cached outlet. */
2966
matchId?: string;
67+
/** Router snapshot used to keep the cached route tree isolated while hidden. */
3068
routerSnapshot?: RouterSnapshot;
69+
/** Whether the cached route has a complete snapshot and can be restored. */
3170
ready?: boolean;
3271
};
3372

73+
/** Cached route entries keyed by normalized pathname. */
3474
export type CachedRoutes = {
3575
[key: string]: CachedRouteData;
3676
};

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export type * from "./types";
1616
declare module "@tanstack/react-router" {
1717
// biome-ignore lint/style/useConsistentTypeDefinitions: TanStack Router static data is extended through interface merging.
1818
interface StaticDataRouteOption {
19+
/**
20+
* Enables retained route-view caching for this route.
21+
*
22+
* Use `true` for default behavior, or pass an options object for
23+
* route-cache-specific settings such as `maxAge`.
24+
*/
1925
routeCache?: RouteCacheStaticOption;
2026
}
2127
}

src/route-cache-static-data.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function getRouteCacheOptions(
2222
}
2323
}
2424

25+
/**
26+
* Builds route options for a cacheable TanStack Router route.
27+
*
28+
* TanStack Router loader-cache options are returned at the route-option level,
29+
* while route-cache-specific options are stored under `staticData.routeCache`.
30+
*/
2531
export function defineRouteCache(options: RouteCacheRouteOptions = {}) {
2632
const { gcTime, maxAge, preloadStaleTime, staleTime } = options;
2733

src/types.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
1+
/** Visibility mode for a cached route container. */
12
export type ActivityMode = "visible" | "hidden";
23

4+
/** Route-cache options stored in TanStack Router `staticData.routeCache`. */
35
export type RouteCacheOptions = {
6+
/**
7+
* Maximum age, in milliseconds, for a retained route view.
8+
*
9+
* Expired cached views are not restored. This only controls the retained
10+
* mounted view; use TanStack Router's `staleTime`, `preloadStaleTime`, and
11+
* `gcTime` route options for loader-data caching.
12+
*
13+
* @defaultValue `Infinity`
14+
*/
415
maxAge?: number;
516
};
617

18+
/**
19+
* Static route-cache opt-in value.
20+
*
21+
* Use `true` to keep the route view cached with default behavior, or an object
22+
* when the retained view needs route-cache-specific options.
23+
*/
724
export type RouteCacheStaticOption = boolean | RouteCacheOptions;
825

26+
/** Options accepted by `defineRouteCache`. */
927
export type RouteCacheRouteOptions = RouteCacheOptions & {
28+
/**
29+
* TanStack Router loader garbage-collection time, in milliseconds.
30+
*
31+
* This is returned as a top-level route option.
32+
*/
1033
gcTime?: number;
34+
/**
35+
* TanStack Router preload freshness time, in milliseconds.
36+
*
37+
* This is returned as a top-level route option.
38+
*/
1139
preloadStaleTime?: number;
40+
/**
41+
* TanStack Router loader freshness time, in milliseconds.
42+
*
43+
* This is returned as a top-level route option and does not control the
44+
* retained route view lifetime. Use `maxAge` for that.
45+
*/
1246
staleTime?: number;
1347
};
1448

49+
/** Emitted when navigation to a ready cached route begins. */
1550
export type RouteCacheNavigationStart = {
51+
/** Normalized pathname for the cached route being restored. */
1652
pathname: string;
53+
/** `performance.now()` timestamp for the navigation start. */
1754
startedAt: number;
1855
};
1956

57+
/** Emitted after a cached route has become visible and painted. */
2058
export type RouteCacheNavigationComplete = RouteCacheNavigationStart & {
59+
/** Total elapsed time from start to painted, in milliseconds. */
2160
duration: number;
61+
/** `performance.now()` timestamp after the visible route painted. */
2262
paintedAt: number;
63+
/** `performance.now()` timestamp when the cached route became visible. */
2364
visibleAt: number;
2465
};

0 commit comments

Comments
 (0)