clear-react-router 2.6.0 → 2.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.6.0",
3
+ "version": "2.6.1",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: getting-started
3
- description: Set up and build with clear-react-router — provider-free routing for React SPAs: nested routes, loaders with per-path+search caching, URL search state, blocking, invalidation and prefetch. Route any AI task about this library here first.
3
+ description: Set up and build with clear-react-router — provider-free routing for React SPAs with nested routes, loaders cached per pathname+search, URL search state, blocking, invalidation and prefetch. Route any AI task about this library here first.
4
4
  metadata:
5
5
  type: framework
6
6
  library: 'clear-react-router'
@@ -8,19 +8,24 @@ metadata:
8
8
  sources:
9
9
  - https://github.com/AndrewBubnov/clear-react-router/blob/master/README.md
10
10
  - https://github.com/AndrewBubnov/clear-react-router/blob/master/src/playground/routes.tsx
11
- - https://github.com/AndrewBubnov/clear-react-router/blob/master/src/e2e-demo.tsx
11
+ - https://github.com/AndrewBubnov/clear-react-router/blob/master/src/clear-router/types.ts
12
12
  ---
13
13
 
14
14
  # clear-react-router
15
15
 
16
- A lightweight, provider-free routing library for client-side React SPAs. There is no
17
- provider and no React context: all state lives in a global store, components subscribe
18
- through hooks. One `<Router>` at the root, hooks anywhere.
16
+ A provider-free routing library for client-side React SPAs: nested routes, loaders
17
+ with caching, actions, navigation blockers, prefetching, scroll restoration and
18
+ view transitions. There is no provider and no React context: all state lives in a
19
+ global store, components subscribe through hooks. One `<Router>` at the root,
20
+ hooks anywhere.
19
21
 
20
22
  ## Setup
21
23
 
24
+ Routes are plain objects created by `createRouter`:
25
+
22
26
  ```tsx
23
- import { Router, createRouter, lazy, useLoaderState, Link } from 'clear-react-router';
27
+ // routes.tsx
28
+ import { Router, createRouter, lazy } from 'clear-react-router';
24
29
 
25
30
  const routes = createRouter([
26
31
  {
@@ -29,35 +34,86 @@ const routes = createRouter([
29
34
  loader: ({ params, searchParams, signal }) => fetchUsers(params, searchParams, signal),
30
35
  staleTime: 10_000, // cache entry lives this long
31
36
  loaderFallback: <PageSkeleton />, // rendered while status is 'pending'
32
- children: [{ path: '/:userId', element: <UserPage /> }], // paths are relative
37
+ layout: UsersLayout, // receives the rendered section as `children`
38
+ children: [{ path: '/:userId', element: <UserPage /> }], // child paths are relative
33
39
  },
34
40
  { path: '*', element: <NotFound /> }, // catch-all, keep it last
35
41
  ]);
36
42
 
43
+ // App.tsx
37
44
  createRoot(root).render(<Router routes={routes} />);
38
45
  ```
39
46
 
40
47
  ```tsx
41
- // Any component
48
+ // Any component — hooks read the global singleton, no prop drilling
49
+ import { useLoaderState, useNavigate, Link } from 'clear-react-router';
50
+
42
51
  const { data, loaderError, beforeLoadError } = useLoaderState<User[]>();
43
52
  const navigate = useNavigate();
44
53
  navigate({ pathname: '/users', search: '?sort=asc' });
54
+ navigate({ path: '/users/:userId', params: { userId: '42' } }); // typed template
55
+ <Link to="/users" prefetch="hover">Users</Link>;
45
56
  ```
46
57
 
58
+ ## Feature map
59
+
60
+ The whole surface, one line each — details live in the references (load by task
61
+ below):
62
+
63
+ - **Routing** — object routes with nested `children` (relative paths), `layout` /
64
+ `layouts` nesting (outermost-first, inherited), lazy `element`s, catch-all `*`,
65
+ typed path templates in `Link` (`path` + `params`) and `useNavigate`, active-link
66
+ matching with `exact`, custom element rendering via `as`.
67
+ - **Loader caching** — per `pathname + search` keys; `staleTime` (route) /
68
+ `defaultStaleTime`, `gcTime` + `maxCacheSize` (LRU eviction),
69
+ `revalidateOnFocus` / `revalidateOnReconnect` background revalidation,
70
+ `invalidate()` (current route, concrete paths, patterns, `withChildren`, `staleOnly`).
71
+ - **Retry** — `retry: { count, delay }` or a number, per route or `defaultRetry`;
72
+ attempts get an `attempt` counter; aborted flights never commit.
73
+ - **Polling** — `pollingInterval` re-runs the loader while the route is active; stops
74
+ on leave, restarts on return.
75
+ - **Loading UX** — `optimistic` (instant cache hit for revisited keys),
76
+ `keepPreviousData` (previous data held during pending, same element),
77
+ `loaderFallback` / `defaultLoaderFallback`, `minLoaderDuration` /
78
+ `defaultMinLoaderDuration` (no skeleton flashing), `errorElement` /
79
+ `defaultErrorElement`.
80
+ - **External data cache** — `externalCache` route flag (+ `defaultExternalCache`):
81
+ hand freshness to TanStack Query or similar; the router stores values but never
82
+ claims them fresh.
83
+ - **Search params** — `useSearchParams()`; every combination is a cached navigation
84
+ (see the dedicated reference — the data-table pattern).
85
+ - **Guards & hooks** — `beforeLoad` (redirect, signal), `afterLoad`,
86
+ `defaultBeforeLoad`, `defaultAfterLoad`, shared context via `useRouterContext`.
87
+ - **Blocking** — `useBlocker` for dirty forms: `unblocked` → `charged` → `blocked`,
88
+ then `process()` / `reset()`.
89
+ - **Actions** — route-owned mutations via `actions` + `useSubmitAction` /
90
+ `useAction`, with automatic invalidation of affected cache.
91
+ - **Prefetch** — `defaultPrefetch: 'hover' | 'viewport' | 'none'` (device-aware
92
+ default), per-link `prefetch` override and `hoverPrefetchDelay`, concurrency 4/2,
93
+ a prefetch never supersedes a running flight.
94
+ - **Scroll restoration** — `scrollRestoration` (window or named containers),
95
+ `defaultScrollRestorationBehavior` (`'auto' | 'smooth'`), `useRestoreScroll`.
96
+ - **View transitions** — `isAnimated` / `animationDuration` wrap navigations in
97
+ `startViewTransition`.
98
+ - **Cache observability** — `useCacheSnapshot()` + `enableCacheSnapshot` expose live
99
+ cache entries (path, freshness, reason) for devtools UIs.
100
+
47
101
  ## Essential constraints
48
102
 
49
103
  - **No context, no provider.** Hooks read a global singleton; render `<Router>` once.
50
- - **The loader is the data owner.** `loader` receives `{ params, searchParams, context,
51
- setContext, location, signal, attempt }` and returns anything serializable. Its result
52
- is cached per **`pathname + search`** — a different query string is a different entry.
53
- - **`beforeLoad` is for guards, not data.** Redirect via `redirect(location)` in its args;
54
- a thrown/rejected error lands in `beforeLoadError` and renders `errorElement`.
104
+ - **The loader is the data owner.** `loader` receives
105
+ `{ params, searchParams, context, setContext, location, signal, attempt }` and
106
+ returns anything serializable. Its result is cached per **`pathname + search`** —
107
+ a different query string is a different entry. `searchParams` is a plain object
108
+ (multi-value keys collapse; read `location.search` raw for repeated keys).
109
+ - **`beforeLoad` is for guards, not data.** Redirect via `redirect()` in its args; a
110
+ thrown/rejected error lands in `beforeLoadError` and renders `errorElement`.
55
111
  - **Search params are navigations.** `setSearchParams` performs a full navigation: it
56
- pushes a history entry and re-runs the loader for the new `pathname + search`. Never
57
- bypass it by writing to the store or calling `history.*` directly.
58
- - **Render during loading is a three-slot pipeline** (route-level opt-ins, see below):
59
- `optimistic` → `keepPreviousData` → `loaderFallback`. Errors always replace content
60
- with `errorElement`.
112
+ pushes a history entry and re-runs the loader for the new `pathname + search`.
113
+ Never bypass it by writing to the store or calling `history.*` directly.
114
+ - **Render during loading is a three-slot pipeline** (route-level opt-ins, see
115
+ below): `optimistic` → `keepPreviousData` → `loaderFallback`. Errors always
116
+ replace content with `errorElement`.
61
117
  - **Routes without a `loader` never have loader data.** Such routes commit
62
118
  `status: 'active'` with `data === undefined`; an element there must not read `data`.
63
119
  - **Aborts are automatic.** Each navigation has one signal, passed to the loader;
@@ -66,17 +122,20 @@ navigate({ pathname: '/users', search: '?sort=asc' });
66
122
 
67
123
  ## Load by task
68
124
 
69
- - Route config, nested routes, layouts, params, catch-all, lazy elements →
125
+ - Route config, nesting, layouts, params, catch-all, lazy components →
70
126
  [routes and loaders](references/routes-and-loaders.md)
71
127
  - Caching, `staleTime`/`gcTime`, loading UX (`optimistic`, `keepPreviousData`,
72
- `loaderFallback`, retry, polling) → [loaders and caching](references/routes-and-loaders.md)
128
+ `loaderFallback`, retry, polling, external cache) →
129
+ [routes and loaders](references/routes-and-loaders.md)
73
130
  - URL query state, filters/sort/pagination tables, multi-value params →
74
131
  [search params as state](references/search-params-state.md)
75
- - Navigation, blockers, invalidation, prefetch, actions →
132
+ - Navigation, blockers, invalidation, prefetch, actions, scroll →
76
133
  [navigation and blocking](references/navigation-blocking-invalidation.md)
77
134
 
78
135
  ## API discovery
79
136
 
80
- Inspect `src/clear-router/index.ts` (the only public entry point), then `src/clear-router/types.ts`
81
- for `RouteItem` and hook signatures. The playground (`src/playground/routes.tsx`) shows every
82
- feature wired end-to-end against a fake in-browser server.
137
+ Inspect `src/clear-router/index.ts` (the only public entry point), then
138
+ `src/clear-router/types.ts` for `RouteItem` and hook signatures. The playground
139
+ (`src/playground/routes.tsx`) shows every feature wired end-to-end against a fake
140
+ in-browser server; the search page (`src/playground/SearchPage.tsx`) is the
141
+ reference for URL-owned query state with TanStack Table.