clear-react-router 1.0.19 → 1.1.0

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.
@@ -3,7 +3,7 @@ type UseHandleNavigation = {
3
3
  routeList: RouteItem[];
4
4
  setLocation: (arg: Location) => void;
5
5
  context: Record<string, unknown>;
6
- revalidateCache(routeItem?: RouteItem): Promise<void>;
6
+ revalidateCache(routeItem?: RouteItem, isCurrentRoute?: boolean): Promise<void>;
7
7
  };
8
8
  export declare const useHandleNavigation: ({ setLocation, routeList, context, revalidateCache }: UseHandleNavigation) => {
9
9
  blockerState: BlockerState;
@@ -3,6 +3,6 @@ export declare const useLoader: (routeList: RouteItem[]) => {
3
3
  loaderCache: unknown;
4
4
  loaderError: boolean;
5
5
  prefetchLoader: (pathname: string) => Promise<void>;
6
- revalidateCache: (routeItem?: RouteItem) => Promise<void>;
6
+ revalidateCache: (routeItem?: RouteItem, isCurrentRoute?: boolean) => Promise<void>;
7
7
  isLoading: boolean;
8
8
  };
package/dist/index.d.ts CHANGED
@@ -9,3 +9,4 @@ export { useBeforeUnload } from './hooks/useBeforeUnload';
9
9
  export { useRouterContext } from './hooks/useRouterContext';
10
10
  export { createRouter } from './utils/utils';
11
11
  export { redirect } from './utils/redirect';
12
+ export type { RouteItem, BlockerState } from './types/global';
package/dist/index.js CHANGED
@@ -128,8 +128,58 @@ var useLatest = (value) => {
128
128
  return ref;
129
129
  };
130
130
  //#endregion
131
+ //#region \0@oxc-project+runtime@0.132.0/helpers/typeof.js
132
+ function _typeof(o) {
133
+ "@babel/helpers - typeof";
134
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
135
+ return typeof o;
136
+ } : function(o) {
137
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
138
+ }, _typeof(o);
139
+ }
140
+ //#endregion
141
+ //#region \0@oxc-project+runtime@0.132.0/helpers/toPrimitive.js
142
+ function toPrimitive(t, r) {
143
+ if ("object" != _typeof(t) || !t) return t;
144
+ var e = t[Symbol.toPrimitive];
145
+ if (void 0 !== e) {
146
+ var i = e.call(t, r || "default");
147
+ if ("object" != _typeof(i)) return i;
148
+ throw new TypeError("@@toPrimitive must return a primitive value.");
149
+ }
150
+ return ("string" === r ? String : Number)(t);
151
+ }
152
+ //#endregion
153
+ //#region \0@oxc-project+runtime@0.132.0/helpers/toPropertyKey.js
154
+ function toPropertyKey(t) {
155
+ var i = toPrimitive(t, "string");
156
+ return "symbol" == _typeof(i) ? i : i + "";
157
+ }
158
+ //#endregion
159
+ //#region \0@oxc-project+runtime@0.132.0/helpers/defineProperty.js
160
+ function _defineProperty(e, r, t) {
161
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
162
+ value: t,
163
+ enumerable: !0,
164
+ configurable: !0,
165
+ writable: !0
166
+ }) : e[r] = t, e;
167
+ }
168
+ //#endregion
169
+ //#region utils/redirect.ts
170
+ var Redirect = class {
171
+ constructor(url, search) {
172
+ _defineProperty(this, "url", void 0);
173
+ _defineProperty(this, "search", void 0);
174
+ _defineProperty(this, "cause", void 0);
175
+ this.url = url;
176
+ this.search = search;
177
+ this.cause = "redirect";
178
+ }
179
+ };
180
+ var redirect = (url, search) => Promise.reject(new Redirect(url, search));
181
+ //#endregion
131
182
  //#region hooks/useHandleNavigation.ts
132
- var isRedirect = (error) => typeof error === "object" && error !== null && error.cause === "redirect";
133
183
  var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache }) => {
134
184
  const [blockedRoute, setBlockedRoute] = useState({
135
185
  from: "",
@@ -145,12 +195,11 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
145
195
  history.pushState(null, "", nextLocation.pathname);
146
196
  prevPathname.current = nextLocation.pathname;
147
197
  }
148
- await revalidateCache(nextItem);
198
+ await revalidateCache(nextItem, true);
149
199
  if (nextItem?.afterLoad) await nextItem?.afterLoad(context);
150
- } catch (error) {
151
- const redirect = error;
152
- if (!isRedirect(redirect)) return redirect;
153
- history.replaceState(null, "", redirect.url);
200
+ } catch (redirect) {
201
+ if (!(redirect instanceof Redirect)) return redirect;
202
+ history.replaceState(null, "", `${redirect.url}${redirect.search || ""}`);
154
203
  setLocation({
155
204
  pathname: redirect.url,
156
205
  search: redirect.search
@@ -218,60 +267,52 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
218
267
  //#endregion
219
268
  //#region hooks/useLoader.ts
220
269
  var useLoader = (routeList) => {
221
- const [loaderCache, setLoaderCache] = useState({});
270
+ const [loaderCache, setLoaderCache] = useState();
222
271
  const [loaderError, setLoaderError] = useState(false);
223
- const [isLoadingMap, setIsLoadingMap] = useState({});
272
+ const [isLoading, setIsLoading] = useState(false);
224
273
  const cacheTimestampsRef = useRef({});
225
- const updateCache = useCallback(({ key, value }) => setLoaderCache((prevState) => ({
226
- ...prevState,
227
- [key]: value
228
- })), []);
274
+ const loaderCacheRef = useRef({});
229
275
  const isCacheItemFresh = useCallback((routeItem) => {
230
276
  if (!routeItem) return true;
231
277
  const currentCacheTimestamp = cacheTimestampsRef.current[routeItem.path];
232
278
  return Boolean(currentCacheTimestamp && Date.now() - currentCacheTimestamp < (routeItem.staleTime || 0));
233
279
  }, []);
234
- const revalidateCache = useCallback(async (routeItem) => {
280
+ const revalidateCache = useCallback(async (routeItem, isCurrentRoute) => {
235
281
  if (!routeItem?.loader) return;
282
+ if (isCacheItemFresh(routeItem) && isCurrentRoute) setLoaderCache(loaderCacheRef.current[routeItem.path]);
236
283
  if (isCacheItemFresh(routeItem)) return;
237
- setIsLoadingMap((prev) => ({
238
- ...prev,
239
- [routeItem.path]: true
240
- }));
241
- setLoaderCache((prevState) => Object.keys(prevState).filter((el) => el !== routeItem.path).reduce((acc, cur) => ({
284
+ if (isCurrentRoute) setIsLoading(true);
285
+ loaderCacheRef.current = Object.keys(loaderCacheRef.current).filter((el) => el !== routeItem.path).reduce((acc, cur) => ({
242
286
  ...acc,
243
- [cur]: prevState[cur]
244
- }), {}));
287
+ [cur]: loaderCacheRef.current[cur]
288
+ }), {});
245
289
  try {
246
- setLoaderError(false);
290
+ if (isCurrentRoute) setLoaderError(false);
247
291
  const result = await routeItem?.loader();
248
292
  cacheTimestampsRef.current = {
249
293
  ...cacheTimestampsRef.current,
250
294
  [routeItem.path]: Date.now()
251
295
  };
252
- updateCache({
253
- key: routeItem.path,
254
- value: result
255
- });
296
+ loaderCacheRef.current = {
297
+ ...loaderCacheRef.current,
298
+ [routeItem.path]: result
299
+ };
300
+ if (isCurrentRoute) setLoaderCache(result);
256
301
  } catch {
257
- setLoaderError(true);
302
+ if (isCurrentRoute) setLoaderError(true);
258
303
  } finally {
259
- setIsLoadingMap((prev) => ({
260
- ...prev,
261
- [routeItem.path]: false
262
- }));
304
+ if (isCurrentRoute) setIsLoading(false);
263
305
  }
264
- }, [isCacheItemFresh, updateCache]);
265
- const prefetchLoader = useCallback(async (pathname) => {
266
- const item = routeList.find((el) => comparePaths(el, pathname));
267
- if (item) await revalidateCache(item);
268
- }, [revalidateCache, routeList]);
306
+ }, [isCacheItemFresh]);
269
307
  return {
270
- loaderCache: loaderCache[window.location.pathname],
308
+ loaderCache,
271
309
  loaderError,
272
- prefetchLoader,
310
+ prefetchLoader: useCallback(async (pathname) => {
311
+ const item = routeList.find((el) => comparePaths(el, pathname));
312
+ if (item) await revalidateCache(item);
313
+ }, [revalidateCache, routeList]),
273
314
  revalidateCache,
274
- isLoading: isLoadingMap[window.location.pathname]
315
+ isLoading
275
316
  };
276
317
  };
277
318
  //#endregion
@@ -425,14 +466,4 @@ var useRouterContext = () => {
425
466
  };
426
467
  };
427
468
  //#endregion
428
- //#region utils/redirect.ts
429
- var redirect = (url, search) => {
430
- const error = /* @__PURE__ */ new Error();
431
- error.cause = "redirect";
432
- throw Object.assign(error, {
433
- url,
434
- search: search || ""
435
- });
436
- };
437
- //#endregion
438
469
  export { Link, Router, createRouter, redirect, useBeforeUnload, useBlocker, useLoaderState, useLocation, useNavigate, useParams, useRouterContext };
@@ -1 +1,7 @@
1
- export declare const redirect: (url: string, search?: string) => never;
1
+ export declare class Redirect {
2
+ url: string;
3
+ search?: string;
4
+ cause: 'redirect';
5
+ constructor(url: string, search?: string);
6
+ }
7
+ export declare const redirect: (url: string, search?: string) => Promise<never>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.0.19",
3
+ "version": "1.1.0",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {