clear-react-router 2.6.2 → 2.6.4

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 CHANGED
@@ -129,6 +129,7 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
129
129
  | `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback for the route's `loader`. Overrides the global `Router.defaultLoaderFallback` |
130
130
  | `retry` | `number \| { count: number; delay: number }` | Overrides the global cache revalidation retry policy for this route |
131
131
  | `optimistic` | `boolean \| undefined` | Instant navigation using stale data while fresh data is loaded in the background |
132
+ | `keepPreviousData` | `boolean \| undefined` | During `pending`, keep the previous loader data on screen instead of showing `loaderFallback` — for same-element navigations (e.g. table filters/sort/pagination). Replaces the previous data with `errorElement` if the new load fails; `optimistic` takes precedence when both are set |
132
133
  | `errorElement` | `ReactElement \| () => ReactElement \| LazyComponent` | Error fallback for the route. Overrides the global `Router.defaultErrorElement` |
133
134
  | `staleTime` | `number \| undefined` | Time in milliseconds before cached loader data is considered stale. Overrides Router.defaultStaleTime. If neither value is provided, data is stale immediately and navigation revalidates it. Use `Infinity` to never consider it stale |
134
135
  | `externalCache` | `boolean \| undefined` | The route's data freshness is owned by the loader's source (e.g. an external cache like TanStack Query): the router cache stores values but never claims freshness, so every navigation re-runs the loader. Inherited by children; a child can opt out with `false` |
@@ -138,6 +139,11 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
138
139
  | `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` |
139
140
  | `scrollRestorationBehavior` | `'auto' \| 'smooth' \| 'instant'` | Scroll restoration behavior |
140
141
 
142
+ > **Render during loading:** while a route is `pending`, the router shows, in order of precedence:
143
+ > `optimistic` (cached snapshot for revisited keys) → `keepPreviousData` (previous data of the same
144
+ > element — table filters, sort and pagination) → `loaderFallback`. An error always replaces the
145
+ > content with `errorElement`.
146
+
141
147
  `beforeLoad`, `loader` and `actions` receive:
142
148
 
143
149
  ```ts
@@ -814,15 +820,19 @@ type NavigationLocation = {
814
820
  pathname: string;
815
821
  search?: string | Record<string, string | number | boolean | null | undefined>;
816
822
  state?: unknown;
823
+ replace?: boolean; // rewrite the current history entry instead of pushing a new one
817
824
  };
818
825
 
819
826
  const navigate = useNavigate();
820
827
 
821
828
  navigate('/about'); // string
822
829
  navigate({ pathname: '/user/123', search: { user: 'Jane Doe' }, state: { from: 'home' } }); // NavigationLocation
830
+ navigate({ pathname: '/dashboard', replace: true }); // e.g. after login, so /login stays out of history
823
831
  navigate(-1); // go back
824
832
  ```
825
833
 
834
+ Redirects declared in `beforeLoad` (`redirect(...)`) always rewrite the current history entry — the user never saw the intermediate URL, so the Back button must not return into the redirect loop.
835
+
826
836
  **Note:** Navigation state can be accessed via [`useLocation()`](#uselocation):
827
837
 
828
838
  ```tsx
package/dist/index.js CHANGED
@@ -113,7 +113,7 @@ var handleBackgroundFailure = (loaderMap, path, error, routeItem, displayedData,
113
113
  };
114
114
  //#endregion
115
115
  //#region utils/commitState.ts
