@rangojs/router 0.36.1 → 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-OPRGB3DF.js → runtime-discovery-T7ZLA6E3.js} +2 -2
- package/dist/bin/{temp-rsc-server-FV4ERMAS.js → temp-rsc-server-FGGXPKVQ.js} +2 -2
- package/dist/types/browser/event-controller.d.ts +53 -2
- package/dist/types/browser/fetched-loader-handles.d.ts +57 -14
- 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 +7 -0
- package/dist/types/handles/pushed-by.d.ts +11 -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/render-route.d.ts +74 -14
- 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 +28 -9
- 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/ux-architecture/SKILL.md +4 -0
- package/src/browser/event-controller.ts +265 -4
- package/src/browser/fetched-loader-handles.ts +227 -54
- 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 +12 -0
- package/src/handles/pushed-by.ts +55 -1
- package/src/loader-store.ts +26 -0
- package/src/rsc/loader-fetch.ts +17 -0
- 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 +5 -0
- package/src/testing/dom.entry.ts +2 -0
- package/src/testing/render-route.tsx +317 -72
- package/src/use-loader.tsx +16 -5
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
|
|
package/skills/links/SKILL.md
CHANGED
|
@@ -700,9 +700,11 @@ The answer is shared between a group of pages
|
|
|
700
700
|
without running them, which is right for the pages with the same params.
|
|
701
701
|
|
|
702
702
|
A scoped answer, page-only or grouped, is sent `cache-control: private,
|
|
703
|
-
no-cache`
|
|
704
|
-
|
|
705
|
-
|
|
703
|
+
no-cache` in place of the route's own header, unless that one says
|
|
704
|
+
`no-store`, which it keeps, and the document cache does not store it: a
|
|
705
|
+
shared cache keys a navigation by its URL, not by the page that sent it
|
|
706
|
+
(`/document-cache` → "What's NOT Cached" and "Cache-Control on a partial
|
|
707
|
+
nothing may reuse").
|
|
706
708
|
|
|
707
709
|
Page-only entries are keyed by the page's full URL, query string and hash
|
|
708
710
|
included, so each page prefetches those targets again: `?page=2` and `?page=3`
|
package/skills/loader/SKILL.md
CHANGED
|
@@ -1078,7 +1078,9 @@ data-shaped, and middleware CAN emit a real pre-stream 302). Data-dependent
|
|
|
1078
1078
|
Loader bodies can WRITE handles with handler parity — `ctx.use(Handle)`
|
|
1079
1079
|
returns the push function, legal for the whole body, streaming loaders
|
|
1080
1080
|
included. This is how data-derived page titles and breadcrumb trails live
|
|
1081
|
-
where the data lives
|
|
1081
|
+
where the data lives. Whether a value belongs in a loader, a handler or a
|
|
1082
|
+
client component (`usePushHandle`) is decided in one place:
|
|
1083
|
+
[hooks → "Where should a handle value come from?"](../hooks/handle-and-actions.md#where-should-a-handle-value-come-from).
|
|
1082
1084
|
|
|
1083
1085
|
```typescript
|
|
1084
1086
|
import { Meta, Breadcrumbs } from "./handles";
|
|
@@ -1118,20 +1120,37 @@ A fetchable loader read on its own — `load()` / `refetch()`, or a
|
|
|
1118
1120
|
loaders it awaits included). The browser applies them after the read's data,
|
|
1119
1121
|
in place of the loader's earlier values in the segment that registers it, so
|
|
1120
1122
|
`useHandle` readers and `<Html.Meta />` (the tab title) show them; a pushed
|
|
1121
|
-
promise applies once it settles.
|
|
1123
|
+
promise applies once it settles. A read that pushes nothing clears the
|
|
1124
|
+
loader's earlier values, as a navigation that re-runs the loader does. The
|
|
1125
|
+
reply carries what the body pushed by the time it returned: a push it makes
|
|
1126
|
+
later (a timer, a promise it did not await) is not applied, and development
|
|
1127
|
+
warns that it came too late, so push a promise or reserve the slot with
|
|
1128
|
+
`.defer()` before returning. This is how a live part of a page keeps its
|
|
1122
1129
|
title current: a rename that reaches the page through a refresh renames the
|
|
1123
|
-
tab too. They apply
|
|
1124
|
-
|
|
1130
|
+
tab too. They apply together with the read's data, exactly when it lands: the
|
|
1131
|
+
newest read of the reader's bucket, for the page it was sent from. The history
|
|
1132
|
+
entry keeps the read's data and title together, so Back shows the page as you
|
|
1133
|
+
left it, and a late push from the document's own render never undoes a read's
|
|
1134
|
+
values. A plain `load()` from a component outside the page's route tree (a
|
|
1135
|
+
`useFetchLoader` in a root-level header) keeps its data with that component
|
|
1136
|
+
and changes no handle value, even for a loader the page registers;
|
|
1137
|
+
development warns once. To change the title from there, refresh the page's
|
|
1138
|
+
own data from a reader inside the route, or call `useRouter().refresh()`.
|
|
1139
|
+
(With a `key` it shares with a reader inside the route, its data is the
|
|
1140
|
+
page's and its values apply.) A value pushed by a loader the
|
|
1125
1141
|
body awaits that the page also registers belongs to that loader's own run,
|
|
1126
1142
|
which the read did not refresh: the page keeps that run's values, as it keeps
|
|
1127
1143
|
its data. A loader that no segment on the page registers with `loader()` (a
|
|
1128
1144
|
`useFetchLoader()` of an unregistered loader) has no segment to own its
|
|
1129
1145
|
values: they are not applied, and development warns once. Register the loader
|
|
1130
|
-
on the route or layout whose title or crumbs it writes
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1146
|
+
on the route or layout whose title or crumbs it writes, or push the values
|
|
1147
|
+
from the component that renders the data with `usePushHandle`. A read for
|
|
1148
|
+
data the page does not show — `load()` with `params` (a hover preview of
|
|
1149
|
+
another item), or with a `body` or a method other than GET (a form post) —
|
|
1150
|
+
keeps its data and drops its pushes: the page's title and crumbs stay those
|
|
1151
|
+
of what it shows. When the page does show that data (a client-side picker
|
|
1152
|
+
that loads the selected item with `params`), the component that renders it
|
|
1153
|
+
pushes its title with `usePushHandle`.
|
|
1135
1154
|
|
|
1136
1155
|
Under `cache()`, loader pushes are not stored with the cached segments: the
|
|
1137
1156
|
loader re-runs on every hit and its live push is the only copy. (`ppr` keeps
|