clear-react-router 2.1.7 → 2.1.8
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 +12 -17
- package/dist/components/Router.d.ts +1 -1
- package/dist/config/routerConfig.d.ts +2 -0
- package/dist/hooks/useReload.d.ts +1 -0
- package/dist/index.js +42 -32
- package/dist/runtime/invalidate.d.ts +1 -1
- package/dist/types.d.ts +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -58,7 +58,8 @@ It provides first-class support for:
|
|
|
58
58
|
| `defaultPrefetch` | `'hover' \| 'render' \| 'viewport' \| 'none'` | `'hover'` for desktop, `'viewport'` for mobile | Default prefetch strategy for all `<Link>` components |
|
|
59
59
|
| `defaultHoverPrefetchDelay` | `number \| undefined` | `150` | Default delay in milliseconds before prefetching on hover (only for `'hover'` strategy) |
|
|
60
60
|
| `defaultScrollRestorationBehavior` | `'auto' \| 'smooth' \| 'instant'` | `auto` | Default scroll restoration behavior |
|
|
61
|
-
|
|
61
|
+
| `revalidateOnFocus` | `boolean \| undefined` | `undefined` | Revalidate the current route's `loader` data when the browser tab regains focus |
|
|
62
|
+
| `revalidateOnReconnect` | `boolean \| undefined` | `undefined` | Revalidate the current route's `loader` data when the browser regains an internet connection |
|
|
62
63
|
|
|
63
64
|
> **Note:** Global lifecycle hooks wrap every route navigation. The global `defaultBeforeLoad` runs **before** the route-specific beforeLoad, while the global `defaultAfterLoad` runs **after** the route-specific afterLoad.
|
|
64
65
|
|
|
@@ -497,14 +498,10 @@ Return value:
|
|
|
497
498
|
| Argument | Type | Description |
|
|
498
499
|
|----------|------|-------------|
|
|
499
500
|
| `action` | `string` | Name of the route action to execute. Must match a key returned from the route's `actions` configuration. |
|
|
500
|
-
| `options` | `Options` | Optional callbacks invoked after the action succeeds or fails
|
|
501
|
+
| `options` | `Options` | Optional callbacks invoked after the action succeeds or fails. |
|
|
501
502
|
|
|
502
503
|
```ts
|
|
503
|
-
type Options =
|
|
504
|
-
onSuccess: (args: unknown) => void;
|
|
505
|
-
onError: (args: unknown) => void;
|
|
506
|
-
withBeforeLoad?: boolean;
|
|
507
|
-
}> | undefined;
|
|
504
|
+
type Options = { onSuccess?: (args: unknown) => void; onError?: (args: unknown) => void }> | undefined;
|
|
508
505
|
```
|
|
509
506
|
|
|
510
507
|
```tsx
|
|
@@ -619,7 +616,7 @@ const UserProfile = () => {
|
|
|
619
616
|
|
|
620
617
|
### `useInvalidate()`
|
|
621
618
|
|
|
622
|
-
Returns a function that revalidates **cached** route data. Calling `invalidate()` clears cached route data and immediately runs the corresponding route loader again.
|
|
619
|
+
Returns a function that revalidates **cached** route data. Calling `invalidate()` clears cached route data and immediately runs the corresponding route `loader` again.
|
|
623
620
|
|
|
624
621
|
#### Current route
|
|
625
622
|
|
|
@@ -680,6 +677,13 @@ await invalidate('/about', { force: true });
|
|
|
680
677
|
await invalidate(['/about', '/post/10'], { force: true });
|
|
681
678
|
```
|
|
682
679
|
|
|
680
|
+
#### Stale-only revalidation
|
|
681
|
+
By default, `invalidate` deletes matching cache entries and refetches them.
|
|
682
|
+
Pass `{ staleOnly: true }` to only revalidate entries that are already stale, leaving fresh ones untouched:
|
|
683
|
+
```tsx
|
|
684
|
+
await invalidate('/notes', { staleOnly: true });
|
|
685
|
+
```
|
|
686
|
+
|
|
683
687
|
#### Including child routes
|
|
684
688
|
|
|
685
689
|
To revalidate routes together with their cached child routes, pass the `withChildren` option:
|
|
@@ -713,15 +717,6 @@ will revalidate the cached child routes:
|
|
|
713
717
|
/post/23
|
|
714
718
|
/post/42/comments
|
|
715
719
|
```
|
|
716
|
-
#### Including `beforeLoad`
|
|
717
|
-
|
|
718
|
-
To include the `beforeLoad` function in the revalidation process, pass the `withBeforeLoad` option:
|
|
719
|
-
|
|
720
|
-
```tsx
|
|
721
|
-
await invalidate('/posts', { withBeforeLoad: true });
|
|
722
|
-
await invalidate(['/posts', '/users'], { withBeforeLoad: true });
|
|
723
|
-
```
|
|
724
|
-
|
|
725
720
|
#### Returns
|
|
726
721
|
|
|
727
722
|
An array of objects with the following structure:
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { RouterProps } from '../types';
|
|
2
|
-
export declare const Router: ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated, optimisticSpinner, defaultMinLoaderDuration, maxCacheSize, defaultPrefetch, defaultHoverPrefetchDelay, defaultScrollRestorationBehavior, errorBoundary: ErrorBoundary, }: RouterProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
2
|
+
export declare const Router: ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated, optimisticSpinner, defaultMinLoaderDuration, revalidateOnFocus, revalidateOnReconnect, maxCacheSize, defaultPrefetch, defaultHoverPrefetchDelay, defaultScrollRestorationBehavior, errorBoundary: ErrorBoundary, }: RouterProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -4,6 +4,8 @@ declare class RouterConfig {
|
|
|
4
4
|
maxCacheSize: number;
|
|
5
5
|
defaultPrefetch: RouterProps['defaultPrefetch'];
|
|
6
6
|
isAnimated: RouterProps['isAnimated'];
|
|
7
|
+
revalidateOnFocus: boolean;
|
|
8
|
+
revalidateOnReconnect: boolean;
|
|
7
9
|
defaultHoverPrefetchDelay: number;
|
|
8
10
|
defaultBeforeLoad?: ClientRouteItem['beforeLoad'];
|
|
9
11
|
defaultAfterLoad?: ClientRouteItem['afterLoad'];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useReload: () => void;
|
package/dist/index.js
CHANGED
|
@@ -92,6 +92,8 @@ var RouterConfig = class {
|
|
|
92
92
|
_defineProperty(this, "maxCacheSize", 0);
|
|
93
93
|
_defineProperty(this, "defaultPrefetch", "hover");
|
|
94
94
|
_defineProperty(this, "isAnimated", false);
|
|
95
|
+
_defineProperty(this, "revalidateOnFocus", false);
|
|
96
|
+
_defineProperty(this, "revalidateOnReconnect", false);
|
|
95
97
|
_defineProperty(this, "defaultHoverPrefetchDelay", 150);
|
|
96
98
|
_defineProperty(this, "defaultBeforeLoad", void 0);
|
|
97
99
|
_defineProperty(this, "defaultAfterLoad", void 0);
|
|
@@ -341,36 +343,20 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
341
343
|
};
|
|
342
344
|
//#endregion
|
|
343
345
|
//#region runtime/invalidate.ts
|
|
344
|
-
var
|
|
345
|
-
var createInvalidate = ({ routeItemDataState, loaderState, loaderMap
|
|
346
|
+
var separatePathname = (text) => text.split("?")[0];
|
|
347
|
+
var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalidateCache) => {
|
|
346
348
|
const invalidatePath = async (routeItem, pathname, options) => {
|
|
347
349
|
const routePathname = routeItemDataState.getState().location.pathname;
|
|
348
|
-
loaderMap.delete(pathname);
|
|
350
|
+
if (!options?.staleOnly) loaderMap.delete(pathname);
|
|
349
351
|
const [path, search = ""] = pathname.split("?");
|
|
350
|
-
const location = {
|
|
351
|
-
pathname: path,
|
|
352
|
-
search
|
|
353
|
-
};
|
|
354
|
-
if (routeItem?.beforeLoad && options?.withBeforeLoad) try {
|
|
355
|
-
await routeItem.beforeLoad({
|
|
356
|
-
redirect,
|
|
357
|
-
...getPartialLoaderArgs(contextState, location, routeItem)
|
|
358
|
-
});
|
|
359
|
-
loaderState.setState((prev) => ({
|
|
360
|
-
...prev,
|
|
361
|
-
beforeLoadError: null
|
|
362
|
-
}));
|
|
363
|
-
} catch (error) {
|
|
364
|
-
loaderState.setState((prev) => ({
|
|
365
|
-
...prev,
|
|
366
|
-
beforeLoadError: error
|
|
367
|
-
}));
|
|
368
|
-
}
|
|
369
352
|
const result = await revalidateCache({
|
|
370
353
|
routeItem,
|
|
371
|
-
location
|
|
354
|
+
location: {
|
|
355
|
+
pathname: path,
|
|
356
|
+
search
|
|
357
|
+
}
|
|
372
358
|
});
|
|
373
|
-
if (result &&
|
|
359
|
+
if (result && path === routePathname) loaderState.setState({
|
|
374
360
|
data: result.data,
|
|
375
361
|
loaderError: result.error,
|
|
376
362
|
beforeLoadError: null
|
|
@@ -381,18 +367,20 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap, contextSta
|
|
|
381
367
|
};
|
|
382
368
|
};
|
|
383
369
|
const invalidateItem = async (pathname, options) => {
|
|
384
|
-
const routeItem = findRoute(pathname
|
|
370
|
+
const routeItem = findRoute(separatePathname(pathname));
|
|
385
371
|
if (!routeItem) return [];
|
|
386
372
|
const pathnameSet = /* @__PURE__ */ new Set();
|
|
387
|
-
for (const [key] of loaderMap) if (comparePaths(routeItem, key)) pathnameSet.add(key);
|
|
373
|
+
for (const [key] of loaderMap) if (comparePaths(routeItem, separatePathname(key))) pathnameSet.add(key);
|
|
388
374
|
if (options?.force) pathnameSet.add(pathname);
|
|
389
375
|
const currentResults = await Promise.all([...pathnameSet].map((pathname) => invalidatePath(routeItem, pathname, options)));
|
|
390
376
|
if (!options?.withChildren || !routeItem.children?.length) return currentResults;
|
|
391
|
-
const
|
|
377
|
+
const [parentPath] = pathname.split("?");
|
|
378
|
+
const childResults = await Promise.all(routeItem.children.map((child) => invalidateItem(`${parentPath}${child.path}`, options)));
|
|
392
379
|
return [...currentResults, ...childResults.flat()];
|
|
393
380
|
};
|
|
394
381
|
return async (pathList, options) => {
|
|
395
|
-
const
|
|
382
|
+
const { pathname, search } = routeItemDataState.getState().location;
|
|
383
|
+
const routePathname = `${pathname}${search}`;
|
|
396
384
|
const pathnameList = Array.isArray(pathList) ? pathList : pathList ? [pathList] : [routePathname];
|
|
397
385
|
return (await Promise.all(pathnameList.map((pathname) => invalidateItem(pathname, options)))).flat();
|
|
398
386
|
};
|
|
@@ -553,8 +541,8 @@ var createRouterInstance = () => {
|
|
|
553
541
|
loadingPromises: /* @__PURE__ */ new Map()
|
|
554
542
|
};
|
|
555
543
|
const revalidateCache = createRevalidateCache(routerState);
|
|
556
|
-
const invalidate = createInvalidate(routerState, revalidateCache);
|
|
557
544
|
const navigate = createNavigate(routerState, revalidateCache);
|
|
545
|
+
const invalidate = createInvalidate(routerState, revalidateCache);
|
|
558
546
|
const prefetch = createPrefetch(routerState, revalidateCache);
|
|
559
547
|
const getCurrentAction = (actionKey) => {
|
|
560
548
|
const { routeItem, location } = routerState.routeItemDataState.getState();
|
|
@@ -614,7 +602,7 @@ var createRouterInstance = () => {
|
|
|
614
602
|
return async (formData) => {
|
|
615
603
|
try {
|
|
616
604
|
const data = await currentAction(formData);
|
|
617
|
-
await invalidate(
|
|
605
|
+
await invalidate();
|
|
618
606
|
options.onSuccess?.(data);
|
|
619
607
|
return {
|
|
620
608
|
data,
|
|
@@ -727,6 +715,25 @@ var useSetInitialContext = (initialContext) => {
|
|
|
727
715
|
}, [initialContext, setContext]);
|
|
728
716
|
};
|
|
729
717
|
//#endregion
|
|
718
|
+
//#region hooks/useReload.ts
|
|
719
|
+
var useReload = () => {
|
|
720
|
+
const { invalidate } = router.runtime;
|
|
721
|
+
useEffect(() => {
|
|
722
|
+
if (!routerConfig.revalidateOnFocus) return;
|
|
723
|
+
const handler = () => {
|
|
724
|
+
if (document.visibilityState === "visible") invalidate("", { staleOnly: true });
|
|
725
|
+
};
|
|
726
|
+
document.addEventListener("visibilitychange", handler);
|
|
727
|
+
return () => document.removeEventListener("visibilitychange", handler);
|
|
728
|
+
}, [invalidate]);
|
|
729
|
+
useEffect(() => {
|
|
730
|
+
if (!routerConfig.revalidateOnReconnect) return;
|
|
731
|
+
const handler = () => invalidate("", { staleOnly: true });
|
|
732
|
+
window.addEventListener("online", handler);
|
|
733
|
+
return () => window.removeEventListener("online", handler);
|
|
734
|
+
}, [invalidate]);
|
|
735
|
+
};
|
|
736
|
+
//#endregion
|
|
730
737
|
//#region ../../node_modules/react/cjs/react-jsx-runtime.production.js
|
|
731
738
|
/**
|
|
732
739
|
* @license React
|
|
@@ -777,7 +784,7 @@ var EmptyBoundary = ({ children }) => children;
|
|
|
777
784
|
var IS_MOBILE = isMobile();
|
|
778
785
|
var MOBILE_CACHE_SIZE = 60;
|
|
779
786
|
var DESKTOP_CACHE_SIZE = 150;
|
|
780
|
-
var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, optimisticSpinner = true, defaultMinLoaderDuration = 0, maxCacheSize = IS_MOBILE ? MOBILE_CACHE_SIZE : DESKTOP_CACHE_SIZE, defaultPrefetch = IS_MOBILE ? "viewport" : "hover", defaultHoverPrefetchDelay = 150, defaultScrollRestorationBehavior = "auto", errorBoundary: ErrorBoundary = EmptyBoundary }) => {
|
|
787
|
+
var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, optimisticSpinner = true, 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 }) => {
|
|
781
788
|
const { useRouteItemData } = router.hooks;
|
|
782
789
|
const [{ routeItem, status, location }] = useRouteItemData();
|
|
783
790
|
const isError = status === "error";
|
|
@@ -794,11 +801,14 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
794
801
|
defaultRetry,
|
|
795
802
|
defaultStaleTime,
|
|
796
803
|
defaultMinLoaderDuration,
|
|
797
|
-
maxCacheSize
|
|
804
|
+
maxCacheSize,
|
|
805
|
+
revalidateOnFocus,
|
|
806
|
+
revalidateOnReconnect
|
|
798
807
|
});
|
|
799
808
|
useApplyCustomAnimation(animationDuration);
|
|
800
809
|
useSetInitialContext(initialContext);
|
|
801
810
|
usePreserveScroll(defaultScrollRestorationBehavior);
|
|
811
|
+
useReload();
|
|
802
812
|
if (isLoading && !isError) return renderElement(routeItem?.loaderFallback || defaultLoaderFallback);
|
|
803
813
|
if (!routeItem) return null;
|
|
804
814
|
if (isError) return renderElement(routeItem.errorElement || defaultErrorElement);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type InvalidateOptions, InvalidateResult, RevalidateCache, RouterState } from '../types';
|
|
2
|
-
export declare const createInvalidate: ({ routeItemDataState, loaderState, loaderMap
|
|
2
|
+
export declare const createInvalidate: ({ routeItemDataState, loaderState, loaderMap }: RouterState, revalidateCache: RevalidateCache) => (pathList?: string | string[], options?: InvalidateOptions) => Promise<InvalidateResult[]>;
|
package/dist/types.d.ts
CHANGED
|
@@ -98,6 +98,8 @@ export type RouterProps = {
|
|
|
98
98
|
defaultBeforeLoad?: ClientRouteItem['beforeLoad'];
|
|
99
99
|
defaultAfterLoad?: ClientRouteItem['afterLoad'];
|
|
100
100
|
defaultScrollRestorationBehavior?: ScrollRestorationBehavior;
|
|
101
|
+
revalidateOnFocus?: boolean;
|
|
102
|
+
revalidateOnReconnect?: boolean;
|
|
101
103
|
context?: Record<string, unknown>;
|
|
102
104
|
};
|
|
103
105
|
export type LoaderStateItem = {
|
|
@@ -153,14 +155,13 @@ export type RouterType = {
|
|
|
153
155
|
};
|
|
154
156
|
export type InvalidateOptions = {
|
|
155
157
|
withChildren?: boolean;
|
|
156
|
-
withBeforeLoad?: boolean;
|
|
157
158
|
force?: boolean;
|
|
159
|
+
staleOnly?: boolean;
|
|
158
160
|
};
|
|
159
161
|
export type RevalidateCache = (args: RevalidateCacheArgs) => LoadingPromise;
|
|
160
162
|
export type Options = Partial<{
|
|
161
163
|
onSuccess: (args: unknown) => void;
|
|
162
164
|
onError: (args: unknown) => void;
|
|
163
|
-
withBeforeLoad?: boolean;
|
|
164
165
|
}> | undefined;
|
|
165
166
|
export type InvalidateResult = {
|
|
166
167
|
path: string;
|