clear-react-router 2.2.3 → 2.2.5

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
@@ -30,7 +30,7 @@ var useGlobalState = ({ subscribe, getState, setState }) => {
30
30
  };
31
31
  //#endregion
32
32
  //#region utils/commitState.ts
33
- var createCommitState = ({ loaderState, routeItemDataState }) => ({ routeItem, loaderStateValue, location }) => {
33
+ var createCommitState = ({ loaderState, routeItemDataState, blockerState }) => ({ routeItem, loaderStateValue, location }) => {
34
34
  const isError = loaderStateValue.loaderError || loaderStateValue.beforeLoadError;
35
35
  routeItemDataState.setState({
36
36
  routeItem,
@@ -38,6 +38,7 @@ var createCommitState = ({ loaderState, routeItemDataState }) => ({ routeItem, l
38
38
  status: isError ? "error" : "active"
39
39
  });
40
40
  loaderState.setState(loaderStateValue);
41
+ if (blockerState.getState() === "blocked") blockerState.setState("unblocked");
41
42
  if ((location.search ? `${location.pathname}${location.search}` : location.pathname) === window.location.pathname + window.location.search) return;
42
43
  history.pushState(null, "", `${location.pathname}${location.search}`);
43
44
  };
@@ -541,15 +542,11 @@ var createRevalidateCache = (routerState) => {
541
542
  if (retry && retry.count > retried) {
542
543
  loadingPromises.delete(path);
543
544
  if (retry.delay) await sleep(retry.delay);
544
- await revalidateCache({
545
+ return revalidateCache({
545
546
  routeItem,
546
547
  location,
547
548
  signal
548
549
  }, retried + 1);
549
- return {
550
- data: null,
551
- error
552
- };
553
550
  } else return {
554
551
  data: null,
555
552
  error
@@ -603,7 +600,6 @@ var createRouterInstance = () => {
603
600
  return action;
604
601
  };
605
602
  return {
606
- state: routerState,
607
603
  runtime: {
608
604
  navigate,
609
605
  invalidate,
@@ -614,6 +610,7 @@ var createRouterInstance = () => {
614
610
  useRouteItemData: () => useGlobalState(routerState.routeItemDataState),
615
611
  useScrollMap: () => useGlobalState(routerState.scrollMapState),
616
612
  useContextState: () => useGlobalState(routerState.contextState),
613
+ useBlockedTargetState: () => useGlobalState(routerState.blockedTargetState),
617
614
  useLoaderState: () => useGlobalState(routerState.loaderState)[0],
618
615
  useParams: () => {
619
616
  const { routeItem, location } = routerState.routeItemDataState.getState();
@@ -678,16 +675,17 @@ var router = createRouterInstance();
678
675
  //#region hooks/useNavigation.ts
679
676
  var useNavigation = () => {
680
677
  const { navigate } = router.runtime;
681
- const { blockerState, routeItemDataState } = router.state;
678
+ const { useRouteItemData, useBlockerState } = router.hooks;
679
+ const [{ location }] = useRouteItemData();
680
+ const [blockerState] = useBlockerState();
682
681
  useEffect(() => {
683
682
  const handler = async (event) => {
684
683
  const newLocation = parseWindowLocation(event.target.location);
685
- const { location } = routeItemDataState.getState();
686
684
  const prevLocation = {
687
685
  pathname: location.pathname,
688
686
  search: location.search
689
687
  };
690
- if (blockerState.getState() === "charged") history.pushState(null, "", routeItemDataState.getState().location.pathname);
688
+ if (blockerState === "charged") history.pushState(null, "", location.pathname);
691
689
  await navigate({
692
690
  ...newLocation,
693
691
  prevLocation
@@ -696,9 +694,10 @@ var useNavigation = () => {
696
694
  window.addEventListener("popstate", handler);
697
695
  return () => window.removeEventListener("popstate", handler);
698
696
  }, [
699
- navigate,
700
697
  blockerState,
701
- routeItemDataState
698
+ location.pathname,
699
+ location.search,
700
+ navigate
702
701
  ]);
703
702
  useEffect(() => {
704
703
  navigate(parseWindowLocation(window.location));
@@ -963,10 +962,10 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
963
962
  };
964
963
  return as({
965
964
  ref: elementRef,
966
- href: to,
967
965
  style: normalizedStyle,
968
966
  className: resultClassName,
969
967
  onClick: clickHandler,
968
+ href: `${to}${searchString}`,
970
969
  onMouseEnter,
971
970
  onMouseLeave,
972
971
  children
@@ -987,9 +986,9 @@ var useInvalidate = () => router.runtime.invalidate;
987
986
  //#endregion
988
987
  //#region hooks/useBlocker.ts
989
988
  var useBlocker = (blockerFn) => {
990
- const { hooks: { useBlockerState, useContextState }, runtime: { navigate }, state: { blockedTargetState } } = router;
991
- const [nextLocation, setNextLocation] = useGlobalState(blockedTargetState);
989
+ const { hooks: { useBlockerState, useContextState, useBlockedTargetState }, runtime: { navigate } } = router;
992
990
  const [blockerState, setBlockerState] = useBlockerState();
991
+ const [nextLocation, setNextLocation] = useBlockedTargetState();
993
992
  const [context] = useContextState();
994
993
  const location = useLocation();
995
994
  const shouldBlock = blockerFn(useMemo(() => ({
@@ -1006,12 +1005,10 @@ var useBlocker = (blockerFn) => {
1006
1005
  state: blockerState,
1007
1006
  process: useCallback(async () => {
1008
1007
  if (nextLocation) await navigate(nextLocation);
1009
- setBlockerState("unblocked");
1010
1008
  setNextLocation(null);
1011
1009
  }, [
1012
1010
  navigate,
1013
1011
  nextLocation,
1014
- setBlockerState,
1015
1012
  setNextLocation
1016
1013
  ]),
1017
1014
  reset: useCallback(() => {
@@ -1041,10 +1038,13 @@ var useRouterContext = () => {
1041
1038
  };
1042
1039
  //#endregion
1043
1040
  //#region hooks/useSearch.ts
1041
+ var isPatched = false;
1044
1042
  var notify = () => {
1045
1043
  window.dispatchEvent(new Event("locationChange"));
1046
1044
  };
1047
1045
  var patchHistory = () => {
1046
+ if (isPatched) return;
1047
+ isPatched = true;
1048
1048
  const push = history.pushState;
1049
1049
  const replace = history.replaceState;
1050
1050
  history.pushState = function(...args) {
package/dist/types.d.ts CHANGED
@@ -128,7 +128,6 @@ export type RouterState = {
128
128
  loadingPromises: Map<string, LoadingPromise>;
129
129
  };
130
130
  export type RouterType = {
131
- state: Omit<RouterState, 'timestampMap'>;
132
131
  runtime: {
133
132
  navigate(arg: Location): Promise<void>;
134
133
  invalidate(pathList?: string | string[], options?: InvalidateOptions): Promise<InvalidateResult[]>;
@@ -139,6 +138,7 @@ export type RouterType = {
139
138
  useRouteItemData(): ReturnType<typeof useGlobalState<RouteItemData>>;
140
139
  useScrollMap(): ReturnType<typeof useGlobalState<ScrollMap>>;
141
140
  useContextState(): ReturnType<typeof useGlobalState<Record<string, unknown>>>;
141
+ useBlockedTargetState(): ReturnType<typeof useGlobalState<Location | null>>;
142
142
  useLoaderState<T>(): LoaderState<T>;
143
143
  useParams<T>(): T;
144
144
  useNavigate(): (arg: NavigationLocation | string | -1) => Promise<void>;
@@ -4,5 +4,5 @@ type CommitState = {
4
4
  loaderStateValue: LoaderState;
5
5
  routeItem?: RouteItem;
6
6
  };
7
- export declare const createCommitState: ({ loaderState, routeItemDataState }: RouterState) => ({ routeItem, loaderStateValue, location }: CommitState) => void;
7
+ export declare const createCommitState: ({ loaderState, routeItemDataState, blockerState }: RouterState) => ({ routeItem, loaderStateValue, location }: CommitState) => void;
8
8
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {