clear-react-router 1.3.6 → 1.3.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 +13 -1
- package/dist/components/Link.d.ts +2 -1
- package/dist/index.js +29 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -88,9 +88,21 @@ Component for client-side navigation with prefetch support.
|
|
|
88
88
|
| Prop | Type | Default |
|
|
89
89
|
|------|------|---------|
|
|
90
90
|
| `to` | `string` | required |
|
|
91
|
-
| `prefetch` | `boolean` | `true` |
|
|
91
|
+
| `prefetch` | `boolean` optional | `true` |
|
|
92
|
+
| `prefetchDelay` | `number` optional | `150` | Delay in ms before prefetch starts (prevents unnecessary requests on quick mouse passes) |
|
|
92
93
|
| `children` | `ReactElement` | required |
|
|
93
94
|
|
|
95
|
+
```
|
|
96
|
+
// Default — prefetch after 150ms hover
|
|
97
|
+
<Link to="/dashboard">Dashboard</Link>
|
|
98
|
+
|
|
99
|
+
// Custom prefetch delay (300ms)
|
|
100
|
+
<Link to="/heavy-page" prefetchDelay={300}>Heavy Page</Link>
|
|
101
|
+
|
|
102
|
+
// Disable prefetch
|
|
103
|
+
<Link to="/about" prefetch={false}>About</Link>
|
|
104
|
+
```
|
|
105
|
+
|
|
94
106
|
### `redirect`
|
|
95
107
|
|
|
96
108
|
Function provided to `beforeLoad` for programmatic redirection.
|
|
@@ -6,6 +6,7 @@ type LinkProps = {
|
|
|
6
6
|
style: CSSProperties;
|
|
7
7
|
}>;
|
|
8
8
|
prefetch?: boolean;
|
|
9
|
+
prefetchDelay?: number;
|
|
9
10
|
};
|
|
10
|
-
export declare const Link: ({ children, to, prefetch }: LinkProps) => import("react/jsx-runtime").JSX.Element;
|
|
11
|
+
export declare const Link: ({ children, to, prefetch, prefetchDelay }: LinkProps) => import("react/jsx-runtime").JSX.Element;
|
|
11
12
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -195,6 +195,7 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache, i
|
|
|
195
195
|
});
|
|
196
196
|
return;
|
|
197
197
|
}
|
|
198
|
+
else setBeforeLoadError(false);
|
|
198
199
|
if (seq !== navigationSeq.current) return;
|
|
199
200
|
await revalidateCache({
|
|
200
201
|
routeItem: nextItem,
|
|
@@ -293,11 +294,17 @@ var useLoader = ({ routeList, context, setContext }) => {
|
|
|
293
294
|
return Date.now() - currentCacheTimestamp < routeItem.staleTime;
|
|
294
295
|
}, []);
|
|
295
296
|
const revalidateCache = useCallback(async ({ routeItem, isCurrentRoute, pathname }) => {
|
|
296
|
-
if (!routeItem?.loader)
|
|
297
|
+
if (!routeItem?.loader) {
|
|
298
|
+
setLoaderError(false);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
297
301
|
if (isCacheItemFresh({
|
|
298
302
|
routeItem,
|
|
299
303
|
pathname
|
|
300
|
-
}) && isCurrentRoute)
|
|
304
|
+
}) && isCurrentRoute) {
|
|
305
|
+
setLoaderCache(loaderCacheRef.current[pathname]);
|
|
306
|
+
setLoaderError(false);
|
|
307
|
+
}
|
|
301
308
|
if (isCacheItemFresh({
|
|
302
309
|
routeItem,
|
|
303
310
|
pathname
|
|
@@ -545,13 +552,31 @@ var useNavigate = () => {
|
|
|
545
552
|
};
|
|
546
553
|
//#endregion
|
|
547
554
|
//#region components/Link.tsx
|
|
548
|
-
var
|
|
555
|
+
var STANDARD_DELAY = 150;
|
|
556
|
+
var Link = ({ children, to, prefetch = true, prefetchDelay = STANDARD_DELAY }) => {
|
|
549
557
|
const { prefetchLoader } = useRouterActions();
|
|
550
558
|
const navigate = useNavigate();
|
|
559
|
+
const timeout = useRef(0);
|
|
551
560
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", {
|
|
552
561
|
style: { cursor: "pointer" },
|
|
553
562
|
onClick: () => navigate(to),
|
|
554
|
-
|
|
563
|
+
onMouseEnter: useCallback(() => {
|
|
564
|
+
if (!prefetch || !prefetchDelay) return;
|
|
565
|
+
if (timeout.current) clearTimeout(timeout.current);
|
|
566
|
+
timeout.current = window.setTimeout(() => prefetchLoader(to), prefetchDelay);
|
|
567
|
+
}, [
|
|
568
|
+
prefetch,
|
|
569
|
+
prefetchDelay,
|
|
570
|
+
prefetchLoader,
|
|
571
|
+
to
|
|
572
|
+
]),
|
|
573
|
+
onMouseLeave: useCallback(() => {
|
|
574
|
+
if (!prefetch || !prefetchDelay) return;
|
|
575
|
+
if (timeout.current) {
|
|
576
|
+
clearTimeout(timeout.current);
|
|
577
|
+
timeout.current = 0;
|
|
578
|
+
}
|
|
579
|
+
}, [prefetch, prefetchDelay]),
|
|
555
580
|
children
|
|
556
581
|
});
|
|
557
582
|
};
|