@canonical/router-react 0.33.0 → 0.35.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.
- package/README.md +144 -105
- package/dist/esm/lib/Link/Link.js +5 -5
- package/dist/esm/lib/Link/Link.js.map +1 -1
- package/dist/esm/lib/hooks/useBlocker.js +15 -9
- package/dist/esm/lib/hooks/useBlocker.js.map +1 -1
- package/dist/esm/lib/index.js +1 -1
- package/dist/esm/lib/index.js.map +1 -1
- package/dist/esm/lib/readDehydratedState.js +46 -0
- package/dist/esm/lib/readDehydratedState.js.map +1 -0
- package/dist/types/lib/Link/Link.d.ts +2 -2
- package/dist/types/lib/Link/types.d.ts +2 -2
- package/dist/types/lib/Link/types.d.ts.map +1 -1
- package/dist/types/lib/hooks/useBlocker.d.ts +3 -0
- package/dist/types/lib/hooks/useBlocker.d.ts.map +1 -1
- package/dist/types/lib/index.d.ts +1 -1
- package/dist/types/lib/index.d.ts.map +1 -1
- package/dist/types/lib/readDehydratedState.d.ts +23 -0
- package/dist/types/lib/readDehydratedState.d.ts.map +1 -0
- package/dist/types/lib/types.d.ts +1 -8
- package/dist/types/lib/types.d.ts.map +1 -1
- package/package.json +10 -10
- package/dist/esm/lib/createHydratedRouter.js +0 -29
- package/dist/esm/lib/createHydratedRouter.js.map +0 -1
- package/dist/types/lib/createHydratedRouter.d.ts +0 -15
- package/dist/types/lib/createHydratedRouter.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
React bindings for `@canonical/router-core`. `@canonical/router-react` turns a core router into React context, hooks, links, outlets, and SSR helpers while preserving the flat-route model from the core package.
|
|
4
4
|
|
|
5
|
+
> **Stability: pre-1.0 / experimental.** The API is still consolidating and breaking changes may land between minor versions; the CHANGELOG and conventional-commit history are the migration record. The design positions behind the API are documented in the core package's [Design rationale](../../runtime/router/README.md#design-rationale).
|
|
6
|
+
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
```bash
|
|
@@ -12,10 +14,14 @@ Requires `react` and `react-dom`.
|
|
|
12
14
|
|
|
13
15
|
## Quick start
|
|
14
16
|
|
|
15
|
-
### 1. Define routes in core
|
|
17
|
+
### 1. Define routes and a router in core
|
|
16
18
|
|
|
17
19
|
```tsx
|
|
18
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
createBrowserAdapter,
|
|
22
|
+
createRouter,
|
|
23
|
+
route,
|
|
24
|
+
} from "@canonical/router-core";
|
|
19
25
|
|
|
20
26
|
export const routes = {
|
|
21
27
|
home: route({
|
|
@@ -28,7 +34,9 @@ export const routes = {
|
|
|
28
34
|
}),
|
|
29
35
|
} as const;
|
|
30
36
|
|
|
31
|
-
export const router = createRouter(routes
|
|
37
|
+
export const router = createRouter(routes, {
|
|
38
|
+
adapter: createBrowserAdapter(),
|
|
39
|
+
});
|
|
32
40
|
```
|
|
33
41
|
|
|
34
42
|
`@canonical/router-core` owns route definitions, matching, and typed navigation. `@canonical/router-react` layers React rendering and subscriptions on top.
|
|
@@ -37,8 +45,8 @@ Route authoring story, in short:
|
|
|
37
45
|
|
|
38
46
|
- define every route with `route()`
|
|
39
47
|
- give it a `url` pattern such as `/docs/:slug`
|
|
40
|
-
- optionally add `
|
|
41
|
-
- create one router from the full flat route map
|
|
48
|
+
- optionally add `warm`, `content`, and `wrappers`
|
|
49
|
+
- create one router from the full flat route map, with an adapter
|
|
42
50
|
- let the router match incoming URLs — React renders the result
|
|
43
51
|
|
|
44
52
|
### 2. Provide the router and render the current match
|
|
@@ -64,7 +72,6 @@ import {
|
|
|
64
72
|
Link,
|
|
65
73
|
useNavigationState,
|
|
66
74
|
useRoute,
|
|
67
|
-
useRouterState,
|
|
68
75
|
useSearchParam,
|
|
69
76
|
useSearchParams,
|
|
70
77
|
} from "@canonical/router-react";
|
|
@@ -72,7 +79,6 @@ import {
|
|
|
72
79
|
function Navigation() {
|
|
73
80
|
const navigationState = useNavigationState();
|
|
74
81
|
const location = useRoute();
|
|
75
|
-
const status = useRouterState((state) => state.match?.status ?? 404);
|
|
76
82
|
const tab = useSearchParam("tab");
|
|
77
83
|
const search = useSearchParams();
|
|
78
84
|
|
|
@@ -83,7 +89,7 @@ function Navigation() {
|
|
|
83
89
|
Docs
|
|
84
90
|
</Link>
|
|
85
91
|
<span>{navigationState}</span>
|
|
86
|
-
<span>{status}</span>
|
|
92
|
+
<span>{location.status}</span>
|
|
87
93
|
<span>{location.pathname}</span>
|
|
88
94
|
<span>{tab ?? "overview"}</span>
|
|
89
95
|
<span>{search.toString()}</span>
|
|
@@ -97,7 +103,7 @@ All hooks and `Link` default to `RegisteredRouteMap`. Register your route map on
|
|
|
97
103
|
```ts
|
|
98
104
|
declare module "@canonical/router-react" {
|
|
99
105
|
interface RouterRegister {
|
|
100
|
-
routes: typeof
|
|
106
|
+
routes: typeof routes;
|
|
101
107
|
}
|
|
102
108
|
}
|
|
103
109
|
```
|
|
@@ -117,12 +123,12 @@ Important distinction:
|
|
|
117
123
|
|
|
118
124
|
The router does not own data. `content()` receives `params` and `search` — not data. Components fetch their own data from their cache library (Relay, TanStack Query, SWR, etc.).
|
|
119
125
|
|
|
120
|
-
The optional `
|
|
126
|
+
The optional `warm()` on routes is a fire-and-forget navigation-time hook. Use it to warm caches, preload assets, or run side effects before the component renders. It does not pass data to `content()` — the component's cache hook finds the data already warm. (Why this is the design, and why loader-data-as-props is rejected, is spelled out in the core [Design rationale](../../runtime/router/README.md#design-rationale).)
|
|
121
127
|
|
|
122
128
|
```tsx
|
|
123
129
|
const userRoute = route({
|
|
124
130
|
url: "/users/:id",
|
|
125
|
-
|
|
131
|
+
warm: async ({ id }) => {
|
|
126
132
|
await queryClient.prefetchQuery(["user", id], () => fetchUser(id));
|
|
127
133
|
},
|
|
128
134
|
content: ({ params }) => <UserProfile id={params.id} />,
|
|
@@ -136,34 +142,80 @@ function UserProfile({ id }: { id: string }) {
|
|
|
136
142
|
|
|
137
143
|
## Error handling
|
|
138
144
|
|
|
139
|
-
|
|
145
|
+
Errors travel through two separate channels. Knowing which one you are in tells you which tool to reach for.
|
|
146
|
+
|
|
147
|
+
### Channel 1 — data errors are state, not exceptions
|
|
148
|
+
|
|
149
|
+
The router commits every data failure as a status on the location: an unmatched URL is a 404, a rejected search schema is a 400, a `StatusResponse`/`Response` thrown from `warm()` carries its own status, and any other throw during a load becomes a 500. **None of these are thrown into React** — a `StatusResponse` thrown during a load never reaches an error boundary; it becomes `location.status`.
|
|
150
|
+
|
|
151
|
+
The bridge into React is one property read: `useRoute().status`. The location object is tracked, so a component reading `status` re-renders only when the status changes:
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
import { useRoute } from "@canonical/router-react";
|
|
155
|
+
|
|
156
|
+
function RouteStatusGate({ children }: { children: React.ReactNode }) {
|
|
157
|
+
const { status } = useRoute();
|
|
158
|
+
|
|
159
|
+
if (status === 404) return <NotFoundPage />;
|
|
160
|
+
if (status === 401) return <LoginPrompt />;
|
|
161
|
+
if (status >= 400) return <ErrorPage status={status} />;
|
|
140
162
|
|
|
141
|
-
|
|
163
|
+
return <>{children}</>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Wrap the outlet once, near the top of the tree:
|
|
167
|
+
// <RouterProvider router={router}>
|
|
168
|
+
// <RouteStatusGate>
|
|
169
|
+
// <Outlet />
|
|
170
|
+
// </RouteStatusGate>
|
|
171
|
+
// </RouterProvider>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Channel 2 — render errors belong to React
|
|
175
|
+
|
|
176
|
+
A component that throws while rendering (including a suspense cache read that rejects) is React's concern, handled by an ordinary error boundary. The router deliberately does not ship one — error UI is application-specific. A complete boundary, inline:
|
|
142
177
|
|
|
143
178
|
```tsx
|
|
144
|
-
import {
|
|
179
|
+
import { useRoute } from "@canonical/router-react";
|
|
180
|
+
import { Component, type ReactNode } from "react";
|
|
181
|
+
|
|
182
|
+
interface ErrorBoundaryProps {
|
|
183
|
+
readonly children: ReactNode;
|
|
184
|
+
readonly fallback: (error: unknown) => ReactNode;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
interface ErrorBoundaryState {
|
|
188
|
+
readonly error: unknown | null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
192
|
+
state: ErrorBoundaryState = { error: null };
|
|
145
193
|
|
|
146
|
-
|
|
147
|
-
|
|
194
|
+
static getDerivedStateFromError(error: unknown): ErrorBoundaryState {
|
|
195
|
+
return { error };
|
|
196
|
+
}
|
|
148
197
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
198
|
+
render(): ReactNode {
|
|
199
|
+
return this.state.error !== null
|
|
200
|
+
? this.props.fallback(this.state.error)
|
|
201
|
+
: this.props.children;
|
|
202
|
+
}
|
|
152
203
|
}
|
|
153
204
|
|
|
154
|
-
|
|
205
|
+
// A plain boundary sticks in its error state after the user navigates away.
|
|
206
|
+
// Key it by the location so navigation remounts it fresh:
|
|
207
|
+
function RouteErrorGate({ children }: { children: ReactNode }) {
|
|
208
|
+
const { href } = useRoute();
|
|
209
|
+
|
|
155
210
|
return (
|
|
156
|
-
<
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
{children}
|
|
160
|
-
</ErrorBoundary>
|
|
161
|
-
</div>
|
|
211
|
+
<ErrorBoundary key={href} fallback={(error) => <CrashPage error={error} />}>
|
|
212
|
+
{children}
|
|
213
|
+
</ErrorBoundary>
|
|
162
214
|
);
|
|
163
215
|
}
|
|
164
216
|
```
|
|
165
217
|
|
|
166
|
-
|
|
218
|
+
Compose the two channels in one place — status gate outside, render boundary inside — and every route gets both behaviors for free.
|
|
167
219
|
|
|
168
220
|
## Search param mutation
|
|
169
221
|
|
|
@@ -192,7 +244,7 @@ function FilterBar() {
|
|
|
192
244
|
|
|
193
245
|
## Navigation blocking
|
|
194
246
|
|
|
195
|
-
`useBlocker()` prevents navigation when the component has unsaved state. The hook returns a state object — the consumer controls the confirmation UI:
|
|
247
|
+
`useBlocker()` prevents navigation when the component has unsaved state. The hook returns a state object — the consumer controls the confirmation UI, and the hook re-renders the moment a navigation is blocked:
|
|
196
248
|
|
|
197
249
|
```tsx
|
|
198
250
|
import { useBlocker } from "@canonical/router-react";
|
|
@@ -254,7 +306,7 @@ function QuickEditForm() {
|
|
|
254
306
|
}
|
|
255
307
|
```
|
|
256
308
|
|
|
257
|
-
The blocker registers on mount and
|
|
309
|
+
The blocker registers on mount and disposes on unmount. Unmounting while a navigation is blocked **discards** that navigation (it is not resumed). Blockers intercept `router.navigate()` only — `setSearchParams()` and browser back/forward are not intercepted.
|
|
258
310
|
|
|
259
311
|
## Creating routes and matching URLs
|
|
260
312
|
|
|
@@ -263,7 +315,11 @@ The blocker registers on mount and unregisters on unmount — when the form is s
|
|
|
263
315
|
Each entry in the route map is a named call to `route()`.
|
|
264
316
|
|
|
265
317
|
```tsx
|
|
266
|
-
import {
|
|
318
|
+
import {
|
|
319
|
+
createBrowserAdapter,
|
|
320
|
+
createRouter,
|
|
321
|
+
route,
|
|
322
|
+
} from "@canonical/router-core";
|
|
267
323
|
|
|
268
324
|
const routes = {
|
|
269
325
|
home: route({
|
|
@@ -272,7 +328,7 @@ const routes = {
|
|
|
272
328
|
}),
|
|
273
329
|
docs: route({
|
|
274
330
|
url: "/docs/:slug",
|
|
275
|
-
|
|
331
|
+
warm: async ({ slug }) => {
|
|
276
332
|
await queryClient.prefetchQuery(["doc", slug], () => fetchDoc(slug));
|
|
277
333
|
},
|
|
278
334
|
content: ({ params }) => <DocPage slug={params.slug} />,
|
|
@@ -283,7 +339,7 @@ const routes = {
|
|
|
283
339
|
}),
|
|
284
340
|
} as const;
|
|
285
341
|
|
|
286
|
-
const router = createRouter(routes);
|
|
342
|
+
const router = createRouter(routes, { adapter: createBrowserAdapter() });
|
|
287
343
|
```
|
|
288
344
|
|
|
289
345
|
Important parts:
|
|
@@ -291,101 +347,80 @@ Important parts:
|
|
|
291
347
|
- the route-map key such as `docs` is the typed navigation name used by `Link` and `router.navigate()`
|
|
292
348
|
- the `url` string is the matcher used for incoming URLs
|
|
293
349
|
- `:slug` segments become typed route params
|
|
294
|
-
- `
|
|
350
|
+
- `warm` runs at navigation time as a fire-and-forget hook
|
|
295
351
|
- `content` renders the matched route, receiving `params` and `search`
|
|
296
352
|
|
|
297
353
|
Routes stay flat even when the UI is nested. Shared layout lives in wrappers from the core package, not in a nested route tree.
|
|
298
354
|
|
|
299
|
-
## SSR
|
|
355
|
+
## SSR and hydration
|
|
356
|
+
|
|
357
|
+
There is one SSR story: the server serializes the router's dehydrated state into the page's `__INITIAL_DATA__` payload (the `INITIAL_DATA_KEY` contract shared with `@canonical/react-ssr`), and the client reads it back with `readDehydratedState()`.
|
|
300
358
|
|
|
301
359
|
### Server side
|
|
302
360
|
|
|
303
|
-
|
|
361
|
+
`renderToStream(router, url)` loads the URL into a server-adapter router, streams the matched React output, and hands back everything the response needs:
|
|
304
362
|
|
|
305
363
|
```tsx
|
|
306
|
-
import {
|
|
307
|
-
import {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
} else if (router.match.kind === "redirect") {
|
|
319
|
-
return res.redirect(router.match.status, router.match.redirectTo);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
const { pipe } = renderToPipeableStream(
|
|
323
|
-
<HeadProvider collector={headCollector}>
|
|
324
|
-
<RouterProvider router={router}>
|
|
325
|
-
<Shell>
|
|
326
|
-
<Outlet />
|
|
327
|
-
</Shell>
|
|
328
|
-
</RouterProvider>
|
|
329
|
-
</HeadProvider>,
|
|
330
|
-
{
|
|
331
|
-
onShellReady() {
|
|
332
|
-
const headHtml = headCollector.toHtml();
|
|
333
|
-
res.setHeader("content-type", "text/html");
|
|
334
|
-
res.write(`<!doctype html><html><head>${headHtml}</head><body>`);
|
|
335
|
-
pipe(res);
|
|
336
|
-
},
|
|
337
|
-
},
|
|
364
|
+
import { createRouter, createServerAdapter } from "@canonical/router-core";
|
|
365
|
+
import { renderToStream } from "@canonical/router-react";
|
|
366
|
+
|
|
367
|
+
app.get("*", async (req, res) => {
|
|
368
|
+
const router = createRouter(routes, {
|
|
369
|
+
adapter: createServerAdapter(req.url),
|
|
370
|
+
notFound: notFoundRoute,
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const { stream, loadResult, bootstrapScriptContent } = await renderToStream(
|
|
374
|
+
router,
|
|
375
|
+
req.url,
|
|
338
376
|
);
|
|
377
|
+
|
|
378
|
+
res.status(loadResult.status); // 200, 404 for not-found, error statuses
|
|
379
|
+
// Inject `bootstrapScriptContent` into the HTML (an inline <script> that
|
|
380
|
+
// sets window.__INITIAL_DATA__), then send the stream.
|
|
339
381
|
});
|
|
340
382
|
```
|
|
341
383
|
|
|
342
|
-
|
|
384
|
+
When you integrate with `@canonical/react-ssr`'s `JSXRenderer` instead (as the reference app does), spread the dehydrated fields — `{ href, kind, routeId, status }` from `router.dehydrate()` — flat into the initial-data object next to your own fields, and pass the document's status through the renderer's `statusCode` option.
|
|
343
385
|
|
|
344
|
-
|
|
345
|
-
// In onShellReady or after render:
|
|
346
|
-
const routerState = router.dehydrate();
|
|
347
|
-
const cacheState = queryClient.dehydrate(); // TanStack Query, Relay, etc.
|
|
348
|
-
|
|
349
|
-
// Inject both into the HTML template
|
|
350
|
-
res.write(`<script>
|
|
351
|
-
window.__ROUTER_STATE__ = ${JSON.stringify(routerState)};
|
|
352
|
-
window.__QUERY_DATA__ = ${JSON.stringify(cacheState)};
|
|
353
|
-
</script>`);
|
|
354
|
-
```
|
|
386
|
+
The router dehydrates **navigation state only**. Data dehydration is the cache library's responsibility.
|
|
355
387
|
|
|
356
388
|
### Client side
|
|
357
389
|
|
|
390
|
+
`readDehydratedState()` reads `window.__INITIAL_DATA__`, validates that it actually carries router fields, and returns `null` otherwise — so the same entry works on server-rendered pages (resume the server match, skip the duplicate initial load) and in SPA builds (normal initial load):
|
|
391
|
+
|
|
358
392
|
```tsx
|
|
359
|
-
import {
|
|
360
|
-
import {
|
|
361
|
-
|
|
393
|
+
import { createBrowserAdapter, createRouter } from "@canonical/router-core";
|
|
394
|
+
import {
|
|
395
|
+
Outlet,
|
|
396
|
+
readDehydratedState,
|
|
397
|
+
RouterProvider,
|
|
398
|
+
} from "@canonical/router-react";
|
|
399
|
+
import { hydrateRoot } from "react-dom/client";
|
|
362
400
|
|
|
363
|
-
const router =
|
|
364
|
-
|
|
401
|
+
const router = createRouter(routes, {
|
|
402
|
+
adapter: createBrowserAdapter(),
|
|
403
|
+
hydratedState: readDehydratedState() ?? undefined,
|
|
365
404
|
});
|
|
366
405
|
|
|
367
406
|
hydrateRoot(
|
|
368
|
-
document
|
|
369
|
-
<
|
|
370
|
-
<
|
|
371
|
-
|
|
372
|
-
<Outlet />
|
|
373
|
-
</Shell>
|
|
374
|
-
</RouterProvider>
|
|
375
|
-
</HeadProvider>,
|
|
407
|
+
document.getElementById("root")!,
|
|
408
|
+
<RouterProvider router={router}>
|
|
409
|
+
<Outlet />
|
|
410
|
+
</RouterProvider>,
|
|
376
411
|
);
|
|
377
412
|
```
|
|
378
413
|
|
|
379
|
-
`
|
|
414
|
+
`createBrowserAdapter` uses the Navigation API when available (Baseline Newly Available since January 2026), falling back to the History API.
|
|
380
415
|
|
|
381
416
|
## Progressive disclosure
|
|
382
417
|
|
|
383
418
|
### `Link`
|
|
384
419
|
|
|
385
|
-
`Link` builds typed hrefs from route names and optional route params, search data, and hash values.
|
|
420
|
+
`Link` builds typed hrefs from route names and optional route params, search data, and hash values. A plain primary-button click is intercepted and routed through `router.navigate()`; the click falls through to native anchor behaviour when it has a modifier key, a non-primary button, `target="_blank"`, a `download` attribute, or was `preventDefault()`ed. Hovering warms the destination through `router.warm()`.
|
|
386
421
|
|
|
387
422
|
```tsx
|
|
388
|
-
<Link
|
|
423
|
+
<Link params={{ slug: "api" }} to="docs">
|
|
389
424
|
API docs
|
|
390
425
|
</Link>
|
|
391
426
|
```
|
|
@@ -402,7 +437,7 @@ hydrateRoot(
|
|
|
402
437
|
|
|
403
438
|
- `useBlocker(isActive)` blocks navigation when `isActive` is `true`. Returns `{ state, proceed, cancel }`.
|
|
404
439
|
- `useNavigationState()` subscribes to the router loading state.
|
|
405
|
-
- `useRoute()` returns a tracked location proxy and rerenders only when an accessed location key changes
|
|
440
|
+
- `useRoute()` returns a tracked location proxy and rerenders only when an accessed location key changes — including `status`.
|
|
406
441
|
- `useRouter()` returns the router instance from context. Use `useRouter().setSearchParams()` for search param mutation.
|
|
407
442
|
- `useRouterState()` is the power-user hook for subscribing to selected slices of `router.getState()`.
|
|
408
443
|
- `useSearchParam()` subscribes to one query-string key.
|
|
@@ -414,7 +449,7 @@ Typical selection strategy:
|
|
|
414
449
|
- reach for `useNavigationState()` when you only need loading lifecycle
|
|
415
450
|
- reach for `useSearchParam()` for one query-string key
|
|
416
451
|
- reach for `useSearchParams()` for a fixed key set or the full query string
|
|
417
|
-
- reach for `useRoute()` for pathname, hash, or full URL reads
|
|
452
|
+
- reach for `useRoute()` for pathname, hash, status, or full URL reads
|
|
418
453
|
- reach for `useRouter()` for search param mutation or direct router access
|
|
419
454
|
- reach for `useRouterState()` when you need `match`, `navigation`, or other advanced state in one selector
|
|
420
455
|
|
|
@@ -422,21 +457,23 @@ Typical selection strategy:
|
|
|
422
457
|
|
|
423
458
|
The reference integration lives in [apps/react/boilerplate-vite](../../../apps/react/boilerplate-vite). It shows:
|
|
424
459
|
|
|
425
|
-
- domain-colocated route modules
|
|
426
|
-
- a shell-as-
|
|
427
|
-
- SSR
|
|
428
|
-
-
|
|
429
|
-
- auth middleware redirect flow
|
|
430
|
-
-
|
|
460
|
+
- domain-colocated route modules assembled into one flat map
|
|
461
|
+
- a shell-as-layout wrapper applied with `group()`
|
|
462
|
+
- SSR with real HTTP statuses and redirects (`resolveRouteDisposition` in its server entry) and hydration via `readDehydratedState()`
|
|
463
|
+
- a route-level `warm` hook priming the Relay store, read by the component's own query hook
|
|
464
|
+
- an auth middleware redirect flow
|
|
465
|
+
- a static redirect route answering 301
|
|
431
466
|
|
|
432
467
|
## Public API
|
|
433
468
|
|
|
434
469
|
### Components and helpers
|
|
435
470
|
|
|
436
|
-
- `
|
|
437
|
-
- `Link` — render a typed anchor that navigates and prefetches through the router.
|
|
471
|
+
- `Link` — render a typed anchor that navigates and warms through the router.
|
|
438
472
|
- `Outlet` — render the current matched subtree.
|
|
439
473
|
- `RouterProvider` — place a router instance into React context.
|
|
474
|
+
- `readDehydratedState()` — read (and validate) dehydrated router state from the page's initial-data payload.
|
|
475
|
+
- `renderToStream()` — load a URL into a router and stream the matched React output with dehydrated state and a bootstrap script.
|
|
476
|
+
- `RouterRegister` / `RegisteredRouteMap` / `RegisteredNotFound` — module-augmentation hooks for generics-free typing.
|
|
440
477
|
- `useBlocker()` — block navigation when the component has unsaved state.
|
|
441
478
|
- `useNavigationState()` — subscribe to the navigation lifecycle state.
|
|
442
479
|
- `useRoute()` — subscribe to a tracked location object.
|
|
@@ -447,4 +484,6 @@ The reference integration lives in [apps/react/boilerplate-vite](../../../apps/r
|
|
|
447
484
|
|
|
448
485
|
### Reference docs
|
|
449
486
|
|
|
487
|
+
- API reference: [docs/references/ROUTER_API.md](../../../docs/references/ROUTER_API.md)
|
|
450
488
|
- Migration guide: [docs/how-to-guides/MIGRATE_TO_PRAGMA_ROUTER.md](../../../docs/how-to-guides/MIGRATE_TO_PRAGMA_ROUTER.md)
|
|
489
|
+
- Design rationale: [packages/runtime/router/README.md](../../runtime/router/README.md#design-rationale)
|
|
@@ -13,8 +13,8 @@ function buildOptionsObject(options) {
|
|
|
13
13
|
*
|
|
14
14
|
* `Link` builds the destination `href` from a typed route name plus optional
|
|
15
15
|
* params, search values, and hash. Primary-button clicks are intercepted and
|
|
16
|
-
* routed through `router.navigate()`, while hover
|
|
17
|
-
* route data through `router.
|
|
16
|
+
* routed through `router.navigate()`, while hover warms the destination
|
|
17
|
+
* route data through `router.warm()`.
|
|
18
18
|
*
|
|
19
19
|
* Sets `aria-current="page"` when the link target matches the current location.
|
|
20
20
|
*/
|
|
@@ -23,7 +23,7 @@ const Link = forwardRef(function Link({ children, download, hash, onClick, onMou
|
|
|
23
23
|
const location = useRoute();
|
|
24
24
|
const buildPath = router.buildPath;
|
|
25
25
|
const navigate = router.navigate;
|
|
26
|
-
const
|
|
26
|
+
const warm = router.warm;
|
|
27
27
|
const buildOptions = buildOptionsObject({
|
|
28
28
|
hash,
|
|
29
29
|
params,
|
|
@@ -57,10 +57,10 @@ const Link = forwardRef(function Link({ children, download, hash, onClick, onMou
|
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
59
|
if (hasOptions) {
|
|
60
|
-
void
|
|
60
|
+
void warm(to, buildOptions);
|
|
61
61
|
}
|
|
62
62
|
else {
|
|
63
|
-
void
|
|
63
|
+
void warm(to);
|
|
64
64
|
}
|
|
65
65
|
}, ref: forwardedRef, target: target, children: children }));
|
|
66
66
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.js","sourceRoot":"","sources":["../../../../src/lib/Link/Link.tsx"],"names":[],"mappings":";AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,QAAQ,MAAM,sBAAsB,CAAC;AAC5C,OAAO,SAAS,MAAM,uBAAuB,CAAC;AAI9C,SAAS,cAAc,CAAC,KAAoC;IAC1D,OAAO,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,QAAQ,CAAC;AAC1E,CAAC;AAED,SAAS,kBAAkB,CACzB,OAAmC;IAEnC,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK,SAA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BACf,QAAQ,CACN,EAAE,EACF,YAAyD,CAC1D,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,EAAE,CAAC,CAAC;YACf,CAAC;QACH,CAAC,EACD,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE;YACtB,YAAY,EAAE,CAAC,KAAK,CAAC,CAAC;YAEtB,IAAI,KAAK,CAAC,gBAAgB,EAAE,CAAC;gBAC3B,OAAO;YACT,CAAC;YAED,IAAI,UAAU,EAAE,CAAC;gBACf,KAAK,IAAI,CACP,EAAE,EACF,YAAyD,CAC1D,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC;YAChB,CAAC;QACH,CAAC,EACD,GAAG,EAAE,YAAY,EACjB,MAAM,EAAE,MAAM,YAEb,QAAQ,GACP,CACL,CAAC;AACJ,CAAC,CAKgB,CAAC;AAElB,eAAe,IAAI,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useEffect,
|
|
1
|
+
import { useCallback, useEffect, useRef, useState, useSyncExternalStore, } from "react";
|
|
2
2
|
import useRouter from "./useRouter.js";
|
|
3
3
|
/**
|
|
4
4
|
* Block navigation when the component has unsaved state.
|
|
@@ -7,6 +7,9 @@ import useRouter from "./useRouter.js";
|
|
|
7
7
|
* continues the blocked navigation, and `cancel()` stays on the page.
|
|
8
8
|
* The consumer controls the confirmation UI.
|
|
9
9
|
*
|
|
10
|
+
* Unmounting while blocked disposes the blocker and discards the pending
|
|
11
|
+
* navigation (it is not resumed).
|
|
12
|
+
*
|
|
10
13
|
* ```tsx
|
|
11
14
|
* const blocker = useBlocker(isDirty);
|
|
12
15
|
*
|
|
@@ -20,23 +23,26 @@ import useRouter from "./useRouter.js";
|
|
|
20
23
|
*/
|
|
21
24
|
export default function useBlocker(isActive) {
|
|
22
25
|
const router = useRouter();
|
|
23
|
-
const
|
|
26
|
+
const isActiveRef = useRef(isActive);
|
|
27
|
+
const [handle, setHandle] = useState(null);
|
|
28
|
+
isActiveRef.current = isActive;
|
|
24
29
|
useEffect(() => {
|
|
25
|
-
|
|
30
|
+
const nextHandle = router.block(() => isActiveRef.current);
|
|
31
|
+
setHandle(nextHandle);
|
|
26
32
|
return () => {
|
|
27
|
-
|
|
33
|
+
nextHandle.dispose();
|
|
28
34
|
};
|
|
29
|
-
}, [router
|
|
30
|
-
const subscribe = useCallback((onStoreChange) =>
|
|
31
|
-
const getSnapshot = useCallback(() =>
|
|
35
|
+
}, [router]);
|
|
36
|
+
const subscribe = useCallback((onStoreChange) => handle ? handle.subscribe(onStoreChange) : () => { }, [handle]);
|
|
37
|
+
const getSnapshot = useCallback(() => handle?.state ?? "idle", [handle]);
|
|
32
38
|
const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
33
39
|
return {
|
|
34
40
|
state,
|
|
35
41
|
proceed() {
|
|
36
|
-
|
|
42
|
+
handle?.proceed();
|
|
37
43
|
},
|
|
38
44
|
cancel() {
|
|
39
|
-
|
|
45
|
+
handle?.cancel();
|
|
40
46
|
},
|
|
41
47
|
};
|
|
42
48
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBlocker.js","sourceRoot":"","sources":["../../../../src/lib/hooks/useBlocker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useBlocker.js","sourceRoot":"","sources":["../../../../src/lib/hooks/useBlocker.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,SAAS,EACT,MAAM,EACN,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AACf,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAQvC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,QAAiB;IAClD,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;IACrC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAA6B,IAAI,CAAC,CAAC;IAEvE,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;IAE/B,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAE3D,SAAS,CAAC,UAAU,CAAC,CAAC;QAEtB,OAAO,GAAG,EAAE;YACV,UAAU,CAAC,OAAO,EAAE,CAAC;QACvB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,aAAyB,EAAE,EAAE,CAC5B,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,GAAE,CAAC,EACrD,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,KAAK,IAAI,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEzE,MAAM,KAAK,GAAG,oBAAoB,CAAC,SAAS,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;IAExE,OAAO;QACL,KAAK;QACL,OAAO;YACL,MAAM,EAAE,OAAO,EAAE,CAAC;QACpB,CAAC;QACD,MAAM;YACJ,MAAM,EAAE,MAAM,EAAE,CAAC;QACnB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/esm/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { default as createHydratedRouter } from "./createHydratedRouter.js";
|
|
2
1
|
export * from "./hooks/index.js";
|
|
3
2
|
export * from "./Link/index.js";
|
|
4
3
|
export * from "./Outlet/index.js";
|
|
5
4
|
export * from "./RouterProvider/index.js";
|
|
5
|
+
export { default as readDehydratedState } from "./readDehydratedState.js";
|
|
6
6
|
export * from "./register.js";
|
|
7
7
|
export { default as renderToStream } from "./renderToStream.js";
|
|
8
8
|
export * from "./types.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC1E,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAChE,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { INITIAL_DATA_KEY } from "@canonical/react-ssr/renderer/constants";
|
|
2
|
+
/**
|
|
3
|
+
* Read the router's dehydrated SSR state from the initial-data payload.
|
|
4
|
+
*
|
|
5
|
+
* The server serializes `router.dehydrate()`'s fields into the page's
|
|
6
|
+
* `__INITIAL_DATA__` payload; passing the result to `createRouter()` as
|
|
7
|
+
* `hydratedState` resumes from the server-rendered match instead of
|
|
8
|
+
* reloading it:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const router = createRouter(routes, {
|
|
12
|
+
* adapter: createBrowserAdapter(),
|
|
13
|
+
* hydratedState: readDehydratedState() ?? undefined,
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Returns `null` when no payload is present or when the payload does not
|
|
18
|
+
* carry dehydrated router state (e.g. an initial-data object without router
|
|
19
|
+
* fields) — the router then performs a normal initial load.
|
|
20
|
+
*/
|
|
21
|
+
export default function readDehydratedState(browserWindow = window) {
|
|
22
|
+
const candidate = browserWindow[INITIAL_DATA_KEY];
|
|
23
|
+
if (typeof candidate !== "object" || candidate === null) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const state = candidate;
|
|
27
|
+
if (typeof state.href !== "string" ||
|
|
28
|
+
typeof state.status !== "number" ||
|
|
29
|
+
(state.kind !== "route" &&
|
|
30
|
+
state.kind !== "not-found" &&
|
|
31
|
+
state.kind !== "unmatched")) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (state.kind === "route" && typeof state.routeId !== "string") {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
// Return a normalized copy so extra payload keys (url, theme, …) and any
|
|
38
|
+
// malformed routeId on non-route kinds never reach hydrate().
|
|
39
|
+
return {
|
|
40
|
+
href: state.href,
|
|
41
|
+
kind: state.kind,
|
|
42
|
+
routeId: state.kind === "route" ? state.routeId : null,
|
|
43
|
+
status: state.status,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=readDehydratedState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readDehydratedState.js","sourceRoot":"","sources":["../../../src/lib/readDehydratedState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yCAAyC,CAAC;AAI3E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAGzC,gBAAiC,MAAoC;IAErE,MAAM,SAAS,GAAI,aAAyC,CAC1D,gBAAgB,CACjB,CAAC;IAEF,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACxD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,SAKb,CAAC;IAEF,IACE,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAC9B,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ;QAChC,CAAC,KAAK,CAAC,IAAI,KAAK,OAAO;YACrB,KAAK,CAAC,IAAI,KAAK,WAAW;YAC1B,KAAK,CAAC,IAAI,KAAK,WAAW,CAAC,EAC7B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,yEAAyE;IACzE,8DAA8D;IAC9D,OAAO;QACL,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI;QACtD,MAAM,EAAE,KAAK,CAAC,MAAM;KACa,CAAC;AACtC,CAAC"}
|
|
@@ -7,8 +7,8 @@ import type { LinkProps } from "./types.js";
|
|
|
7
7
|
*
|
|
8
8
|
* `Link` builds the destination `href` from a typed route name plus optional
|
|
9
9
|
* params, search values, and hash. Primary-button clicks are intercepted and
|
|
10
|
-
* routed through `router.navigate()`, while hover
|
|
11
|
-
* route data through `router.
|
|
10
|
+
* routed through `router.navigate()`, while hover warms the destination
|
|
11
|
+
* route data through `router.warm()`.
|
|
12
12
|
*
|
|
13
13
|
* Sets `aria-current="page"` when the link target matches the current location.
|
|
14
14
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AnyRoute, HasParams, ParamsOf, RouteMap, RouteName, RouteOf, SearchOf } from "@canonical/router-core";
|
|
2
2
|
import type { AnchorHTMLAttributes, MouseEventHandler, ReactNode, Ref } from "react";
|
|
3
3
|
/**
|
|
4
|
-
* Route-building options shared by `Link`, `navigate()`, and `
|
|
4
|
+
* Route-building options shared by `Link`, `navigate()`, and `warm()`.
|
|
5
5
|
*/
|
|
6
6
|
export type LinkBuildOptions<TRoute extends AnyRoute> = {
|
|
7
7
|
/** Optional URL hash fragment without the leading `#`. */
|
|
@@ -23,7 +23,7 @@ export type LinkProps<TRoutes extends RouteMap, TName extends RouteName<TRoutes>
|
|
|
23
23
|
readonly download?: AnchorHTMLAttributes<HTMLAnchorElement>["download"];
|
|
24
24
|
/** Optional click handler called before router interception runs. */
|
|
25
25
|
readonly onClick?: MouseEventHandler<HTMLAnchorElement>;
|
|
26
|
-
/** Optional hover handler called before router
|
|
26
|
+
/** Optional hover handler called before router warming runs. */
|
|
27
27
|
readonly onMouseEnter?: MouseEventHandler<HTMLAnchorElement>;
|
|
28
28
|
/** Ref forwarded to the underlying anchor element. */
|
|
29
29
|
readonly ref?: Ref<HTMLAnchorElement>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/lib/Link/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,OAAO,EACP,QAAQ,EACT,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EACV,oBAAoB,EACpB,iBAAiB,EACjB,SAAS,EACT,GAAG,EACJ,MAAM,OAAO,CAAC;AAEf;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,QAAQ,IAAI;IACtD,0DAA0D;IAC1D,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,oEAAoE;IACpE,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,sDAAsD;IACtD,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;CACpC,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,IAAI,GAC/B;IAAE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;CAAE,GACrC;IAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;CAAE,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E,GACrC;IAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;CAAE,CAAC,CAAC;AAE5C,gCAAgC;AAChC,MAAM,MAAM,SAAS,CACnB,OAAO,SAAS,QAAQ,EACxB,KAAK,SAAS,SAAS,CAAC,OAAO,CAAC,IAC9B,IAAI,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC,GACvD,gBAAgB,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,GAAG;IAC1C,4DAA4D;IAC5D,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;IAC9B,iEAAiE;IACjE,QAAQ,CAAC,QAAQ,CAAC,EAAE,oBAAoB,CAAC,iBAAiB,CAAC,CAAC,UAAU,CAAC,CAAC;IACxE,qEAAqE;IACrE,QAAQ,CAAC,OAAO,CAAC,EAAE,iBAAiB,CAAC,iBAAiB,CAAC,CAAC;IACxD,gEAAgE;IAChE,QAAQ,CAAC,YAAY,CAAC,EAAE,iBAAiB,CAAC,iBAAiB,CAAC,CAAC;IAC7D,sDAAsD;IACtD,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,iBAAiB,CAAC,CAAC;IACtC,sCAAsC;IACtC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;CACpB,CAAC"}
|
|
@@ -10,6 +10,9 @@ export interface BlockerState {
|
|
|
10
10
|
* continues the blocked navigation, and `cancel()` stays on the page.
|
|
11
11
|
* The consumer controls the confirmation UI.
|
|
12
12
|
*
|
|
13
|
+
* Unmounting while blocked disposes the blocker and discards the pending
|
|
14
|
+
* navigation (it is not resumed).
|
|
15
|
+
*
|
|
13
16
|
* ```tsx
|
|
14
17
|
* const blocker = useBlocker(isDirty);
|
|
15
18
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBlocker.d.ts","sourceRoot":"","sources":["../../../../src/lib/hooks/useBlocker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useBlocker.d.ts","sourceRoot":"","sources":["../../../../src/lib/hooks/useBlocker.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,OAAO,IAAI,IAAI,CAAC;IAChB,MAAM,IAAI,IAAI,CAAC;CAChB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,QAAQ,EAAE,OAAO,GAAG,YAAY,CAoClE"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { default as createHydratedRouter } from "./createHydratedRouter.js";
|
|
2
1
|
export * from "./hooks/index.js";
|
|
3
2
|
export * from "./Link/index.js";
|
|
4
3
|
export * from "./Outlet/index.js";
|
|
5
4
|
export * from "./RouterProvider/index.js";
|
|
5
|
+
export { default as readDehydratedState } from "./readDehydratedState.js";
|
|
6
6
|
export * from "./register.js";
|
|
7
7
|
export { default as renderToStream } from "./renderToStream.js";
|
|
8
8
|
export * from "./types.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC1E,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAChE,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RouteMap, RouterDehydratedState } from "@canonical/router-core";
|
|
2
|
+
import type { HydrationWindow } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Read the router's dehydrated SSR state from the initial-data payload.
|
|
5
|
+
*
|
|
6
|
+
* The server serializes `router.dehydrate()`'s fields into the page's
|
|
7
|
+
* `__INITIAL_DATA__` payload; passing the result to `createRouter()` as
|
|
8
|
+
* `hydratedState` resumes from the server-rendered match instead of
|
|
9
|
+
* reloading it:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const router = createRouter(routes, {
|
|
13
|
+
* adapter: createBrowserAdapter(),
|
|
14
|
+
* hydratedState: readDehydratedState() ?? undefined,
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Returns `null` when no payload is present or when the payload does not
|
|
19
|
+
* carry dehydrated router state (e.g. an initial-data object without router
|
|
20
|
+
* fields) — the router then performs a normal initial load.
|
|
21
|
+
*/
|
|
22
|
+
export default function readDehydratedState<TRoutes extends RouteMap = RouteMap>(browserWindow?: HydrationWindow): RouterDehydratedState<TRoutes> | null;
|
|
23
|
+
//# sourceMappingURL=readDehydratedState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readDehydratedState.d.ts","sourceRoot":"","sources":["../../../src/lib/readDehydratedState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,mBAAmB,CACzC,OAAO,SAAS,QAAQ,GAAG,QAAQ,EAEnC,aAAa,GAAE,eAAsD,GACpE,qBAAqB,CAAC,OAAO,CAAC,GAAG,IAAI,CAsCvC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyRoute, RouteMap, RouterDehydratedState, RouterLoadResult, RouterNavigationState
|
|
1
|
+
import type { AnyRoute, RouteMap, RouterDehydratedState, RouterLoadResult, RouterNavigationState } from "@canonical/router-core";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
3
|
/**
|
|
4
4
|
* Shared types for SSR, hydration, and stream rendering.
|
|
@@ -29,13 +29,6 @@ export interface HydrationWindow {
|
|
|
29
29
|
/** Arbitrary global values, including the router's initial data payload. */
|
|
30
30
|
readonly [key: string]: unknown;
|
|
31
31
|
}
|
|
32
|
-
/** Options accepted by `createHydratedRouter()`. */
|
|
33
|
-
export interface CreateHydratedRouterOptions<TNotFound extends AnyRoute | undefined> extends Omit<RouterOptions<TNotFound>, "adapter"> {
|
|
34
|
-
/** Alternate window-like object to read initial hydration data from. */
|
|
35
|
-
readonly browserWindow?: HydrationWindow;
|
|
36
|
-
}
|
|
37
|
-
/** Alias for the window-like value consumed by `createHydratedRouter()`. */
|
|
38
|
-
export type CreateHydratedRouterWindow = HydrationWindow;
|
|
39
32
|
/** Alias for the navigation state values emitted by router-react hooks. */
|
|
40
33
|
export type HydratedNavigationState = RouterNavigationState;
|
|
41
34
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,EAChB,qBAAqB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC;;;;;GAKG;AAEH,gDAAgD;AAChD,MAAM,WAAW,qBAAqB;IACpC,8DAA8D;IAC9D,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CACnC,OAAO,SAAS,QAAQ,EACxB,SAAS,SAAS,QAAQ,GAAG,SAAS,GAAG,SAAS;IAElD,kFAAkF;IAClF,QAAQ,CAAC,sBAAsB,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/C,+DAA+D;IAC/D,QAAQ,CAAC,WAAW,EAAE,qBAAqB,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;IAC5D,iDAAiD;IACjD,QAAQ,CAAC,UAAU,EAAE,gBAAgB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC1D,uEAAuE;IACvE,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;CACjC;AAED,wDAAwD;AACxD,MAAM,WAAW,eAAe;IAC9B,4EAA4E;IAC5E,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACjC;AAED,2EAA2E;AAC3E,MAAM,MAAM,uBAAuB,GAAG,qBAAqB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canonical/router-react",
|
|
3
3
|
"description": "React bindings for @canonical/router-core",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.35.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
7
|
"types": "dist/types/index.d.ts",
|
|
@@ -44,20 +44,20 @@
|
|
|
44
44
|
"test:coverage": "vitest run --coverage"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@canonical/react-ssr": "^0.
|
|
48
|
-
"@canonical/router-core": "^0.
|
|
47
|
+
"@canonical/react-ssr": "^0.35.0",
|
|
48
|
+
"@canonical/router-core": "^0.35.0",
|
|
49
49
|
"react": "^19.2.4",
|
|
50
50
|
"react-dom": "^19.2.4"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@biomejs/biome": "2.4.9",
|
|
54
|
-
"@canonical/biome-config": "^0.
|
|
55
|
-
"@canonical/typescript-config-react": "^0.
|
|
56
|
-
"@canonical/vitest-config-react": "^0.
|
|
57
|
-
"@canonical/webarchitect": "^0.
|
|
58
|
-
"@testing-library/jest-dom": "^
|
|
54
|
+
"@canonical/biome-config": "^0.35.0",
|
|
55
|
+
"@canonical/typescript-config-react": "^0.35.0",
|
|
56
|
+
"@canonical/vitest-config-react": "^0.35.0",
|
|
57
|
+
"@canonical/webarchitect": "^0.35.0",
|
|
58
|
+
"@testing-library/jest-dom": "^7.0.0",
|
|
59
59
|
"@testing-library/react": "^16.3.2",
|
|
60
|
-
"@types/node": "^
|
|
60
|
+
"@types/node": "^26.0.0",
|
|
61
61
|
"@types/react": "^19.2.14",
|
|
62
62
|
"@types/react-dom": "^19.2.3",
|
|
63
63
|
"@vitejs/plugin-react": "^6.0.0",
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"vite": "^8.0.1",
|
|
68
68
|
"vitest": "^4.0.18"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "5198c5399ef657959272c0e35924391573d81146"
|
|
71
71
|
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { INITIAL_DATA_KEY } from "@canonical/react-ssr/renderer/constants";
|
|
2
|
-
import { createRouter as createCoreRouter, createHistoryAdapter, } from "@canonical/router-core";
|
|
3
|
-
function readInitialState(browserWindow) {
|
|
4
|
-
return (browserWindow[INITIAL_DATA_KEY] ??
|
|
5
|
-
null);
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Create a browser-backed router that reuses dehydrated server state.
|
|
9
|
-
*
|
|
10
|
-
* The function reads the initial router payload from the configured window,
|
|
11
|
-
* creates a browser adapter, and passes both into `createRouter()` so client
|
|
12
|
-
* hydration resumes from the server-rendered match instead of reloading it.
|
|
13
|
-
*
|
|
14
|
-
* @param routes - The application's route map.
|
|
15
|
-
* @param options - Router options plus an optional window-like object to read
|
|
16
|
-
* hydration state from.
|
|
17
|
-
*/
|
|
18
|
-
export default function createHydratedRouter(routes, options) {
|
|
19
|
-
const browserWindow = options?.browserWindow ?? window;
|
|
20
|
-
const { browserWindow: _unusedBrowserWindow, ...routerOptions } = options ?? {};
|
|
21
|
-
const initialState = readInitialState(browserWindow);
|
|
22
|
-
const router = createCoreRouter(routes, {
|
|
23
|
-
...routerOptions,
|
|
24
|
-
adapter: createHistoryAdapter(browserWindow),
|
|
25
|
-
hydratedState: initialState ?? undefined,
|
|
26
|
-
});
|
|
27
|
-
return router;
|
|
28
|
-
}
|
|
29
|
-
//# sourceMappingURL=createHydratedRouter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createHydratedRouter.js","sourceRoot":"","sources":["../../../src/lib/createHydratedRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yCAAyC,CAAC;AAO3E,OAAO,EACL,YAAY,IAAI,gBAAgB,EAChC,oBAAoB,GACrB,MAAM,wBAAwB,CAAC;AAMhC,SAAS,gBAAgB,CACvB,aAAyC;IAEzC,OAAO,CAAE,aAAyC,CAAC,gBAAgB,CAAC;QAClE,IAAI,CAA0C,CAAC;AACnD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAI1C,MAAe,EACf,OAAgD;IAEhD,MAAM,aAAa,GACjB,OAAO,EAAE,aAAa,IAAK,MAAgD,CAAC;IAC9E,MAAM,EAAE,aAAa,EAAE,oBAAoB,EAAE,GAAG,aAAa,EAAE,GAC7D,OAAO,IAAI,EAAE,CAAC;IAChB,MAAM,YAAY,GAAG,gBAAgB,CAAU,aAAa,CAAC,CAAC;IAC9D,MAAM,MAAM,GAAG,gBAAgB,CAAC,MAAM,EAAE;QACtC,GAAG,aAAa;QAChB,OAAO,EAAE,oBAAoB,CAAC,aAAsB,CAAC;QACrD,aAAa,EACV,YAA2D,IAAI,SAAS;KAC5E,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { AnyRoute, RouteMap, Router } from "@canonical/router-core";
|
|
2
|
-
import type { CreateHydratedRouterOptions } from "./types.js";
|
|
3
|
-
/**
|
|
4
|
-
* Create a browser-backed router that reuses dehydrated server state.
|
|
5
|
-
*
|
|
6
|
-
* The function reads the initial router payload from the configured window,
|
|
7
|
-
* creates a browser adapter, and passes both into `createRouter()` so client
|
|
8
|
-
* hydration resumes from the server-rendered match instead of reloading it.
|
|
9
|
-
*
|
|
10
|
-
* @param routes - The application's route map.
|
|
11
|
-
* @param options - Router options plus an optional window-like object to read
|
|
12
|
-
* hydration state from.
|
|
13
|
-
*/
|
|
14
|
-
export default function createHydratedRouter<const TRoutes extends RouteMap, const TNotFound extends AnyRoute | undefined = undefined>(routes: TRoutes, options?: CreateHydratedRouterOptions<TNotFound>): Router<TRoutes, TNotFound>;
|
|
15
|
-
//# sourceMappingURL=createHydratedRouter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createHydratedRouter.d.ts","sourceRoot":"","sources":["../../../src/lib/createHydratedRouter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,MAAM,EAEP,MAAM,wBAAwB,CAAC;AAKhC,OAAO,KAAK,EACV,2BAA2B,EAE5B,MAAM,YAAY,CAAC;AASpB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAC1C,KAAK,CAAC,OAAO,SAAS,QAAQ,EAC9B,KAAK,CAAC,SAAS,SAAS,QAAQ,GAAG,SAAS,GAAG,SAAS,EAExD,MAAM,EAAE,OAAO,EACf,OAAO,CAAC,EAAE,2BAA2B,CAAC,SAAS,CAAC,GAC/C,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAc5B"}
|