116
- var createCommitState = ({ routeDataState, blockerState }) => ({ routeItem, loaderStateValue, location }) => {
116
+ var createCommitState = ({ routeDataState, blockerState }) => ({ routeItem, loaderStateValue, location, replace }) => {
117
117
  const isError = loaderStateValue.loaderError || loaderStateValue.beforeLoadError;
118
118
  routeDataState.setState({
119
119
  routeItem,
@@ -123,7 +123,8 @@ var createCommitState = ({ routeDataState, blockerState }) => ({ routeItem, load
123
123
  });
124
124
  if (blockerState.getState() === "blocked") blockerState.setState("unblocked");
125
125
  if ((location.search ? `${location.pathname}${location.search}` : location.pathname) === window.location.pathname + window.location.search) return;
126
- history.pushState(location.state ?? null, "", `${location.pathname}${location.search}`);
126
+ if (replace) history.replaceState(location.state ?? null, "", `${location.pathname}${location.search}`);
127
+ else history.pushState(location.state ?? null, "", `${location.pathname}${location.search}`);
127
128
  };
128
129
  //#endregion
129
130
  //#region utils/commitNavigation.ts
@@ -303,12 +304,14 @@ var createNavigate = (routerState, revalidateCache, syncCacheSnapshot) => {
303
304
  return await navigate({
304
305
  pathname,
305
306
  search,
306
- prevLocation
307
+ prevLocation,
308
+ replace: true
307
309
  });
308
310
  }
309
311
  return await navigate({
310
312
  ...redirected,
311
- prevLocation
313
+ prevLocation,
314
+ replace: true
312
315
  });
313
316
  };
314
317
  try {
@@ -476,8 +479,9 @@ var createNavigate = (routerState, revalidateCache, syncCacheSnapshot) => {
476
479
  });
477
480
  };
478
481
  const navigate = async (rawLocation) => {
482
+ const { replace: replaceEntry, ...locationFields } = rawLocation;
479
483
  const nextLocation = {
480
- ...rawLocation,
484
+ ...locationFields,
481
485
  search: rawLocation.search ?? ""
482
486
  };
483
487
  if (checkBlocked(nextLocation)) return;
@@ -502,7 +506,8 @@ var createNavigate = (routerState, revalidateCache, syncCacheSnapshot) => {
502
506
  commitNavigation(() => commitState({
503
507
  location: nextLocation,
504
508
  routeItem: nextItem,
505
- loaderStateValue
509
+ loaderStateValue,
510
+ replace: replaceEntry === true
506
511
  }));
507
512
  clearCurrentGcTimeout(nextItem, nextLocation);
508
513
  afterLoad(nextItem, nextLocation, params);
@@ -1,2 +1,4 @@
1
1
  import { Location, RevalidateCache, RouterState } from '../types';
2
- export declare const createNavigate: (routerState: RouterState, revalidateCache: RevalidateCache, syncCacheSnapshot: () => void) => (rawLocation: Location) => Promise<void>;
2
+ export declare const createNavigate: (routerState: RouterState, revalidateCache: RevalidateCache, syncCacheSnapshot: () => void) => (rawLocation: Location & {
3
+ replace?: boolean;
4
+ }) => Promise<void>;
package/dist/types.d.ts CHANGED
@@ -179,7 +179,9 @@ export type RouterState = {
179
179
  };
180
180
  export type RouterType = {
181
181
  runtime: {
182
- navigate(arg: Location): Promise<void>;
182
+ navigate(arg: Location & {
183
+ replace?: boolean;
184
+ }): Promise<void>;
183
185
  invalidate(pathList?: string | string[], options?: InvalidateOptions): Promise<InvalidateResult[]>;
184
186
  prefetch(location: Location, signal?: AbortSignal): Promise<void>;
185
187
  syncCacheSnapshot(): void;
@@ -222,6 +224,8 @@ export type InvalidateResult = {
222
224
  export type SearchObject = Record<string, string | number | boolean | null | undefined>;
223
225
  export type NavigationLocation = Omit<Location, 'search'> & {
224
226
  search?: string | SearchObject;
227
+ /** Rewrite the current history entry instead of pushing a new one. */
228
+ replace?: boolean;
225
229
  };
226
230
  export type PathParams = Record<string, string | number>;
227
231
  export type ParamsFor<TPath extends string> = TPath extends `${string}:${infer Param}/${infer Rest}` ? {
@@ -275,6 +279,8 @@ export type NavigatePathInput = {
275
279
  params?: PathParams;
276
280
  search?: string | SearchObject;
277
281
  state?: unknown;
282
+ /** Rewrite the current history entry instead of pushing a new one. */
283
+ replace?: boolean;
278
284
  };
279
285
  export type Synchronizer = <T>(subscribe: (updater: () => void) => () => void, getSnapshot: () => T, getServerSnapshot?: () => T) => T;
280
286
  export {};
@@ -3,6 +3,7 @@ type CommitState = {
3
3
  location: Location;
4
4
  loaderStateValue: LoaderState;
5
5
  routeItem?: RouteItem;
6
+ replace?: boolean;
6
7
  };
7
- export declare const createCommitState: ({ routeDataState, blockerState }: RouterState) => ({ routeItem, loaderStateValue, location }: CommitState) => void;
8
+ export declare const createCommitState: ({ routeDataState, blockerState }: RouterState) => ({ routeItem, loaderStateValue, location, replace }: CommitState) => void;
8
9
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {
@@ -60,7 +60,9 @@ navigate({ path: '/users/:userId', params: { userId: '42' } }); // typed templat
60
60
  The whole surface, one line each — details live in the references (load by task
61
61
  below):
62
62
 
63
- - **Routing** — object routes with nested `children` (relative paths), `layout` /
63
+ - **Routing** — `replace?: boolean` on any navigation rewrites the current history
64
+ entry (post-login flows); redirects declared in `beforeLoad` always rewrite the
65
+ current entry, so Back never returns into a redirect loop. Object routes with nested `children` (relative paths), `layout` /
64
66
  `layouts` nesting (outermost-first, inherited), lazy `element`s, catch-all `*`,
65
67
  typed path templates in `Link` (`path` + `params`) and `useNavigate`, active-link
66
68
  matching with `exact`, custom element rendering via `as`.