clear-react-router 1.0.20 → 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.js
CHANGED
|
@@ -195,7 +195,7 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
|
|
|
195
195
|
history.pushState(null, "", nextLocation.pathname);
|
|
196
196
|
prevPathname.current = nextLocation.pathname;
|
|
197
197
|
}
|
|
198
|
-
await revalidateCache(nextItem);
|
|
198
|
+
await revalidateCache(nextItem, true);
|
|
199
199
|
if (nextItem?.afterLoad) await nextItem?.afterLoad(context);
|
|
200
200
|
} catch (redirect) {
|
|
201
201
|
if (!(redirect instanceof Redirect)) return redirect;
|
|
@@ -267,60 +267,52 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache })
|
|
|
267
267
|
//#endregion
|
|
268
268
|
//#region hooks/useLoader.ts
|
|
269
269
|
var useLoader = (routeList) => {
|
|
270
|
-
const [loaderCache, setLoaderCache] = useState(
|
|
270
|
+
const [loaderCache, setLoaderCache] = useState();
|
|
271
271
|
const [loaderError, setLoaderError] = useState(false);
|
|
272
|
-
const [
|
|
272
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
273
273
|
const cacheTimestampsRef = useRef({});
|
|
274
|
-
const
|
|
275
|
-
...prevState,
|
|
276
|
-
[key]: value
|
|
277
|
-
})), []);
|
|
274
|
+
const loaderCacheRef = useRef({});
|
|
278
275
|
const isCacheItemFresh = useCallback((routeItem) => {
|
|
279
276
|
if (!routeItem) return true;
|
|
280
277
|
const currentCacheTimestamp = cacheTimestampsRef.current[routeItem.path];
|
|
281
278
|
return Boolean(currentCacheTimestamp && Date.now() - currentCacheTimestamp < (routeItem.staleTime || 0));
|
|
282
279
|
}, []);
|
|
283
|
-
const revalidateCache = useCallback(async (routeItem) => {
|
|
280
|
+
const revalidateCache = useCallback(async (routeItem, isCurrentRoute) => {
|
|
284
281
|
if (!routeItem?.loader) return;
|
|
282
|
+
if (isCacheItemFresh(routeItem) && isCurrentRoute) setLoaderCache(loaderCacheRef.current[routeItem.path]);
|
|
285
283
|
if (isCacheItemFresh(routeItem)) return;
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
[routeItem.path]: true
|
|
289
|
-
}));
|
|
290
|
-
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) => ({
|
|
291
286
|
...acc,
|
|
292
|
-
[cur]:
|
|
293
|
-
}), {})
|
|
287
|
+
[cur]: loaderCacheRef.current[cur]
|
|
288
|
+
}), {});
|
|
294
289
|
try {
|
|
295
|
-
setLoaderError(false);
|
|
290
|
+
if (isCurrentRoute) setLoaderError(false);
|
|
296
291
|
const result = await routeItem?.loader();
|
|
297
292
|
cacheTimestampsRef.current = {
|
|
298
293
|
...cacheTimestampsRef.current,
|
|
299
294
|
[routeItem.path]: Date.now()
|
|
300
295
|
};
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
296
|
+
loaderCacheRef.current = {
|
|
297
|
+
...loaderCacheRef.current,
|
|
298
|
+
[routeItem.path]: result
|
|
299
|
+
};
|
|
300
|
+
if (isCurrentRoute) setLoaderCache(result);
|
|
305
301
|
} catch {
|
|
306
|
-
setLoaderError(true);
|
|
302
|
+
if (isCurrentRoute) setLoaderError(true);
|
|
307
303
|
} finally {
|
|
308
|
-
|
|
309
|
-
...prev,
|
|
310
|
-
[routeItem.path]: false
|
|
311
|
-
}));
|
|
304
|
+
if (isCurrentRoute) setIsLoading(false);
|
|
312
305
|
}
|
|
313
|
-
}, [isCacheItemFresh
|
|
314
|
-
const prefetchLoader = useCallback(async (pathname) => {
|
|
315
|
-
const item = routeList.find((el) => comparePaths(el, pathname));
|
|
316
|
-
if (item) await revalidateCache(item);
|
|
317
|
-
}, [revalidateCache, routeList]);
|
|
306
|
+
}, [isCacheItemFresh]);
|
|
318
307
|
return {
|
|
319
|
-
loaderCache
|
|
308
|
+
loaderCache,
|
|
320
309
|
loaderError,
|
|
321
|
-
prefetchLoader
|
|
310
|
+
prefetchLoader: useCallback(async (pathname) => {
|
|
311
|
+
const item = routeList.find((el) => comparePaths(el, pathname));
|
|
312
|
+
if (item) await revalidateCache(item);
|
|
313
|
+
}, [revalidateCache, routeList]),
|
|
322
314
|
revalidateCache,
|
|
323
|
-
isLoading
|
|
315
|
+
isLoading
|
|
324
316
|
};
|
|
325
317
|
};
|
|
326
318
|
//#endregion
|