@rangojs/router 0.0.0-experimental.fa8a383a → 0.0.0-experimental.fb4fdc18
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 +188 -35
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +1884 -537
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +7 -5
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +8 -0
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +33 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +93 -17
- package/skills/loader/SKILL.md +123 -46
- package/skills/middleware/SKILL.md +36 -3
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/parallel/SKILL.md +133 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/rango/SKILL.md +26 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +75 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/server-actions/SKILL.md +739 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +19 -1
- package/src/__internal.ts +1 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +44 -4
- package/src/browser/navigation-bridge.ts +95 -7
- package/src/browser/navigation-client.ts +128 -53
- package/src/browser/navigation-store.ts +68 -9
- package/src/browser/partial-update.ts +93 -12
- package/src/browser/prefetch/cache.ts +129 -21
- package/src/browser/prefetch/fetch.ts +156 -18
- package/src/browser/prefetch/queue.ts +92 -29
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +72 -8
- package/src/browser/react/NavigationProvider.tsx +82 -21
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +17 -4
- package/src/browser/react/use-router.ts +29 -9
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/rsc-router.tsx +60 -9
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +46 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +52 -25
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +46 -5
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/cache/taint.ts +55 -0
- package/src/client.tsx +84 -230
- package/src/context-var.ts +72 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +6 -1
- package/src/index.ts +49 -6
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +28 -2
- package/src/route-definition/dsl-helpers.ts +210 -35
- package/src/route-definition/helpers-types.ts +73 -20
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-types.ts +18 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +102 -25
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/lazy-includes.ts +6 -6
- package/src/router/loader-resolution.ts +159 -21
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +128 -192
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-middleware/background-revalidation.ts +12 -1
- package/src/router/match-middleware/cache-lookup.ts +74 -14
- package/src/router/match-middleware/cache-store.ts +21 -4
- package/src/router/match-middleware/segment-resolution.ts +53 -0
- package/src/router/match-result.ts +112 -9
- package/src/router/metrics.ts +6 -1
- package/src/router/middleware-types.ts +20 -33
- package/src/router/middleware.ts +56 -12
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +101 -17
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/revalidation.ts +15 -1
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +1 -0
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +114 -18
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/revalidation.ts +257 -127
- package/src/router/trie-matching.ts +18 -13
- package/src/router/types.ts +1 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +55 -7
- package/src/rsc/handler.ts +478 -383
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +18 -2
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +20 -1
- package/src/rsc/server-action.ts +12 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +15 -1
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +22 -62
- package/src/server/context.ts +76 -4
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +185 -57
- package/src/ssr/index.tsx +8 -1
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +145 -68
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +12 -1
- package/src/types/segments.ts +18 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +39 -6
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +18 -16
- package/src/use-loader.tsx +77 -5
- package/src/vite/debug.ts +184 -0
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +36 -4
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +175 -74
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +13 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- package/src/vite/plugins/cjs-to-esm.ts +5 -0
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +16 -4
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +52 -28
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +563 -316
- package/src/vite/plugins/performance-tracks.ts +96 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/use-cache-transform.ts +56 -43
- package/src/vite/plugins/version-injector.ts +37 -11
- package/src/vite/rango.ts +63 -11
- package/src/vite/router-discovery.ts +732 -86
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +38 -5
- package/src/vite/utils/shared-utils.ts +3 -2
package/skills/route/SKILL.md
CHANGED
|
@@ -33,6 +33,26 @@ urls(({ path }) => [
|
|
|
33
33
|
]);
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
### Optional URL params at runtime
|
|
37
|
+
|
|
38
|
+
Absent optional params are **omitted from `ctx.params`** — `ctx.params.<name>`
|
|
39
|
+
reads as `undefined`, matching the `RouteParams<"name">` type
|
|
40
|
+
(`{ query?: string }`). Use `??` to default and `=== undefined` to check
|
|
41
|
+
absence:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
path("/search/:query?", (ctx) => {
|
|
45
|
+
const query = ctx.params.query ?? ""; // works — undefined coalesces
|
|
46
|
+
if (ctx.params.query === undefined) return <EmptySearch />;
|
|
47
|
+
return <Results query={ctx.params.query} />;
|
|
48
|
+
}, { name: "search" });
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
For the common pattern of an optional locale prefix
|
|
52
|
+
(`include("/:locale?", routes)`) and the wider react-intl integration —
|
|
53
|
+
locale detection, fallback chains, URL generation with absent locale —
|
|
54
|
+
see `/i18n`.
|
|
55
|
+
|
|
36
56
|
## Route Handler Patterns
|
|
37
57
|
|
|
38
58
|
### Component Function
|
|
@@ -181,6 +201,37 @@ String keys still work (`ctx.set("key", value)` / `ctx.get("key")`), but
|
|
|
181
201
|
Only route handlers and middleware can call `ctx.set()`. Layouts, parallels,
|
|
182
202
|
and intercepts can only read via `ctx.get()`.
|
|
183
203
|
|
|
204
|
+
#### Non-cacheable context variables
|
|
205
|
+
|
|
206
|
+
Mark a var as non-cacheable when it holds inherently request-specific data
|
|
207
|
+
(sessions, auth tokens, per-request IDs). There are two ways:
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
// Var-level: every value written to this var is non-cacheable
|
|
211
|
+
const Session = createVar<SessionData>({ cache: false });
|
|
212
|
+
|
|
213
|
+
// Write-level: escalate a normally-cacheable var for this specific write
|
|
214
|
+
const Theme = createVar<string>();
|
|
215
|
+
ctx.set(Theme, userTheme, { cache: false });
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
"Least cacheable wins" — if either the var definition or the write site says
|
|
219
|
+
`cache: false`, the value is non-cacheable.
|
|
220
|
+
|
|
221
|
+
Reading a non-cacheable var inside `cache()` or `"use cache"` throws at
|
|
222
|
+
runtime. This prevents request-specific data from leaking into cached output:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// This throws — Session is non-cacheable
|
|
226
|
+
async function CachedWidget(ctx) {
|
|
227
|
+
"use cache";
|
|
228
|
+
const session = ctx.get(Session); // Error: non-cacheable var read inside cache scope
|
|
229
|
+
return <Widget />;
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Cacheable vars (the default) can be read freely inside cache scopes.
|
|
234
|
+
|
|
184
235
|
### Revalidation Contracts for Handler Data
|
|
185
236
|
|
|
186
237
|
Handler-first guarantees apply within a single full render pass. For partial
|
|
@@ -352,6 +403,30 @@ urls(({ path, layout }) => [
|
|
|
352
403
|
])
|
|
353
404
|
```
|
|
354
405
|
|
|
406
|
+
## Handler-attached `.use`
|
|
407
|
+
|
|
408
|
+
Page handlers can carry their own loader, middleware, error boundaries, parallels, and other defaults via a `.use` callback — so the page is self-contained and reusable across mount sites without re-wiring the same items.
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
const ProductPage: Handler<"/product/:slug"> = async (ctx) => {
|
|
412
|
+
const product = await ctx.use(ProductLoader);
|
|
413
|
+
return <ProductView product={product} />;
|
|
414
|
+
};
|
|
415
|
+
ProductPage.use = () => [
|
|
416
|
+
loader(ProductLoader),
|
|
417
|
+
loading(<ProductSkeleton />),
|
|
418
|
+
middleware(async (ctx, next) => {
|
|
419
|
+
await next();
|
|
420
|
+
ctx.header("Cache-Control", "private, max-age=60");
|
|
421
|
+
}),
|
|
422
|
+
];
|
|
423
|
+
|
|
424
|
+
// Mount site has no per-page wiring — defaults travel with the handler.
|
|
425
|
+
path("/product/:slug", ProductPage, { name: "product" });
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
Explicit `use()` at the mount site merges with `handler.use` (handler defaults first, explicit second). See [skills/handler-use](../handler-use/SKILL.md) for the merge order, allowed item types per mount site, and override semantics.
|
|
429
|
+
|
|
355
430
|
## Complete Example
|
|
356
431
|
|
|
357
432
|
```typescript
|
|
@@ -78,16 +78,21 @@ interface RSCRouterOptions<TEnv> {
|
|
|
78
78
|
// Document component wrapping entire app
|
|
79
79
|
document?: ComponentType<{ children: ReactNode }>;
|
|
80
80
|
|
|
81
|
+
// URL prefix for sub-path deployments (e.g. "/admin")
|
|
82
|
+
// All routes, reverse(), href(), Link, redirect(), and router.use()
|
|
83
|
+
// patterns are automatically prefixed. Route names stay unprefixed.
|
|
84
|
+
basename?: string;
|
|
85
|
+
|
|
81
86
|
// Enable per-request performance timeline (console waterfall + Server-Timing header)
|
|
82
87
|
debugPerformance?: boolean;
|
|
83
88
|
|
|
84
89
|
// Default error boundary
|
|
85
90
|
defaultErrorBoundary?: ReactNode | ErrorBoundaryHandler;
|
|
86
91
|
|
|
87
|
-
// Default not-found boundary
|
|
92
|
+
// Default not-found boundary for notFound() thrown in handlers/loaders
|
|
88
93
|
defaultNotFoundBoundary?: ReactNode | NotFoundBoundaryHandler;
|
|
89
94
|
|
|
90
|
-
// Component for 404
|
|
95
|
+
// Component for 404 (no route match, or notFound() without a boundary)
|
|
91
96
|
notFound?: ReactNode | ((props: { pathname: string }) => ReactNode);
|
|
92
97
|
|
|
93
98
|
// Error logging callback
|
|
@@ -124,6 +129,36 @@ interface RSCRouterOptions<TEnv> {
|
|
|
124
129
|
}
|
|
125
130
|
```
|
|
126
131
|
|
|
132
|
+
## Basename (Sub-Path Deployment)
|
|
133
|
+
|
|
134
|
+
When your app is served under a sub-path (e.g. `/admin` or `/v2`), set `basename`:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
const router = createRouter({
|
|
138
|
+
basename: "/admin",
|
|
139
|
+
document: Document,
|
|
140
|
+
}).routes(({ path, include }) => [
|
|
141
|
+
path("/", Dashboard, { name: "home" }), // matches /admin
|
|
142
|
+
path("/users", Users, { name: "users" }), // matches /admin/users
|
|
143
|
+
include("/api", apiPatterns, { name: "api" }), // matches /admin/api/*
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
router.reverse("home"); // "/admin"
|
|
147
|
+
router.reverse("users"); // "/admin/users"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Router-owned APIs are basename-aware:
|
|
151
|
+
|
|
152
|
+
- `reverse()` returns prefixed paths
|
|
153
|
+
- `<Link to="/users">` renders `<a href="/admin/users">`
|
|
154
|
+
- `redirect("/login")` redirects to `"/admin/login"`
|
|
155
|
+
- `router.use("/users/*", mw)` matches `/admin/users/*`
|
|
156
|
+
- `useRouter().push("/users")` navigates to `/admin/users`
|
|
157
|
+
- Route names stay unprefixed (`"home"`, not `"admin.home"`)
|
|
158
|
+
|
|
159
|
+
Note: `href()` is a raw path helper and does **not** auto-prefix with basename.
|
|
160
|
+
Use `reverse()` or `<Link>` for basename-aware URLs.
|
|
161
|
+
|
|
127
162
|
## Using the Request Handler
|
|
128
163
|
|
|
129
164
|
The router provides a `fetch` method to handle RSC requests:
|
|
@@ -290,6 +325,56 @@ const router = createRouter({
|
|
|
290
325
|
export default router;
|
|
291
326
|
```
|
|
292
327
|
|
|
328
|
+
## Not Found Handling
|
|
329
|
+
|
|
330
|
+
Two distinct 404 scenarios:
|
|
331
|
+
|
|
332
|
+
**1. No route matches the URL** — the router renders the `notFound` component from `createRouter()` config. This is automatic.
|
|
333
|
+
|
|
334
|
+
**2. A handler/loader calls `notFound()`** — signals that the route matched but the data doesn't exist (e.g., invalid product ID).
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
import { notFound } from "@rangojs/router";
|
|
338
|
+
|
|
339
|
+
// In a handler or loader
|
|
340
|
+
path("/product/:slug", async (ctx) => {
|
|
341
|
+
const product = await db.getProduct(ctx.params.slug);
|
|
342
|
+
if (!product) notFound("Product not found");
|
|
343
|
+
return <ProductPage product={product} />;
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Fallback chain for `notFound()`
|
|
348
|
+
|
|
349
|
+
When `notFound()` is thrown, the router looks for a fallback in this order:
|
|
350
|
+
|
|
351
|
+
1. **`notFoundBoundary()`** — nearest boundary in the route tree (route-level)
|
|
352
|
+
2. **`defaultNotFoundBoundary`** — from `createRouter()` config (app-level)
|
|
353
|
+
3. **`notFound`** — from `createRouter()` config (same component used for no-route-match)
|
|
354
|
+
4. **Default `<h1>Not Found</h1>`** — built-in fallback
|
|
355
|
+
|
|
356
|
+
All cases set HTTP 404 status.
|
|
357
|
+
|
|
358
|
+
### notFoundBoundary
|
|
359
|
+
|
|
360
|
+
Wrap routes with `notFoundBoundary()` for route-specific not-found UI:
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
urls(({ path, layout }) => [
|
|
364
|
+
layout(ShopLayout, () => [
|
|
365
|
+
notFoundBoundary(({ notFound: info }) => (
|
|
366
|
+
<div>
|
|
367
|
+
<h1>Not Found</h1>
|
|
368
|
+
<p>{info.message}</p>
|
|
369
|
+
</div>
|
|
370
|
+
)),
|
|
371
|
+
path("/product/:slug", ProductPage),
|
|
372
|
+
]),
|
|
373
|
+
]);
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
`notFoundBoundary` receives `{ notFound: NotFoundInfo }` where `NotFoundInfo` contains `message`, `segmentId`, `segmentType`, and `pathname`.
|
|
377
|
+
|
|
293
378
|
## Including Sub-patterns
|
|
294
379
|
|
|
295
380
|
```typescript
|