clear-react-router 2.1.0 → 2.1.2

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 CHANGED
@@ -589,8 +589,7 @@ const UserProfile = () => {
589
589
  }
590
590
  ```
591
591
 
592
- - Stale entries are cleaned up on every navigation, so cache growth stays tied to how often you actually revisit stale data — not to how long the session lasts.
593
- - On top of that, the cache is bounded by `maxCacheSize` — once the limit is reached, the least recently used entry is evicted to make room for a new one, regardless of whether it's still fresh. This caps memory usage for apps with many high-cardinality dynamic routes (e.g. `/product/:id` across a large catalog). It defaults to a device-aware value (lower on mobile) and can be overridden on the `Router`:
592
+ - The cache is bounded by `maxCacheSize` — once the limit is reached, the least recently used entry is evicted to make room for a new one, regardless of whether it's still fresh. This caps memory usage for apps with many high-cardinality dynamic routes (e.g. `/product/:id` across a large catalog). It defaults to a device-aware value (lower on mobile) and can be overridden on the `Router`:
594
593
 
595
594
  ```tsx
596
595
  <Router routes={routes} maxCacheSize={200} />
package/dist/index.js CHANGED
@@ -30,13 +30,12 @@ var useGlobalState = ({ subscribe, getState, setState }) => {
30
30
  };
31
31
  //#endregion
32
32
  //#region utils/commitState.ts
33
- var createCommitState = ({ routeItemDataState, statusState, isOptimisticLoading, loaderStateRef, loaderState }) => (nextLocation, routeItem) => {
33
+ var createCommitState = ({ routeItemDataState, statusState, loaderStateRef, loaderState }) => (nextLocation, routeItem) => {
34
34
  routeItemDataState.setState({
35
35
  routeItem,
36
36
  location: nextLocation
37
37
  });
38
38
  statusState.setState("active");
39
- isOptimisticLoading.setState(false);
40
39
  loaderState.setState(loaderStateRef.value);
41
40
  if ((nextLocation.search ? `${nextLocation.pathname}${nextLocation.search}` : nextLocation.pathname) === window.location.pathname + window.location.search) return;
42
41
  history.pushState(null, "", `${nextLocation.pathname}${nextLocation.search}`);
@@ -201,7 +200,7 @@ var createNavigate = (routerState, revalidateCache) => {
201
200
  let navigationSeq = 0;
202
201
  let interval = 0;
203
202
  let abortController = null;
204
- const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState, isOptimisticLoading } = routerState;
203
+ const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState } = routerState;
205
204
  const navigationExecutor = createCommitState(routerState);
206
205
  const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
207
206
  const createSignal = () => {
@@ -253,7 +252,7 @@ var createNavigate = (routerState, revalidateCache) => {
253
252
  routeItem,
254
253
  location
255
254
  });
256
- isOptimisticLoading.setState(true);
255
+ statusState.setState("optimistic");
257
256
  const currentLoaderState = loaderMap.get(path)?.state;
258
257
  if (currentLoaderState) loaderState.setState(currentLoaderState);
259
258
  } else {
@@ -285,7 +284,8 @@ var createNavigate = (routerState, revalidateCache) => {
285
284
  }), getLoaderDurationPromise(routeItem, nextLocation)]);
286
285
  if (result) loaderStateRef.set((prev) => ({
287
286
  ...prev,
288
- ...result
287
+ data: result.data,
288
+ loaderError: result.error
289
289
  }));
290
290
  if (seq !== navigationSeq) return;
291
291
  polling(routeItem, nextLocation);
@@ -527,7 +527,6 @@ var createRouterInstance = () => {
527
527
  from: "",
528
528
  to: ""
529
529
  }),
530
- isOptimisticLoading: create(false),
531
530
  loaderState: create(EMPTY_LOADER_STATE),
532
531
  loaderStateRef: new Cell(EMPTY_LOADER_STATE),
533
532
  loaderMap: /* @__PURE__ */ new Map(),
