clear-react-router 2.5.0 → 2.5.2
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/README.md +24 -12
- package/dist/index.js +20 -6
- package/dist/types.d.ts +1 -0
- package/dist/utils/utils.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -856,7 +856,7 @@ const UserProfile = () => {
|
|
|
856
856
|
|
|
857
857
|
- Use `staleTime: Infinity` when data should stay fresh until it is explicitly invalidated, garbage-collected or evicted.
|
|
858
858
|
|
|
859
|
-
- The cache is bounded by `maxCacheSize` — once the limit is reached, the least recently used entry is evicted to make room for a new one, regardless of whether it's still fresh. This caps memory usage for apps with many high-cardinality dynamic routes (e.g. `/product/:id` across a large catalog). It defaults to a device-aware value (lower on mobile) and can be overridden on the `Router`:
|
|
859
|
+
- The cache is bounded by `maxCacheSize` — once the limit is reached, the least recently used entry is evicted to make room for a new one, regardless of whether it's still fresh. The currently active route's entry is never evicted. This caps memory usage for apps with many high-cardinality dynamic routes (e.g. `/product/:id` across a large catalog). It defaults to a device-aware value (lower on mobile) and can be overridden on the `Router`:
|
|
860
860
|
|
|
861
861
|
```tsx
|
|
862
862
|
<Router routes={routes} maxCacheSize={200} />
|
|
@@ -878,7 +878,8 @@ const CachePanel = () => {
|
|
|
878
878
|
<ul>
|
|
879
879
|
{entries.map(entry => (
|
|
880
880
|
<li key={entry.path}>
|
|
881
|
-
{entry.path} — {entry.reason ?? 'unknown'} —
|
|
881
|
+
{entry.path} — {entry.reason ?? 'unknown'} —{' '}
|
|
882
|
+
{entry.isCached ? (entry.isLoading ? 'revalidating' : 'cached') : 'loading'}
|
|
882
883
|
</li>
|
|
883
884
|
))}
|
|
884
885
|
</ul>
|
|
@@ -899,6 +900,7 @@ type CacheSnapshotEntry = {
|
|
|
899
900
|
reason?: CacheReason; // what wrote the entry last
|
|
900
901
|
gcExpiresAt: number | undefined; // GC deadline, if a GC timer is pending
|
|
901
902
|
gcDuration: number | undefined; // the route's gcTime, for progress display
|
|
903
|
+
isCached: boolean; // a loader result exists in memory (fresh or stale)
|
|
902
904
|
isLoading: boolean; // a loader for this path is currently running
|
|
903
905
|
};
|
|
904
906
|
|
|
@@ -908,7 +910,8 @@ type CacheSnapshot = CacheSnapshotEntry[];
|
|
|
908
910
|
Notes:
|
|
909
911
|
|
|
910
912
|
- The snapshot is a projection, not a second cache — changing it does nothing.
|
|
911
|
-
- Entries appear for in-flight first loads too (`data: undefined`, `isLoading: true`), so a devtool never misses a request.
|
|
913
|
+
- Entries appear for in-flight first loads too (`data: undefined`, `isCached: false`, `isLoading: true`), so a devtool never misses a request.
|
|
914
|
+
- A cached entry being revalidated has `isCached: true` and `isLoading: true`; read `data` to show the previous result while it refreshes.
|
|
912
915
|
- Mounting the hook refreshes the snapshot, so a panel opened after navigation still sees existing entries.
|
|
913
916
|
- `enableCacheSnapshot` is process-wide (the router is a singleton): if several `<Router>`s render with different values, the last rendered one wins. Enable it only when a consumer is actually mounted.
|
|
914
917
|
|
|
@@ -944,15 +947,21 @@ await invalidate(['/posts', '/profile', '/settings']);
|
|
|
944
947
|
|
|
945
948
|
#### Dynamic routes
|
|
946
949
|
|
|
947
|
-
|
|
950
|
+
Pass a concrete dynamic pathname to revalidate only that cached route:
|
|
948
951
|
|
|
949
|
-
|
|
952
|
+
```tsx
|
|
953
|
+
await invalidate('/post/17');
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
Only `/post/17` is refetched; cached `/post/1` and `/post/42` are left untouched.
|
|
957
|
+
|
|
958
|
+
Pass a dynamic route pattern to revalidate every cached route that matches it:
|
|
950
959
|
|
|
951
960
|
```tsx
|
|
952
|
-
await invalidate('/post/[id]');
|
|
961
|
+
await invalidate('/post/[id]'); // or await invalidate('/post/:id')
|
|
953
962
|
```
|
|
954
963
|
|
|
955
|
-
|
|
964
|
+
This revalidates all cached routes such as:
|
|
956
965
|
|
|
957
966
|
```text
|
|
958
967
|
/post/1
|
|
@@ -960,14 +969,17 @@ will revalidate all cached routes such as:
|
|
|
960
969
|
/post/42
|
|
961
970
|
```
|
|
962
971
|
|
|
972
|
+
Pattern-based invalidation is cache-only: it never sends `:param`/`[param]` to a loader.
|
|
973
|
+
If no cached route matches, nothing is fetched.
|
|
974
|
+
|
|
963
975
|
This also works for nested dynamic routes:
|
|
964
976
|
|
|
965
977
|
```tsx
|
|
966
|
-
await invalidate('/post/[id]/comment
|
|
978
|
+
await invalidate('/post/[id]/comment/:commentId');
|
|
967
979
|
```
|
|
968
980
|
|
|
969
981
|
#### Force revalidation
|
|
970
|
-
By default
|
|
982
|
+
By default concrete pathnames are always revalidated, even if they were never cached
|
|
971
983
|
— this also makes `invalidate()` work on an error page with an empty cache.
|
|
972
984
|
Pass `{ force: false }` to go back to cache-only revalidation:
|
|
973
985
|
|
|
@@ -975,8 +987,8 @@ Pass `{ force: false }` to go back to cache-only revalidation:
|
|
|
975
987
|
await invalidate('/about', { force: false });
|
|
976
988
|
```
|
|
977
989
|
|
|
978
|
-
Explicit `{ force: true }` behaves the same as the default
|
|
979
|
-
|
|
990
|
+
Explicit `{ force: true }` behaves the same as the default for concrete paths. Pattern
|
|
991
|
+
targets stay cache-only regardless of `force`.
|
|
980
992
|
|
|
981
993
|
#### Stale-only revalidation
|
|
982
994
|
By default, `invalidate` deletes matching cache entries and refetches them.
|
|
@@ -1044,7 +1056,7 @@ Each object represents a revalidated route, where `path` is the route pathname,
|
|
|
1044
1056
|
|
|
1045
1057
|
#### Notes
|
|
1046
1058
|
|
|
1047
|
-
* Without `force: false`,
|
|
1059
|
+
* Without `force: false`, concrete pathnames you pass are always revalidated (and stored in the cache), even if they were never cached. Dynamic patterns only revalidate cached matches and never fetch the pattern itself. Pass `{ force: false }` to revalidate **only routes that already have cached data**.
|
|
1048
1060
|
* With `{ staleOnly: true }`, uncached paths are never fetched — the default force does not apply.
|
|
1049
1061
|
* Cached data is cleared before the new loader starts.
|
|
1050
1062
|
* When used as an event handler, wrap the call in an arrow function:
|
package/dist/index.js
CHANGED
|
@@ -143,6 +143,12 @@ var parseWindowLocation = (location) => ({
|
|
|
143
143
|
search: location.search,
|
|
144
144
|
state: history.state ?? void 0
|
|
145
145
|
});
|
|
146
|
+
var isParamSegment = (segment) => segment.startsWith(":") || /^\[[^\]]+]$/.test(segment);
|
|
147
|
+
var matchPathPattern = (pattern, pathname) => {
|
|
148
|
+
const patternSegments = pattern.split("/").filter(Boolean);
|
|
149
|
+
const pathSegments = pathname.split("/").filter(Boolean);
|
|
150
|
+
return patternSegments.length === pathSegments.length ? patternSegments.every((segment, index) => isParamSegment(segment) || segment === pathSegments[index]) : false;
|
|
151
|
+
};
|
|
146
152
|
var comparePaths = (route, pathname) => {
|
|
147
153
|
const pattern = route.pattern.split("/").filter(Boolean);
|
|
148
154
|
const current = pathname.split("/").filter(Boolean);
|
|
@@ -427,7 +433,7 @@ var createNavigate = (routerState, revalidateCache, syncCacheSnapshot) => {
|
|
|
427
433
|
};
|
|
428
434
|
//#endregion
|
|
429
435
|
//#region runtime/invalidate.ts
|
|
430
|
-
var separatePathname = (text) => text.split("?")[0];
|
|
436
|
+
var separatePathname = (text) => text.split("?")[0].replace(/\/+$/, "") || "/";
|
|
431
437
|
var createInvalidate = ({ routeDataState, loaderMap, loadingPromises }, revalidateCache, syncCacheSnapshot) => {
|
|
432
438
|
const invalidatePath = async (routeItem, pathname, options) => {
|
|
433
439
|
const routePathname = routeDataState.getState().location.pathname;
|
|
@@ -463,11 +469,15 @@ var createInvalidate = ({ routeDataState, loaderMap, loadingPromises }, revalida
|
|
|
463
469
|
const invalidateItem = async (pathname, options) => {
|
|
464
470
|
const routeItem = findRoute(separatePathname(pathname));
|
|
465
471
|
if (!routeItem) return [];
|
|
472
|
+
const targetPathname = separatePathname(pathname);
|
|
473
|
+
const isPattern = targetPathname.split("/").filter(Boolean).some(isParamSegment);
|
|
466
474
|
const pathnameSet = /* @__PURE__ */ new Set();
|
|
467
|
-
for (const [key] of loaderMap)
|
|
468
|
-
|
|
475
|
+
for (const [key] of loaderMap) {
|
|
476
|
+
const keyPathname = separatePathname(key);
|
|
477
|
+
if (isPattern ? matchPathPattern(targetPathname, keyPathname) : keyPathname === targetPathname) pathnameSet.add(key);
|
|
478
|
+
}
|
|
469
479
|
const implicitForce = options?.force === void 0 && !options?.staleOnly;
|
|
470
|
-
if (options?.force === true || implicitForce
|
|
480
|
+
if (!isPattern && (options?.force === true || implicitForce)) pathnameSet.add(pathname);
|
|
471
481
|
const currentResults = await Promise.all([...pathnameSet].map((pathname) => invalidatePath(routeItem, pathname, options)));
|
|
472
482
|
if (!options?.withChildren || !routeItem.children?.length) return currentResults;
|
|
473
483
|
const [parentPath] = pathname.split("?");
|
|
@@ -541,11 +551,13 @@ var getRetry = (routeItem) => {
|
|
|
541
551
|
};
|
|
542
552
|
};
|
|
543
553
|
var createRevalidateCache = (routerState, syncCacheSnapshot) => {
|
|
544
|
-
const { loaderMap, loadingPromises, contextState } = routerState;
|
|
554
|
+
const { loaderMap, loadingPromises, contextState, routeDataState } = routerState;
|
|
545
555
|
const flightOwners = /* @__PURE__ */ new Map();
|
|
546
556
|
const evict = () => {
|
|
547
557
|
if (loaderMap.size <= routerConfig.maxCacheSize) return;
|
|
548
|
-
const
|
|
558
|
+
const { pathname, search = "" } = routeDataState.getState().location;
|
|
559
|
+
const currentPath = `${pathname}${search}`;
|
|
560
|
+
const oldestKey = [...loaderMap.keys()].find((key) => key !== currentPath);
|
|
549
561
|
if (oldestKey) {
|
|
550
562
|
loaderMap.delete(oldestKey);
|
|
551
563
|
syncCacheSnapshot();
|
|
@@ -672,6 +684,7 @@ var createCacheSnapshot = (loaderMap, loadingPromises) => {
|
|
|
672
684
|
reason: item.reason,
|
|
673
685
|
gcExpiresAt: item.gcExpiresAt,
|
|
674
686
|
gcDuration: item.gcDuration,
|
|
687
|
+
isCached: true,
|
|
675
688
|
isLoading: loadingPromises.has(path)
|
|
676
689
|
});
|
|
677
690
|
for (const path of loadingPromises.keys()) {
|
|
@@ -683,6 +696,7 @@ var createCacheSnapshot = (loaderMap, loadingPromises) => {
|
|
|
683
696
|
staleTime: routerConfig.defaultStaleTime ?? 0,
|
|
684
697
|
gcExpiresAt: void 0,
|
|
685
698
|
gcDuration: void 0,
|
|
699
|
+
isCached: false,
|
|
686
700
|
isLoading: true
|
|
687
701
|
});
|
|
688
702
|
}
|
package/dist/types.d.ts
CHANGED
package/dist/utils/utils.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { Store } from '../create';
|
|
|
2
2
|
import { Location, RouteItem, RouteData, RouterState, ScrollMap, SearchObject } from '../types';
|
|
3
3
|
export declare const getParams: (location: Location, routeItem?: RouteItem) => Record<string, string>;
|
|
4
4
|
export declare const parseWindowLocation: (location: typeof window.location) => Location;
|
|
5
|
+
export declare const isParamSegment: (segment: string) => boolean;
|
|
6
|
+
export declare const matchPathPattern: (pattern: string, pathname: string) => boolean;
|
|
5
7
|
export declare const comparePaths: (route: RouteItem, pathname: string) => boolean;
|
|
6
8
|
export declare const isMobile: () => boolean;
|
|
7
9
|
export declare const sleep: (ms: number) => Promise<unknown>;
|