clear-react-router 2.3.1 → 2.4.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 +1 -1
- package/dist/hooks/useLoaderState.d.ts +2 -1
- package/dist/hooks/useRestoreRouteScroll.d.ts +2 -0
- package/dist/index.js +122 -105
- package/dist/runtime/invalidate.d.ts +1 -1
- package/dist/runtime/prefetch.d.ts +1 -1
- package/dist/runtime/revalidateCache.d.ts +1 -1
- package/dist/types.d.ts +8 -7
- package/dist/utils/commitState.d.ts +1 -1
- package/dist/utils/utils.d.ts +2 -2
- package/package.json +1 -1
- package/dist/hooks/useIsRoutePending.d.ts +0 -1
- package/dist/hooks/usePreserveScroll.d.ts +0 -2
package/README.md
CHANGED
|
@@ -86,7 +86,7 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
|
|
|
86
86
|
| `staleTime` | `number \| undefined` | Time in milliseconds before cached loader data is considered stale. Overrides Router.defaultStaleTime. If neither value is provided, cached data never expires |
|
|
87
87
|
| `gcTime` | `number \| undefined` | How long, in milliseconds, an unused cache entry is kept in memory after you navigate away, before it's garbage-collected |
|
|
88
88
|
| `actions` | `({ params, context, searchParams, setContext, location }) => Record<string, (data: Record<string, unknown>) => unknown \| Promise<unknown>>` | Defines route actions for data mutations. |
|
|
89
|
-
| `pollingInterval` | `number \| undefined` | Polling interval (in milliseconds) for automatically revalidating data while the route is active |
|
|
89
|
+
| `pollingInterval` | `number \| undefined` | Polling interval (in milliseconds) for automatically revalidating data while the route is active. Each tick refetches unconditionally, ignoring `staleTime` |
|
|
90
90
|
| `scrollRestoration` | `boolean \| string[] \| undefined` | Restore scroll position when navigating back to this route. `true` restores the window scroll; a string array restores scroll inside specific scrollable elements, matched by their `id` |
|
|
91
91
|
| `scrollRestorationBehavior` | `'auto' \| 'smooth' \| 'instant'` | Scroll restoration behavior |
|
|
92
92
|
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import type { LoaderState } from '../types';
|
|
2
|
+
export declare const useLoaderState: <T = unknown>() => LoaderState<T>;
|
package/dist/index.js
CHANGED
|
@@ -28,14 +28,14 @@ var useGlobalState = ({ subscribe, getState, setState }, synchronizer) => {
|
|
|
28
28
|
};
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region utils/commitState.ts
|
|
31
|
-
var createCommitState = ({
|
|
31
|
+
var createCommitState = ({ routeDataState, blockerState }) => ({ routeItem, loaderStateValue, location }) => {
|
|
32
32
|
const isError = loaderStateValue.loaderError || loaderStateValue.beforeLoadError;
|
|
33
|
-
|
|
33
|
+
routeDataState.setState({
|
|
34
34
|
routeItem,
|
|
35
35
|
location,
|
|
36
|
-
status: isError ? "error" : "active"
|
|
36
|
+
status: isError ? "error" : "active",
|
|
37
|
+
loaderState: loaderStateValue
|
|
37
38
|
});
|
|
38
|
-
loaderState.setState(loaderStateValue);
|
|
39
39
|
if (blockerState.getState() === "blocked") blockerState.setState("unblocked");
|
|
40
40
|
if ((location.search ? `${location.pathname}${location.search}` : location.pathname) === window.location.pathname + window.location.search) return;
|
|
41
41
|
history.pushState(location.state ?? null, "", `${location.pathname}${location.search}`);
|
|
@@ -163,8 +163,8 @@ var getPartialLoaderArgs = (contextState, location, routeItem) => {
|
|
|
163
163
|
};
|
|
164
164
|
};
|
|
165
165
|
var isVerticalScroll = (el) => !!el && el.scrollHeight > el.clientHeight;
|
|
166
|
-
var updateScrollMap = (
|
|
167
|
-
const { routeItem, location: { pathname } } =
|
|
166
|
+
var updateScrollMap = (routeDataState, scrollMapState) => {
|
|
167
|
+
const { routeItem, location: { pathname } } = routeDataState.getState();
|
|
168
168
|
scrollMapState.setState((prevState) => {
|
|
169
169
|
if (Array.isArray(routeItem?.scrollRestoration)) return {
|
|
170
170
|
...prevState,
|
|
@@ -213,7 +213,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
213
213
|
pollingController?.abort();
|
|
214
214
|
pollingController = null;
|
|
215
215
|
};
|
|
216
|
-
const {
|
|
216
|
+
const { contextState, loaderMap, routeDataState, scrollMapState, blockerState, blockedTargetState } = routerState;
|
|
217
217
|
const commitState = createCommitState(routerState);
|
|
218
218
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
219
219
|
const createSignal = () => {
|
|
@@ -233,7 +233,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
233
233
|
const { defaultBeforeLoad } = routerConfig;
|
|
234
234
|
const runBeforeLoad = async (loaderFn) => {
|
|
235
235
|
const redirect = async (redirected) => {
|
|
236
|
-
const { location } =
|
|
236
|
+
const { location } = routeDataState.getState();
|
|
237
237
|
const prevLocation = {
|
|
238
238
|
pathname: location.pathname,
|
|
239
239
|
search: location.search
|
|
@@ -262,7 +262,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
262
262
|
return error;
|
|
263
263
|
};
|
|
264
264
|
const createGcTimeout = () => {
|
|
265
|
-
const { routeItem, location } =
|
|
265
|
+
const { routeItem, location } = routeDataState.getState();
|
|
266
266
|
if (!routeItem?.gcTime) return;
|
|
267
267
|
const path = `${location.pathname}${location.search}`;
|
|
268
268
|
const currentLoaderEntry = loaderMap.get(path);
|
|
@@ -275,25 +275,27 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
275
275
|
});
|
|
276
276
|
};
|
|
277
277
|
const commitOptimisticState = (routeItem, location, path) => {
|
|
278
|
-
commitNavigation(() => routeItemDataState.setState({
|
|
279
|
-
routeItem,
|
|
280
|
-
location,
|
|
281
|
-
status: "optimistic"
|
|
282
|
-
}));
|
|
283
278
|
const currentLoaderState = loaderMap.get(path)?.state;
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
279
|
+
commitNavigation(() => {
|
|
280
|
+
routeDataState.setState((prevState) => ({
|
|
281
|
+
...prevState,
|
|
282
|
+
routeItem,
|
|
283
|
+
location,
|
|
284
|
+
status: "optimistic",
|
|
285
|
+
...currentLoaderState && { loaderState: currentLoaderState }
|
|
286
|
+
}));
|
|
287
|
+
});
|
|
292
288
|
};
|
|
289
|
+
const commitPendingState = (routeItem, location) => commitNavigation(() => routeDataState.setState((prevState) => ({
|
|
290
|
+
...prevState,
|
|
291
|
+
routeItem,
|
|
292
|
+
location,
|
|
293
|
+
status: "pending"
|
|
294
|
+
})));
|
|
293
295
|
const isOptimisticHit = (routeItem, path) => !!routeItem?.optimistic && loaderMap.has(path);
|
|
294
296
|
const needsLoading = (routeItem, path) => !!routeItem?.loader && !isCacheItemFresh(path);
|
|
295
297
|
const prepareNavigation = (routeItem, location, hasError) => {
|
|
296
|
-
updateScrollMap(
|
|
298
|
+
updateScrollMap(routeDataState, scrollMapState);
|
|
297
299
|
createGcTimeout();
|
|
298
300
|
if (hasError) return;
|
|
299
301
|
const path = getPath(location);
|
|
@@ -305,11 +307,25 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
305
307
|
pollingController?.abort();
|
|
306
308
|
pollingController = new AbortController();
|
|
307
309
|
const signal = pollingController.signal;
|
|
308
|
-
interval = window.setInterval(() =>
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
310
|
+
interval = window.setInterval(async () => {
|
|
311
|
+
const result = await revalidateCache({
|
|
312
|
+
routeItem,
|
|
313
|
+
location: nextLocation,
|
|
314
|
+
signal,
|
|
315
|
+
force: true
|
|
316
|
+
});
|
|
317
|
+
if (!result || signal.aborted) return;
|
|
318
|
+
const { location: currentLocation } = routeDataState.getState();
|
|
319
|
+
if (`${currentLocation.pathname}${currentLocation.search ?? ""}` !== getPath(nextLocation)) return;
|
|
320
|
+
routeDataState.setState((prevState) => ({
|
|
321
|
+
...prevState,
|
|
322
|
+
loaderState: {
|
|
323
|
+
data: result.data,
|
|
324
|
+
loaderError: result.error,
|
|
325
|
+
beforeLoadError: null
|
|
326
|
+
}
|
|
327
|
+
}));
|
|
328
|
+
}, routeItem.pollingInterval);
|
|
313
329
|
};
|
|
314
330
|
const getLoaderDurationPromise = (routeItem, nextLocation) => {
|
|
315
331
|
const minLoaderDuration = routeItem?.minLoaderDuration ?? routerConfig.defaultMinLoaderDuration ?? 0;
|
|
@@ -394,9 +410,9 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
394
410
|
//#endregion
|
|
395
411
|
//#region runtime/invalidate.ts
|
|
396
412
|
var separatePathname = (text) => text.split("?")[0];
|
|
397
|
-
var createInvalidate = ({
|
|
413
|
+
var createInvalidate = ({ routeDataState, loaderMap }, revalidateCache) => {
|
|
398
414
|
const invalidatePath = async (routeItem, pathname, options) => {
|
|
399
|
-
const routePathname =
|
|
415
|
+
const routePathname = routeDataState.getState().location.pathname;
|
|
400
416
|
if (!options?.staleOnly) loaderMap.delete(pathname);
|
|
401
417
|
const [path, search = ""] = pathname.split("?");
|
|
402
418
|
const result = await revalidateCache({
|
|
@@ -406,17 +422,15 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalida
|
|
|
406
422
|
search
|
|
407
423
|
}
|
|
408
424
|
});
|
|
409
|
-
if (result && path === routePathname) {
|
|
410
|
-
|
|
425
|
+
if (result && path === routePathname) routeDataState.setState((prevState) => ({
|
|
426
|
+
...prevState,
|
|
427
|
+
loaderState: {
|
|
411
428
|
data: result.data,
|
|
412
429
|
loaderError: result.error,
|
|
413
430
|
beforeLoadError: null
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
status: result.error ? "error" : "active"
|
|
418
|
-
}));
|
|
419
|
-
}
|
|
431
|
+
},
|
|
432
|
+
status: result.error ? "error" : "active"
|
|
433
|
+
}));
|
|
420
434
|
return {
|
|
421
435
|
path: pathname,
|
|
422
436
|
...result
|
|
@@ -435,7 +449,7 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalida
|
|
|
435
449
|
return [...currentResults, ...childResults.flat()];
|
|
436
450
|
};
|
|
437
451
|
return async (pathList, options) => {
|
|
438
|
-
const { pathname, search } =
|
|
452
|
+
const { pathname, search } = routeDataState.getState().location;
|
|
439
453
|
const routePathname = `${pathname}${search}`;
|
|
440
454
|
const pathnameList = Array.isArray(pathList) ? pathList : pathList ? [pathList] : [routePathname];
|
|
441
455
|
return (await Promise.all(pathnameList.map((pathname) => invalidateItem(pathname, options)))).flat();
|
|
@@ -443,10 +457,10 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalida
|
|
|
443
457
|
};
|
|
444
458
|
//#endregion
|
|
445
459
|
//#region runtime/prefetch.ts
|
|
446
|
-
var createPrefetch = ({
|
|
460
|
+
var createPrefetch = ({ routeDataState }, revalidateCache) => async (location) => {
|
|
447
461
|
const item = findRoute(location.pathname);
|
|
448
462
|
if (item) {
|
|
449
|
-
const currentLocation =
|
|
463
|
+
const currentLocation = routeDataState.getState().location;
|
|
450
464
|
if (location.pathname === currentLocation.pathname && location.search === currentLocation.search) return;
|
|
451
465
|
await item.preloadElement?.();
|
|
452
466
|
await revalidateCache({
|
|
@@ -490,7 +504,7 @@ var createRevalidateCache = (routerState) => {
|
|
|
490
504
|
}
|
|
491
505
|
return item;
|
|
492
506
|
};
|
|
493
|
-
const revalidateCache = async ({ routeItem, location, signal }, retried = 0) => {
|
|
507
|
+
const revalidateCache = async ({ routeItem, location, signal, force }, retried = 0) => {
|
|
494
508
|
if (!routeItem?.loader) return;
|
|
495
509
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
496
510
|
const { pathname, search = "" } = location;
|
|
@@ -506,7 +520,7 @@ var createRevalidateCache = (routerState) => {
|
|
|
506
520
|
return loadingPromises.get(path);
|
|
507
521
|
}
|
|
508
522
|
}
|
|
509
|
-
if (isCacheItemFresh(path)) {
|
|
523
|
+
if (!force && isCacheItemFresh(path)) {
|
|
510
524
|
const item = moveItemToLastPosition(path);
|
|
511
525
|
if (!item?.state) return void 0;
|
|
512
526
|
return item.state.loaderError ? {
|
|
@@ -527,7 +541,8 @@ var createRevalidateCache = (routerState) => {
|
|
|
527
541
|
try {
|
|
528
542
|
const result = await routeItem?.loader({
|
|
529
543
|
...getPartialLoaderArgs(contextState, location, routeItem),
|
|
530
|
-
signal: effectiveSignal
|
|
544
|
+
signal: effectiveSignal,
|
|
545
|
+
attempt: retried + 1
|
|
531
546
|
});
|
|
532
547
|
loaderMap.set(path, {
|
|
533
548
|
state: {
|
|
@@ -558,7 +573,8 @@ var createRevalidateCache = (routerState) => {
|
|
|
558
573
|
return revalidateCache({
|
|
559
574
|
routeItem,
|
|
560
575
|
location,
|
|
561
|
-
signal
|
|
576
|
+
signal,
|
|
577
|
+
force
|
|
562
578
|
}, retried + 1);
|
|
563
579
|
} else return {
|
|
564
580
|
data: null,
|
|
@@ -580,15 +596,15 @@ var createRevalidateCache = (routerState) => {
|
|
|
580
596
|
//#region creators/createRouterInstance.ts
|
|
581
597
|
var createRouterInstance = (synchronizer) => {
|
|
582
598
|
const routerState = {
|
|
583
|
-
|
|
599
|
+
routeDataState: create({
|
|
584
600
|
routeItem: void 0,
|
|
585
601
|
location: {},
|
|
586
|
-
status: "idle"
|
|
602
|
+
status: "idle",
|
|
603
|
+
loaderState: EMPTY_LOADER_STATE
|
|
587
604
|
}),
|
|
588
605
|
scrollMapState: create({}),
|
|
589
606
|
contextState: create({}),
|
|
590
607
|
blockerState: create("unblocked"),
|
|
591
|
-
loaderState: create(EMPTY_LOADER_STATE),
|
|
592
608
|
blockedTargetState: create(null),
|
|
593
609
|
loaderMap: /* @__PURE__ */ new Map(),
|
|
594
610
|
loadingPromises: /* @__PURE__ */ new Map()
|
|
@@ -598,7 +614,7 @@ var createRouterInstance = (synchronizer) => {
|
|
|
598
614
|
const invalidate = createInvalidate(routerState, revalidateCache);
|
|
599
615
|
const prefetch = createPrefetch(routerState, revalidateCache);
|
|
600
616
|
const getCurrentAction = (actionKey) => {
|
|
601
|
-
const { routeItem, location } = routerState.
|
|
617
|
+
const { routeItem, location } = routerState.routeDataState.getState();
|
|
602
618
|
if (!routeItem) throw new Error("Route not found");
|
|
603
619
|
if (!routeItem.actions) throw new Error("Route action creator not found");
|
|
604
620
|
const context = routerState.contextState.getState();
|
|
@@ -623,17 +639,15 @@ var createRouterInstance = (synchronizer) => {
|
|
|
623
639
|
},
|
|
624
640
|
hooks: {
|
|
625
641
|
useBlockerState: () => useGlobalState(routerState.blockerState, synchronizer),
|
|
626
|
-
useRouteItemData: () => useGlobalState(routerState.routeItemDataState, synchronizer),
|
|
627
642
|
useScrollMap: () => useGlobalState(routerState.scrollMapState, synchronizer),
|
|
628
643
|
useContextState: () => useGlobalState(routerState.contextState, synchronizer),
|
|
629
644
|
useBlockedTargetState: () => useGlobalState(routerState.blockedTargetState, synchronizer),
|
|
630
|
-
useLoaderState: () => useGlobalState(routerState.loaderState, synchronizer)[0],
|
|
631
645
|
useParams: () => {
|
|
632
|
-
const { routeItem, location } = routerState.
|
|
646
|
+
const { routeItem, location } = routerState.routeDataState.getState();
|
|
633
647
|
return getParams(location, routeItem);
|
|
634
648
|
},
|
|
635
649
|
useNavigate: () => async (arg) => {
|
|
636
|
-
const { location } = routerState.
|
|
650
|
+
const { location } = routerState.routeDataState.getState();
|
|
637
651
|
const prevLocation = {
|
|
638
652
|
pathname: location.pathname,
|
|
639
653
|
search: location.search
|
|
@@ -675,7 +689,7 @@ var createRouterInstance = (synchronizer) => {
|
|
|
675
689
|
};
|
|
676
690
|
},
|
|
677
691
|
useScrollRestoration: (restorationBehavior) => () => {
|
|
678
|
-
const { routeItem, location: { pathname } } = routerState.
|
|
692
|
+
const { routeItem, location: { pathname } } = routerState.routeDataState.getState();
|
|
679
693
|
const scrollMap = routerState.scrollMapState.getState();
|
|
680
694
|
if (!routeItem || routeItem.scrollRestoration === false || !scrollMap[pathname]) return;
|
|
681
695
|
const behavior = routeItem.scrollRestorationBehavior ?? restorationBehavior;
|
|
@@ -700,10 +714,14 @@ var createRouterInstance = (synchronizer) => {
|
|
|
700
714
|
});
|
|
701
715
|
});
|
|
702
716
|
},
|
|
703
|
-
|
|
704
|
-
const state = routerState.
|
|
717
|
+
useRouteDataSelector: (callback) => {
|
|
718
|
+
const state = routerState.routeDataState;
|
|
705
719
|
return synchronizer(state.subscribe, () => callback(state.getState()));
|
|
706
|
-
}
|
|
720
|
+
},
|
|
721
|
+
useSetLocation: () => (location) => routerState.routeDataState.setState((prevState) => ({
|
|
722
|
+
...prevState,
|
|
723
|
+
location
|
|
724
|
+
}))
|
|
707
725
|
}
|
|
708
726
|
};
|
|
709
727
|
};
|
|
@@ -714,8 +732,8 @@ var router = createRouterInstance(useSyncExternalStore);
|
|
|
714
732
|
//#region hooks/useNavigation.ts
|
|
715
733
|
var useNavigation = () => {
|
|
716
734
|
const { navigate } = router.runtime;
|
|
717
|
-
const {
|
|
718
|
-
const
|
|
735
|
+
const { useRouteDataSelector, useBlockerState } = router.hooks;
|
|
736
|
+
const location = useRouteDataSelector((state) => state.location);
|
|
719
737
|
const [blockerState] = useBlockerState();
|
|
720
738
|
useEffect(() => {
|
|
721
739
|
const handler = async (event) => {
|
|
@@ -763,10 +781,10 @@ var useRestoreScroll = (behavior) => {
|
|
|
763
781
|
return useScrollRestoration(behavior ?? defaultScrollRestorationBehavior);
|
|
764
782
|
};
|
|
765
783
|
//#endregion
|
|
766
|
-
//#region hooks/
|
|
767
|
-
var
|
|
784
|
+
//#region hooks/useRestoreRouteScroll.ts
|
|
785
|
+
var useRestoreRouteScroll = (behavior) => {
|
|
768
786
|
const restoreScroll = useRestoreScroll(behavior);
|
|
769
|
-
useEffect(
|
|
787
|
+
useEffect(restoreScroll, [restoreScroll]);
|
|
770
788
|
};
|
|
771
789
|
//#endregion
|
|
772
790
|
//#region hooks/useSetRouterConfig.ts
|
|
@@ -814,8 +832,10 @@ var IS_MOBILE = isMobile();
|
|
|
814
832
|
var MOBILE_CACHE_SIZE = 60;
|
|
815
833
|
var DESKTOP_CACHE_SIZE = 150;
|
|
816
834
|
var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, defaultMinLoaderDuration = 0, revalidateOnFocus = false, revalidateOnReconnect = false, maxCacheSize = IS_MOBILE ? MOBILE_CACHE_SIZE : DESKTOP_CACHE_SIZE, defaultPrefetch = IS_MOBILE ? "viewport" : "hover", defaultHoverPrefetchDelay = 150, defaultScrollRestorationBehavior = "auto", errorBoundary: ErrorBoundary = EmptyBoundary }) => {
|
|
817
|
-
const {
|
|
818
|
-
const
|
|
835
|
+
const { useRouteDataSelector } = router.hooks;
|
|
836
|
+
const routeItem = useRouteDataSelector((state) => state.routeItem);
|
|
837
|
+
const location = useRouteDataSelector((state) => state.location);
|
|
838
|
+
const status = useRouteDataSelector((state) => state.status);
|
|
819
839
|
const isError = status === "error";
|
|
820
840
|
const isLoading = status === "pending";
|
|
821
841
|
useNavigation();
|
|
@@ -836,7 +856,7 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
836
856
|
});
|
|
837
857
|
useApplyCustomAnimation(animationDuration);
|
|
838
858
|
useSetInitialContext(initialContext);
|
|
839
|
-
|
|
859
|
+
useRestoreRouteScroll(defaultScrollRestorationBehavior);
|
|
840
860
|
useReload();
|
|
841
861
|
if (isLoading && !isError) return renderElement(routeItem?.loaderFallback || defaultLoaderFallback);
|
|
842
862
|
if (!routeItem) return null;
|
|
@@ -847,12 +867,6 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
847
867
|
});
|
|
848
868
|
};
|
|
849
869
|
//#endregion
|
|
850
|
-
//#region hooks/useIsRoutePending.ts
|
|
851
|
-
var useIsRoutePending = (routePath) => {
|
|
852
|
-
const { useRouteItemDataSelector } = router.hooks;
|
|
853
|
-
return useRouteItemDataSelector(({ location: { pathname }, status }) => pathname === routePath && status === "pending");
|
|
854
|
-
};
|
|
855
|
-
//#endregion
|
|
856
870
|
//#region hooks/useNavigate.ts
|
|
857
871
|
var useNavigate = router.hooks.useNavigate;
|
|
858
872
|
//#endregion
|
|
@@ -860,15 +874,14 @@ var useNavigate = router.hooks.useNavigate;
|
|
|
860
874
|
var defaultAs = (props) => /* @__PURE__ */ jsx("a", { ...props });
|
|
861
875
|
var comparator = (to, pathname, exact) => to === "/" ? pathname === "/" : exact ? pathname === to : pathname === to || pathname?.startsWith(`${to}/`);
|
|
862
876
|
var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact = false, activeClassName = "active-link", pendingClassName = "pending-link", ...rest }) => {
|
|
863
|
-
const {
|
|
864
|
-
const isPending =
|
|
877
|
+
const { useRouteDataSelector } = router.hooks;
|
|
878
|
+
const isPending = useRouteDataSelector(({ location: { pathname }, status }) => pathname === to && status === "pending");
|
|
865
879
|
const navigate = useNavigate();
|
|
866
|
-
const isActive =
|
|
880
|
+
const isActive = useRouteDataSelector(({ location: { pathname } }) => comparator(to, pathname, exact));
|
|
867
881
|
const timeout = useRef(0);
|
|
868
882
|
const elementRef = useRef(null);
|
|
869
|
-
const
|
|
870
|
-
const
|
|
871
|
-
const prefetchDelay = hoverPrefetchDelay ?? configPrefetchDelay;
|
|
883
|
+
const getPrefetchStrategy = useCallback(() => linkPrefetch || routerConfig.defaultPrefetch, [linkPrefetch]);
|
|
884
|
+
const getPrefetchDelay = useCallback(() => hoverPrefetchDelay ?? routerConfig.defaultHoverPrefetchDelay, [hoverPrefetchDelay]);
|
|
872
885
|
const searchString = typeof search === "object" ? formatSearchObject(search) : search;
|
|
873
886
|
const location = useMemo(() => ({
|
|
874
887
|
pathname: to,
|
|
@@ -880,29 +893,30 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
880
893
|
to
|
|
881
894
|
]);
|
|
882
895
|
const onMouseEnter = useCallback(() => {
|
|
883
|
-
|
|
896
|
+
const prefetchDelay = getPrefetchDelay();
|
|
897
|
+
if (getPrefetchStrategy() !== "hover" || !prefetchDelay) return;
|
|
884
898
|
if (timeout.current) clearTimeout(timeout.current);
|
|
885
899
|
timeout.current = window.setTimeout(() => router.runtime.prefetch(location), prefetchDelay);
|
|
886
900
|
}, [
|
|
887
|
-
|
|
888
|
-
|
|
901
|
+
getPrefetchStrategy,
|
|
902
|
+
getPrefetchDelay,
|
|
889
903
|
location
|
|
890
904
|
]);
|
|
891
905
|
const onMouseLeave = useCallback(() => {
|
|
892
|
-
if (
|
|
906
|
+
if (getPrefetchStrategy() !== "hover" || !getPrefetchDelay()) return;
|
|
893
907
|
if (timeout.current) {
|
|
894
908
|
clearTimeout(timeout.current);
|
|
895
909
|
timeout.current = 0;
|
|
896
910
|
}
|
|
897
|
-
}, [
|
|
911
|
+
}, [getPrefetchStrategy, getPrefetchDelay]);
|
|
898
912
|
useEffect(() => {
|
|
899
|
-
if (
|
|
913
|
+
if (getPrefetchStrategy() !== "render") return;
|
|
900
914
|
(async () => {
|
|
901
915
|
await router.runtime.prefetch(location);
|
|
902
916
|
})();
|
|
903
|
-
}, [
|
|
917
|
+
}, [getPrefetchStrategy, location]);
|
|
904
918
|
useEffect(() => {
|
|
905
|
-
if (
|
|
919
|
+
if (getPrefetchStrategy() !== "viewport") return;
|
|
906
920
|
const element = elementRef.current;
|
|
907
921
|
if (!element) return;
|
|
908
922
|
const observer = new IntersectionObserver(async ([entry]) => {
|
|
@@ -912,7 +926,7 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
912
926
|
});
|
|
913
927
|
observer.observe(element);
|
|
914
928
|
return () => observer.disconnect();
|
|
915
|
-
}, [
|
|
929
|
+
}, [getPrefetchStrategy, location]);
|
|
916
930
|
useEffect(() => () => {
|
|
917
931
|
if (timeout.current) clearTimeout(timeout.current);
|
|
918
932
|
}, []);
|
|
@@ -957,10 +971,13 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
957
971
|
var useParams = router.hooks.useParams;
|
|
958
972
|
//#endregion
|
|
959
973
|
//#region hooks/useLocation.ts
|
|
960
|
-
var useLocation = () => router.hooks.
|
|
974
|
+
var useLocation = () => router.hooks.useRouteDataSelector((state) => state.location);
|
|
961
975
|
//#endregion
|
|
962
976
|
//#region hooks/useLoaderState.ts
|
|
963
|
-
var useLoaderState =
|
|
977
|
+
var useLoaderState = () => {
|
|
978
|
+
const { useRouteDataSelector } = router.hooks;
|
|
979
|
+
return useRouteDataSelector((state) => state.loaderState);
|
|
980
|
+
};
|
|
964
981
|
//#endregion
|
|
965
982
|
//#region hooks/useInvalidate.ts
|
|
966
983
|
var useInvalidate = () => router.runtime.invalidate;
|
|
@@ -1004,8 +1021,8 @@ var useAction = router.hooks.useAction;
|
|
|
1004
1021
|
//#endregion
|
|
1005
1022
|
//#region hooks/useRouteStatus.ts
|
|
1006
1023
|
function useRouteStatus(predicate) {
|
|
1007
|
-
const {
|
|
1008
|
-
return
|
|
1024
|
+
const { useRouteDataSelector } = router.hooks;
|
|
1025
|
+
return useRouteDataSelector(({ status }) => predicate ? predicate(status) : status);
|
|
1009
1026
|
}
|
|
1010
1027
|
//#endregion
|
|
1011
1028
|
//#region hooks/useRouterContext.ts
|
|
@@ -1026,8 +1043,10 @@ var useLatest = (value) => {
|
|
|
1026
1043
|
return ref;
|
|
1027
1044
|
};
|
|
1028
1045
|
var useSearchParams = () => {
|
|
1029
|
-
const
|
|
1030
|
-
const
|
|
1046
|
+
const { useRouteDataSelector, useSetLocation } = router.hooks;
|
|
1047
|
+
const location = useRouteDataSelector((state) => state.location);
|
|
1048
|
+
const { search = window.location.search } = location;
|
|
1049
|
+
const setLocation = useSetLocation();
|
|
1031
1050
|
const searchRef = useLatest(search);
|
|
1032
1051
|
const searchParams = useMemo(() => new URLSearchParams(search), [search]);
|
|
1033
1052
|
const getSearchParams = useCallback((param) => {
|
|
@@ -1037,15 +1056,12 @@ var useSearchParams = () => {
|
|
|
1037
1056
|
const navigateWithSearchParams = useCallback((params) => {
|
|
1038
1057
|
const newSearch = params.toString();
|
|
1039
1058
|
const search = newSearch ? `?${newSearch}` : "";
|
|
1040
|
-
history.replaceState(history.state, "", pathname + search);
|
|
1041
|
-
|
|
1042
|
-
...
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
}
|
|
1047
|
-
}));
|
|
1048
|
-
}, [pathname, setRouteItemData]);
|
|
1059
|
+
history.replaceState(history.state, "", (location ?? window.location).pathname + search);
|
|
1060
|
+
setLocation({
|
|
1061
|
+
...location,
|
|
1062
|
+
search
|
|
1063
|
+
});
|
|
1064
|
+
}, [location, setLocation]);
|
|
1049
1065
|
return {
|
|
1050
1066
|
searchParams,
|
|
1051
1067
|
getSearchParams,
|
|
@@ -1107,15 +1123,16 @@ var createLazyComponent = (importFn, fallback) => {
|
|
|
1107
1123
|
var LAZY_MARKER = Symbol("clear-router-lazy");
|
|
1108
1124
|
//#endregion
|
|
1109
1125
|
//#region creators/createRouter.ts
|
|
1110
|
-
var isLazy = (
|
|
1126
|
+
var isLazy = (component) => typeof component === "object" && component !== null && LAZY_MARKER in component;
|
|
1127
|
+
var resolve = (component, fallback) => isLazy(component) ? createLazyComponent(component.importFn, fallback).Component : component;
|
|
1111
1128
|
var parseClientRouteItem = (el, parentPattern = "") => {
|
|
1112
1129
|
const pattern = `${parentPattern}/${el.path}`.replace(/\/+/g, "/");
|
|
1113
|
-
const preloadElement = isLazy(el) ? createLazyComponent(el.element.importFn, el.fallback).preloadElement : void 0;
|
|
1114
|
-
const resolvedElement = isLazy(el) ? createLazyComponent(el.element.importFn, el.fallback).Component : el.element;
|
|
1130
|
+
const preloadElement = isLazy(el.element) ? createLazyComponent(el.element.importFn, el.fallback).preloadElement : void 0;
|
|
1115
1131
|
return [{
|
|
1116
1132
|
...el,
|
|
1117
1133
|
pattern,
|
|
1118
|
-
element:
|
|
1134
|
+
element: resolve(el.element, el.fallback),
|
|
1135
|
+
errorElement: resolve(el.errorElement, el.fallback),
|
|
1119
1136
|
preloadElement
|
|
1120
1137
|
}, ...el.children?.flatMap((child) => parseClientRouteItem(child, pattern)) ?? []];
|
|
1121
1138
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type InvalidateOptions, InvalidateResult, RevalidateCache, RouterState } from '../types';
|
|
2
|
-
export declare const createInvalidate: ({
|
|
2
|
+
export declare const createInvalidate: ({ routeDataState, loaderMap }: RouterState, revalidateCache: RevalidateCache) => (pathList?: string | string[], options?: InvalidateOptions) => Promise<InvalidateResult[]>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { RevalidateCache, RouterState, Location } from '../types';
|
|
2
|
-
export declare const createPrefetch: ({
|
|
2
|
+
export declare const createPrefetch: ({ routeDataState }: RouterState, revalidateCache: RevalidateCache) => (location: Location) => Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { LoadingPromise, RevalidateCacheArgs, RouterState } from '../types';
|
|
2
|
-
export declare const createRevalidateCache: (routerState: RouterState) => ({ routeItem, location, signal }: RevalidateCacheArgs, retried?: number) => LoadingPromise;
|
|
2
|
+
export declare const createRevalidateCache: (routerState: RouterState) => ({ routeItem, location, signal, force }: RevalidateCacheArgs, retried?: number) => LoadingPromise;
|
package/dist/types.d.ts
CHANGED
|
@@ -24,9 +24,10 @@ export type ClientRouteItem = {
|
|
|
24
24
|
element: RenderElement | LazyComponent;
|
|
25
25
|
loader?(arg: LoaderArgs & {
|
|
26
26
|
signal: AbortSignal;
|
|
27
|
+
attempt: number;
|
|
27
28
|
}): Promise<unknown>;
|
|
28
29
|
loaderFallback?: RenderElement;
|
|
29
|
-
errorElement?: RenderElement;
|
|
30
|
+
errorElement?: RenderElement | LazyComponent;
|
|
30
31
|
fallback?: RenderElement;
|
|
31
32
|
children?: ClientRouteItem[];
|
|
32
33
|
staleTime?: number;
|
|
@@ -64,6 +65,7 @@ export type RevalidateCacheArgs = {
|
|
|
64
65
|
location: Location;
|
|
65
66
|
routeItem?: RouteItem;
|
|
66
67
|
signal?: AbortSignal;
|
|
68
|
+
force?: boolean;
|
|
67
69
|
};
|
|
68
70
|
export type LoaderState<T = unknown> = {
|
|
69
71
|
data: T;
|
|
@@ -71,10 +73,11 @@ export type LoaderState<T = unknown> = {
|
|
|
71
73
|
beforeLoadError: Error | null;
|
|
72
74
|
};
|
|
73
75
|
export type Status = 'idle' | 'pending' | 'active' | 'optimistic' | 'error';
|
|
74
|
-
export type
|
|
76
|
+
export type RouteData = {
|
|
75
77
|
location: Location;
|
|
76
78
|
routeItem: RouteItem | undefined;
|
|
77
79
|
status: Status;
|
|
80
|
+
loaderState: LoaderState;
|
|
78
81
|
};
|
|
79
82
|
type ObjectRetry = {
|
|
80
83
|
count: number;
|
|
@@ -116,11 +119,10 @@ export type LoadingPromise = Promise<{
|
|
|
116
119
|
} | undefined>;
|
|
117
120
|
export type ScrollMap = Record<string, [string, number][]>;
|
|
118
121
|
export type RouterState = {
|
|
119
|
-
|
|
122
|
+
routeDataState: Store<RouteData>;
|
|
120
123
|
scrollMapState: Store<ScrollMap>;
|
|
121
124
|
contextState: Store<Record<string, unknown>>;
|
|
122
125
|
blockerState: Store<BlockerState>;
|
|
123
|
-
loaderState: Store<LoaderState>;
|
|
124
126
|
blockedTargetState: Store<Location | null>;
|
|
125
127
|
loaderMap: Map<string, LoaderStateItem>;
|
|
126
128
|
loadingPromises: Map<string, LoadingPromise>;
|
|
@@ -133,11 +135,9 @@ export type RouterType = {
|
|
|
133
135
|
};
|
|
134
136
|
hooks: {
|
|
135
137
|
useBlockerState(): ReturnType<typeof useGlobalState<BlockerState>>;
|
|
136
|
-
useRouteItemData(): ReturnType<typeof useGlobalState<RouteItemData>>;
|
|
137
138
|
useScrollMap(): ReturnType<typeof useGlobalState<ScrollMap>>;
|
|
138
139
|
useContextState(): ReturnType<typeof useGlobalState<Record<string, unknown>>>;
|
|
139
140
|
useBlockedTargetState(): ReturnType<typeof useGlobalState<Location | null>>;
|
|
140
|
-
useLoaderState<T>(): LoaderState<T>;
|
|
141
141
|
useParams<T>(): T;
|
|
142
142
|
useNavigate(): (arg: NavigationLocation | string | -1) => Promise<void>;
|
|
143
143
|
useAction(action: string, options?: Options): (arg: Record<string, unknown>) => Promise<{
|
|
@@ -145,7 +145,8 @@ export type RouterType = {
|
|
|
145
145
|
error: Error | null;
|
|
146
146
|
}>;
|
|
147
147
|
useScrollRestoration(restorationBehavior: ScrollRestorationBehavior): () => void;
|
|
148
|
-
|
|
148
|
+
useRouteDataSelector<T>(callback: (arg: RouteData) => T): T;
|
|
149
|
+
useSetLocation(): (location: Location) => void;
|
|
149
150
|
};
|
|
150
151
|
};
|
|
151
152
|
export type InvalidateOptions = {
|
|
@@ -4,5 +4,5 @@ type CommitState = {
|
|
|
4
4
|
loaderStateValue: LoaderState;
|
|
5
5
|
routeItem?: RouteItem;
|
|
6
6
|
};
|
|
7
|
-
export declare const createCommitState: ({
|
|
7
|
+
export declare const createCommitState: ({ routeDataState, blockerState }: RouterState) => ({ routeItem, loaderStateValue, location }: CommitState) => void;
|
|
8
8
|
export {};
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Store } from '../create';
|
|
2
|
-
import { Location, RouteItem,
|
|
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
5
|
export declare const comparePaths: (route: RouteItem, pathname: string) => boolean;
|
|
@@ -13,5 +13,5 @@ export declare const getPartialLoaderArgs: (contextState: RouterState["contextSt
|
|
|
13
13
|
location: Location;
|
|
14
14
|
};
|
|
15
15
|
export declare const isVerticalScroll: (el: Element | null) => boolean;
|
|
16
|
-
export declare const updateScrollMap: (
|
|
16
|
+
export declare const updateScrollMap: (routeDataState: Store<RouteData>, scrollMapState: Store<ScrollMap>) => void;
|
|
17
17
|
export declare const formatSearchObject: (searchObject: SearchObject) => string;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useIsRoutePending: (routePath: string) => boolean;
|