@@ -569,7 +568,6 @@ var createRouterInstance = () => {
569
568
  useScrollMap: () => useGlobalState(routerState.scrollMapState),
570
569
  useStatus: () => useGlobalState(routerState.statusState),
571
570
  useContextState: () => useGlobalState(routerState.contextState),
572
- useOptimisticLoading: () => useGlobalState(routerState.isOptimisticLoading),
573
571
  useLoaderState: () => useGlobalState(routerState.loaderState)[0],
574
572
  useParams: () => getParamsObject(),
575
573
  useNavigate: () => {
@@ -774,12 +772,12 @@ var IS_MOBILE = isMobile();
774
772
  var MOBILE_CACHE_SIZE = 60;
775
773
  var DESKTOP_CACHE_SIZE = 150;
776
774
  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 }) => {
777
- const { useRouteItemData, useStatus, useOptimisticLoading } = router.hooks;
775
+ const { useRouteItemData, useStatus } = router.hooks;
778
776
  const [routeItemData] = useRouteItemData();
779
777
  const [status] = useStatus();
780
- const [isOptimisticLoading] = useOptimisticLoading();
781
778
  const loaderState = useLoaderState();
782
779
  const isLoading = status === "pending";
780
+ const isOptimistic = status === "optimistic";
783
781
  useNavigation();
784
782
  useSetRouterConfig({
785
783
  routes,
@@ -803,7 +801,7 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
803
801
  if (showErrorElement) return renderElement(routeItem.errorElement || defaultErrorElement);
804
802
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
805
803
  style: { viewTransitionName: "page" },
806
- children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, { children: renderElement(routeItem.element) }, location.pathname), optimisticSpinner && isOptimisticLoading && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Spinner, {})]
804
+ children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, { children: renderElement(routeItem.element) }, location.pathname), optimisticSpinner && isOptimistic && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Spinner, {})]
807
805
  });
808
806
  };
809
807
  //#endregion
package/dist/types.d.ts CHANGED
@@ -119,7 +119,7 @@ export type LoadingPromise = Promise<{
119
119
  data: null;
120
120
  error: unknown;
121
121
  } | undefined>;
122
- export type Status = 'idle' | 'pending' | 'active';
122
+ export type Status = 'idle' | 'pending' | 'active' | 'optimistic';
123
123
  export type ScrollMap = Record<string, [string, number][]>;
124
124
  export type RouterState = {
125
125
  routeItemDataState: Store<RouteItemData>;
@@ -130,7 +130,6 @@ export type RouterState = {
130
130
  from: string;
131
131
  to: string;
132
132
  }>;
133
- isOptimisticLoading: Store<boolean>;
134
133
  loaderState: Store<LoaderState>;
135
134
  loaderStateRef: Cell<LoaderState>;
136
135
  loaderMap: Map<string, LoaderStateItem>;
@@ -152,7 +151,6 @@ export type RouterType = {
152
151
  useScrollMap: () => ReturnType<typeof useGlobalState<ScrollMap>>;
153
152
  useStatus: () => ReturnType<typeof useGlobalState<Status>>;
154
153
  useContextState: () => ReturnType<typeof useGlobalState<Record<string, unknown>>>;
155
- useOptimisticLoading: () => ReturnType<typeof useGlobalState<boolean>>;
156
154
  useLoaderState: <T>() => LoaderState<T>;
157
155
  useParams: <T>() => T;
158
156
  useNavigate: () => (arg: Location | string | -1) => Promise<void>;
@@ -1,2 +1,2 @@
1
1
  import { Location, RouteItem, RouterState } from '../types';
2
- export declare const createCommitState: ({ routeItemDataState, statusState, isOptimisticLoading, loaderStateRef, loaderState }: RouterState) => (nextLocation: Location, routeItem: RouteItem | undefined) => void;
2
+ export declare const createCommitState: ({ routeItemDataState, statusState, loaderStateRef, loaderState }: RouterState) => (nextLocation: Location, routeItem: RouteItem | undefined) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {