clear-react-router 2.2.9 → 2.3.0
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 +25 -6
- package/dist/index.js +39 -19
- package/dist/utils/commitState.d.ts +2 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -706,20 +706,25 @@ await invalidate('/post/[id]/comment/[id]');
|
|
|
706
706
|
```
|
|
707
707
|
|
|
708
708
|
#### Force revalidation
|
|
709
|
-
By default
|
|
710
|
-
|
|
709
|
+
By default the exact path(s) you pass are always revalidated, even if they were never cached
|
|
710
|
+
— this also makes `invalidate()` work on an error page with an empty cache.
|
|
711
|
+
Pass `{ force: false }` to go back to cache-only revalidation:
|
|
711
712
|
|
|
712
713
|
```tsx
|
|
713
|
-
await invalidate('/about', { force:
|
|
714
|
-
await invalidate(['/about', '/post/10'], { force: true });
|
|
714
|
+
await invalidate('/about', { force: false });
|
|
715
715
|
```
|
|
716
716
|
|
|
717
|
+
Explicit `{ force: true }` behaves the same as the default; it exists for readability
|
|
718
|
+
and for combining with other options (see below).
|
|
719
|
+
|
|
717
720
|
#### Stale-only revalidation
|
|
718
721
|
By default, `invalidate` deletes matching cache entries and refetches them.
|
|
719
722
|
Pass `{ staleOnly: true }` to only revalidate entries that are already stale, leaving fresh ones untouched:
|
|
720
723
|
```tsx
|
|
721
724
|
await invalidate('/notes', { staleOnly: true });
|
|
722
725
|
```
|
|
726
|
+
Note: `staleOnly` disables the default force — uncached paths are left alone, so no new
|
|
727
|
+
network requests are started for routes you never visited.
|
|
723
728
|
|
|
724
729
|
#### Including child routes
|
|
725
730
|
|
|
@@ -754,6 +759,20 @@ will revalidate the cached child routes:
|
|
|
754
759
|
/post/23
|
|
755
760
|
/post/42/comments
|
|
756
761
|
```
|
|
762
|
+
#### Status transitions
|
|
763
|
+
|
|
764
|
+
When the revalidated path is the currently active route, a successful revalidation also
|
|
765
|
+
resets its status to `active`, while a failed one sets it to `error` — so recovering from
|
|
766
|
+
an error page is just `await invalidate()`:
|
|
767
|
+
|
|
768
|
+
```tsx
|
|
769
|
+
// On an errorElement: refetch, and show the page again on success
|
|
770
|
+
await invalidate();
|
|
771
|
+
```
|
|
772
|
+
|
|
773
|
+
Revalidating any other route never touches the current page status — background refreshes
|
|
774
|
+
stay invisible.
|
|
775
|
+
|
|
757
776
|
#### Returns
|
|
758
777
|
|
|
759
778
|
An array of objects with the following structure:
|
|
@@ -764,8 +783,8 @@ Each object represents a revalidated route, where `path` is the route pathname,
|
|
|
764
783
|
|
|
765
784
|
#### Notes
|
|
766
785
|
|
|
767
|
-
* Without `force
|
|
768
|
-
* With `
|
|
786
|
+
* Without `force: false`, the exact pathnames you pass are always revalidated (and stored in the cache), even if they were never cached. Pass `{ force: false }` to revalidate **only routes that already have cached data**.
|
|
787
|
+
* With `{ staleOnly: true }`, uncached paths are never fetched — the default force does not apply.
|
|
769
788
|
* Cached data is cleared before the new loader starts.
|
|
770
789
|
* When used as an event handler, wrap the call in an arrow function:
|
|
771
790
|
|
package/dist/index.js
CHANGED
|
@@ -28,12 +28,15 @@ var useGlobalState = ({ subscribe, getState, setState }, synchronizer) => {
|
|
|
28
28
|
};
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region utils/commitState.ts
|
|
31
|
-
var createCommitState = ({ loaderState, routeItemDataState, blockerState }) => ({
|
|
31
|
+
var createCommitState = ({ loaderState, routeItemDataState, blockerState }) => ({ loaderStateValue, location }) => {
|
|
32
32
|
const isError = loaderStateValue.loaderError || loaderStateValue.beforeLoadError;
|
|
33
|
-
routeItemDataState.setState({
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
routeItemDataState.setState((prevState) => {
|
|
34
|
+
if (prevState.status === "active") return prevState;
|
|
35
|
+
return {
|
|
36
|
+
...prevState,
|
|
37
|
+
location,
|
|
38
|
+
status: isError ? "error" : "active"
|
|
39
|
+
};
|
|
37
40
|
});
|
|
38
41
|
loaderState.setState(loaderStateValue);
|
|
39
42
|
if (blockerState.getState() === "blocked") blockerState.setState("unblocked");
|
|
@@ -277,7 +280,14 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
277
280
|
const prepareNavigation = (routeItem, location, hasError) => {
|
|
278
281
|
updateScrollMap(routeItemDataState, scrollMapState);
|
|
279
282
|
createGcTimeout();
|
|
280
|
-
if (hasError)
|
|
283
|
+
if (hasError) {
|
|
284
|
+
commitNavigation(() => routeItemDataState.setState({
|
|
285
|
+
routeItem,
|
|
286
|
+
location,
|
|
287
|
+
status: "error"
|
|
288
|
+
}));
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
281
291
|
const path = getPath(location);
|
|
282
292
|
if (routeItem?.optimistic && loaderMap.has(path)) {
|
|
283
293
|
commitNavigation(() => routeItemDataState.setState({
|
|
@@ -289,10 +299,18 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
289
299
|
if (currentLoaderState) loaderState.setState(currentLoaderState);
|
|
290
300
|
return;
|
|
291
301
|
}
|
|
292
|
-
if (routeItem?.loader && !isCacheItemFresh(path))
|
|
302
|
+
if (routeItem?.loader && !isCacheItemFresh(path)) {
|
|
303
|
+
commitNavigation(() => routeItemDataState.setState({
|
|
304
|
+
routeItem,
|
|
305
|
+
location,
|
|
306
|
+
status: "pending"
|
|
307
|
+
}));
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
commitNavigation(() => routeItemDataState.setState({
|
|
293
311
|
routeItem,
|
|
294
312
|
location,
|
|
295
|
-
status: "
|
|
313
|
+
status: "active"
|
|
296
314
|
}));
|
|
297
315
|
};
|
|
298
316
|
const polling = (routeItem, nextLocation) => {
|
|
@@ -378,7 +396,6 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
378
396
|
};
|
|
379
397
|
commitNavigation(() => commitState({
|
|
380
398
|
location: nextLocation,
|
|
381
|
-
routeItem: nextItem,
|
|
382
399
|
loaderStateValue
|
|
383
400
|
}));
|
|
384
401
|
clearCurrentGcTimeout(nextItem, nextLocation);
|
|
@@ -401,11 +418,17 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalida
|
|
|
401
418
|
search
|
|
402
419
|
}
|
|
403
420
|
});
|
|
404
|
-
if (result && path === routePathname)
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
421
|
+
if (result && path === routePathname) {
|
|
422
|
+
loaderState.setState({
|
|
423
|
+
data: result.data,
|
|
424
|
+
loaderError: result.error,
|
|
425
|
+
beforeLoadError: null
|
|
426
|
+
});
|
|
427
|
+
routeItemDataState.setState((prevState) => ({
|
|
428
|
+
...prevState,
|
|
429
|
+
status: result.error ? "error" : "active"
|
|
430
|
+
}));
|
|
431
|
+
}
|
|
409
432
|
return {
|
|
410
433
|
path: pathname,
|
|
411
434
|
...result
|
|
@@ -416,7 +439,7 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalida
|
|
|
416
439
|
if (!routeItem) return [];
|
|
417
440
|
const pathnameSet = /* @__PURE__ */ new Set();
|
|
418
441
|
for (const [key] of loaderMap) if (comparePaths(routeItem, separatePathname(key))) pathnameSet.add(key);
|
|
419
|
-
if (options?.force) pathnameSet.add(pathname);
|
|
442
|
+
if (options?.force === true || options?.force === void 0 && !options?.staleOnly) pathnameSet.add(pathname);
|
|
420
443
|
const currentResults = await Promise.all([...pathnameSet].map((pathname) => invalidatePath(routeItem, pathname, options)));
|
|
421
444
|
if (!options?.withChildren || !routeItem.children?.length) return currentResults;
|
|
422
445
|
const [parentPath] = pathname.split("?");
|
|
@@ -979,10 +1002,7 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
979
1002
|
var useParams = router.hooks.useParams;
|
|
980
1003
|
//#endregion
|
|
981
1004
|
//#region hooks/useLocation.ts
|
|
982
|
-
var useLocation = () =>
|
|
983
|
-
const [routeItemData] = router.hooks.useRouteItemData();
|
|
984
|
-
return routeItemData.location;
|
|
985
|
-
};
|
|
1005
|
+
var useLocation = () => router.hooks.useRouteItemDataSelector((state) => state.location);
|
|
986
1006
|
//#endregion
|
|
987
1007
|
//#region hooks/useLoaderState.ts
|
|
988
1008
|
var useLoaderState = router.hooks.useLoaderState;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { LoaderState, Location,
|
|
1
|
+
import { LoaderState, Location, RouterState } from '../types';
|
|
2
2
|
type CommitState = {
|
|
3
3
|
location: Location;
|
|
4
4
|
loaderStateValue: LoaderState;
|
|
5
|
-
routeItem?: RouteItem;
|
|
6
5
|
};
|
|
7
|
-
export declare const createCommitState: ({ loaderState, routeItemDataState, blockerState }: RouterState) => ({
|
|
6
|
+
export declare const createCommitState: ({ loaderState, routeItemDataState, blockerState }: RouterState) => ({ loaderStateValue, location }: CommitState) => void;
|
|
8
7
|
export {};
|