@rangojs/router 0.36.0 → 0.37.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/dist/bin/{chunk-THW7E5A2.js → chunk-A64EG6MI.js} +22 -19
- package/dist/bin/{client-urls-projection-DHUUOJZM.js → client-urls-projection-RLDSA3BL.js} +1 -1
- package/dist/bin/rango.js +1 -1
- package/dist/bin/{runtime-discovery-UECTPF6K.js → runtime-discovery-T7ZLA6E3.js} +2 -2
- package/dist/bin/{temp-rsc-server-QLNMTFEE.js → temp-rsc-server-FGGXPKVQ.js} +2 -2
- package/dist/types/browser/event-controller.d.ts +61 -0
- package/dist/types/browser/fetched-loader-handles.d.ts +76 -0
- package/dist/types/browser/navigation-store.d.ts +3 -2
- package/dist/types/browser/react/use-handle.d.ts +18 -5
- package/dist/types/browser/react/use-push-handle.d.ts +44 -0
- package/dist/types/browser/types.d.ts +44 -0
- package/dist/types/client.d.ts +1 -0
- package/dist/types/client.rsc.d.ts +1 -0
- package/dist/types/handle.d.ts +13 -5
- package/dist/types/handles/client-pushes.d.ts +51 -0
- package/dist/types/handles/deferred-resolution.d.ts +14 -0
- package/dist/types/handles/pushed-by.d.ts +36 -0
- package/dist/types/loader-store.d.ts +13 -0
- package/dist/types/rsc/prefetch-render-error.d.ts +2 -1
- package/dist/types/server/handle-store.d.ts +8 -0
- package/dist/types/testing/dom.entry.d.ts +1 -1
- package/dist/types/testing/flight.entry.d.ts +1 -1
- package/dist/types/testing/render-route.d.ts +76 -14
- package/dist/types/testing/serve-shell-request.d.ts +31 -0
- package/dist/vite/index.js +23 -20
- package/package.json +1 -1
- package/skills/breadcrumbs/SKILL.md +68 -6
- package/skills/caching/SKILL.md +4 -2
- package/skills/catalog.json +3 -3
- package/skills/client-urls/SKILL.md +13 -1
- package/skills/document-cache/SKILL.md +31 -1
- package/skills/hooks/SKILL.md +8 -4
- package/skills/hooks/data.md +10 -1
- package/skills/hooks/handle-and-actions.md +402 -26
- package/skills/hooks/navigation.md +4 -0
- package/skills/hooks/urls.md +3 -0
- package/skills/links/SKILL.md +5 -3
- package/skills/loader/SKILL.md +40 -1
- package/skills/migrate-nextjs/SKILL.md +5 -0
- package/skills/migrate-react-router/component-migration.md +4 -1
- package/skills/parallel/SKILL.md +8 -0
- package/skills/ppr/SKILL.md +1 -0
- package/skills/rango/SKILL.md +6 -3
- package/skills/testing/client-components.md +35 -32
- package/skills/testing/handles.md +58 -6
- package/skills/testing/serve-shell-request.md +33 -14
- package/skills/ux-architecture/SKILL.md +4 -0
- package/src/browser/event-controller.ts +311 -2
- package/src/browser/fetched-loader-handles.ts +269 -0
- package/src/browser/navigation-store.ts +261 -2
- package/src/browser/partial-update.ts +3 -2
- package/src/browser/react/use-handle.ts +74 -27
- package/src/browser/react/use-push-handle.ts +113 -0
- package/src/browser/rsc-router.tsx +11 -14
- package/src/browser/types.ts +48 -0
- package/src/cache/document-cache-policy.ts +6 -5
- package/src/client.rsc.tsx +1 -0
- package/src/client.tsx +31 -1
- package/src/handle.ts +13 -5
- package/src/handles/client-pushes.ts +120 -0
- package/src/handles/deferred-resolution.ts +41 -2
- package/src/handles/pushed-by.ts +130 -1
- package/src/loader-store.ts +26 -0
- package/src/rsc/loader-fetch.ts +64 -5
- package/src/rsc/prefetch-render-error.ts +2 -1
- package/src/rsc/request-stages.ts +7 -6
- package/src/rsc/rsc-rendering.ts +32 -14
- package/src/server/handle-store.ts +23 -0
- package/src/ssr/ssr-root.tsx +6 -0
- package/src/testing/dom.entry.ts +2 -0
- package/src/testing/flight.entry.ts +1 -0
- package/src/testing/render-route.tsx +319 -72
- package/src/testing/serve-shell-request.ts +62 -1
- package/src/use-loader.tsx +44 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: client-urls
|
|
3
|
-
description: Define client-component route groups with clientUrls() in a "use client" module — no handlers, useLoader at read sites, server-run revalidate predicates, loader-thrown notFound/redirect, loader handle writes, and the ssr:"await" SSR-completeness opt-in. Use when a route group's pages are client components, when building high-navigation-speed UIs (dashboards, admin panels, settings) where transitions must feel instant, when moving data reads from handler ctx.use() to useLoader at consumption sites, or when asking how routes defined in a "use client" file work.
|
|
3
|
+
description: Define client-component route groups with clientUrls() in a "use client" module — no handlers, useLoader at read sites, server-run revalidate predicates, loader-thrown notFound/redirect, loader handle writes and client pushes (usePushHandle), and the ssr:"await" SSR-completeness opt-in. Use when a route group's pages are client components, when building high-navigation-speed UIs (dashboards, admin panels, settings) where transitions must feel instant, when moving data reads from handler ctx.use() to useLoader at consumption sites, or when asking how routes defined in a "use client" file work.
|
|
4
4
|
argument-hint: "[setup]"
|
|
5
5
|
---
|
|
6
6
|
|
|
@@ -560,6 +560,18 @@ ride the SSR handle snapshot; later ones stream and apply post-hydration
|
|
|
560
560
|
without `ssr: "await"` never ran during the capture, so its pushes are never in
|
|
561
561
|
the shell and always apply client-side. See `/loader` for the full contract.
|
|
562
562
|
|
|
563
|
+
**Client pushes.** A group has no handlers, so a handle value has two
|
|
564
|
+
writers: the group's loaders, and the components that read them. Push from
|
|
565
|
+
the loader when the value comes from the loader's data: registered with
|
|
566
|
+
`ssr: "await"` it is in the document, and every read of the loader (a
|
|
567
|
+
navigation, `load()`, a group refresh) brings the new one. Push with `usePushHandle` beside the
|
|
568
|
+
`useLoader` read when the value comes from what the client holds (a draft,
|
|
569
|
+
a count, a selection): it shows after the loader's values in the same
|
|
570
|
+
segment, so a `Meta` title or a `Breadcrumbs` crumb for the same `href` takes
|
|
571
|
+
the client's value while the component pushes it. One worked example of
|
|
572
|
+
each, and the rule against pushing one value from both:
|
|
573
|
+
[hooks → "Where should a handle value come from?"](../hooks/handle-and-actions.md#in-a-clienturls-group).
|
|
574
|
+
|
|
563
575
|
## `ssr: "await"` — the SSR-completeness opt-in
|
|
564
576
|
|
|
565
577
|
By default every loader streams on every render, so nothing a slow loader
|
|
@@ -222,7 +222,8 @@ suffixes are still appended.
|
|
|
222
222
|
`fromRouteName` kept a segment, or an intercept targets the route. The key
|
|
223
223
|
names the segments the client holds, not its page, so `/c/tv` would get the
|
|
224
224
|
answer `/c/audio` got for `/c/audio?sort=x` and keep its own hero. It is sent
|
|
225
|
-
`private, no-cache`
|
|
225
|
+
`private, no-cache` in place of the route's `Cache-Control`, or keeps the
|
|
226
|
+
route's own when that says `no-store`, and is not stored. One
|
|
226
227
|
whose rule re-rendered the segment is right from every page and is stored as
|
|
227
228
|
usual.
|
|
228
229
|
- Non-GET requests
|
|
@@ -255,6 +256,35 @@ suffixes are still appended.
|
|
|
255
256
|
That happens only when `Cache-Control` was set after the render (see
|
|
256
257
|
"Theme" below).
|
|
257
258
|
|
|
259
|
+
### Cache-Control on a partial nothing may reuse
|
|
260
|
+
|
|
261
|
+
Four partial responses must never be reused by a shared cache or answer
|
|
262
|
+
another request, so the router chooses their `Cache-Control`:
|
|
263
|
+
|
|
264
|
+
| Response | Route set nothing, or a weaker header | Route set `no-store` (any case) |
|
|
265
|
+
| --------------------------------------------------------------------------------- | ------------------------------------- | ------------------------------- |
|
|
266
|
+
| A prefetch scoped to its source page (`x-rsc-prefetch-scope`) | `private, no-cache` | the route's header, unchanged |
|
|
267
|
+
| A prefetch that defers `prefetch: false` work | `private, no-cache` | the route's header, unchanged |
|
|
268
|
+
| The fill that follows it (`_rsc_fill`) | `private, no-cache` | the route's header, unchanged |
|
|
269
|
+
| A stale partial (`_rsc_stale`: after an action, Back/Forward, `router.refresh()`) | `private, no-cache` | the route's header, unchanged |
|
|
270
|
+
|
|
271
|
+
"The route's header" is what a middleware set before `next()` or a handler
|
|
272
|
+
set with `ctx.headers`. A handler's header reaches only a response that runs
|
|
273
|
+
the handler, and a fill (or any partial that keeps the route's segment) does
|
|
274
|
+
not, so a header every response needs, such as a signed-in app's `no-store`,
|
|
275
|
+
belongs in middleware. A middleware that writes `Cache-Control` after
|
|
276
|
+
`next()` still has the last word on all four. Any other partial keeps the
|
|
277
|
+
route's header as it is.
|
|
278
|
+
|
|
279
|
+
Why not always `no-store`: Chromium reports a `no-store` response whose body
|
|
280
|
+
is read through a stream, as every Flight payload is, as canceled
|
|
281
|
+
(`net::ERR_ABORTED` in DevTools) although every byte arrived, and
|
|
282
|
+
Playwright's `response.finished()` never settles on it. A route that sends
|
|
283
|
+
`no-store` (a signed-in app's pages) accepts that report on these four
|
|
284
|
+
responses, as it already does on its navigations. In an app's e2e tests, wait
|
|
285
|
+
for the response's headers (`page.waitForResponse(...)`) and then for what the
|
|
286
|
+
page shows, never for `response.finished()`.
|
|
287
|
+
|
|
258
288
|
## Theme
|
|
259
289
|
|
|
260
290
|
With `theme` enabled, the page payload carries the theme `useTheme()` starts
|
package/skills/hooks/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: hooks
|
|
3
|
-
description: Client-side React hooks for navigation, loaders, and state in @rangojs/router. Use when a client component needs the current URL, params, search params, navigation state, or
|
|
3
|
+
description: Client-side React hooks for navigation, loaders, handles and state in @rangojs/router. Use when a client component needs the current URL, params, search params, navigation state, loader data, or handle data (useHandle), or pushes a handle value from client state (usePushHandle, for a page title, Meta tag or breadcrumb from a live value, a draft or a fetch) — e.g. "how do I read the route param in a component", "set the page title from a client component".
|
|
4
4
|
argument-hint: [hook-name]
|
|
5
5
|
---
|
|
6
6
|
|
|
@@ -40,6 +40,7 @@ the one for your case.
|
|
|
40
40
|
| Loader data with on-demand fetch | `useFetchLoader()` | [`./data.md`](./data.md) |
|
|
41
41
|
| Refresh multiple loaders across groups | `useRefreshLoaders()` | [`./data.md`](./data.md) |
|
|
42
42
|
| Accumulated handle data from route segments | `useHandle()` | [`./handle-and-actions.md`](./handle-and-actions.md) |
|
|
43
|
+
| Push a handle value from a client component | `usePushHandle()` | [`./handle-and-actions.md`](./handle-and-actions.md) |
|
|
43
44
|
| Server action invocation state | `useAction()` | [`./handle-and-actions.md`](./handle-and-actions.md) |
|
|
44
45
|
| Type-safe history state (persistent or flash) | `useLocationState()` | [`./state.md`](./state.md) |
|
|
45
46
|
| Force the client's caches to miss after a mutation | `invalidateClientCache()` | [`./state.md`](./state.md) |
|
|
@@ -59,8 +60,9 @@ the one for your case.
|
|
|
59
60
|
- [`./data.md`](./data.md) — `useLoader`, `useFetchLoader` (shared refetch
|
|
60
61
|
scoping, `key`, `refreshGroup` + `useRefreshLoaders`, load options, file
|
|
61
62
|
uploads).
|
|
62
|
-
- [`./handle-and-actions.md`](./handle-and-actions.md) —
|
|
63
|
-
|
|
63
|
+
- [`./handle-and-actions.md`](./handle-and-actions.md) — handles (what they
|
|
64
|
+
are, how values are ordered, who writes them, where a value should come
|
|
65
|
+
from), `useHandle`, `usePushHandle`, `useAction`. For the full server-action guide (defining actions,
|
|
64
66
|
`useActionState`, `useOptimistic`, validation, revalidation, error
|
|
65
67
|
handling, file uploads), see `/server-actions`; `useAction()` here is the
|
|
66
68
|
Rango-specific hook for tracking actions called outside a
|
|
@@ -94,6 +96,7 @@ the one for your case.
|
|
|
94
96
|
| `useFetchLoader()` | Loader with on-demand fetch | data, load, isLoading, error, refetch |
|
|
95
97
|
| `useRefreshLoaders()` | Refresh cross-loader group(s) | `() => (groups: string \| string[]) => Promise<void>` |
|
|
96
98
|
| `useHandle()` | Accumulated handle data | T (handle type) |
|
|
99
|
+
| `usePushHandle()` | Push handle values from a client component, during render | `(value: T \| Promise<T>) => void` |
|
|
97
100
|
| `useAction()` | Server action state | state, actionId, payload, error, result |
|
|
98
101
|
| `useLocationState()` | History state (persists or flash) | T \| undefined |
|
|
99
102
|
| `invalidateClientCache()` | Force client caches to miss (function, not a hook; root entry) | `void` |
|
|
@@ -111,7 +114,8 @@ cannot mismatch its server HTML.
|
|
|
111
114
|
|
|
112
115
|
- Navigation state, params and action state: the component shows the live
|
|
113
116
|
value right after it hydrates.
|
|
114
|
-
- Handle data a loader pushed late,
|
|
117
|
+
- Handle data a loader pushed late, values a client component pushes
|
|
118
|
+
(`usePushHandle`), and the entry's location state: all wait
|
|
115
119
|
until the whole document has streamed and been revealed, and every boundary
|
|
116
120
|
has hydrated, including any boundary still suspended on the client (a lazy
|
|
117
121
|
chunk, a deferred-hydration gate; that wait has no upper bound). Then
|
package/skills/hooks/data.md
CHANGED
|
@@ -161,6 +161,13 @@ semantics as the old per-component `useState`):
|
|
|
161
161
|
registered on the current route. Two unrelated components calling
|
|
162
162
|
`load()` on the same fetchable-but-unregistered loader keep
|
|
163
163
|
independent results.
|
|
164
|
+
- The handle values of any `load()` from a component outside the page's
|
|
165
|
+
route tree (no route context), even for a loader the page registers: its
|
|
166
|
+
data stays with that component, so its pushes change no title or crumb
|
|
167
|
+
(development warns once). To change the page's title from there, refresh
|
|
168
|
+
the page's own data from a reader inside the route, or call
|
|
169
|
+
`useRouter().refresh()`. A `key` it shares with a reader inside the route
|
|
170
|
+
makes its data the page's, and then its values apply.
|
|
164
171
|
|
|
165
172
|
So the search/list pattern still works — two components calling
|
|
166
173
|
`load({ params: { q } })` with different `q` values each keep their
|
|
@@ -204,7 +211,9 @@ A refreshed value belongs to the route data of the readers on screen when
|
|
|
204
211
|
`load()` was called: a reader that mounts on another page or on a new visit,
|
|
205
212
|
or that a navigation brings new route data, shows that route data from its
|
|
206
213
|
first frame, and a `load()` still in flight when you leave lands on no other
|
|
207
|
-
page; Back onto the refreshed page shows the refresh again
|
|
214
|
+
page; Back onto the refreshed page shows the refresh again, with the title
|
|
215
|
+
and crumbs the refresh pushed: the history entry keeps a refresh's data and
|
|
216
|
+
handle values together.
|
|
208
217
|
|
|
209
218
|
**Refreshing multiple loaders together (`refreshGroup` + `useRefreshLoaders`)**:
|
|
210
219
|
|
|
@@ -1,10 +1,343 @@
|
|
|
1
1
|
# Handle and Action Hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This page is the home of handles: what they are, who writes them, how their
|
|
4
|
+
values are ordered, how to read them, and where a value should come from.
|
|
5
|
+
Other skills link here instead of repeating it.
|
|
6
|
+
|
|
7
|
+
## Handles
|
|
8
|
+
|
|
9
|
+
### What a handle is
|
|
10
|
+
|
|
11
|
+
A handle collects values across the segments a page matched: its layouts,
|
|
12
|
+
routes, parallel slots and intercepts. Each segment holds its own list of
|
|
13
|
+
values; a reader gets the result of the handle's `collect` over those lists,
|
|
14
|
+
in tree order (parent before child, a slot after the segment that declares
|
|
15
|
+
it).
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// handles/page-title.ts
|
|
19
|
+
import { createHandle } from "@rangojs/router";
|
|
20
|
+
|
|
21
|
+
// The last value pushed anywhere on the page wins.
|
|
22
|
+
export const PageTitle = createHandle<string, string>(
|
|
23
|
+
(segments) => segments.flat().at(-1) ?? "My App",
|
|
24
|
+
);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`createHandle()` with no collect returns the lists as they are
|
|
28
|
+
(`T[][]`, one per segment that pushed). Two handles ship with the router:
|
|
29
|
+
`Meta` (the document's `<head>`, rendered by `<Html.Meta />`) and
|
|
30
|
+
`Breadcrumbs` (a trail, deduped by `href`). See "Meta and Breadcrumbs" below.
|
|
31
|
+
|
|
32
|
+
### How values are ordered
|
|
33
|
+
|
|
34
|
+
Within one segment, the server's values come first, in the order the
|
|
35
|
+
segment's handler and loaders pushed them, then the values client components
|
|
36
|
+
in that segment push:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
[ layout: [server...], route: [server..., client...] ]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The position is fixed by the tree, never by arrival time: a server value that
|
|
43
|
+
resolves late (`push(promise)`, a streamed loader, a `ppr` live-lane push)
|
|
44
|
+
lands before the client's values of its segment, and a client value never
|
|
45
|
+
overwrites a server one, nor the reverse. Two client components that push one
|
|
46
|
+
handle in one segment show in tree order, whichever of them commits first. A
|
|
47
|
+
component nested inside another that pushes the same handle shows before it,
|
|
48
|
+
so the outer one wins a last-wins collect. The collect then decides what
|
|
49
|
+
wins: `Meta`'s title and `PageTitle` above take the last one, so a client
|
|
50
|
+
title beats the server title of the same segment; `Breadcrumbs` keeps one
|
|
51
|
+
crumb per `href`, with the last value.
|
|
52
|
+
|
|
53
|
+
### Who writes a handle
|
|
54
|
+
|
|
55
|
+
The server pushes what it knows when a segment renders: a handler, or a
|
|
56
|
+
loader. A client component pushes live data and live edits on the page, as a
|
|
57
|
+
value or as a promise.
|
|
58
|
+
|
|
59
|
+
| Writer | Where | When its values reach the page |
|
|
60
|
+
| --------------------------------------------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
61
|
+
| `ctx.use(Handle)` in a handler | route, layout, slot, intercept | With the segment's render; in the SSR document |
|
|
62
|
+
| `ctx.use(Handle)` in a loader body | any loader | In the SSR document when the loader is registered with `ssr: "await"`. Otherwise usually in it when pushed before the page's handlers finish, else applied on the client right after hydration. A read of the loader on its own (`load()`, a group refresh) carries its pushes too |
|
|
63
|
+
| `usePushHandle(Handle)` in a client component | the component's own segment | With the render that pushed them, on the client only (a promise once it resolves); never in SSR HTML or a `ppr` shell |
|
|
64
|
+
|
|
65
|
+
Middleware has no handle APIs. A server component that pushes after an
|
|
66
|
+
`await` throws `LateHandlePushError`: reserve the slot in the handler with
|
|
67
|
+
`ctx.use(Handle).defer()` (`/breadcrumbs`, "Deferred content"). Loader
|
|
68
|
+
delivery in detail: `/loader` → "Writing Handles from Loaders".
|
|
69
|
+
|
|
70
|
+
### Where should a handle value come from?
|
|
71
|
+
|
|
72
|
+
Ask where the value is known and whether the document must carry it.
|
|
73
|
+
|
|
74
|
+
1. **Known on the server when the segment renders** (the project's name, the
|
|
75
|
+
product's title): push from the handler, or from the loader that produced
|
|
76
|
+
the data. A loader's pushes ride its reads too, so a `load()`, `refetch()`
|
|
77
|
+
or group refresh of a registered loader keeps the title current with no
|
|
78
|
+
client code.
|
|
79
|
+
2. **Must be in the document** (the SSR `<head>`, a title a crawler or a link
|
|
80
|
+
preview sees, a `ppr` shell): push from a handler, or from a loader
|
|
81
|
+
registered with `ssr: "await"`. A loader without it is in the document
|
|
82
|
+
only when it pushes before the page's handlers finish, which a push
|
|
83
|
+
derived from fetched data usually does not. A `usePushHandle` value is
|
|
84
|
+
never in the SSR HTML or a `ppr` shell: the server renders the component
|
|
85
|
+
but runs no effects, and the push publishes from a layout effect. On a
|
|
86
|
+
client navigation it shows in the page's own commit.
|
|
87
|
+
3. **Live on the page** (learned or changed on the client after render):
|
|
88
|
+
`usePushHandle` in the component that holds the data, with a value or a
|
|
89
|
+
promise. That is a live chat's unread count, a live edit page's draft
|
|
90
|
+
title, optimistic state, a `useFetchLoader()` read of a loader no segment
|
|
91
|
+
registers, or a `load({ params })` the page shows (the router drops the
|
|
92
|
+
server pushes of a read with params, since it is usually data the page
|
|
93
|
+
does not show).
|
|
94
|
+
4. **Both** (a title visible from the first paint that the client then
|
|
95
|
+
changes): push the first value on the server and the changed one on the
|
|
96
|
+
client. This works when the collect keeps one value per key: `Meta` for
|
|
97
|
+
its keyed descriptors (below) and `Breadcrumbs` (one crumb per `href`).
|
|
98
|
+
The client's value follows the server's and replaces it.
|
|
99
|
+
|
|
100
|
+
Never push one value from the server and the client into one segment when
|
|
101
|
+
the collect keeps every value: pushes append, so it shows twice. That holds
|
|
102
|
+
for a flat handle and for the `Meta` descriptors without a key. `Meta`
|
|
103
|
+
dedupes `title`, `charSet`, a `name`, `property` or `httpEquiv` with
|
|
104
|
+
`content`, and a `link` whose `rel` is `canonical`, `icon` or
|
|
105
|
+
`apple-touch-icon`; every other descriptor (`script:ld+json`, any other
|
|
106
|
+
`link`) appends.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
// Wrong: the product's JSON-LD is in <head> twice.
|
|
110
|
+
// handler: ctx.use(Meta)({ "script:ld+json": productJsonLd(product) });
|
|
111
|
+
// client: usePushHandle(Meta)({ "script:ld+json": productJsonLd(product) });
|
|
112
|
+
|
|
113
|
+
// Wrong: a flat Tags handle shows "draft" twice.
|
|
114
|
+
export const Tags = createHandle<string, string[]>((s) => s.flat());
|
|
115
|
+
// handler: ctx.use(Tags)("draft");
|
|
116
|
+
// client: usePushHandle(Tags)("draft");
|
|
117
|
+
|
|
118
|
+
// Fix: one writer per value. Push it where it is known (the server here),
|
|
119
|
+
// or let the collect keep one value per key:
|
|
120
|
+
export const UniqueTags = createHandle<string, string[]>((s) => [
|
|
121
|
+
...new Set(s.flat()),
|
|
122
|
+
]);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Live chat
|
|
126
|
+
|
|
127
|
+
The handler pushes the chat's title, which the document, a crawler and a
|
|
128
|
+
link preview see. The unread count is live on the page: the component pushes
|
|
129
|
+
the promise of the count's fetch. While it is pending, readers keep the title
|
|
130
|
+
they show. A count of zero resolves to `null`, which is dropped, so the
|
|
131
|
+
server's title shows again.
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
// urls.tsx
|
|
135
|
+
path(
|
|
136
|
+
"/chat/:id",
|
|
137
|
+
(ctx) => {
|
|
138
|
+
ctx.use(Meta)({ title: "Support chat" });
|
|
139
|
+
return <ChatRoom id={ctx.params.id} />;
|
|
140
|
+
},
|
|
141
|
+
{ name: "chat" },
|
|
142
|
+
);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
```tsx
|
|
146
|
+
"use client";
|
|
147
|
+
import { useMemo } from "react";
|
|
148
|
+
import { Meta, usePushHandle } from "@rangojs/router/client";
|
|
149
|
+
|
|
150
|
+
// `version` moves when a message arrives; a promise is the same value only
|
|
151
|
+
// as the same reference, so the memo keeps one per version.
|
|
152
|
+
function UnreadTitle({ id, version }: { id: string; version: number }) {
|
|
153
|
+
const title = useMemo(
|
|
154
|
+
() =>
|
|
155
|
+
fetchUnread(id).then((count) =>
|
|
156
|
+
count > 0 ? { title: `(${count}) Support chat` } : null,
|
|
157
|
+
),
|
|
158
|
+
[id, version],
|
|
159
|
+
);
|
|
160
|
+
usePushHandle(Meta)(title);
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### Live edit
|
|
166
|
+
|
|
167
|
+
The handler pushes the saved title; the editor pushes the draft over it while
|
|
168
|
+
it differs. The draft's title follows the server's in the route's segment,
|
|
169
|
+
so `Meta` shows it, and the saved one shows again once they match.
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
// urls.tsx
|
|
173
|
+
path(
|
|
174
|
+
"/posts/:id/edit",
|
|
175
|
+
async (ctx) => {
|
|
176
|
+
const post = await getPost(ctx.params.id);
|
|
177
|
+
ctx.use(Meta)({ title: `${post.title} | Blog` });
|
|
178
|
+
return <PostEditor post={post} />;
|
|
179
|
+
},
|
|
180
|
+
{ name: "postEdit" },
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
"use client";
|
|
186
|
+
function PostEditor({ post }: { post: { title: string } }) {
|
|
187
|
+
const [draft, setDraft] = useState(post.title);
|
|
188
|
+
const pushTitle = usePushHandle(Meta);
|
|
189
|
+
if (draft !== post.title) pushTitle({ title: `${draft} (editing) | Blog` });
|
|
190
|
+
return <input value={draft} onChange={(e) => setDraft(e.target.value)} />;
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### In a clientUrls group
|
|
195
|
+
|
|
196
|
+
A `clientUrls()` group has no handlers: its data is read with `useLoader` at
|
|
197
|
+
the read site, and its handle writes are its loaders' bodies or its
|
|
198
|
+
components. Push from the group's loader when the value comes from the
|
|
199
|
+
loader's data; push with `usePushHandle` beside the `useLoader` read when the
|
|
200
|
+
value comes from what the client holds.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
// From the loader, registered with ssr: "await" so its push is in the
|
|
204
|
+
// document; every read of the loader (a navigation, load(), a group
|
|
205
|
+
// refresh) brings the new one.
|
|
206
|
+
export const ProductLoader = createLoader(async (ctx) => {
|
|
207
|
+
const product = await getProduct(ctx.params.slug);
|
|
208
|
+
ctx.use(Meta)({ title: `${product.name} | Shop` });
|
|
209
|
+
return product;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// In the group:
|
|
213
|
+
// path("/:slug", ProductEditor, { name: "product" }, () => [
|
|
214
|
+
// loader(ProductLoader, { ssr: "await" }),
|
|
215
|
+
// ]),
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
```tsx
|
|
219
|
+
"use client";
|
|
220
|
+
// Beside the read: a draft the client edits. The loader's title shows
|
|
221
|
+
// until a draft is set, then the draft's, after the loader's in the segment.
|
|
222
|
+
function ProductEditor() {
|
|
223
|
+
const { data } = useLoader(ProductLoader);
|
|
224
|
+
const [draft, setDraft] = useState("");
|
|
225
|
+
const pushTitle = usePushHandle(Meta);
|
|
226
|
+
if (draft) pushTitle({ title: `${draft} (editing) | Shop` });
|
|
227
|
+
return (
|
|
228
|
+
<input
|
|
229
|
+
value={draft || data.name}
|
|
230
|
+
onChange={(e) => setDraft(e.target.value)}
|
|
231
|
+
/>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### usePushHandle()
|
|
237
|
+
|
|
238
|
+
A `useFetchLoader()` read of a loader no segment registers (case 3 above):
|
|
239
|
+
its data arrives on `load()`, and the component pushes the title and the
|
|
240
|
+
crumb from it.
|
|
241
|
+
|
|
242
|
+
```tsx
|
|
243
|
+
"use client";
|
|
244
|
+
import {
|
|
245
|
+
Meta,
|
|
246
|
+
Breadcrumbs,
|
|
247
|
+
useFetchLoader,
|
|
248
|
+
usePushHandle,
|
|
249
|
+
} from "@rangojs/router/client";
|
|
250
|
+
|
|
251
|
+
function ProjectHeader() {
|
|
252
|
+
const { data, load } = useFetchLoader(ProjectLoader);
|
|
253
|
+
const pushTitle = usePushHandle(Meta);
|
|
254
|
+
const pushCrumb = usePushHandle(Breadcrumbs);
|
|
255
|
+
if (data) {
|
|
256
|
+
pushTitle({ title: `${data.name} | Projects` });
|
|
257
|
+
pushCrumb({ label: data.name, href: `/projects/${data.id}` });
|
|
258
|
+
}
|
|
259
|
+
return (
|
|
260
|
+
<header>
|
|
261
|
+
<h1>{data?.name ?? "Project"}</h1>
|
|
262
|
+
<button onClick={() => load().catch(() => {})}>Load</button>
|
|
263
|
+
</header>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
`usePushHandle(Handle)` returns `push`, the client side of the server's
|
|
269
|
+
`ctx.use(Handle)`. There is no set, write or replace: a push adds a value, as
|
|
270
|
+
on the server, and the collect decides what wins.
|
|
271
|
+
|
|
272
|
+
- **During render.** Call `push(value)` while the component renders. The
|
|
273
|
+
values of that render are published in its commit, so they show with the
|
|
274
|
+
render that made them: a render React throws away, or one that suspends,
|
|
275
|
+
publishes nothing. Each render replaces the component's values; a render
|
|
276
|
+
that pushes nothing removes them.
|
|
277
|
+
- **Not after the commit.** Once the render that created it has committed,
|
|
278
|
+
`push` throws `[rango] usePushHandle(<id>): push() was called after the
|
|
279
|
+
render that created it committed ...`. That is a call from an effect, an
|
|
280
|
+
event handler, a timer, or a child that received `push` as a prop and
|
|
281
|
+
re-rendered on its own. Keep the value in state and push it in render, or
|
|
282
|
+
call `usePushHandle` in the child. A child that renders in the same pass as
|
|
283
|
+
the owner adds to the owner's values; pass the data down instead of `push`.
|
|
284
|
+
- **Its own segment.** The values belong to the segment the component renders
|
|
285
|
+
in: the nearest route, layout, parallel slot or intercept. A component in a
|
|
286
|
+
layout pushes into the layout's segment, so a route's server title still
|
|
287
|
+
beats it under `Meta`. A route's own `@meta` slot (`/parallel`) is
|
|
288
|
+
collected after the route, so its server title beats a client title the
|
|
289
|
+
route's page pushes: push the live title from a component the `@meta` slot
|
|
290
|
+
renders, or move that server title into the route's handler or loader. In
|
|
291
|
+
a `clientUrls()` group the page and its group layouts all render inside the
|
|
292
|
+
matched route's segment, so their pushes land there, after the values the
|
|
293
|
+
route's loaders pushed.
|
|
294
|
+
- **A value or a promise.** `push(promise)` works as on the server: the value
|
|
295
|
+
takes its place when it is pushed, readers keep what they showed until it
|
|
296
|
+
resolves (never a blank or a pending value), and one that rejects or
|
|
297
|
+
resolves to `null` or `undefined` is dropped. Resolution is shallow: a
|
|
298
|
+
promise inside a value (a crumb's `content`) passes through, for the reader
|
|
299
|
+
to `use()`. A promise is the same value only as the same reference, so push
|
|
300
|
+
a stable one: from a loader, a `load()` kept in state, or `useMemo`. A
|
|
301
|
+
newer render's push or an unmount drops a pending promise's result.
|
|
302
|
+
- **Order between components.** Two components that push the same handle in
|
|
303
|
+
the same segment show in tree order, whichever commits first: when one
|
|
304
|
+
joins, the segment's pushers of that handle render once more, before
|
|
305
|
+
paint. One nested inside the other shows before it, so the outer one wins a
|
|
306
|
+
last-wins collect; push such values from one component.
|
|
307
|
+
- **Unmount removes them.** A component that unmounts (a toggle, a tab, an
|
|
308
|
+
error boundary that replaces it, a navigation away) takes its values with
|
|
309
|
+
it in the same commit, and the server's show again. A remount with new data
|
|
310
|
+
pushes new values; nothing is left from the last mount.
|
|
311
|
+
- **Equal values do nothing.** A fresh object literal on each render is
|
|
312
|
+
compared by structure (arrays, plain objects, React elements, dates), so a
|
|
313
|
+
component that pushes and reads the same handle does not re-render itself;
|
|
314
|
+
a fresh top-level promise publishes only when what it resolves to changes.
|
|
315
|
+
A function, a `Map`, a `Set`, a class instance, or a promise inside a value
|
|
316
|
+
compares by identity, and a cyclic value never compares equal: a fresh one
|
|
317
|
+
on every render publishes every render, and a component that also reads
|
|
318
|
+
the handle loops. Keep such values stable (`useCallback`, `useMemo`) or out
|
|
319
|
+
of the pushed value.
|
|
320
|
+
- **Router store only.** The values live in the router's store, apart from
|
|
321
|
+
the server's: a history entry never stores them, so Back/Forward restores
|
|
322
|
+
the server's values and the restored page's components push again when
|
|
323
|
+
they commit.
|
|
324
|
+
|
|
325
|
+
When client values show (the server's own timing is in "When handle values
|
|
326
|
+
change (by design)" below):
|
|
327
|
+
|
|
328
|
+
| Case | What readers show |
|
|
329
|
+
| ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
330
|
+
| Document load | The server's values (the HTML), until every boundary has hydrated; then the client's, in one update. Where the server pushed nothing into the pushing component's segment, development warns that the first paint showed another value: push its first value from a handler or an `ssr: "await"` loader if the first paint must show it, or let a value only the client knows change after hydration. |
|
|
331
|
+
| Navigation that mounts the pusher | The client's values in the page's commit, before the browser paints, for readers already on screen and readers mounted with the pusher alike. With view transitions, when the transition's `ready` resolves, as the server's values do. |
|
|
332
|
+
| Navigation away | The leaving component's values are gone in the new page's commit. |
|
|
333
|
+
| A `load()` or state change while a navigation is held | The page on screen re-renders with its new data and its new values at once, with that page's server values; the held page brings its own when it commits. |
|
|
334
|
+
| A pushed promise | What readers showed, until it resolves; then its value, in the place it was pushed. |
|
|
335
|
+
| A late server push into the same segment | Lands before the client's values of that segment; with a last-wins collect the client's still show. |
|
|
336
|
+
| An action's revalidation | The segment's new server values, the client's after them. |
|
|
4
337
|
|
|
5
338
|
### useHandle()
|
|
6
339
|
|
|
7
|
-
|
|
340
|
+
Read a handle's collected value in a client component:
|
|
8
341
|
|
|
9
342
|
```tsx
|
|
10
343
|
"use client";
|
|
@@ -12,12 +345,12 @@ import { useHandle, Breadcrumbs } from "@rangojs/router/client";
|
|
|
12
345
|
|
|
13
346
|
function BreadcrumbNav() {
|
|
14
347
|
const crumbs = useHandle(Breadcrumbs);
|
|
15
|
-
// Array of { label, href }
|
|
348
|
+
// Array of { label, href } from every segment, server and client values
|
|
16
349
|
|
|
17
350
|
return (
|
|
18
351
|
<nav>
|
|
19
352
|
{crumbs.map((c, i) => (
|
|
20
|
-
<span key={
|
|
353
|
+
<span key={c.href}>
|
|
21
354
|
<a href={c.href}>{c.label}</a>
|
|
22
355
|
{i < crumbs.length - 1 && " > "}
|
|
23
356
|
</span>
|
|
@@ -30,20 +363,71 @@ function BreadcrumbNav() {
|
|
|
30
363
|
const lastCrumb = useHandle(Breadcrumbs, (data) => data.at(-1));
|
|
31
364
|
```
|
|
32
365
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
366
|
+
`useHandle` is read-only and re-renders when a handle update changes what it
|
|
367
|
+
returns: a navigation, an action, a late server push, a loader read, a client
|
|
368
|
+
push. On a document load it hydrates with the handle data its HTML was
|
|
369
|
+
rendered from, wherever the reader sits; late server pushes and client pushes
|
|
370
|
+
reach every reader once the document has finished streaming and every
|
|
371
|
+
boundary has hydrated (see `SKILL.md`, "Hydration").
|
|
372
|
+
|
|
373
|
+
**Equal content keeps its reference.** A re-delivery brings fresh objects (an
|
|
374
|
+
action's revalidation, a refresh, a Back/Forward, a late push, a client push
|
|
375
|
+
that changes nothing the reader collects). When what the reader returns
|
|
376
|
+
(the collected value, or the selector's result) is equal to what it holds,
|
|
377
|
+
`useHandle` returns the reference it already had and does not render, so an
|
|
378
|
+
effect keyed on `useHandle(Breadcrumbs)`, `useHandle(Meta)` or a default
|
|
379
|
+
`T[][]` collect runs once per change. A real change brings a new reference,
|
|
380
|
+
in the commit its page's values land in (the timing tables above and below).
|
|
381
|
+
Equal is the rule pushed values are compared with:
|
|
382
|
+
|
|
383
|
+
- arrays and plain objects by their entries, React elements by type, key and
|
|
384
|
+
props, dates by time;
|
|
385
|
+
- anything else by identity: a promise (a crumb's `content` promise included),
|
|
386
|
+
a `URL`, a function, a `Map`, a `Set`, a class instance;
|
|
387
|
+
- down to 32 levels; a value nested deeper, or a cyclic one, counts as
|
|
388
|
+
changed.
|
|
389
|
+
|
|
390
|
+
The comparison walks the value on each update, so it costs what the value's
|
|
391
|
+
size does; a sub-object shared at several places is compared once.
|
|
392
|
+
|
|
393
|
+
**What a response always brings fresh.** A server-pushed value reaches the
|
|
394
|
+
client through the RSC response, which decodes some values anew each time, and
|
|
395
|
+
those never compare equal:
|
|
396
|
+
|
|
397
|
+
- a client component element (`content: <OrderIcon />` on a crumb): its type
|
|
398
|
+
decodes as a new lazy reference on every response;
|
|
399
|
+
- async server content (an async server component), decoded the same way;
|
|
400
|
+
- a promise (`push(promise)` resolves it, but a promise nested in a value,
|
|
401
|
+
such as a crumb's `content`, stays one).
|
|
402
|
+
|
|
403
|
+
A value holding one of these, anywhere inside, is a new reference on every
|
|
404
|
+
re-delivery, as is a value holding a fresh function from a client push. Key
|
|
405
|
+
the effect on a primitive the value carries instead, such as a crumb's `href`
|
|
406
|
+
or `label`: `useHandle(Breadcrumbs, (c) => c.at(-1)?.href)`. Plain text and
|
|
407
|
+
host elements (`<b>`, `<span>`) holding only those decode equal.
|
|
408
|
+
|
|
409
|
+
### Meta and Breadcrumbs
|
|
410
|
+
|
|
411
|
+
- `Meta` takes descriptors (`{ title }`, `{ name, content }`,
|
|
412
|
+
`{ property, content }`, ...). A keyed descriptor (`title`, `charSet`, a
|
|
413
|
+
`name`, `property` or `httpEquiv` with `content`, a `link` whose `rel` is
|
|
414
|
+
`canonical`, `icon` or `apple-touch-icon`) keeps its last value, across
|
|
415
|
+
segments and within one, so a client `{ title }` replaces the server title
|
|
416
|
+
of its segment and of every segment above it. Every other descriptor
|
|
417
|
+
(`script:ld+json`, any other `link`) appends, so it needs one writer.
|
|
418
|
+
`<Html.Meta />` renders the result in `<head>` and keeps `document.title`
|
|
419
|
+
current.
|
|
420
|
+
- `Breadcrumbs` takes `{ label, href, content? }` and keeps one crumb per
|
|
421
|
+
`href`, in the position of its first push and with its last value: a client
|
|
422
|
+
crumb for the same `href` renames the server's in place. Guide:
|
|
423
|
+
`/breadcrumbs`.
|
|
41
424
|
|
|
42
425
|
### When handle values change (by design)
|
|
43
426
|
|
|
44
427
|
Handle values are reactive and arrive on their own channel, by design. A value
|
|
45
428
|
belongs to the segment that pushed it and reaches `useHandle` when that push
|
|
46
|
-
settles.
|
|
429
|
+
settles. This table is the server's values; a client component's are in
|
|
430
|
+
"When client values show" above. The page never waits for a handle, and a handle never shows the next
|
|
47
431
|
page's values over the page being left:
|
|
48
432
|
|
|
49
433
|
| Case | What `useHandle` returns |
|
|
@@ -124,10 +508,11 @@ fires when it arrives. Trade-offs:
|
|
|
124
508
|
navigations count; a same-URL refresh or an action's revalidation re-delivers
|
|
125
509
|
an equal value and sends nothing. Under dev StrictMode the mount effect runs
|
|
126
510
|
twice; the path check covers it.
|
|
127
|
-
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
511
|
+
- An equal value keeps its reference (`useHandle()` above), so an action's
|
|
512
|
+
revalidation that re-delivers the same page view does not run the effect,
|
|
513
|
+
with the default collector too. Collecting the item, as here, keeps the
|
|
514
|
+
value small and the effect simple; with the default collector,
|
|
515
|
+
`useHandle(PageView, (s) => s.at(-1)?.at(-1))` does the same.
|
|
131
516
|
- Async values arrive later. With `push(promise)` the page_view fires when the
|
|
132
517
|
promise resolves, with the resolved value; read the title from the value, not
|
|
133
518
|
from `document.title`.
|
|
@@ -136,15 +521,6 @@ fires when it arrives. Trade-offs:
|
|
|
136
521
|
navigation so every route counts.
|
|
137
522
|
- Back/Forward fires (the entry's path differs from the current one).
|
|
138
523
|
|
|
139
|
-
A server component that pushes after an `await` throws `LateHandlePushError`:
|
|
140
|
-
reserve the slot in the handler with `ctx.use(Handle).defer()` and resolve it
|
|
141
|
-
from the component (`/breadcrumbs`, "Deferred content").
|
|
142
|
-
|
|
143
|
-
On a document load `useHandle` hydrates with the handle data its HTML was
|
|
144
|
-
rendered from, wherever the reader sits; late loader pushes reach every reader
|
|
145
|
-
once the document has finished streaming and every boundary has hydrated (see
|
|
146
|
-
`SKILL.md`, "Hydration").
|
|
147
|
-
|
|
148
524
|
Handles can be passed as props from server to client components:
|
|
149
525
|
|
|
150
526
|
```tsx
|
|
@@ -175,6 +175,10 @@ skips the route's `transition()` for this navigation, see below), and `state`
|
|
|
175
175
|
(location state: `[MyState(value)]` for typed entries, or plain
|
|
176
176
|
structured-clone-safe data; see [`./state.md`](./state.md)).
|
|
177
177
|
|
|
178
|
+
A forward navigation (Link, push, replace, setSearchParams) whose server answer
|
|
179
|
+
redraws nothing (every segment held) does not scroll, whatever `scroll` says.
|
|
180
|
+
Back/Forward still restores its scroll.
|
|
181
|
+
|
|
178
182
|
Target resolution: a path starting with `/` is app-absolute and gets the
|
|
179
183
|
router `basename` prefixed. A relative path (`"cart"`, `"./cart"`) resolves
|
|
180
184
|
against the current `include()` mount, so `router.push("cart")` inside
|
package/skills/hooks/urls.md
CHANGED
|
@@ -108,6 +108,9 @@ setSearchParams({ view: "grid" }, { revalidate: false });
|
|
|
108
108
|
Options: `replace` (default false — push), `scroll` (default true),
|
|
109
109
|
`revalidate` (default true; `false` skips the server fetch — legal because
|
|
110
110
|
the setter never changes the pathname).
|
|
111
|
+
A forward navigation (Link, push, replace, setSearchParams) whose server answer
|
|
112
|
+
redraws nothing (every segment held) does not scroll, whatever `scroll` says.
|
|
113
|
+
Back/Forward still restores its scroll. A `revalidate: false` update still scrolls.
|
|
111
114
|
|
|
112
115
|
### When the URL hooks change
|
|
113
116
|
|