clear-react-router 2.4.7 → 2.4.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/dist/index.js CHANGED
@@ -464,9 +464,15 @@ var createInvalidate = ({ routeDataState, loaderMap }, revalidateCache) => {
464
464
  };
465
465
  //#endregion
466
466
  //#region runtime/prefetch.ts
467
- var createPrefetch = ({ routeDataState }, revalidateCache) => async (location) => {
468
- const item = findRoute(location.pathname);
469
- if (item) {
467
+ var MOBILE_PREFETCH_CONCURRENCY = 2;
468
+ var DESKTOP_PREFETCH_CONCURRENCY = 4;
469
+ var createPrefetch = ({ routeDataState }, revalidateCache) => {
470
+ const maxConcurrent = isMobile() ? MOBILE_PREFETCH_CONCURRENCY : DESKTOP_PREFETCH_CONCURRENCY;
471
+ const queue = [];
472
+ let active = 0;
473
+ const run = async (location) => {
474
+ const item = findRoute(location.pathname);
475
+ if (!item) return;
470
476
  const currentLocation = routeDataState.getState().location;
471
477
  if (location.pathname === currentLocation.pathname && location.search === currentLocation.search) return;
472
478
  await item.preloadElement?.();
@@ -474,7 +480,25 @@ var createPrefetch = ({ routeDataState }, revalidateCache) => async (location) =
474
480
  routeItem: item,
475
481
  location
476
482
  });
477
- }
483
+ };
484
+ const drain = () => {
485
+ while (active < maxConcurrent && queue.length) {
486
+ active++;
487
+ queue.shift()();
488
+ }
489
+ };
490
+ return (location, signal) => new Promise((resolve) => {
491
+ queue.push(async () => {
492
+ try {
493
+ if (!signal?.aborted) await run(location);
494
+ } catch {} finally {
495
+ active--;
496
+ resolve();
497
+ drain();
498
+ }
499
+ });
500
+ drain();
501
+ });
478
502
  };
479
503
  //#endregion
480
504
  //#region runtime/revalidateCache.ts
@@ -893,6 +917,7 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
893
917
  var useNavigate = router.hooks.useNavigate;
894
918
  //#endregion
895
919
  //#region components/Link.tsx
920
+ var VIEWPORT_DWELL_MS = 250;
896
921
  var defaultAs = (props) => /* @__PURE__ */ jsx("a", { ...props });
897
922
  var comparator = (to, pathname, exact) => to === "/" ? pathname === "/" : exact ? pathname === to : pathname === to || pathname?.startsWith(`${to}/`);
898
923
  var Link = ({ children, to, path, params, search = "", state, as = defaultAs, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact = false, activeClassName = "active-link", pendingClassName = "pending-link", ...rest }) => {
@@ -934,22 +959,51 @@ var Link = ({ children, to, path, params, search = "", state, as = defaultAs, pr
934
959
  }, [getPrefetchStrategy, getPrefetchDelay]);
935
960
  useEffect(() => {
936
961
  if (getPrefetchStrategy() !== "render") return;
937
- (async () => {
938
- await router.runtime.prefetch(location);
939
- })();
940
- }, [getPrefetchStrategy, location]);
962
+ const controller = new AbortController();
963
+ router.runtime.prefetch({
964
+ pathname,
965
+ search: searchString
966
+ }, controller.signal);
967
+ return () => controller.abort();
968
+ }, [
969
+ getPrefetchStrategy,
970
+ pathname,
971
+ searchString
972
+ ]);
941
973
  useEffect(() => {
942
974
  if (getPrefetchStrategy() !== "viewport") return;
943
975
  const element = elementRef.current;
944
976
  if (!element) return;
945
- const observer = new IntersectionObserver(async ([entry]) => {
946
- if (!entry.isIntersecting) return;
947
- await router.runtime.prefetch(location);
948
- observer.disconnect();
977
+ let dwellTimer = 0;
978
+ let controller = null;
979
+ const observer = new IntersectionObserver((entries) => {
980
+ const isVisible = entries[entries.length - 1].isIntersecting;
981
+ window.clearTimeout(dwellTimer);
982
+ if (!isVisible) {
983
+ controller?.abort();
984
+ return;
985
+ }
986
+ dwellTimer = window.setTimeout(async () => {
987
+ const attempt = new AbortController();
988
+ controller = attempt;
989
+ await router.runtime.prefetch({
990
+ pathname,
991
+ search: searchString
992
+ }, attempt.signal);
993
+ if (!attempt.signal.aborted) observer.disconnect();
994
+ }, VIEWPORT_DWELL_MS);
949
995
  });
950
996
  observer.observe(element);
951
- return () => observer.disconnect();
952
- }, [getPrefetchStrategy, location]);
997
+ return () => {
998
+ window.clearTimeout(dwellTimer);
999
+ controller?.abort();
1000
+ observer.disconnect();
1001
+ };
1002
+ }, [
1003
+ getPrefetchStrategy,
1004
+ pathname,
1005
+ searchString
1006
+ ]);
953
1007
  useEffect(() => () => {
954
1008
  if (timeout.current) clearTimeout(timeout.current);
955
1009
  }, []);
@@ -1,2 +1,2 @@
1
1
  import { RevalidateCache, RouterState, Location } from '../types';
2
- export declare const createPrefetch: ({ routeDataState }: RouterState, revalidateCache: RevalidateCache) => (location: Location) => Promise<void>;
2
+ export declare const createPrefetch: ({ routeDataState }: RouterState, revalidateCache: RevalidateCache) => (location: Location, signal?: AbortSignal) => Promise<void>;
package/dist/types.d.ts CHANGED
@@ -133,7 +133,7 @@ export type RouterType = {
133
133
  runtime: {
134
134
  navigate(arg: Location): Promise<void>;
135
135
  invalidate(pathList?: string | string[], options?: InvalidateOptions): Promise<InvalidateResult[]>;
136
- prefetch(location: Location): Promise<void>;
136
+ prefetch(location: Location, signal?: AbortSignal): Promise<void>;
137
137
  };
138
138
  hooks: {
139
139
  useBlockerState(): ReturnType<typeof useGlobalState<BlockerState>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.4.7",
3
+ "version": "2.4.8",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {