clear-react-router 1.0.20 → 1.1.1

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.js CHANGED
@@ -128,58 +128,8 @@ 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
182
131
  //#region hooks/useHandleNavigation.ts
132
+ var isRedirect = (error) => typeof error === "object" && error !== null && error.cause === "redirect";
183
133
  var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache }) => {
184
134
  const [blockedRoute, setBlockedRoute] = useState({
185
135
  from: "",
@@ -191,15 +141,14 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
191
141
  const nextItem = routeList.find((el) => comparePaths(el, nextLocation.pathname));
192
142
  if (nextItem?.beforeLoad) await nextItem?.beforeLoad(context);
193
143
  setLocation(nextLocation);
194
- if (nextLocation.pathname !== window.location.pathname) {
195
- history.pushState(null, "", nextLocation.pathname);
196
- prevPathname.current = nextLocation.pathname;
197
- }
198
- await revalidateCache(nextItem);
144
+ prevPathname.current = nextLocation.pathname;
145
+ if (nextLocation.pathname !== window.location.pathname) history.pushState(null, "", nextLocation.pathname);
146
+ await revalidateCache(nextItem, true);
199
147
  if (nextItem?.afterLoad) await nextItem?.afterLoad(context);
200
- } catch (redirect) {
201
- if (!(redirect instanceof Redirect)) return redirect;
202
- history.replaceState(null, "", `${redirect.url}${redirect.search || ""}`);
148
+ } catch (error) {
149
+ const redirect = error;
150
+ if (!isRedirect(redirect)) return redirect;
151
+ history.replaceState(null, "", redirect.url);
203
152
  setLocation({
204
153
  pathname: redirect.url,
205
154
  search: redirect.search
@@ -243,7 +192,7 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
243
192
  from: prevPathname.current,
244
193
  to: newLocation.pathname
245
194
  });
246
- history.replaceState(null, "", prevPathname.current);
195
+ history.pushState(null, "", prevPathname.current);
247
196
  } else setNextLocationRef.current(newLocation);
248
197
  };
249
198
  window.addEventListener("popstate", handler);
@@ -267,60 +216,52 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
267
216
  //#endregion
268
217
  //#region hooks/useLoader.ts
269
218
  var useLoader = (routeList) => {
270
- const [loaderCache, setLoaderCache] = useState({});
219
+ const [loaderCache, setLoaderCache] = useState();
271
220
  const [loaderError, setLoaderError] = useState(false);
272
- const [isLoadingMap, setIsLoadingMap] = useState({});
221
+ const [isLoading, setIsLoading] = useState(false);
273
222
  const cacheTimestampsRef = useRef({});
274
- const updateCache = useCallback(({ key, value }) => setLoaderCache((prevState) => ({
275
- ...prevState,
276
- [key]: value
277
- })), []);
223
+ const loaderCacheRef = useRef({});
278
224
  const isCacheItemFresh = useCallback((routeItem) => {
279
225
  if (!routeItem) return true;
280
226
  const currentCacheTimestamp = cacheTimestampsRef.current[routeItem.path];
281
227
  return Boolean(currentCacheTimestamp && Date.now() - currentCacheTimestamp < (routeItem.staleTime || 0));
282
228
  }, []);
283
- const revalidateCache = useCallback(async (routeItem) => {
229
+ const revalidateCache = useCallback(async (routeItem, isCurrentRoute) => {
284
230
  if (!routeItem?.loader) return;
231
+ if (isCacheItemFresh(routeItem) && isCurrentRoute) setLoaderCache(loaderCacheRef.current[routeItem.path]);
285
232
  if (isCacheItemFresh(routeItem)) return;
286
- setIsLoadingMap((prev) => ({
287
- ...prev,
288
- [routeItem.path]: true
289
- }));
290
- setLoaderCache((prevState) => Object.keys(prevState).filter((el) => el !== routeItem.path).reduce((acc, cur) => ({
233
+ if (isCurrentRoute) setIsLoading(true);
234
+ loaderCacheRef.current = Object.keys(loaderCacheRef.current).filter((el) => el !== routeItem.path).reduce((acc, cur) => ({
291
235
  ...acc,
292
- [cur]: prevState[cur]
293
- }), {}));
236
+ [cur]: loaderCacheRef.current[cur]
237
+ }), {});
294
238
  try {
295
- setLoaderError(false);
239
+ if (isCurrentRoute) setLoaderError(false);
296
240
  const result = await routeItem?.loader();
297
241
  cacheTimestampsRef.current = {
298
242
  ...cacheTimestampsRef.current,
299
243
  [routeItem.path]: Date.now()
300
244
  };
301
- updateCache({
302
- key: routeItem.path,
303
- value: result
304
- });
245
+ loaderCacheRef.current = {
246
+ ...loaderCacheRef.current,
247
+ [routeItem.path]: result
248
+ };
249
+ if (isCurrentRoute) setLoaderCache(result);
305
250
  } catch {
306
- setLoaderError(true);
251
+ if (isCurrentRoute) setLoaderError(true);
307
252
  } finally {
308
- setIsLoadingMap((prev) => ({
309
- ...prev,
310
- [routeItem.path]: false
311
- }));
253
+ if (isCurrentRoute) setIsLoading(false);
312
254
  }
313
- }, [isCacheItemFresh, updateCache]);
314
- const prefetchLoader = useCallback(async (pathname) => {
315
- const item = routeList.find((el) => comparePaths(el, pathname));
316
- if (item) await revalidateCache(item);
317
- }, [revalidateCache, routeList]);
255
+ }, [isCacheItemFresh]);
318
256
  return {
319
- loaderCache: loaderCache[window.location.pathname],
257
+ loaderCache,
320
258
  loaderError,
321
- prefetchLoader,
259
+ prefetchLoader: useCallback(async (pathname) => {
260
+ const item = routeList.find((el) => comparePaths(el, pathname));
261
+ if (item) await revalidateCache(item);
262
+ }, [revalidateCache, routeList]),
322
263
  revalidateCache,
323
- isLoading: isLoadingMap[window.location.pathname]
264
+ isLoading
324
265
  };
325
266
  };
326
267
  //#endregion
@@ -474,4 +415,55 @@ var useRouterContext = () => {
474
415
  };
475
416
  };
476
417
  //#endregion
418
+ //#region \0@oxc-project+runtime@0.132.0/helpers/typeof.js
419
+ function _typeof(o) {
420
+ "@babel/helpers - typeof";
421
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
422
+ return typeof o;
423
+ } : function(o) {
424
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
425
+ }, _typeof(o);
426
+ }
427
+ //#endregion
428
+ //#region \0@oxc-project+runtime@0.132.0/helpers/toPrimitive.js
429
+ function toPrimitive(t, r) {
430
+ if ("object" != _typeof(t) || !t) return t;
431
+ var e = t[Symbol.toPrimitive];
432
+ if (void 0 !== e) {
433
+ var i = e.call(t, r || "default");
434
+ if ("object" != _typeof(i)) return i;
435
+ throw new TypeError("@@toPrimitive must return a primitive value.");
436
+ }
437
+ return ("string" === r ? String : Number)(t);
438
+ }
439
+ //#endregion
440
+ //#region \0@oxc-project+runtime@0.132.0/helpers/toPropertyKey.js
441
+ function toPropertyKey(t) {
442
+ var i = toPrimitive(t, "string");
443
+ return "symbol" == _typeof(i) ? i : i + "";
444
+ }
445
+ //#endregion
446
+ //#region \0@oxc-project+runtime@0.132.0/helpers/defineProperty.js
447
+ function _defineProperty(e, r, t) {
448
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
449
+ value: t,
450
+ enumerable: !0,
451
+ configurable: !0,
452
+ writable: !0
453
+ }) : e[r] = t, e;
454
+ }
455
+ //#endregion
456
+ //#region utils/redirect.ts
457
+ var Redirect = class {
458
+ constructor(url, search) {
459
+ _defineProperty(this, "url", void 0);
460
+ _defineProperty(this, "search", void 0);
461
+ _defineProperty(this, "cause", void 0);
462
+ this.url = url;
463
+ this.search = search;
464
+ this.cause = "redirect";
465
+ }
466
+ };
467
+ var redirect = (url, search) => Promise.reject(new Redirect(url, search));
468
+ //#endregion
477
469
  export { Link, Router, createRouter, redirect, useBeforeUnload, useBlocker, useLoaderState, useLocation, useNavigate, useParams, useRouterContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.0.20",
3
+ "version": "1.1.1",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {