@rangojs/router 0.0.0-experimental.20 → 0.0.0-experimental.20dbba0c
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/AGENTS.md +4 -0
- package/README.md +172 -50
- package/dist/bin/rango.js +138 -50
- package/dist/vite/index.js +1160 -508
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +17 -16
- package/skills/breadcrumbs/SKILL.md +252 -0
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +49 -8
- package/skills/document-cache/SKILL.md +2 -2
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +61 -51
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +91 -17
- package/skills/loader/SKILL.md +107 -24
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +560 -0
- package/skills/migrate-react-router/SKILL.md +765 -0
- package/skills/parallel/SKILL.md +185 -0
- package/skills/prerender/SKILL.md +112 -70
- package/skills/rango/SKILL.md +24 -23
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +58 -4
- package/skills/router-setup/SKILL.md +95 -5
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +38 -24
- package/src/__internal.ts +92 -0
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +5 -0
- package/src/browser/link-interceptor.ts +4 -0
- package/src/browser/navigation-bridge.ts +175 -17
- package/src/browser/navigation-client.ts +177 -44
- package/src/browser/navigation-store.ts +68 -9
- package/src/browser/navigation-transaction.ts +11 -9
- package/src/browser/partial-update.ts +113 -17
- package/src/browser/prefetch/cache.ts +275 -28
- package/src/browser/prefetch/fetch.ts +191 -46
- package/src/browser/prefetch/policy.ts +6 -0
- package/src/browser/prefetch/queue.ts +123 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +98 -14
- package/src/browser/react/NavigationProvider.tsx +89 -14
- package/src/browser/react/context.ts +7 -2
- 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 +11 -1
- package/src/browser/react/use-router.ts +29 -9
- package/src/browser/rsc-router.tsx +177 -66
- package/src/browser/scroll-restoration.ts +41 -42
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +73 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +67 -25
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +223 -74
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +48 -7
- package/src/cache/cf/cf-cache-store.ts +455 -15
- package/src/cache/cf/index.ts +5 -1
- package/src/cache/document-cache.ts +17 -7
- package/src/cache/index.ts +1 -0
- package/src/cache/taint.ts +55 -0
- package/src/client.rsc.tsx +2 -1
- package/src/client.tsx +85 -276
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/handle.ts +40 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/host/index.ts +0 -3
- package/src/index.rsc.ts +9 -36
- package/src/index.ts +79 -70
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +57 -15
- package/src/prerender.ts +138 -77
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +27 -2
- package/src/route-definition/dsl-helpers.ts +240 -40
- package/src/route-definition/helpers-types.ts +67 -19
- package/src/route-definition/index.ts +3 -3
- package/src/route-definition/redirect.ts +11 -3
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +18 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +129 -26
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +10 -7
- package/src/router/loader-resolution.ts +160 -22
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +128 -193
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +94 -17
- package/src/router/match-middleware/cache-store.ts +53 -10
- package/src/router/match-middleware/intercept-resolution.ts +9 -7
- package/src/router/match-middleware/segment-resolution.ts +61 -5
- package/src/router/match-result.ts +103 -18
- package/src/router/metrics.ts +238 -13
- package/src/router/middleware-types.ts +48 -27
- package/src/router/middleware.ts +201 -86
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +77 -11
- package/src/router/prerender-match.ts +114 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/revalidation.ts +27 -7
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +50 -5
- package/src/router/router-options.ts +50 -19
- package/src/router/segment-resolution/fresh.ts +215 -19
- package/src/router/segment-resolution/helpers.ts +30 -25
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +454 -301
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/trie-matching.ts +30 -6
- package/src/router/types.ts +1 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +89 -17
- package/src/rsc/handler.ts +563 -364
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/index.ts +0 -20
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +37 -10
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +47 -44
- package/src/rsc/server-action.ts +24 -10
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +11 -1
- package/src/search-params.ts +16 -13
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +109 -23
- package/src/server/context.ts +174 -19
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +218 -65
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/theme/index.ts +4 -13
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +140 -72
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-config.ts +17 -8
- package/src/types/route-entry.ts +19 -1
- package/src/types/segments.ts +2 -5
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +39 -6
- package/src/urls/path-helper.ts +48 -13
- 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/discovery/bundle-postprocess.ts +61 -89
- package/src/vite/discovery/discover-routers.ts +7 -4
- package/src/vite/discovery/prerender-collection.ts +162 -88
- package/src/vite/discovery/state.ts +17 -13
- package/src/vite/index.ts +8 -3
- package/src/vite/plugin-types.ts +51 -79
- 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 +1 -3
- 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-internal-ids.ts +257 -40
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +190 -217
- package/src/vite/router-discovery.ts +241 -45
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/package-resolution.ts +34 -1
- package/src/vite/utils/prerender-utils.ts +97 -5
- package/src/vite/utils/shared-utils.ts +3 -2
- package/skills/testing/SKILL.md +0 -226
- package/src/route-definition/route-function.ts +0 -119
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: host-router
|
|
3
|
+
description: Multi-app host routing with domain/subdomain patterns
|
|
4
|
+
argument-hint:
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Host Router
|
|
8
|
+
|
|
9
|
+
Route requests to different apps based on domain, subdomain, or path prefix patterns. Supports middleware, lazy loading, cookie-based host override for dev, and a fallback handler.
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createHostRouter, defineHosts } from "@rangojs/router/host";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Basic Setup
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// host-router.ts
|
|
21
|
+
import { createHostRouter } from "@rangojs/router/host";
|
|
22
|
+
|
|
23
|
+
const router = createHostRouter();
|
|
24
|
+
|
|
25
|
+
router.host(["."]).map(() => import("./apps/main"));
|
|
26
|
+
router.host(["admin.*"]).map(() => import("./apps/admin"));
|
|
27
|
+
router.host(["api.*"]).map(() => import("./apps/api"));
|
|
28
|
+
|
|
29
|
+
export default {
|
|
30
|
+
fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
|
31
|
+
return router.match(request, { env, ctx });
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Each `.map()` receives either a direct handler `(request, input) => Response` or a lazy import `() => import(...)`. Lazy imports resolve a module with a `default` export that is either a handler function or another `HostRouter` (for nesting).
|
|
37
|
+
|
|
38
|
+
## Pattern Syntax
|
|
39
|
+
|
|
40
|
+
| Pattern | Matches |
|
|
41
|
+
| ----------------- | ---------------------------------------------- |
|
|
42
|
+
| `.` or `*` | Any apex domain (`example.com`) |
|
|
43
|
+
| `**` | Any domain (apex + all subdomains) |
|
|
44
|
+
| `*.` | Any single-level subdomain (`www.example.com`) |
|
|
45
|
+
| `**. ` | Any multi-level subdomain (`a.b.example.com`) |
|
|
46
|
+
| `example.com` | Exact domain |
|
|
47
|
+
| `*.com` | Any apex `.com` domain |
|
|
48
|
+
| `*.example.com` | Single subdomain of `example.com` |
|
|
49
|
+
| `**.example.com` | Any depth subdomain of `example.com` |
|
|
50
|
+
| `admin.*` | `admin` subdomain of any apex domain |
|
|
51
|
+
| `admin.**` | `admin` subdomain of any domain |
|
|
52
|
+
| `admin.` | `admin` subdomain of any apex (no wildcard) |
|
|
53
|
+
| `example.com/api` | Domain + path prefix (prefix match) |
|
|
54
|
+
|
|
55
|
+
Patterns are tested in registration order. First match wins.
|
|
56
|
+
|
|
57
|
+
## `defineHosts` for Type Safety
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { defineHosts } from "@rangojs/router/host";
|
|
61
|
+
|
|
62
|
+
const hosts = defineHosts({
|
|
63
|
+
admin: "admin.*",
|
|
64
|
+
api: "api.*",
|
|
65
|
+
app: [".", "www.*"],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
router.host(hosts.admin).map(() => import("./apps/admin"));
|
|
69
|
+
router.host(hosts.app).map(() => import("./apps/main"));
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Returns a frozen object — keys are autocompleted by TypeScript.
|
|
73
|
+
|
|
74
|
+
## Middleware
|
|
75
|
+
|
|
76
|
+
Global middleware runs for every matched route. Per-route middleware runs only for that host pattern.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const router = createHostRouter();
|
|
80
|
+
|
|
81
|
+
// Global — runs for all routes
|
|
82
|
+
router.use(async (request, input, next) => {
|
|
83
|
+
console.log(`[${new Date().toISOString()}] ${request.url}`);
|
|
84
|
+
return next();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Per-route
|
|
88
|
+
router
|
|
89
|
+
.host(["admin.*"])
|
|
90
|
+
.use(requireAuth)
|
|
91
|
+
.map(() => import("./apps/admin"));
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Middleware signature: `(request: Request, input: RouterRequestInput, next: () => Promise<Response>) => Promise<Response>`
|
|
95
|
+
|
|
96
|
+
Calling `next()` more than once throws.
|
|
97
|
+
|
|
98
|
+
## Fallback Handler
|
|
99
|
+
|
|
100
|
+
Handles cookie-override errors when `hostOverride` is configured (e.g., override from a disallowed host, invalid cookie hostname). The fallback does **not** catch unmatched hosts — those throw `NoRouteMatchError`. Catch that at the worker level if you need a 404.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const router = createHostRouter({
|
|
104
|
+
hostOverride: { cookieName: "x-dev-host", allowedHosts: ["localhost"] },
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Called when cookie override fails (not for general unmatched hosts)
|
|
108
|
+
router.fallback().map((request) => {
|
|
109
|
+
return new Response("Invalid host override", { status: 400 });
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For unmatched hosts without `hostOverride`, catch `NoRouteMatchError` in your worker fetch:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { NoRouteMatchError } from "@rangojs/router/host";
|
|
117
|
+
|
|
118
|
+
export default {
|
|
119
|
+
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
|
120
|
+
try {
|
|
121
|
+
return await router.match(request, { env, ctx });
|
|
122
|
+
} catch (err) {
|
|
123
|
+
if (err instanceof NoRouteMatchError) {
|
|
124
|
+
return new Response("Not Found", { status: 404 });
|
|
125
|
+
}
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Cookie-Based Host Override
|
|
133
|
+
|
|
134
|
+
For development: route requests to a different app based on a cookie value, allowing developers to test different host routes from a single domain.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
const router = createHostRouter({
|
|
138
|
+
hostOverride: {
|
|
139
|
+
cookieName: "x-dev-host",
|
|
140
|
+
allowedHosts: ["localhost", "**.dev.example.com"],
|
|
141
|
+
validate: (request, cookieValue, input) => {
|
|
142
|
+
// Optional custom validation — return the effective hostname
|
|
143
|
+
return cookieValue;
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
When a request arrives:
|
|
150
|
+
|
|
151
|
+
1. If no cookie → use actual hostname
|
|
152
|
+
2. If cookie present and host is in `allowedHosts` → use cookie value as hostname
|
|
153
|
+
3. If cookie present but host not allowed → throw `HostOverrideNotAllowedError`
|
|
154
|
+
|
|
155
|
+
Without a custom `validate`, the cookie value is validated as a hostname via `new URL()`.
|
|
156
|
+
|
|
157
|
+
## Debug Mode
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const router = createHostRouter({ debug: true });
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Logs pattern matching, route registration, and cookie override decisions to console.
|
|
164
|
+
|
|
165
|
+
## Testing
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { createTestRequest, testPattern } from "@rangojs/router/host/testing";
|
|
169
|
+
|
|
170
|
+
// Test pattern matching
|
|
171
|
+
testPattern("admin.*", "admin.example.com"); // true
|
|
172
|
+
testPattern([".", "www.*"], "example.com"); // true
|
|
173
|
+
|
|
174
|
+
// Create requests for integration tests
|
|
175
|
+
const request = createTestRequest({
|
|
176
|
+
host: "admin.example.com",
|
|
177
|
+
path: "/dashboard",
|
|
178
|
+
cookies: { "x-dev-host": "api.example.com" },
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Test which route would match (without executing)
|
|
182
|
+
router.test("admin.example.com"); // { pattern, handler } | null
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Error Types
|
|
186
|
+
|
|
187
|
+
All errors extend `HostRouterError`:
|
|
188
|
+
|
|
189
|
+
| Error | When |
|
|
190
|
+
| ----------------------------- | ------------------------------------------- |
|
|
191
|
+
| `InvalidPatternError` | Pattern is empty, non-string, or has spaces |
|
|
192
|
+
| `HostOverrideNotAllowedError` | Cookie override from disallowed host |
|
|
193
|
+
| `InvalidHostnameError` | Cookie value isn't a valid hostname |
|
|
194
|
+
| `HostValidationError` | Custom `validate` function threw |
|
|
195
|
+
| `NoRouteMatchError` | No host pattern matched the request |
|
|
196
|
+
| `InvalidHandlerError` | Handler is not a function |
|
|
197
|
+
|
|
198
|
+
See the fallback section above for a `NoRouteMatchError` catch example.
|
|
199
|
+
|
|
200
|
+
## Nesting Host Routers
|
|
201
|
+
|
|
202
|
+
A lazy handler can resolve to another `HostRouter`:
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
// apps/regional.ts
|
|
206
|
+
import { createHostRouter } from "@rangojs/router/host";
|
|
207
|
+
|
|
208
|
+
const regional = createHostRouter();
|
|
209
|
+
regional.host(["us.*"]).map(() => import("./regions/us"));
|
|
210
|
+
regional.host(["eu.*"]).map(() => import("./regions/eu"));
|
|
211
|
+
|
|
212
|
+
export default regional;
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
// host-router.ts
|
|
217
|
+
router.host(["**.regional.example.com"]).map(() => import("./apps/regional"));
|
|
218
|
+
```
|
|
@@ -311,3 +311,23 @@ export const shopPatterns = urls(({
|
|
|
311
311
|
]),
|
|
312
312
|
]);
|
|
313
313
|
```
|
|
314
|
+
|
|
315
|
+
## Handler-attached `.use`
|
|
316
|
+
|
|
317
|
+
Intercept handlers can carry their own middleware, loaders, loading state, error/notFound boundaries, and even nested `layout`/`route`/`when` defaults via `.use` — useful for self-contained modal components that travel with their own data and chrome.
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
const QuickViewModal: Handler = async (ctx) => {
|
|
321
|
+
const product = await ctx.use(ProductLoader);
|
|
322
|
+
return <QuickView product={product} />;
|
|
323
|
+
};
|
|
324
|
+
QuickViewModal.use = () => [
|
|
325
|
+
loader(ProductLoader),
|
|
326
|
+
loading(<QuickViewSkeleton />),
|
|
327
|
+
layout(<ModalChrome />),
|
|
328
|
+
];
|
|
329
|
+
|
|
330
|
+
intercept("@modal", "product", QuickViewModal);
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Explicit `use()` at the mount site merges with `handler.use` (handler defaults first, explicit second). See [skills/handler-use](../handler-use/SKILL.md) for merge order and the per-mount-site allowed-types table.
|
package/skills/layout/SKILL.md
CHANGED
|
@@ -308,3 +308,25 @@ export const shopPatterns = urls(({ path, layout, parallel, loader, revalidate }
|
|
|
308
308
|
]),
|
|
309
309
|
]);
|
|
310
310
|
```
|
|
311
|
+
|
|
312
|
+
## Handler-attached `.use`
|
|
313
|
+
|
|
314
|
+
Layout handlers can carry their own middleware, default parallels, and includes via `.use` so a layout becomes a self-contained unit reusable across mount sites.
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
const AdminLayout: Handler = (ctx) => {
|
|
318
|
+
const user = ctx.get(CurrentUser);
|
|
319
|
+
return <Admin user={user} />;
|
|
320
|
+
};
|
|
321
|
+
AdminLayout.use = () => [
|
|
322
|
+
middleware(requireAdmin),
|
|
323
|
+
parallel({ "@adminNotifs": AdminNotifsSlot }),
|
|
324
|
+
];
|
|
325
|
+
|
|
326
|
+
// Mount site declares structure only; defaults travel with the layout.
|
|
327
|
+
layout(AdminLayout, () => [
|
|
328
|
+
path("/admin", AdminIndex, { name: "admin.index" }),
|
|
329
|
+
]);
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Allowed item types in a layout's `.use` mirror the layout `use()` callback (the broadest set). Explicit `use()` at the mount site merges with `handler.use` (handler defaults first, explicit second). See [skills/handler-use](../handler-use/SKILL.md) for merge order and per-mount-site allowed types.
|
package/skills/links/SKILL.md
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: links
|
|
3
|
-
description: URL generation with ctx.reverse (server), href (client), useHref (mounted), useMount, and scopedReverse
|
|
4
|
-
argument-hint: [href|useHref|useMount|scopedReverse]
|
|
3
|
+
description: URL generation with ctx.reverse (server default), href (client), useHref (mounted), useMount, and scopedReverse
|
|
4
|
+
argument-hint: [ctx.reverse|href|useHref|useMount|scopedReverse]
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# Links & URL Generation
|
|
8
8
|
|
|
9
9
|
@rangojs/router provides different href APIs for server and client contexts.
|
|
10
10
|
|
|
11
|
+
**Default server API: `ctx.reverse()`.** Generate URLs from the handler context — it's typed, auto-fills mount params, and resolves local (`.name`) and absolute (`name.sub`) names.
|
|
12
|
+
|
|
13
|
+
**`reverse()` is server-only.** It depends on the route manifest and handler context, neither of which are available in the browser. Client components receive URLs as props, loader data, or server-action return values — they never call `reverse` directly.
|
|
14
|
+
|
|
11
15
|
## Server: ctx.reverse()
|
|
12
16
|
|
|
13
|
-
Available in route handlers via HandlerContext. Resolves named routes using the full route map.
|
|
17
|
+
Available in route handlers via HandlerContext. Resolves named routes using the full route map. This is the default way to generate URLs on the server.
|
|
14
18
|
|
|
15
19
|
```typescript
|
|
16
20
|
import { urls, scopedReverse } from "@rangojs/router";
|
|
@@ -103,7 +107,7 @@ path("/search", (ctx) => {
|
|
|
103
107
|
|
|
104
108
|
### scopedReverse() - type-safe ctx.reverse
|
|
105
109
|
|
|
106
|
-
Wraps `ctx.reverse` with local route type information for autocomplete and validation:
|
|
110
|
+
Wraps `ctx.reverse` with local route type information for autocomplete and validation. Runtime behavior is identical to `ctx.reverse` — `scopedReverse` is a type-only cast. The same dot-prefix rule applies: local names use `.name`, global names use `name.sub`.
|
|
107
111
|
|
|
108
112
|
```typescript
|
|
109
113
|
import { scopedReverse } from "@rangojs/router";
|
|
@@ -111,18 +115,83 @@ import { scopedReverse } from "@rangojs/router";
|
|
|
111
115
|
path("/product/:slug", (ctx) => {
|
|
112
116
|
const reverse = scopedReverse<typeof shopPatterns>(ctx.reverse);
|
|
113
117
|
|
|
114
|
-
reverse("cart"); //
|
|
115
|
-
reverse("product", { slug: "widget" }); //
|
|
116
|
-
reverse("blog.post");
|
|
117
|
-
reverse("/about"); // Path-based always allowed
|
|
118
|
+
reverse(".cart"); // Local name (dot-prefixed) — resolves in include scope
|
|
119
|
+
reverse(".product", { slug: "widget" }); // Local name with params
|
|
120
|
+
reverse("blog.post", { slug: "hi" }); // Global name (dotted) — full route map
|
|
118
121
|
|
|
119
122
|
return <ProductPage slug={ctx.params.slug} />;
|
|
120
123
|
}, { name: "product" })
|
|
121
124
|
```
|
|
122
125
|
|
|
126
|
+
`reverse()` does not accept raw path strings (`"/about"`). For static paths in client components, use `href("/about")`; on the server, look up the route by name.
|
|
127
|
+
|
|
128
|
+
## Client components: receive URLs as props
|
|
129
|
+
|
|
130
|
+
`reverse()` is not available inside `"use client"` modules — there is no handler context and no route manifest in the browser bundle. Generate the URL on the server and hand it to the client component.
|
|
131
|
+
|
|
132
|
+
Three patterns, in order of preference:
|
|
133
|
+
|
|
134
|
+
1. Pass as a prop from a server component:
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
// server
|
|
138
|
+
function BlogPostPage(ctx: HandlerContext) {
|
|
139
|
+
return <ShareButton url={ctx.reverse(".post", { slug: ctx.params.slug })} />;
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
"use client";
|
|
145
|
+
|
|
146
|
+
export function ShareButton({ url }: { url: string }) {
|
|
147
|
+
return (
|
|
148
|
+
<button onClick={() => navigator.clipboard.writeText(url)}>Share</button>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
2. Return from a loader (attached to the route via the DSL):
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
// server — loaders/nav.ts
|
|
157
|
+
export const NavLoader = createLoader((ctx) => ({
|
|
158
|
+
home: ctx.reverse("home"),
|
|
159
|
+
blog: ctx.reverse("blog.index"),
|
|
160
|
+
}));
|
|
161
|
+
|
|
162
|
+
// server — urls.tsx: attach the loader so useLoader has data in context
|
|
163
|
+
const urlpatterns = urls(({ path, loader }) => [
|
|
164
|
+
path("/", HomePage, { name: "home" }, () => [loader(NavLoader)]),
|
|
165
|
+
]);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
```tsx
|
|
169
|
+
"use client";
|
|
170
|
+
|
|
171
|
+
function Nav() {
|
|
172
|
+
const { data } = useLoader(NavLoader);
|
|
173
|
+
return <Link to={data.home}>Home</Link>;
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`useLoader()` requires the loader to be attached to an active route. If you need on-demand fetching instead, use `useFetchLoader()`.
|
|
178
|
+
|
|
179
|
+
3. Return from a server action:
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
"use server";
|
|
183
|
+
|
|
184
|
+
export async function getProductUrl(slug: string) {
|
|
185
|
+
const ctx = getRequestContext();
|
|
186
|
+
return ctx.reverse("product", { slug });
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
For static path strings (not named routes), client components can use `href()` — see below.
|
|
191
|
+
|
|
123
192
|
## Client: href()
|
|
124
193
|
|
|
125
|
-
Plain function for absolute path-based URLs. No hook needed - works anywhere.
|
|
194
|
+
Plain function for absolute path-based URLs. No hook needed - works anywhere in client components. `href()` validates paths at compile time, but does **not** resolve named routes — for named routes, use one of the patterns above.
|
|
126
195
|
|
|
127
196
|
```typescript
|
|
128
197
|
"use client";
|
|
@@ -139,7 +208,9 @@ function GlobalNav() {
|
|
|
139
208
|
}
|
|
140
209
|
```
|
|
141
210
|
|
|
142
|
-
`href()`
|
|
211
|
+
`href()` provides compile-time validation via `ValidPaths` type. Paths are validated against registered route patterns using `PatternToPath`.
|
|
212
|
+
|
|
213
|
+
`href()` is a raw path helper — it is **not** basename-aware. It returns the path as-is (or with the include mount prefix via `useHref()`). For basename-aware navigation, use `Link`, `useRouter().push()`, or `reverse()`, which auto-prefix root-relative paths with the router's basename.
|
|
143
214
|
|
|
144
215
|
## Client: useHref()
|
|
145
216
|
|
|
@@ -185,13 +256,16 @@ function MountInfo() {
|
|
|
185
256
|
|
|
186
257
|
## When to use what
|
|
187
258
|
|
|
188
|
-
| Context | API
|
|
189
|
-
| ---------------- |
|
|
190
|
-
| Server handler | `ctx.reverse("name")`
|
|
191
|
-
| Server handler | `scopedReverse<T>(ctx.reverse)`
|
|
192
|
-
| Client component |
|
|
193
|
-
| Client component | `
|
|
194
|
-
| Client component | `
|
|
259
|
+
| Context | API | Resolves | Use for |
|
|
260
|
+
| ---------------- | -------------------------------------------------- | ------------------------------- | ---------------------------------------------------------------- |
|
|
261
|
+
| Server handler | `ctx.reverse("name")` | Named routes (local + absolute) | **Default** server-side URL generation |
|
|
262
|
+
| Server handler | `scopedReverse<T>(ctx.reverse)` | Same, with type safety | Type-safe server URLs |
|
|
263
|
+
| Client component | (URL passed as prop / loader data / action return) | Named routes | Any URL derived from a named route — generate on server, pass in |
|
|
264
|
+
| Client component | `href("/path")` | Absolute paths (static strings) | Static navigation where no named-route lookup is needed |
|
|
265
|
+
| Client component | `useHref()` | Mount-prefixed paths | Local navigation inside `include()` |
|
|
266
|
+
| Client component | `useMount()` | Raw mount path | Custom mount-aware logic |
|
|
267
|
+
|
|
268
|
+
> `reverse()` is server-only. Client components never import or call it — they receive the already-resolved string.
|
|
195
269
|
|
|
196
270
|
## Complete example: mounted module
|
|
197
271
|
|
package/skills/loader/SKILL.md
CHANGED
|
@@ -65,19 +65,10 @@ export const urlpatterns = urls(({ path, loader }) => [
|
|
|
65
65
|
|
|
66
66
|
## Consuming Loader Data
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
import { ProductLoader } from "./loaders/product";
|
|
73
|
-
|
|
74
|
-
async function ProductPage() {
|
|
75
|
-
const { product } = await useLoader(ProductLoader);
|
|
76
|
-
return <h1>{product.name}</h1>;
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### In Client Components
|
|
68
|
+
Register loaders with `loader()` in the DSL and consume them in client
|
|
69
|
+
components with `useLoader()`. This is the recommended pattern — it keeps
|
|
70
|
+
data fetching on the server and consumption on the client, with a clean
|
|
71
|
+
separation that works correctly with `cache()`.
|
|
81
72
|
|
|
82
73
|
```typescript
|
|
83
74
|
"use client";
|
|
@@ -90,9 +81,87 @@ function ProductDetails() {
|
|
|
90
81
|
}
|
|
91
82
|
```
|
|
92
83
|
|
|
84
|
+
```typescript
|
|
85
|
+
// Route definition — loader() registration required
|
|
86
|
+
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
87
|
+
loader(ProductLoader),
|
|
88
|
+
]);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
DSL loaders are the **live data layer** — they resolve fresh on every
|
|
92
|
+
request, even when the route is inside a `cache()` boundary. The router
|
|
93
|
+
excludes them from the segment cache at storage time and re-resolves them
|
|
94
|
+
on retrieval. This means `cache()` gives you cached UI + fresh data by
|
|
95
|
+
default.
|
|
96
|
+
|
|
97
|
+
### Cache safety
|
|
98
|
+
|
|
99
|
+
DSL loaders can safely read `createVar({ cache: false })` variables
|
|
100
|
+
because they are always resolved fresh. The read guard is bypassed for
|
|
101
|
+
loader functions — they never produce stale data.
|
|
102
|
+
|
|
103
|
+
### ctx.use(Loader) — escape hatch
|
|
104
|
+
|
|
105
|
+
For cases where you need loader data in the server handler itself (e.g.,
|
|
106
|
+
to set ctx variables or make routing decisions), use `ctx.use(Loader)`:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
path("/product/:slug", async (ctx) => {
|
|
110
|
+
const { product } = await ctx.use(ProductLoader);
|
|
111
|
+
ctx.set(Product, product); // make available to children
|
|
112
|
+
return <ProductPage />;
|
|
113
|
+
}, { name: "product" }, () => [
|
|
114
|
+
loader(ProductLoader), // still register for client consumption
|
|
115
|
+
])
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
When you register with `loader()` in the DSL, `ctx.use()` returns the
|
|
119
|
+
same memoized result — loaders never run twice per request.
|
|
120
|
+
|
|
121
|
+
**Limitations of ctx.use(Loader):**
|
|
122
|
+
|
|
123
|
+
- The handler output depends on the loader data. If the route is inside
|
|
124
|
+
`cache()`, the handler is cached with the loader result baked in —
|
|
125
|
+
defeating the live data guarantee.
|
|
126
|
+
- Non-cacheable variable reads (`createVar({ cache: false })`) inside the
|
|
127
|
+
handler still throw, even if the data came from a loader.
|
|
128
|
+
- Prefer DSL `loader()` + client `useLoader()` for data that depends on
|
|
129
|
+
non-cacheable context variables.
|
|
130
|
+
|
|
131
|
+
**Never use `useLoader()` in server components** — it is a client-only API.
|
|
132
|
+
|
|
133
|
+
### Summary
|
|
134
|
+
|
|
135
|
+
| Pattern | API | Cache-safe | Recommended |
|
|
136
|
+
| ---------------------- | ------------------- | ---------- | ----------- |
|
|
137
|
+
| DSL + client component | `useLoader(Loader)` | Yes | Yes |
|
|
138
|
+
| Handler escape hatch | `ctx.use(Loader)` | No | When needed |
|
|
139
|
+
|
|
93
140
|
## Loader Context
|
|
94
141
|
|
|
95
|
-
Loaders receive the same context as route handlers
|
|
142
|
+
Loaders receive the same context shape as route handlers.
|
|
143
|
+
|
|
144
|
+
### Full field surface
|
|
145
|
+
|
|
146
|
+
| Field | Type | Notes |
|
|
147
|
+
| -------------- | ------------------------------ | --------------------------------------------------------------------------------------------------- |
|
|
148
|
+
| `params` | `TParams` | Merged route + explicit loader params; overridable by fetchable `load({ params })`. |
|
|
149
|
+
| `routeParams` | `Record<string, string>` | Server-trusted route params from URL pattern matching; cannot be overridden. |
|
|
150
|
+
| `request` | `Request` | The incoming `Request` (headers, method, body, `signal` for abort). |
|
|
151
|
+
| `url` | `URL` | Parsed request URL. |
|
|
152
|
+
| `pathname` | `string` | URL pathname (shortcut for `ctx.url.pathname`). |
|
|
153
|
+
| `searchParams` | `URLSearchParams` | Shortcut for `ctx.url.searchParams`. |
|
|
154
|
+
| `search` | `ResolveSearchSchema<TSearch>` | Typed query params when a search schema is declared on the route; `{}` otherwise. |
|
|
155
|
+
| `env` | `TEnv` | Plain bindings from `createRouter<TEnv>()` (DB, KV, secrets, etc.). |
|
|
156
|
+
| `get` | `(key \| ContextVar) => value` | Reads variables/context-vars set by middleware. |
|
|
157
|
+
| `use` | `(loader \| handle) => T` | Access another loader's data (Promise) or a handle's collected data (after `await ctx.rendered()`). |
|
|
158
|
+
| `rendered` | `() => Promise<void>` | **Experimental.** DSL loaders only — waits for non-loader segments before reading handle data. |
|
|
159
|
+
| `method` | `string` | HTTP method. `"GET"` for SSR loader runs; reflects real method for fetchable loaders. |
|
|
160
|
+
| `body` | `TBody \| undefined` | Parsed request body for fetchable POST/PUT/PATCH/DELETE calls. |
|
|
161
|
+
| `formData` | `FormData \| undefined` | Present when a fetchable loader is invoked via form submission. |
|
|
162
|
+
| `reverse` | `ScopedReverseFunction` | Generate type-checked URLs from route names (same scoped semantics as route handlers). |
|
|
163
|
+
|
|
164
|
+
### Example
|
|
96
165
|
|
|
97
166
|
```typescript
|
|
98
167
|
export const ProductLoader = createLoader(async (ctx) => {
|
|
@@ -116,10 +185,21 @@ export const ProductLoader = createLoader(async (ctx) => {
|
|
|
116
185
|
// Variables set by middleware (from RSCRouter.Vars augmentation)
|
|
117
186
|
const user = ctx.get("user");
|
|
118
187
|
|
|
119
|
-
|
|
188
|
+
// Type-checked URLs for payloads. `.name` resolves within the current
|
|
189
|
+
// include() scope; a bare `name` resolves globally. See /route and
|
|
190
|
+
// /typesafety for scope rules and route-name autocomplete.
|
|
191
|
+
const detailUrl = ctx.reverse(".detail", { slug });
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
product: await fetchProduct(slug),
|
|
195
|
+
links: { self: detailUrl },
|
|
196
|
+
};
|
|
120
197
|
});
|
|
121
198
|
```
|
|
122
199
|
|
|
200
|
+
See `/route` for the full handler-context contract (shared with loaders) and
|
|
201
|
+
`/typesafety` for route-name typing that powers `ctx.reverse` autocomplete.
|
|
202
|
+
|
|
123
203
|
### params vs routeParams
|
|
124
204
|
|
|
125
205
|
- `ctx.params` — merged route params + explicit loader params. For fetchable
|
|
@@ -507,7 +587,7 @@ export const ProductLoader = createLoader(async (ctx) => {
|
|
|
507
587
|
.first();
|
|
508
588
|
|
|
509
589
|
if (!product) {
|
|
510
|
-
|
|
590
|
+
notFound("Product not found");
|
|
511
591
|
}
|
|
512
592
|
|
|
513
593
|
return { product };
|
|
@@ -523,10 +603,9 @@ export const CartLoader = createLoader(async (ctx) => {
|
|
|
523
603
|
return { cart };
|
|
524
604
|
});
|
|
525
605
|
|
|
526
|
-
// urls.tsx
|
|
606
|
+
// urls.tsx — register loaders in the DSL
|
|
527
607
|
export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalidate }) => [
|
|
528
608
|
layout(<ShopLayout />, () => [
|
|
529
|
-
// Shared cart loader for all shop routes
|
|
530
609
|
loader(CartLoader, () => [
|
|
531
610
|
revalidate(({ actionId }) => actionId?.includes("Cart") ?? false),
|
|
532
611
|
]),
|
|
@@ -538,18 +617,22 @@ export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalid
|
|
|
538
617
|
]),
|
|
539
618
|
]);
|
|
540
619
|
|
|
541
|
-
//
|
|
542
|
-
|
|
620
|
+
// components/ProductDetails.tsx — consume in client component
|
|
621
|
+
"use client";
|
|
622
|
+
import { useLoader } from "@rangojs/router/client";
|
|
543
623
|
import { ProductLoader, CartLoader } from "./loaders/shop";
|
|
544
624
|
|
|
545
|
-
|
|
546
|
-
const { product } =
|
|
547
|
-
const { cart } =
|
|
625
|
+
function ProductDetails() {
|
|
626
|
+
const { data: { product } } = useLoader(ProductLoader);
|
|
627
|
+
const { data: { cart } } = useLoader(CartLoader);
|
|
548
628
|
|
|
549
629
|
return (
|
|
550
630
|
<div>
|
|
551
631
|
<h1>{product.name}</h1>
|
|
552
|
-
<AddToCartButton
|
|
632
|
+
<AddToCartButton
|
|
633
|
+
productId={product.id}
|
|
634
|
+
inCart={cart?.items.includes(product.id)}
|
|
635
|
+
/>
|
|
553
636
|
</div>
|
|
554
637
|
);
|
|
555
638
|
}
|
|
@@ -26,6 +26,8 @@ const router = createRouter<AppEnv>({})
|
|
|
26
26
|
.routes(urlpatterns);
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
When the router has a `basename`, pattern-scoped `.use()` patterns are automatically prefixed. For example, with `basename: "/app"`, `.use("/admin/*", mw)` matches `/app/admin/*`.
|
|
30
|
+
|
|
29
31
|
### Route middleware (`middleware()` in `urls()`)
|
|
30
32
|
|
|
31
33
|
Registered inside `urls()` callback. Wraps **rendering only** -- it does NOT wrap server action execution. Actions run before route middleware, so when route middleware executes during post-action revalidation, it can observe state that the action set (cookies, context variables, headers).
|
|
@@ -135,17 +137,46 @@ export const urlpatterns = urls(({ path, layout, middleware }) => [
|
|
|
135
137
|
## Middleware with Multiple Handlers
|
|
136
138
|
|
|
137
139
|
```typescript
|
|
138
|
-
//
|
|
140
|
+
// Group multiple middleware in an array
|
|
139
141
|
export const shopMiddleware = [loggerMiddleware, mockAuthMiddleware];
|
|
140
142
|
|
|
141
|
-
// In routes
|
|
143
|
+
// In routes — pass the array directly
|
|
142
144
|
layout(<ShopLayout />, () => [
|
|
143
|
-
middleware(
|
|
145
|
+
middleware(shopMiddleware),
|
|
144
146
|
|
|
145
147
|
path("/shop", ShopIndex, { name: "shop" }),
|
|
146
148
|
])
|
|
147
149
|
```
|
|
148
150
|
|
|
151
|
+
## Wrapping Middleware (Scoped to Children)
|
|
152
|
+
|
|
153
|
+
Use the wrapping form to scope middleware to a subset of routes without
|
|
154
|
+
introducing a visible layout:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
urls(({ path, middleware }) => [
|
|
158
|
+
// authMw only applies to /admin and /admin/settings
|
|
159
|
+
middleware(authMw, () => [
|
|
160
|
+
path("/admin", AdminPage, { name: "admin" }),
|
|
161
|
+
path("/admin/settings", SettingsPage, { name: "settings" }),
|
|
162
|
+
]),
|
|
163
|
+
|
|
164
|
+
// Public route — no authMw
|
|
165
|
+
path("/", HomePage, { name: "home" }),
|
|
166
|
+
]);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Multiple middleware with wrapping:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
middleware([authMw, loggingMw], () => [
|
|
173
|
+
path("/admin", AdminPage, { name: "admin" }),
|
|
174
|
+
]);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
This creates a transparent layout (`<Outlet />`) that carries the middleware.
|
|
178
|
+
The middleware does not affect sibling routes outside the callback.
|
|
179
|
+
|
|
149
180
|
## Middleware Context
|
|
150
181
|
|
|
151
182
|
```typescript
|