tanstack-router-cache 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/cached-outlet.cjs +12 -0
- package/dist/components/cached-outlet.js +12 -0
- package/dist/components/off-screen-in.cjs +130 -0
- package/dist/components/off-screen-in.js +130 -0
- package/dist/components/off-screen.cjs +8 -0
- package/dist/components/off-screen.js +8 -0
- package/dist/components/restore-cached-href.cjs +28 -0
- package/dist/components/restore-cached-href.js +28 -0
- package/dist/components/route-cache-manager.cjs +485 -0
- package/dist/components/route-cache-manager.js +485 -0
- package/dist/components/router-cache-outlet.cjs +9 -0
- package/dist/components/router-cache-outlet.js +9 -0
- package/dist/contexts/router-cache.cjs +237 -0
- package/dist/contexts/router-cache.d.ts +40 -0
- package/dist/contexts/router-cache.js +235 -0
- package/dist/dom/dismiss-transient-ui.cjs +230 -0
- package/dist/dom/dismiss-transient-ui.js +228 -0
- package/dist/hooks/use-event-listener.cjs +76 -0
- package/dist/hooks/use-event-listener.js +76 -0
- package/dist/hooks/use-route-cache-active.cjs +19 -0
- package/dist/hooks/use-route-cache-active.js +19 -0
- package/dist/hooks/use-route-cache-activity.cjs +12 -0
- package/dist/hooks/use-route-cache-activity.js +12 -0
- package/dist/hooks/use-route-cache-effect.cjs +38 -0
- package/dist/hooks/use-route-cache-effect.js +38 -0
- package/dist/hooks/use-route-cache-error-boundary.cjs +23 -0
- package/dist/hooks/use-route-cache-error-boundary.js +23 -0
- package/dist/hooks/use-route-cache-navigation.cjs +36 -0
- package/dist/hooks/use-route-cache-navigation.js +36 -0
- package/dist/hooks/use-router-cache-debug.cjs +85 -0
- package/dist/hooks/use-router-cache-debug.js +85 -0
- package/dist/hooks/use-router-cache.cjs +32 -0
- package/dist/hooks/use-router-cache.js +32 -0
- package/dist/hooks/use-update.cjs +8 -0
- package/dist/hooks/use-update.js +8 -0
- package/dist/index.cjs +18 -1402
- package/dist/index.d.ts +9 -1
- package/dist/index.js +10 -1395
- package/dist/pathname.cjs +8 -0
- package/dist/pathname.js +8 -0
- package/dist/route-cache-static-data.cjs +43 -0
- package/dist/route-cache-static-data.d.ts +45 -0
- package/dist/route-cache-static-data.js +41 -0
- package/dist/types.d.ts +28 -0
- package/docs/architecture.md +8 -5
- package/docs/cache-behavior.md +17 -0
- package/docs/components.md +1 -2
- package/docs/getting-started.md +38 -3
- package/docs/types.md +6 -0
- package/package.json +2 -2
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const require_use_route_cache_activity = require("./use-route-cache-activity.cjs");
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/use-route-cache-effect.ts
|
|
4
|
+
function areDependenciesEqual(previousDeps, nextDeps) {
|
|
5
|
+
if (previousDeps?.length !== nextDeps.length) return false;
|
|
6
|
+
for (let index = 0; index < nextDeps.length; index += 1) if (!Object.is(previousDeps[index], nextDeps[index])) return false;
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
function useRouteCacheEffect(activeCallback, deps = []) {
|
|
10
|
+
const returnValue = (0, react.useRef)(void 0);
|
|
11
|
+
const isActiveRef = (0, react.useRef)(false);
|
|
12
|
+
const previousCallbackRef = (0, react.useRef)(void 0);
|
|
13
|
+
const previousDepsRef = (0, react.useRef)(void 0);
|
|
14
|
+
(0, react.useEffect)(() => {
|
|
15
|
+
const callbackChanged = previousCallbackRef.current !== activeCallback;
|
|
16
|
+
const dependenciesChanged = !areDependenciesEqual(previousDepsRef.current, deps);
|
|
17
|
+
if (!(callbackChanged || dependenciesChanged)) return;
|
|
18
|
+
previousCallbackRef.current = activeCallback;
|
|
19
|
+
previousDepsRef.current = deps;
|
|
20
|
+
if (!isActiveRef.current) return;
|
|
21
|
+
returnValue.current?.();
|
|
22
|
+
returnValue.current = activeCallback();
|
|
23
|
+
});
|
|
24
|
+
(0, react.useEffect)(() => () => {
|
|
25
|
+
returnValue.current?.();
|
|
26
|
+
}, []);
|
|
27
|
+
require_use_route_cache_activity.useRouteCacheActivity((active) => {
|
|
28
|
+
if (active) {
|
|
29
|
+
isActiveRef.current = true;
|
|
30
|
+
returnValue.current = activeCallback();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
isActiveRef.current = false;
|
|
34
|
+
returnValue.current?.();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
exports.useRouteCacheEffect = useRouteCacheEffect;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useRouteCacheActivity } from "./use-route-cache-activity.js";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
//#region src/hooks/use-route-cache-effect.ts
|
|
4
|
+
function areDependenciesEqual(previousDeps, nextDeps) {
|
|
5
|
+
if (previousDeps?.length !== nextDeps.length) return false;
|
|
6
|
+
for (let index = 0; index < nextDeps.length; index += 1) if (!Object.is(previousDeps[index], nextDeps[index])) return false;
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
function useRouteCacheEffect(activeCallback, deps = []) {
|
|
10
|
+
const returnValue = useRef(void 0);
|
|
11
|
+
const isActiveRef = useRef(false);
|
|
12
|
+
const previousCallbackRef = useRef(void 0);
|
|
13
|
+
const previousDepsRef = useRef(void 0);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const callbackChanged = previousCallbackRef.current !== activeCallback;
|
|
16
|
+
const dependenciesChanged = !areDependenciesEqual(previousDepsRef.current, deps);
|
|
17
|
+
if (!(callbackChanged || dependenciesChanged)) return;
|
|
18
|
+
previousCallbackRef.current = activeCallback;
|
|
19
|
+
previousDepsRef.current = deps;
|
|
20
|
+
if (!isActiveRef.current) return;
|
|
21
|
+
returnValue.current?.();
|
|
22
|
+
returnValue.current = activeCallback();
|
|
23
|
+
});
|
|
24
|
+
useEffect(() => () => {
|
|
25
|
+
returnValue.current?.();
|
|
26
|
+
}, []);
|
|
27
|
+
useRouteCacheActivity((active) => {
|
|
28
|
+
if (active) {
|
|
29
|
+
isActiveRef.current = true;
|
|
30
|
+
returnValue.current = activeCallback();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
isActiveRef.current = false;
|
|
34
|
+
returnValue.current?.();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { useRouteCacheEffect };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const require_pathname = require("../pathname.cjs");
|
|
2
|
+
const require_router_cache = require("../contexts/router-cache.cjs");
|
|
3
|
+
let _tanstack_react_router = require("@tanstack/react-router");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
//#region src/hooks/use-route-cache-error-boundary.ts
|
|
6
|
+
function useRouteCacheErrorBoundary(pathname) {
|
|
7
|
+
const context = require_router_cache.useOptionalRouterCacheContext();
|
|
8
|
+
const routePathname = (0, _tanstack_react_router.useLocation)({ select: (location) => location.pathname });
|
|
9
|
+
(0, react.useEffect)(() => {
|
|
10
|
+
if (!context) return;
|
|
11
|
+
const targetPathname = require_pathname.normalizeCachedRoutePathname(pathname ?? routePathname);
|
|
12
|
+
context.retainErroredRoute(targetPathname);
|
|
13
|
+
return () => {
|
|
14
|
+
context.releaseErroredRoute(targetPathname);
|
|
15
|
+
};
|
|
16
|
+
}, [
|
|
17
|
+
context,
|
|
18
|
+
pathname,
|
|
19
|
+
routePathname
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.useRouteCacheErrorBoundary = useRouteCacheErrorBoundary;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { normalizeCachedRoutePathname } from "../pathname.js";
|
|
2
|
+
import { useOptionalRouterCacheContext } from "../contexts/router-cache.js";
|
|
3
|
+
import { useLocation } from "@tanstack/react-router";
|
|
4
|
+
import { useEffect } from "react";
|
|
5
|
+
//#region src/hooks/use-route-cache-error-boundary.ts
|
|
6
|
+
function useRouteCacheErrorBoundary(pathname) {
|
|
7
|
+
const context = useOptionalRouterCacheContext();
|
|
8
|
+
const routePathname = useLocation({ select: (location) => location.pathname });
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (!context) return;
|
|
11
|
+
const targetPathname = normalizeCachedRoutePathname(pathname ?? routePathname);
|
|
12
|
+
context.retainErroredRoute(targetPathname);
|
|
13
|
+
return () => {
|
|
14
|
+
context.releaseErroredRoute(targetPathname);
|
|
15
|
+
};
|
|
16
|
+
}, [
|
|
17
|
+
context,
|
|
18
|
+
pathname,
|
|
19
|
+
routePathname
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { useRouteCacheErrorBoundary };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const require_use_event_listener = require("./use-event-listener.cjs");
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/use-route-cache-navigation.ts
|
|
4
|
+
const INITIAL_STATE = {
|
|
5
|
+
activeNavigation: null,
|
|
6
|
+
lastCompletedNavigation: null
|
|
7
|
+
};
|
|
8
|
+
function useRouteCacheNavigation() {
|
|
9
|
+
const [state, setState] = (0, react.useState)(INITIAL_STATE);
|
|
10
|
+
require_use_event_listener.useEventListener({ on: {
|
|
11
|
+
cachedNavigationStart: (navigation) => {
|
|
12
|
+
setState((current) => ({
|
|
13
|
+
...current,
|
|
14
|
+
activeNavigation: navigation
|
|
15
|
+
}));
|
|
16
|
+
},
|
|
17
|
+
cachedNavigationCancel: (navigation) => {
|
|
18
|
+
setState((current) => {
|
|
19
|
+
if (current.activeNavigation?.pathname !== navigation.pathname) return current;
|
|
20
|
+
return {
|
|
21
|
+
...current,
|
|
22
|
+
activeNavigation: null
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
cachedNavigationComplete: (navigation) => {
|
|
27
|
+
setState((current) => ({
|
|
28
|
+
activeNavigation: current.activeNavigation?.pathname === navigation.pathname ? null : current.activeNavigation,
|
|
29
|
+
lastCompletedNavigation: navigation
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
} });
|
|
33
|
+
return state;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
exports.useRouteCacheNavigation = useRouteCacheNavigation;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useEventListener } from "./use-event-listener.js";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
//#region src/hooks/use-route-cache-navigation.ts
|
|
4
|
+
const INITIAL_STATE = {
|
|
5
|
+
activeNavigation: null,
|
|
6
|
+
lastCompletedNavigation: null
|
|
7
|
+
};
|
|
8
|
+
function useRouteCacheNavigation() {
|
|
9
|
+
const [state, setState] = useState(INITIAL_STATE);
|
|
10
|
+
useEventListener({ on: {
|
|
11
|
+
cachedNavigationStart: (navigation) => {
|
|
12
|
+
setState((current) => ({
|
|
13
|
+
...current,
|
|
14
|
+
activeNavigation: navigation
|
|
15
|
+
}));
|
|
16
|
+
},
|
|
17
|
+
cachedNavigationCancel: (navigation) => {
|
|
18
|
+
setState((current) => {
|
|
19
|
+
if (current.activeNavigation?.pathname !== navigation.pathname) return current;
|
|
20
|
+
return {
|
|
21
|
+
...current,
|
|
22
|
+
activeNavigation: null
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
cachedNavigationComplete: (navigation) => {
|
|
27
|
+
setState((current) => ({
|
|
28
|
+
activeNavigation: current.activeNavigation?.pathname === navigation.pathname ? null : current.activeNavigation,
|
|
29
|
+
lastCompletedNavigation: navigation
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
} });
|
|
33
|
+
return state;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { useRouteCacheNavigation };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
//#region src/hooks/use-router-cache-debug.ts
|
|
3
|
+
const DYNAMIC_SEGMENT_PATTERNS = [
|
|
4
|
+
/^\d{4,}$/u,
|
|
5
|
+
/^[0-9a-f]{8,}$/iu,
|
|
6
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu
|
|
7
|
+
];
|
|
8
|
+
function isDynamicLookingSegment(segment) {
|
|
9
|
+
for (const pattern of DYNAMIC_SEGMENT_PATTERNS) if (pattern.test(segment)) return true;
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
function isDynamicLookingPathname(pathname) {
|
|
13
|
+
return pathname.split("/").filter(Boolean).some(isDynamicLookingSegment);
|
|
14
|
+
}
|
|
15
|
+
function countHiddenContainers() {
|
|
16
|
+
if (typeof document === "undefined") return 0;
|
|
17
|
+
return document.querySelectorAll("[data-router-cache-container=\"true\"][data-router-cache-mode=\"hidden\"]").length;
|
|
18
|
+
}
|
|
19
|
+
function buildSnapshot(cachedRoutes, visiblePathname) {
|
|
20
|
+
const cachedRoutePathnames = Object.keys(cachedRoutes).sort((a, b) => a.localeCompare(b));
|
|
21
|
+
const dynamicLookingRoutePathnames = cachedRoutePathnames.filter(isDynamicLookingPathname);
|
|
22
|
+
const hiddenCachedRoutePathnames = cachedRoutePathnames.filter((pathname) => pathname !== visiblePathname);
|
|
23
|
+
return {
|
|
24
|
+
cachedRoutePathnames,
|
|
25
|
+
dynamicLookingRouteCount: dynamicLookingRoutePathnames.length,
|
|
26
|
+
dynamicLookingRoutePathnames,
|
|
27
|
+
hiddenCachedRouteCount: hiddenCachedRoutePathnames.length,
|
|
28
|
+
hiddenContainerCount: countHiddenContainers(),
|
|
29
|
+
totalCachedRouteCount: cachedRoutePathnames.length,
|
|
30
|
+
visiblePathname
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function getDebugWindow() {
|
|
34
|
+
return globalThis.window;
|
|
35
|
+
}
|
|
36
|
+
function isProduction() {
|
|
37
|
+
return globalThis.process?.env?.NODE_ENV === "production";
|
|
38
|
+
}
|
|
39
|
+
function useRouterCacheDebug(cachedRoutes, visiblePathname) {
|
|
40
|
+
const warningThresholdRef = (0, react.useRef)(null);
|
|
41
|
+
const lastWarnedCountRef = (0, react.useRef)(null);
|
|
42
|
+
const lastSnapshotRef = (0, react.useRef)(null);
|
|
43
|
+
if (lastSnapshotRef.current === null) lastSnapshotRef.current = buildSnapshot(cachedRoutes, visiblePathname);
|
|
44
|
+
const snapshot = (0, react.useMemo)(() => buildSnapshot(cachedRoutes, visiblePathname), [cachedRoutes, visiblePathname]);
|
|
45
|
+
(0, react.useEffect)(() => {
|
|
46
|
+
if (isProduction()) return;
|
|
47
|
+
const debugWindow = getDebugWindow();
|
|
48
|
+
if (!debugWindow) return;
|
|
49
|
+
const refresh = () => {
|
|
50
|
+
lastSnapshotRef.current = buildSnapshot(cachedRoutes, visiblePathname);
|
|
51
|
+
return lastSnapshotRef.current;
|
|
52
|
+
};
|
|
53
|
+
const api = {
|
|
54
|
+
getSnapshot: () => lastSnapshotRef.current ?? snapshot,
|
|
55
|
+
lastSnapshot: snapshot,
|
|
56
|
+
refresh,
|
|
57
|
+
setWarningThreshold: (nextThreshold) => {
|
|
58
|
+
warningThresholdRef.current = typeof nextThreshold === "number" ? nextThreshold : null;
|
|
59
|
+
lastWarnedCountRef.current = null;
|
|
60
|
+
},
|
|
61
|
+
warningThreshold: warningThresholdRef.current
|
|
62
|
+
};
|
|
63
|
+
debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ = api;
|
|
64
|
+
const rafId = globalThis.requestAnimationFrame(() => {
|
|
65
|
+
const nextSnapshot = refresh();
|
|
66
|
+
debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ = {
|
|
67
|
+
...api,
|
|
68
|
+
lastSnapshot: nextSnapshot,
|
|
69
|
+
warningThreshold: warningThresholdRef.current
|
|
70
|
+
};
|
|
71
|
+
const warningThreshold = warningThresholdRef.current;
|
|
72
|
+
if (typeof warningThreshold === "number" && nextSnapshot.totalCachedRouteCount > warningThreshold && lastWarnedCountRef.current !== nextSnapshot.totalCachedRouteCount) lastWarnedCountRef.current = nextSnapshot.totalCachedRouteCount;
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
globalThis.cancelAnimationFrame(rafId);
|
|
76
|
+
if (debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ === api) debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ = void 0;
|
|
77
|
+
};
|
|
78
|
+
}, [
|
|
79
|
+
cachedRoutes,
|
|
80
|
+
snapshot,
|
|
81
|
+
visiblePathname
|
|
82
|
+
]);
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
exports.useRouterCacheDebug = useRouterCacheDebug;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
2
|
+
//#region src/hooks/use-router-cache-debug.ts
|
|
3
|
+
const DYNAMIC_SEGMENT_PATTERNS = [
|
|
4
|
+
/^\d{4,}$/u,
|
|
5
|
+
/^[0-9a-f]{8,}$/iu,
|
|
6
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu
|
|
7
|
+
];
|
|
8
|
+
function isDynamicLookingSegment(segment) {
|
|
9
|
+
for (const pattern of DYNAMIC_SEGMENT_PATTERNS) if (pattern.test(segment)) return true;
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
function isDynamicLookingPathname(pathname) {
|
|
13
|
+
return pathname.split("/").filter(Boolean).some(isDynamicLookingSegment);
|
|
14
|
+
}
|
|
15
|
+
function countHiddenContainers() {
|
|
16
|
+
if (typeof document === "undefined") return 0;
|
|
17
|
+
return document.querySelectorAll("[data-router-cache-container=\"true\"][data-router-cache-mode=\"hidden\"]").length;
|
|
18
|
+
}
|
|
19
|
+
function buildSnapshot(cachedRoutes, visiblePathname) {
|
|
20
|
+
const cachedRoutePathnames = Object.keys(cachedRoutes).sort((a, b) => a.localeCompare(b));
|
|
21
|
+
const dynamicLookingRoutePathnames = cachedRoutePathnames.filter(isDynamicLookingPathname);
|
|
22
|
+
const hiddenCachedRoutePathnames = cachedRoutePathnames.filter((pathname) => pathname !== visiblePathname);
|
|
23
|
+
return {
|
|
24
|
+
cachedRoutePathnames,
|
|
25
|
+
dynamicLookingRouteCount: dynamicLookingRoutePathnames.length,
|
|
26
|
+
dynamicLookingRoutePathnames,
|
|
27
|
+
hiddenCachedRouteCount: hiddenCachedRoutePathnames.length,
|
|
28
|
+
hiddenContainerCount: countHiddenContainers(),
|
|
29
|
+
totalCachedRouteCount: cachedRoutePathnames.length,
|
|
30
|
+
visiblePathname
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function getDebugWindow() {
|
|
34
|
+
return globalThis.window;
|
|
35
|
+
}
|
|
36
|
+
function isProduction() {
|
|
37
|
+
return globalThis.process?.env?.NODE_ENV === "production";
|
|
38
|
+
}
|
|
39
|
+
function useRouterCacheDebug(cachedRoutes, visiblePathname) {
|
|
40
|
+
const warningThresholdRef = useRef(null);
|
|
41
|
+
const lastWarnedCountRef = useRef(null);
|
|
42
|
+
const lastSnapshotRef = useRef(null);
|
|
43
|
+
if (lastSnapshotRef.current === null) lastSnapshotRef.current = buildSnapshot(cachedRoutes, visiblePathname);
|
|
44
|
+
const snapshot = useMemo(() => buildSnapshot(cachedRoutes, visiblePathname), [cachedRoutes, visiblePathname]);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (isProduction()) return;
|
|
47
|
+
const debugWindow = getDebugWindow();
|
|
48
|
+
if (!debugWindow) return;
|
|
49
|
+
const refresh = () => {
|
|
50
|
+
lastSnapshotRef.current = buildSnapshot(cachedRoutes, visiblePathname);
|
|
51
|
+
return lastSnapshotRef.current;
|
|
52
|
+
};
|
|
53
|
+
const api = {
|
|
54
|
+
getSnapshot: () => lastSnapshotRef.current ?? snapshot,
|
|
55
|
+
lastSnapshot: snapshot,
|
|
56
|
+
refresh,
|
|
57
|
+
setWarningThreshold: (nextThreshold) => {
|
|
58
|
+
warningThresholdRef.current = typeof nextThreshold === "number" ? nextThreshold : null;
|
|
59
|
+
lastWarnedCountRef.current = null;
|
|
60
|
+
},
|
|
61
|
+
warningThreshold: warningThresholdRef.current
|
|
62
|
+
};
|
|
63
|
+
debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ = api;
|
|
64
|
+
const rafId = globalThis.requestAnimationFrame(() => {
|
|
65
|
+
const nextSnapshot = refresh();
|
|
66
|
+
debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ = {
|
|
67
|
+
...api,
|
|
68
|
+
lastSnapshot: nextSnapshot,
|
|
69
|
+
warningThreshold: warningThresholdRef.current
|
|
70
|
+
};
|
|
71
|
+
const warningThreshold = warningThresholdRef.current;
|
|
72
|
+
if (typeof warningThreshold === "number" && nextSnapshot.totalCachedRouteCount > warningThreshold && lastWarnedCountRef.current !== nextSnapshot.totalCachedRouteCount) lastWarnedCountRef.current = nextSnapshot.totalCachedRouteCount;
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
globalThis.cancelAnimationFrame(rafId);
|
|
76
|
+
if (debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ === api) debugWindow.__TANSTACK_ROUTER_CACHE_DEBUG__ = void 0;
|
|
77
|
+
};
|
|
78
|
+
}, [
|
|
79
|
+
cachedRoutes,
|
|
80
|
+
snapshot,
|
|
81
|
+
visiblePathname
|
|
82
|
+
]);
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
export { useRouterCacheDebug };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const require_pathname = require("../pathname.cjs");
|
|
2
|
+
const require_router_cache = require("../contexts/router-cache.cjs");
|
|
3
|
+
const require_use_event_listener = require("./use-event-listener.cjs");
|
|
4
|
+
const require_use_update = require("./use-update.cjs");
|
|
5
|
+
//#region src/hooks/use-router-cache.ts
|
|
6
|
+
function useRouterCache() {
|
|
7
|
+
const { cachedRoutes, deleteCachedRoutes } = require_router_cache.useRouterCacheContext();
|
|
8
|
+
const update = require_use_update.useUpdate();
|
|
9
|
+
require_use_event_listener.useEventListener({ on: { activeChange: () => {
|
|
10
|
+
update();
|
|
11
|
+
} } });
|
|
12
|
+
const destroy = (pathname) => {
|
|
13
|
+
deleteCachedRoutes(Array.isArray(pathname) ? pathname : [pathname]);
|
|
14
|
+
};
|
|
15
|
+
const destroyAll = () => {
|
|
16
|
+
deleteCachedRoutes(Object.keys(cachedRoutes));
|
|
17
|
+
};
|
|
18
|
+
const invalidateWhere = (predicate) => {
|
|
19
|
+
const pathnames = Object.entries(cachedRoutes).flatMap(([pathname, route]) => predicate(pathname, route) ? [pathname] : []);
|
|
20
|
+
if (pathnames.length > 0) deleteCachedRoutes(pathnames);
|
|
21
|
+
return pathnames;
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
cachedRoutes,
|
|
25
|
+
destroy,
|
|
26
|
+
destroyAll,
|
|
27
|
+
invalidateWhere,
|
|
28
|
+
isCached: (pathname) => Object.hasOwn(cachedRoutes, require_pathname.normalizeCachedRoutePathname(pathname))
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
exports.useRouterCache = useRouterCache;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { normalizeCachedRoutePathname } from "../pathname.js";
|
|
2
|
+
import { useRouterCacheContext } from "../contexts/router-cache.js";
|
|
3
|
+
import { useEventListener } from "./use-event-listener.js";
|
|
4
|
+
import { useUpdate } from "./use-update.js";
|
|
5
|
+
//#region src/hooks/use-router-cache.ts
|
|
6
|
+
function useRouterCache() {
|
|
7
|
+
const { cachedRoutes, deleteCachedRoutes } = useRouterCacheContext();
|
|
8
|
+
const update = useUpdate();
|
|
9
|
+
useEventListener({ on: { activeChange: () => {
|
|
10
|
+
update();
|
|
11
|
+
} } });
|
|
12
|
+
const destroy = (pathname) => {
|
|
13
|
+
deleteCachedRoutes(Array.isArray(pathname) ? pathname : [pathname]);
|
|
14
|
+
};
|
|
15
|
+
const destroyAll = () => {
|
|
16
|
+
deleteCachedRoutes(Object.keys(cachedRoutes));
|
|
17
|
+
};
|
|
18
|
+
const invalidateWhere = (predicate) => {
|
|
19
|
+
const pathnames = Object.entries(cachedRoutes).flatMap(([pathname, route]) => predicate(pathname, route) ? [pathname] : []);
|
|
20
|
+
if (pathnames.length > 0) deleteCachedRoutes(pathnames);
|
|
21
|
+
return pathnames;
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
cachedRoutes,
|
|
25
|
+
destroy,
|
|
26
|
+
destroyAll,
|
|
27
|
+
invalidateWhere,
|
|
28
|
+
isCached: (pathname) => Object.hasOwn(cachedRoutes, normalizeCachedRoutePathname(pathname))
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useRouterCache };
|