clear-react-router 2.2.0 → 2.2.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 +76 -6
- package/dist/index.js +112 -75
- package/dist/runtime/prefetch.d.ts +1 -1
- package/dist/types.d.ts +5 -4
- package/dist/utils/commitState.d.ts +8 -2
- package/package.json +1 -1
- package/dist/cell.d.ts +0 -6
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
|
|
|
73
73
|
| `element` | `ReactElement \| () => ReactElement \| LazyComponent` | Component to render |
|
|
74
74
|
| `beforeLoad` | `({ params, context, redirect, setContext, location }) => Promise<unknown> \| undefined \| void` | Runs before every route navigation. Auth checks and redirects. Can update context via `setContext`. `redirect` is provided by the router |
|
|
75
75
|
| `loader` | `({ params, context, setContext, searchParams, signal }) => Promise<unknown>` | Fetch data using route params, search params, abort controller signal and context. Can update context via `setContext` |
|
|
76
|
-
| `afterLoad` | `({ params, context,
|
|
76
|
+
| `afterLoad` | `({ params, context, searchParams }) => Promise<void> \| void` | Runs after a successful navigation once the route has finished loading. Analytics, side effects after data is loaded. Can update context via `setContext` |
|
|
77
77
|
| `minLoaderDuration` | `number \| undefined` | `undefined` | Minimum time the loader fallback stays visible, to avoid flickering |
|
|
78
78
|
| `fallback` | `ReactElement \| () => ReactElement` | Loading fallback (for lazy loading) |
|
|
79
79
|
| `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback for the route's `loader`. Overrides the global `defaultLoaderFallback` set in `Router` |
|
|
@@ -81,13 +81,12 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
|
|
|
81
81
|
| `optimistic` | `boolean \| undefined` | Instant navigation using stale data while fresh data is loaded in the background |
|
|
82
82
|
| `errorElement` | `ReactElement \| () => ReactElement` | Error fallback for the route. Overrides the global `defaultErrorElement` set in `Router` |
|
|
83
83
|
| `staleTime` | `number \| undefined` | Time in milliseconds before cached loader data is considered stale. Overrides Router.defaultStaleTime. If neither value is provided, cached data never expires |
|
|
84
|
+
| `gcTime` | `number \| undefined` | How long, in milliseconds, an unused cache entry is kept in memory after you navigate away, before it's garbage-collected |
|
|
84
85
|
| `actions` | `({ params, context, invalidate, setContext }) => Record<string, (formData: FormData) => unknown \| Promise<unknown>>` | Defines route actions for data mutations. Actions receive `FormData`, can update context via `setContext`, and can invalidate cached loader data using the router-provided `invalidate` |
|
|
85
86
|
| `pollingInterval` | `number \| undefined` | Polling interval (in milliseconds) for automatically revalidating data while the route is active |
|
|
86
87
|
| `scrollRestoration` | `boolean \| string[] \| undefined` | Restore scroll position when navigating back to this route. `true` restores the window scroll; a string array restores scroll inside specific scrollable elements, matched by their `id` |
|
|
87
88
|
| `scrollRestorationBehavior` | `'auto' \| 'smooth' \| 'instant'` | Scroll restoration behavior |
|
|
88
89
|
|
|
89
|
-
Before load arguments (see [`redirect`](#redirect) for details on programmatic redirects):
|
|
90
|
-
|
|
91
90
|
`beforeLoad` and `loader` both receive:
|
|
92
91
|
|
|
93
92
|
```ts
|
|
@@ -99,20 +98,24 @@ Before load arguments (see [`redirect`](#redirect) for details on programmatic r
|
|
|
99
98
|
location: Location; // Route location
|
|
100
99
|
}
|
|
101
100
|
```
|
|
102
|
-
|
|
101
|
+
see [`Location`](#uselocation) and [`Where you came from`](#where-you-came-from) for type details
|
|
102
|
+
|
|
103
103
|
`beforeLoad` additionally receives:
|
|
104
104
|
```ts
|
|
105
105
|
{
|
|
106
106
|
redirect: (arg: Location | string) => Promise<void>; // Programmatic redirection, see [redirect](#redirect)
|
|
107
107
|
}
|
|
108
108
|
```
|
|
109
|
+
see [`redirect`](#redirect) for details on programmatic redirects
|
|
109
110
|
|
|
110
111
|
`loader` additionally receives:
|
|
111
112
|
```ts
|
|
112
113
|
{
|
|
113
|
-
signal: AbortSignal;
|
|
114
|
+
signal: AbortSignal; // Aborted if a newer navigation supersedes this one — pass to fetch() to cancel in-flight requests
|
|
114
115
|
}
|
|
115
116
|
```
|
|
117
|
+
> **Note:** `gcTime` is independent from `staleTime` — `staleTime` controls when cached data is considered outdated (and needs a refetch), while `gcTime` controls how long the cache entry stays in memory at all once you leave that route. Data can be fresh and still get garbage-collected, or stale and still linger in memory, depending on which you set. The timer starts only when you navigate away from the route and is cancelled if you come back before it fires — it isn't affected by prefetching. This is mainly useful for routes with heavy or fast-changing data (large tables, dashboards with charts) that you don't want lingering in memory indefinitely, even when `maxCacheSize` hasn't been reached yet.
|
|
118
|
+
If a route is both `optimistic` and has `gcTime` set, keep in mind the cached snapshot can disappear while you're relying on it for an instant render — combine the two deliberately, not by default.
|
|
116
119
|
|
|
117
120
|
### `Link`
|
|
118
121
|
|
|
@@ -550,7 +553,7 @@ navigate({ pathname: '/user/123', search: { user: 'Jane Doe' }, state: { from: '
|
|
|
550
553
|
navigate(-1); // go back
|
|
551
554
|
```
|
|
552
555
|
|
|
553
|
-
**Note:** Navigation state can be accessed via `useLocation()
|
|
556
|
+
**Note:** Navigation state can be accessed via [`useLocation()`](#uselocation):
|
|
554
557
|
|
|
555
558
|
```tsx
|
|
556
559
|
const navigate = useNavigate();
|
|
@@ -574,8 +577,39 @@ const params = useParams<{ userId: string }>();
|
|
|
574
577
|
|
|
575
578
|
Returns current location `{ pathname, search, state }`.
|
|
576
579
|
```tsx
|
|
580
|
+
type Location = {
|
|
581
|
+
pathname: string;
|
|
582
|
+
search?: string;
|
|
583
|
+
state?: unknown;
|
|
584
|
+
prevLocation?: Omit<Location, 'state' | 'prevLocation'>;
|
|
585
|
+
}
|
|
586
|
+
|
|
577
587
|
const { pathname, search, state } = useLocation();
|
|
578
588
|
```
|
|
589
|
+
### Where you came from
|
|
590
|
+
|
|
591
|
+
Every `location` carries `prevLocation` — the pathname and search of the route you
|
|
592
|
+
navigated away from. Useful for a contextual "Back" link, or for redirect logic in
|
|
593
|
+
`beforeLoad`:
|
|
594
|
+
|
|
595
|
+
```tsx
|
|
596
|
+
const location = useLocation();
|
|
597
|
+
|
|
598
|
+
<Link to={location.prevLocation?.pathname ?? '/'}>← Back</Link>
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
```tsx
|
|
602
|
+
// beforeLoad
|
|
603
|
+
beforeLoad: async ({ location, redirect }) => {
|
|
604
|
+
if (!isAuthenticated && location.prevLocation?.pathname !== '/login') {
|
|
605
|
+
await redirect('/login');
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
> `prevLocation` reflects only the immediately preceding route — it doesn't accumulate
|
|
611
|
+
> a full navigation history, and it's `undefined` on the very first render (nothing to
|
|
612
|
+
> navigate away from yet).
|
|
579
613
|
|
|
580
614
|
### `useLoaderState<T>()`
|
|
581
615
|
|
|
@@ -870,6 +904,42 @@ import { lazy } from 'clear-react-router';
|
|
|
870
904
|
fallback: () => <div>Loading...</div>,
|
|
871
905
|
}
|
|
872
906
|
```
|
|
907
|
+
|
|
908
|
+
## Deferred data (React 19 `use()`)
|
|
909
|
+
|
|
910
|
+
`loader` can return a value containing unresolved promises — the router doesn't await
|
|
911
|
+
anything beyond what your `loader` itself awaits. This lets you unblock navigation on
|
|
912
|
+
fast data while slow data streams in separately:
|
|
913
|
+
|
|
914
|
+
```tsx
|
|
915
|
+
{
|
|
916
|
+
path: '/about',
|
|
917
|
+
loader: async () => {
|
|
918
|
+
const fast = await fetchFast();
|
|
919
|
+
const slow = fetchSlow(); // not awaited — passed through as a promise
|
|
920
|
+
return { fast, slow };
|
|
921
|
+
},
|
|
922
|
+
}
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
```tsx
|
|
926
|
+
const Slow = () => {
|
|
927
|
+
const { data } = useLoaderState<{ slow: Promise<string> }>();
|
|
928
|
+
return use(data.slow);
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
const About = () => (
|
|
932
|
+
<Suspense fallback={<Skeleton />}>
|
|
933
|
+
<Slow />
|
|
934
|
+
</Suspense>
|
|
935
|
+
);
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
> Wrap deferred parts in their own `ErrorBoundary` — a rejected promise passed to `use()`
|
|
939
|
+
> is not caught by the route's `errorElement`. Also set `staleTime` generously enough to
|
|
940
|
+
> outlast your slowest deferred request, or a fast repeat visit may trigger a redundant
|
|
941
|
+
> refetch while the previous one is still resolving.
|
|
942
|
+
|
|
873
943
|
## Animations
|
|
874
944
|
|
|
875
945
|
Clear Router supports smooth page transitions using the native View Transitions API. When animations are enabled, the router waits for all data to load before starting the transition, ensuring a jank-free experience.
|
package/dist/index.js
CHANGED
|
@@ -30,16 +30,16 @@ var useGlobalState = ({ subscribe, getState, setState }) => {
|
|
|
30
30
|
};
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region utils/commitState.ts
|
|
33
|
-
var createCommitState = ({
|
|
34
|
-
const isError =
|
|
33
|
+
var createCommitState = ({ loaderState, routeItemDataState }) => ({ routeItem, loaderStateValue, location }) => {
|
|
34
|
+
const isError = loaderStateValue.loaderError || loaderStateValue.beforeLoadError;
|
|
35
35
|
routeItemDataState.setState({
|
|
36
36
|
routeItem,
|
|
37
|
-
location
|
|
37
|
+
location,
|
|
38
38
|
status: isError ? "error" : "active"
|
|
39
39
|
});
|
|
40
|
-
loaderState.setState(
|
|
41
|
-
if ((
|
|
42
|
-
history.pushState(null, "", `${
|
|
40
|
+
loaderState.setState(loaderStateValue);
|
|
41
|
+
if ((location.search ? `${location.pathname}${location.search}` : location.pathname) === window.location.pathname + window.location.search) return;
|
|
42
|
+
history.pushState(null, "", `${location.pathname}${location.search}`);
|
|
43
43
|
};
|
|
44
44
|
//#endregion
|
|
45
45
|
//#region constants.ts
|
|
@@ -227,7 +227,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
227
227
|
let navigationSeq = 0;
|
|
228
228
|
let interval = 0;
|
|
229
229
|
let abortController = null;
|
|
230
|
-
const { loaderState,
|
|
230
|
+
const { loaderState, contextState, loaderMap, routeItemDataState, scrollMapState, blockerState, blockedTargetState } = routerState;
|
|
231
231
|
const commitState = createCommitState(routerState);
|
|
232
232
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
233
233
|
const createSignal = () => {
|
|
@@ -236,12 +236,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
236
236
|
return abortController.signal;
|
|
237
237
|
};
|
|
238
238
|
const getPath = (nextLocation) => `${nextLocation.pathname}${nextLocation.search}`;
|
|
239
|
-
const getContext = () => ({
|
|
240
|
-
context: contextState.getState(),
|
|
241
|
-
setContext: contextState.setState
|
|
242
|
-
});
|
|
243
239
|
const routeResolve = (location) => {
|
|
244
|
-
loaderStateRef.set(EMPTY_LOADER_STATE);
|
|
245
240
|
const nextItem = findRoute(location.pathname, true);
|
|
246
241
|
return {
|
|
247
242
|
nextItem,
|
|
@@ -251,28 +246,52 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
251
246
|
const beforeLoad = async (routeItem, nextLocation) => {
|
|
252
247
|
const { defaultBeforeLoad } = routerConfig;
|
|
253
248
|
const runBeforeLoad = async (loaderFn) => {
|
|
254
|
-
const redirect = async (redirected) =>
|
|
249
|
+
const redirect = async (redirected) => {
|
|
250
|
+
const { location } = routeItemDataState.getState();
|
|
251
|
+
const prevLocation = {
|
|
252
|
+
pathname: location.pathname,
|
|
253
|
+
search: location.search
|
|
254
|
+
};
|
|
255
|
+
return await navigate(typeof redirected === "string" ? {
|
|
256
|
+
pathname: redirected,
|
|
257
|
+
prevLocation
|
|
258
|
+
} : {
|
|
259
|
+
...redirected,
|
|
260
|
+
prevLocation
|
|
261
|
+
});
|
|
262
|
+
};
|
|
255
263
|
try {
|
|
256
264
|
await loaderFn({
|
|
257
265
|
redirect,
|
|
258
266
|
...getPartialLoaderArgs(contextState, nextLocation, routeItem)
|
|
259
267
|
});
|
|
260
|
-
|
|
261
|
-
...prev,
|
|
262
|
-
beforeLoadError: null
|
|
263
|
-
}));
|
|
268
|
+
return null;
|
|
264
269
|
} catch (error) {
|
|
265
|
-
|
|
266
|
-
...prev,
|
|
267
|
-
beforeLoadError: error
|
|
268
|
-
}));
|
|
270
|
+
return error;
|
|
269
271
|
}
|
|
270
272
|
};
|
|
271
|
-
|
|
272
|
-
if (
|
|
273
|
+
let error = null;
|
|
274
|
+
if (defaultBeforeLoad) error = await runBeforeLoad(defaultBeforeLoad);
|
|
275
|
+
if (routeItem?.beforeLoad && !error) error = await runBeforeLoad(routeItem?.beforeLoad);
|
|
276
|
+
return error;
|
|
277
|
+
};
|
|
278
|
+
const createGcTimeout = () => {
|
|
279
|
+
const { routeItem, location } = routeItemDataState.getState();
|
|
280
|
+
if (!routeItem?.gcTime) return;
|
|
281
|
+
const path = `${location.pathname}${location.search}`;
|
|
282
|
+
const currentLoaderEntry = loaderMap.get(path);
|
|
283
|
+
if (!currentLoaderEntry) return;
|
|
284
|
+
if (currentLoaderEntry.gcTimeout) window.clearTimeout(currentLoaderEntry?.gcTimeout);
|
|
285
|
+
const gcTimeout = window.setTimeout(() => loaderMap.delete(path), routeItem.gcTime);
|
|
286
|
+
loaderMap.set(path, {
|
|
287
|
+
...currentLoaderEntry,
|
|
288
|
+
gcTimeout
|
|
289
|
+
});
|
|
273
290
|
};
|
|
274
|
-
const prepareNavigation = (routeItem, location) => {
|
|
291
|
+
const prepareNavigation = (routeItem, location, hasError) => {
|
|
275
292
|
updateScrollMap(routeItemDataState, scrollMapState);
|
|
293
|
+
createGcTimeout();
|
|
294
|
+
if (hasError) return;
|
|
276
295
|
const path = getPath(location);
|
|
277
296
|
if (routeItem?.optimistic && loaderMap.has(path)) {
|
|
278
297
|
commitNavigation(() => routeItemDataState.setState({
|
|
@@ -301,7 +320,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
301
320
|
};
|
|
302
321
|
const getLoaderDurationPromise = (routeItem, nextLocation) => {
|
|
303
322
|
const minLoaderDuration = routeItem?.minLoaderDuration ?? routerConfig.defaultMinLoaderDuration ?? 0;
|
|
304
|
-
return minLoaderDuration && !isCacheItemFresh(getPath(nextLocation)) ? sleep(minLoaderDuration) : Promise.resolve;
|
|
323
|
+
return minLoaderDuration && !isCacheItemFresh(getPath(nextLocation)) ? sleep(minLoaderDuration) : Promise.resolve();
|
|
305
324
|
};
|
|
306
325
|
const loader = async (routeItem, nextLocation, seq) => {
|
|
307
326
|
if (!routeItem?.loader) return;
|
|
@@ -312,24 +331,20 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
312
331
|
location: nextLocation,
|
|
313
332
|
signal
|
|
314
333
|
}), getLoaderDurationPromise(routeItem, nextLocation)]);
|
|
315
|
-
if (result) loaderStateRef.set((prev) => ({
|
|
316
|
-
...prev,
|
|
317
|
-
data: result.data,
|
|
318
|
-
loaderError: result.error
|
|
319
|
-
}));
|
|
320
334
|
if (seq !== navigationSeq) return;
|
|
321
335
|
polling(routeItem, nextLocation);
|
|
336
|
+
return result;
|
|
322
337
|
};
|
|
323
338
|
const afterLoad = async (routeItem, params) => {
|
|
324
339
|
const { defaultAfterLoad } = routerConfig;
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
340
|
+
const searchParams = Object.fromEntries(new URLSearchParams(location.search).entries());
|
|
341
|
+
const args = {
|
|
342
|
+
context: contextState.getState(),
|
|
343
|
+
params,
|
|
344
|
+
searchParams
|
|
345
|
+
};
|
|
346
|
+
if (routeItem?.afterLoad) await routeItem.afterLoad(args);
|
|
347
|
+
if (defaultAfterLoad) await defaultAfterLoad(args);
|
|
333
348
|
};
|
|
334
349
|
const checkBlocked = (nextLocation) => {
|
|
335
350
|
if (blockerState.getState() === "charged") {
|
|
@@ -339,6 +354,17 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
339
354
|
}
|
|
340
355
|
return false;
|
|
341
356
|
};
|
|
357
|
+
const clearCurrentGcTimeout = (routeItem, location) => {
|
|
358
|
+
const path = `${location.pathname}${location.search}`;
|
|
359
|
+
if (!routeItem?.gcTime) return;
|
|
360
|
+
const currentLoaderEntry = loaderMap.get(path);
|
|
361
|
+
if (!currentLoaderEntry?.gcTimeout) return;
|
|
362
|
+
window.clearTimeout(currentLoaderEntry.gcTimeout);
|
|
363
|
+
loaderMap.set(path, {
|
|
364
|
+
...currentLoaderEntry,
|
|
365
|
+
gcTimeout: void 0
|
|
366
|
+
});
|
|
367
|
+
};
|
|
342
368
|
const navigate = async (rawLocation) => {
|
|
343
369
|
const nextLocation = {
|
|
344
370
|
...rawLocation,
|
|
@@ -348,13 +374,27 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
348
374
|
navigationSeq = navigationSeq + 1;
|
|
349
375
|
const seq = navigationSeq;
|
|
350
376
|
const { nextItem, params } = routeResolve(nextLocation);
|
|
351
|
-
await beforeLoad(nextItem, nextLocation);
|
|
377
|
+
const beforeLoadError = await beforeLoad(nextItem, nextLocation);
|
|
352
378
|
if (seq !== navigationSeq) return;
|
|
353
|
-
prepareNavigation(nextItem, nextLocation);
|
|
354
|
-
|
|
379
|
+
prepareNavigation(nextItem, nextLocation, !!beforeLoadError);
|
|
380
|
+
const result = beforeLoadError ? {
|
|
381
|
+
data: null,
|
|
382
|
+
error: null,
|
|
383
|
+
beforeLoadError
|
|
384
|
+
} : await loader(nextItem, nextLocation, seq);
|
|
355
385
|
if (seq !== navigationSeq) return;
|
|
356
|
-
|
|
357
|
-
|
|
386
|
+
const loaderStateValue = {
|
|
387
|
+
data: result?.data,
|
|
388
|
+
loaderError: result?.error ?? null,
|
|
389
|
+
beforeLoadError
|
|
390
|
+
};
|
|
391
|
+
commitNavigation(() => commitState({
|
|
392
|
+
location: nextLocation,
|
|
393
|
+
routeItem: nextItem,
|
|
394
|
+
loaderStateValue
|
|
395
|
+
}));
|
|
396
|
+
clearCurrentGcTimeout(nextItem, nextLocation);
|
|
397
|
+
afterLoad(nextItem, params);
|
|
358
398
|
};
|
|
359
399
|
return navigate;
|
|
360
400
|
};
|
|
@@ -524,20 +564,6 @@ var createRevalidateCache = (routerState) => {
|
|
|
524
564
|
return revalidateCache;
|
|
525
565
|
};
|
|
526
566
|
//#endregion
|
|
527
|
-
//#region cell.ts
|
|
528
|
-
var Cell = class {
|
|
529
|
-
constructor(_value) {
|
|
530
|
-
_defineProperty(this, "_value", void 0);
|
|
531
|
-
this._value = _value;
|
|
532
|
-
}
|
|
533
|
-
get value() {
|
|
534
|
-
return this._value;
|
|
535
|
-
}
|
|
536
|
-
set(action) {
|
|
537
|
-
this._value = typeof action === "function" ? action(this._value) : action;
|
|
538
|
-
}
|
|
539
|
-
};
|
|
540
|
-
//#endregion
|
|
541
567
|
//#region creators/createRouterInstance.ts
|
|
542
568
|
var createRouterInstance = () => {
|
|
543
569
|
const routerState = {
|
|
@@ -550,7 +576,6 @@ var createRouterInstance = () => {
|
|
|
550
576
|
contextState: create({}),
|
|
551
577
|
blockerState: create("unblocked"),
|
|
552
578
|
loaderState: create(EMPTY_LOADER_STATE),
|
|
553
|
-
loaderStateRef: new Cell(EMPTY_LOADER_STATE),
|
|
554
579
|
blockedTargetState: create(null),
|
|
555
580
|
loaderMap: /* @__PURE__ */ new Map(),
|
|
556
581
|
loadingPromises: /* @__PURE__ */ new Map()
|
|
@@ -594,24 +619,28 @@ var createRouterInstance = () => {
|
|
|
594
619
|
const { routeItem, location } = routerState.routeItemDataState.getState();
|
|
595
620
|
return getParams(location, routeItem);
|
|
596
621
|
},
|
|
597
|
-
useNavigate: () => {
|
|
622
|
+
useNavigate: () => async (arg) => {
|
|
598
623
|
const { location } = routerState.routeItemDataState.getState();
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
const [pathname, search = ""] = arg.split("?");
|
|
603
|
-
if (pathname !== location.pathname || search !== location.search) await navigate({
|
|
604
|
-
pathname,
|
|
605
|
-
search
|
|
606
|
-
});
|
|
607
|
-
return;
|
|
608
|
-
}
|
|
609
|
-
const search = typeof arg.search === "object" ? formatSearchObject(arg.search) : arg.search;
|
|
610
|
-
await navigate({
|
|
611
|
-
...arg,
|
|
612
|
-
search
|
|
613
|
-
});
|
|
624
|
+
const prevLocation = {
|
|
625
|
+
pathname: location.pathname,
|
|
626
|
+
search: location.search
|
|
614
627
|
};
|
|
628
|
+
if (arg === -1) return history.go(arg);
|
|
629
|
+
if (typeof arg === "string") {
|
|
630
|
+
const [pathname, search = ""] = arg.split("?");
|
|
631
|
+
if (pathname !== location.pathname || search !== location.search) await navigate({
|
|
632
|
+
pathname,
|
|
633
|
+
search,
|
|
634
|
+
prevLocation
|
|
635
|
+
});
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const search = typeof arg.search === "object" ? formatSearchObject(arg.search) : arg.search;
|
|
639
|
+
await navigate({
|
|
640
|
+
...arg,
|
|
641
|
+
search,
|
|
642
|
+
prevLocation
|
|
643
|
+
});
|
|
615
644
|
},
|
|
616
645
|
useAction: (action, options = {}) => {
|
|
617
646
|
const currentAction = getCurrentAction(action);
|
|
@@ -653,8 +682,16 @@ var useNavigation = () => {
|
|
|
653
682
|
useEffect(() => {
|
|
654
683
|
const handler = async (event) => {
|
|
655
684
|
const newLocation = parseWindowLocation(event.target.location);
|
|
685
|
+
const { location } = routeItemDataState.getState();
|
|
686
|
+
const prevLocation = {
|
|
687
|
+
pathname: location.pathname,
|
|
688
|
+
search: location.search
|
|
689
|
+
};
|
|
656
690
|
if (blockerState.getState() === "charged") history.pushState(null, "", routeItemDataState.getState().location.pathname);
|
|
657
|
-
await navigate(
|
|
691
|
+
await navigate({
|
|
692
|
+
...newLocation,
|
|
693
|
+
prevLocation
|
|
694
|
+
});
|
|
658
695
|
};
|
|
659
696
|
window.addEventListener("popstate", handler);
|
|
660
697
|
return () => window.removeEventListener("popstate", handler);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { RevalidateCache, RouterState } from '../types';
|
|
1
|
+
import { RevalidateCache, RouterState, Location } from '../types';
|
|
2
2
|
export declare const createPrefetch: ({ routeItemDataState }: RouterState, revalidateCache: RevalidateCache) => (location: Location) => Promise<void>;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ComponentType, type CSSProperties, Dispatch, MouseEvent, ReactElement, ReactNode, Ref, SetStateAction } from 'react';
|
|
2
2
|
import { Store, useGlobalState } from './create';
|
|
3
|
-
import { Cell } from './cell';
|
|
4
3
|
export declare const LAZY_MARKER: unique symbol;
|
|
5
4
|
export type LazyComponent = {
|
|
6
5
|
readonly [LAZY_MARKER]: true;
|
|
@@ -31,6 +30,7 @@ export type ClientRouteItem = {
|
|
|
31
30
|
fallback?: RenderElement;
|
|
32
31
|
children?: ClientRouteItem[];
|
|
33
32
|
staleTime?: number;
|
|
33
|
+
gcTime?: number;
|
|
34
34
|
optimistic?: boolean;
|
|
35
35
|
pollingInterval?: number;
|
|
36
36
|
retry?: Retry;
|
|
@@ -41,8 +41,8 @@ export type ClientRouteItem = {
|
|
|
41
41
|
afterLoad?: (arg: {
|
|
42
42
|
context: Record<string, unknown>;
|
|
43
43
|
params: Record<string, string>;
|
|
44
|
-
|
|
45
|
-
}) => Promise<void
|
|
44
|
+
searchParams: Record<string, string>;
|
|
45
|
+
}) => Promise<void> | void;
|
|
46
46
|
actions?: (arg: LoaderArgs) => Record<string, (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown>;
|
|
47
47
|
};
|
|
48
48
|
export type RouteItem = ClientRouteItem & {
|
|
@@ -57,6 +57,7 @@ export type Location = {
|
|
|
57
57
|
pathname: string;
|
|
58
58
|
search?: string;
|
|
59
59
|
state?: unknown;
|
|
60
|
+
prevLocation?: Omit<Location, 'state' | 'prevLocation'>;
|
|
60
61
|
};
|
|
61
62
|
export type BlockerState = 'blocked' | 'unblocked' | 'charged';
|
|
62
63
|
export type RevalidateCacheArgs = {
|
|
@@ -106,6 +107,7 @@ export type LoaderStateItem = {
|
|
|
106
107
|
state: LoaderState;
|
|
107
108
|
timestamp: number;
|
|
108
109
|
staleTime: number | undefined;
|
|
110
|
+
gcTimeout?: number;
|
|
109
111
|
};
|
|
110
112
|
export type LoadingPromise = Promise<{
|
|
111
113
|
data: unknown;
|
|
@@ -122,7 +124,6 @@ export type RouterState = {
|
|
|
122
124
|
blockerState: Store<BlockerState>;
|
|
123
125
|
loaderState: Store<LoaderState>;
|
|
124
126
|
blockedTargetState: Store<Location | null>;
|
|
125
|
-
loaderStateRef: Cell<LoaderState>;
|
|
126
127
|
loaderMap: Map<string, LoaderStateItem>;
|
|
127
128
|
loadingPromises: Map<string, LoadingPromise>;
|
|
128
129
|
};
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
import { Location, RouteItem, RouterState } from '../types';
|
|
2
|
-
|
|
1
|
+
import { LoaderState, Location, RouteItem, RouterState } from '../types';
|
|
2
|
+
type CommitState = {
|
|
3
|
+
location: Location;
|
|
4
|
+
loaderStateValue: LoaderState;
|
|
5
|
+
routeItem?: RouteItem;
|
|
6
|
+
};
|
|
7
|
+
export declare const createCommitState: ({ loaderState, routeItemDataState }: RouterState) => ({ routeItem, loaderStateValue, location }: CommitState) => void;
|
|
8
|
+
export {};
|
package/package.json
CHANGED