@rangojs/router 0.0.0-experimental.8a4d0430 → 0.0.0-experimental.8bcfea43
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 +126 -38
- package/dist/bin/rango.js +138 -50
- package/dist/vite/index.js +1171 -461
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +19 -16
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +45 -4
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +28 -20
- 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 +88 -45
- 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 +110 -68
- package/skills/rango/SKILL.md +24 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +55 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +13 -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 +5 -0
- package/src/browser/navigation-bridge.ts +90 -16
- package/src/browser/navigation-client.ts +167 -59
- 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 +184 -16
- package/src/browser/prefetch/fetch.ts +180 -33
- 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 +81 -9
- 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 +168 -65
- 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 +49 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +50 -24
- 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.tsx +84 -230
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -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 +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 -0
- 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 +101 -25
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +10 -7
- package/src/router/loader-resolution.ts +159 -21
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +127 -192
- 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 +104 -10
- package/src/router/metrics.ts +6 -1
- package/src/router/middleware-types.ts +8 -30
- package/src/router/middleware.ts +36 -10
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +60 -9
- 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/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +198 -20
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +438 -300
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/trie-matching.ts +10 -4
- package/src/router/types.ts +1 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +60 -8
- package/src/rsc/handler.ts +478 -374
- 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 +16 -2
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +19 -1
- package/src/rsc/server-action.ts +10 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +9 -1
- 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 +166 -17
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +194 -60
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +137 -65
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +19 -1
- package/src/types/segments.ts +2 -0
- 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/debug.ts +55 -0
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +128 -74
- package/src/vite/discovery/state.ts +13 -6
- package/src/vite/index.ts +4 -0
- 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 +86 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +204 -217
- package/src/vite/router-discovery.ts +335 -64
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +37 -5
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Node ESM loader hook that resolves `cloudflare:*` imports to the same
|
|
2
|
+
// stub ESM the Vite transform produces for rewritten specifiers.
|
|
3
|
+
//
|
|
4
|
+
// Why both? The Vite transform (cloudflare-protocol-stub.ts) catches
|
|
5
|
+
// imports in modules that flow through Vite's plugin pipeline — covers
|
|
6
|
+
// user source and any node_modules package Vite fetches and transforms.
|
|
7
|
+
// But Vite/Rollup externalize certain packages (e.g. `partyserver`,
|
|
8
|
+
// which has `import { DurableObject, env } from "cloudflare:workers"`
|
|
9
|
+
// at its top level, and similar "workerd-native" libraries). Externalized
|
|
10
|
+
// modules bypass the transform: Rollup hands their resolution to Node's
|
|
11
|
+
// native ESM loader, which rejects URL-scheme specifiers. This loader
|
|
12
|
+
// hook registers via `module.register()` from `createTempRscServer` and
|
|
13
|
+
// intercepts `cloudflare:*` at Node's resolve layer — before the default
|
|
14
|
+
// loader throws ERR_UNSUPPORTED_ESM_URL_SCHEME.
|
|
15
|
+
//
|
|
16
|
+
// Lifecycle: the hook runs in a dedicated worker thread (Node ESM loader
|
|
17
|
+
// architecture) with its own globalThis. It cannot see the main thread's
|
|
18
|
+
// `__rango_build_env__` bridge, so the `env` export here is always `{}`.
|
|
19
|
+
// That's fine in practice — externalized libraries don't typically touch
|
|
20
|
+
// `env` at module top level; they read it at request time in workerd
|
|
21
|
+
// where the real module exists. Build-time prerender handlers in user
|
|
22
|
+
// source DO read `env`, but they flow through the Vite transform (which
|
|
23
|
+
// does bridge `env` from `getPlatformProxy()`), not through this loader.
|
|
24
|
+
//
|
|
25
|
+
// Keep STUBS in sync with cloudflare-protocol-stub.ts — both paths need
|
|
26
|
+
// to hand out the same base classes.
|
|
27
|
+
|
|
28
|
+
const CF_PREFIX = "cloudflare:";
|
|
29
|
+
|
|
30
|
+
const STUBS = {
|
|
31
|
+
"cloudflare:workers": `
|
|
32
|
+
export class DurableObject { constructor(_ctx, _env) {} }
|
|
33
|
+
export class WorkerEntrypoint { constructor(_ctx, _env) {} }
|
|
34
|
+
export class WorkflowEntrypoint { constructor(_ctx, _env) {} }
|
|
35
|
+
export class RpcTarget {}
|
|
36
|
+
export const env = {};
|
|
37
|
+
export default {};
|
|
38
|
+
`,
|
|
39
|
+
"cloudflare:email": `
|
|
40
|
+
export class EmailMessage { constructor(_from, _to, _raw) {} }
|
|
41
|
+
export default {};
|
|
42
|
+
`,
|
|
43
|
+
"cloudflare:sockets": `
|
|
44
|
+
export function connect() { return {}; }
|
|
45
|
+
export default {};
|
|
46
|
+
`,
|
|
47
|
+
"cloudflare:workflows": `
|
|
48
|
+
export class NonRetryableError extends Error {
|
|
49
|
+
constructor(message, name) { super(message); this.name = name ?? "NonRetryableError"; }
|
|
50
|
+
}
|
|
51
|
+
export default {};
|
|
52
|
+
`,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Policy: unknown `cloudflare:*` specifiers resolve permissively to an
|
|
56
|
+
// empty default export rather than throwing. Same reasoning as
|
|
57
|
+
// cloudflare-protocol-stub.ts's FALLBACK_STUB — we prioritize
|
|
58
|
+
// dependency-graph resilience over strict validation, because third-party
|
|
59
|
+
// packages can pull `cloudflare:*` modules we haven't curated.
|
|
60
|
+
const FALLBACK_STUB = `export default {};\n`;
|
|
61
|
+
|
|
62
|
+
function dataUrlFor(specifier) {
|
|
63
|
+
const body = STUBS[specifier] ?? FALLBACK_STUB;
|
|
64
|
+
return "data:text/javascript;base64," + Buffer.from(body).toString("base64");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function resolve(specifier, context, nextResolve) {
|
|
68
|
+
if (specifier.startsWith(CF_PREFIX)) {
|
|
69
|
+
return {
|
|
70
|
+
shortCircuit: true,
|
|
71
|
+
url: dataUrlFor(specifier),
|
|
72
|
+
format: "module",
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return nextResolve(specifier, context);
|
|
76
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rangojs/router",
|
|
3
|
-
"version": "0.0.0-experimental.
|
|
3
|
+
"version": "0.0.0-experimental.8bcfea43",
|
|
4
4
|
"description": "Django-inspired RSC router with composable URL patterns",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -132,28 +132,39 @@
|
|
|
132
132
|
"access": "public",
|
|
133
133
|
"tag": "experimental"
|
|
134
134
|
},
|
|
135
|
+
"scripts": {
|
|
136
|
+
"build": "pnpm dlx esbuild src/vite/index.ts --bundle --format=esm --outfile=dist/vite/index.js --platform=node --packages=external && mkdir -p dist/vite/plugins && cp src/vite/plugins/cloudflare-protocol-loader-hook.mjs dist/vite/plugins/cloudflare-protocol-loader-hook.mjs && pnpm dlx esbuild src/bin/rango.ts --bundle --format=esm --outfile=dist/bin/rango.js --platform=node --packages=external --banner:js='#!/usr/bin/env node' && chmod +x dist/bin/rango.js",
|
|
137
|
+
"prepublishOnly": "pnpm build",
|
|
138
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.strict-check.json --noEmit",
|
|
139
|
+
"test": "playwright test",
|
|
140
|
+
"test:ui": "playwright test --ui",
|
|
141
|
+
"test:unit": "vitest run",
|
|
142
|
+
"test:unit:watch": "vitest"
|
|
143
|
+
},
|
|
135
144
|
"dependencies": {
|
|
136
|
-
"@vitejs/plugin-rsc": "^0.5.
|
|
145
|
+
"@vitejs/plugin-rsc": "^0.5.23",
|
|
146
|
+
"debug": "^4.4.1",
|
|
137
147
|
"magic-string": "^0.30.17",
|
|
138
148
|
"picomatch": "^4.0.3",
|
|
139
149
|
"rsc-html-stream": "^0.0.7"
|
|
140
150
|
},
|
|
141
151
|
"devDependencies": {
|
|
142
152
|
"@playwright/test": "^1.49.1",
|
|
153
|
+
"@types/debug": "^4.1.12",
|
|
143
154
|
"@types/node": "^24.10.1",
|
|
144
|
-
"@types/react": "
|
|
145
|
-
"@types/react-dom": "
|
|
155
|
+
"@types/react": "catalog:",
|
|
156
|
+
"@types/react-dom": "catalog:",
|
|
146
157
|
"esbuild": "^0.27.0",
|
|
147
158
|
"jiti": "^2.6.1",
|
|
148
|
-
"react": "
|
|
149
|
-
"react-dom": "
|
|
159
|
+
"react": "catalog:",
|
|
160
|
+
"react-dom": "catalog:",
|
|
150
161
|
"tinyexec": "^0.3.2",
|
|
151
162
|
"typescript": "^5.3.0",
|
|
152
163
|
"vitest": "^4.0.0"
|
|
153
164
|
},
|
|
154
165
|
"peerDependencies": {
|
|
155
166
|
"@cloudflare/vite-plugin": "^1.25.0",
|
|
156
|
-
"@vitejs/plugin-rsc": "^0.5.
|
|
167
|
+
"@vitejs/plugin-rsc": "^0.5.23",
|
|
157
168
|
"react": "^18.0.0 || ^19.0.0",
|
|
158
169
|
"vite": "^7.3.0"
|
|
159
170
|
},
|
|
@@ -164,13 +175,5 @@
|
|
|
164
175
|
"vite": {
|
|
165
176
|
"optional": true
|
|
166
177
|
}
|
|
167
|
-
},
|
|
168
|
-
"scripts": {
|
|
169
|
-
"build": "pnpm dlx esbuild src/vite/index.ts --bundle --format=esm --outfile=dist/vite/index.js --platform=node --packages=external && pnpm dlx esbuild src/bin/rango.ts --bundle --format=esm --outfile=dist/bin/rango.js --platform=node --packages=external --banner:js='#!/usr/bin/env node' && chmod +x dist/bin/rango.js",
|
|
170
|
-
"typecheck": "tsc --noEmit",
|
|
171
|
-
"test": "playwright test",
|
|
172
|
-
"test:ui": "playwright test --ui",
|
|
173
|
-
"test:unit": "vitest run",
|
|
174
|
-
"test:unit:watch": "vitest"
|
|
175
178
|
}
|
|
176
|
-
}
|
|
179
|
+
}
|
|
@@ -141,9 +141,11 @@ path("/dashboard", (ctx) => {
|
|
|
141
141
|
breadcrumb({ label: "Dashboard", href: "/dashboard" });
|
|
142
142
|
return <DashboardNav handle={Breadcrumbs} />;
|
|
143
143
|
});
|
|
144
|
+
```
|
|
144
145
|
|
|
146
|
+
```tsx
|
|
145
147
|
// Client component
|
|
146
|
-
|
|
148
|
+
"use client";
|
|
147
149
|
import { useHandle, type Breadcrumbs } from "@rangojs/router/client";
|
|
148
150
|
|
|
149
151
|
function DashboardNav({ handle }: { handle: typeof Breadcrumbs }) {
|
|
@@ -162,6 +162,38 @@ middleware(async (ctx, next) => {
|
|
|
162
162
|
});
|
|
163
163
|
```
|
|
164
164
|
|
|
165
|
+
## Context Variable Cache Safety
|
|
166
|
+
|
|
167
|
+
Context variables created with `createVar()` are cacheable by default and can
|
|
168
|
+
be read freely inside `cache()` and `"use cache"` scopes. Non-cacheable vars
|
|
169
|
+
throw at read time to prevent request-specific data from being captured.
|
|
170
|
+
|
|
171
|
+
There are two ways to mark a value as non-cacheable:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
// Var-level policy — inherently request-specific data
|
|
175
|
+
const Session = createVar<SessionData>({ cache: false });
|
|
176
|
+
|
|
177
|
+
// Write-level escalation — this specific write is non-cacheable
|
|
178
|
+
ctx.set(Theme, derivedTheme, { cache: false });
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
"Least cacheable wins": if either the var definition or the `ctx.set()` call
|
|
182
|
+
specifies `cache: false`, the value is non-cacheable.
|
|
183
|
+
|
|
184
|
+
**Behavior inside cache scopes:**
|
|
185
|
+
|
|
186
|
+
| Operation | Inside `cache()` / `"use cache"` |
|
|
187
|
+
| ----------------------------------- | -------------------------------- |
|
|
188
|
+
| `ctx.get(cacheableVar)` | Allowed |
|
|
189
|
+
| `ctx.get(nonCacheableVar)` | Throws |
|
|
190
|
+
| `ctx.set(var, value)` (cacheable) | Allowed |
|
|
191
|
+
| `ctx.header()`, `ctx.cookie()`, etc | Throws (response side effects) |
|
|
192
|
+
|
|
193
|
+
Write is dumb — `ctx.set()` stores the cache metadata but does not enforce.
|
|
194
|
+
Enforcement happens at read time (`ctx.get()`), where ALS detects the cache
|
|
195
|
+
scope and rejects non-cacheable reads.
|
|
196
|
+
|
|
165
197
|
## Loaders Are Always Fresh
|
|
166
198
|
|
|
167
199
|
Loaders are **never cached** by route-level `cache()`. Even on a full cache hit
|
package/skills/caching/SKILL.md
CHANGED
|
@@ -120,9 +120,9 @@ const store = new MemorySegmentCacheStore({
|
|
|
120
120
|
});
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
### Cloudflare
|
|
123
|
+
### Cloudflare Edge Cache Store
|
|
124
124
|
|
|
125
|
-
For distributed caching on Cloudflare Workers:
|
|
125
|
+
For distributed caching on Cloudflare Workers using the Cache API:
|
|
126
126
|
|
|
127
127
|
```typescript
|
|
128
128
|
import { CFCacheStore } from "@rangojs/router/cache";
|
|
@@ -132,14 +132,55 @@ const router = createRouter<AppBindings>({
|
|
|
132
132
|
urls: urlpatterns,
|
|
133
133
|
cache: (env, ctx) => ({
|
|
134
134
|
store: new CFCacheStore({
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
ctx,
|
|
136
|
+
defaults: { ttl: 60, swr: 300 },
|
|
137
137
|
}),
|
|
138
138
|
enabled: true,
|
|
139
139
|
}),
|
|
140
140
|
});
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
### With KV L2 Persistence
|
|
144
|
+
|
|
145
|
+
Add a KV namespace for global cross-colo persistence. On Cache API miss, KV is
|
|
146
|
+
checked and hits are promoted back to L1. Writes go to both layers.
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { CFCacheStore } from "@rangojs/router/cache";
|
|
150
|
+
|
|
151
|
+
const router = createRouter<AppBindings>({
|
|
152
|
+
document: Document,
|
|
153
|
+
urls: urlpatterns,
|
|
154
|
+
cache: (env, ctx) => ({
|
|
155
|
+
store: new CFCacheStore({
|
|
156
|
+
ctx,
|
|
157
|
+
kv: env.CACHE_KV, // optional KV namespace binding
|
|
158
|
+
defaults: { ttl: 60, swr: 300 },
|
|
159
|
+
}),
|
|
160
|
+
enabled: true,
|
|
161
|
+
}),
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**How the two layers work:**
|
|
166
|
+
|
|
167
|
+
| Scenario | L1 (Cache API) | L2 (KV) | Result |
|
|
168
|
+
| ------------ | -------------- | ------- | ----------------------------- |
|
|
169
|
+
| Hot request | HIT | — | Serve from L1 (fast) |
|
|
170
|
+
| Cold colo | MISS | HIT | Serve from KV, promote to L1 |
|
|
171
|
+
| First render | MISS | MISS | Render, write to both L1 + KV |
|
|
172
|
+
|
|
173
|
+
KV entries require `expirationTtl >= 60s`. Short-lived entries (< 60s total TTL)
|
|
174
|
+
are only cached in L1.
|
|
175
|
+
|
|
176
|
+
## Context Variables Inside Cache Boundaries
|
|
177
|
+
|
|
178
|
+
Context variables (`createVar`) are cacheable by default and can be read and
|
|
179
|
+
written inside `cache()` scopes. Variables marked with `{ cache: false }` (at
|
|
180
|
+
the var level or write level) throw when read inside a cache scope. Response
|
|
181
|
+
side effects (`ctx.header()`, `ctx.cookie()`) always throw inside cache
|
|
182
|
+
boundaries. See `/cache-guide` for the full cache safety table.
|
|
183
|
+
|
|
143
184
|
## Nested Cache Boundaries
|
|
144
185
|
|
|
145
186
|
Override cache settings for specific sections:
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: handler-use
|
|
3
|
+
description: Attach default loaders, middleware, parallels, and other use items directly to handlers via handler.use, and compose them with explicit use() at mount sites
|
|
4
|
+
argument-hint: "[handler]"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Handler-Attached `.use`
|
|
8
|
+
|
|
9
|
+
A handler function (or branded `Static`/`Prerender`/`Passthrough` definition) can carry its own defaults via a `.use` callback that returns an array of `use` items (loader, middleware, parallel, intercept, layout, loading, etc.). The mount-site DSL (`path()`, `layout()`, `parallel()`, `intercept()`) merges those defaults with any explicit `use()` callback supplied at the registration site.
|
|
10
|
+
|
|
11
|
+
This lets handlers be **self-contained, reusable units** — a page brings its own loader, a layout brings its own middleware, a parallel slot brings its own data + skeleton — without forcing every caller to wire the same items at every mount site.
|
|
12
|
+
|
|
13
|
+
Canonical implementation reference:
|
|
14
|
+
[src/route-definition/resolve-handler-use.ts](../../src/route-definition/resolve-handler-use.ts)
|
|
15
|
+
|
|
16
|
+
## Defining a handler with `.use`
|
|
17
|
+
|
|
18
|
+
Attach `.use` to the function (or to the branded definition for `Static()`/`Prerender()`/`Passthrough()`):
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import {
|
|
22
|
+
loader,
|
|
23
|
+
middleware,
|
|
24
|
+
loading,
|
|
25
|
+
createLoader,
|
|
26
|
+
type Handler,
|
|
27
|
+
} from "@rangojs/router";
|
|
28
|
+
|
|
29
|
+
export const ProductLoader = createLoader(async (ctx) =>
|
|
30
|
+
fetchProduct(ctx.params.slug),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const ProductPage: Handler<"/product/:slug"> = async (ctx) => {
|
|
34
|
+
const product = await ctx.use(ProductLoader);
|
|
35
|
+
return <ProductView product={product} />;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
ProductPage.use = () => [
|
|
39
|
+
loader(ProductLoader),
|
|
40
|
+
loading(<ProductSkeleton />),
|
|
41
|
+
middleware(async (ctx, next) => {
|
|
42
|
+
await next();
|
|
43
|
+
ctx.header("Cache-Control", "private, max-age=60");
|
|
44
|
+
}),
|
|
45
|
+
];
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Now `ProductPage` carries its loader, loading state, and response-header middleware regardless of where it is mounted.
|
|
49
|
+
|
|
50
|
+
## Allowed items per mount site
|
|
51
|
+
|
|
52
|
+
`handler.use()` is the same callback shape regardless of where the handler runs, but the runtime validates that the items it returns are valid for the mount site. Driven by `MOUNT_SITE_ALLOWED_TYPES` in [resolve-handler-use.ts](../../src/route-definition/resolve-handler-use.ts):
|
|
53
|
+
|
|
54
|
+
| Mount site | Allowed item types |
|
|
55
|
+
| ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
56
|
+
| `path()` / `route()` | `layout`, `parallel`, `intercept`, `middleware`, `revalidate`, `loader`, `loading`, `errorBoundary`, `notFoundBoundary`, `cache`, `transition` |
|
|
57
|
+
| `layout()` | All of the above, plus `route`, `include` |
|
|
58
|
+
| `parallel()` (per slot) | `revalidate`, `loader`, `loading`, `errorBoundary`, `notFoundBoundary`, `transition` |
|
|
59
|
+
| `intercept()` | `middleware`, `revalidate`, `loader`, `loading`, `errorBoundary`, `notFoundBoundary`, `layout`, `route`, `when`, `transition` |
|
|
60
|
+
| Response routes (`path.json()`, `path.text()`, …) | `middleware`, `cache` |
|
|
61
|
+
|
|
62
|
+
If `handler.use()` returns a disallowed item for a mount site, registration throws:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
handler.use() returned middleware() which is not valid inside parallel().
|
|
66
|
+
Allowed types: revalidate, loader, loading, errorBoundary, notFoundBoundary, transition.
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The narrowest contract is `parallel()` — slots cannot bring their own middleware or layout; only data, loading, error/notFound boundaries, revalidation, and transitions.
|
|
70
|
+
|
|
71
|
+
## Composition with explicit `use()`
|
|
72
|
+
|
|
73
|
+
Every mount site that takes a `use` callback merges in this order:
|
|
74
|
+
|
|
75
|
+
1. **`handler.use()` items first** — the handler's defaults.
|
|
76
|
+
2. **Explicit `use()` items second** — overrides specified at the mount site.
|
|
77
|
+
|
|
78
|
+
Items of the same kind from the explicit `use()` follow the existing override rules of that item type. The most important ones for composition:
|
|
79
|
+
|
|
80
|
+
- **`loading()`** — last definition wins, so explicit `loading()` replaces the handler's default.
|
|
81
|
+
- **`parallel({ "@slot": … })`** — the last `parallel()` call wins per slot name. Other slots from earlier calls are preserved (see `skills/parallel`).
|
|
82
|
+
- **`loader()`, `middleware()`, etc.** — accumulate; both the handler's and the explicit ones run.
|
|
83
|
+
|
|
84
|
+
Skip the boilerplate: if neither `handler.use` nor explicit `use()` is provided, no merge happens.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
// Handler brings a loader + a (placeholder) loading; explicit use replaces loading.
|
|
88
|
+
const SidebarSlot: Handler = async (ctx) => {
|
|
89
|
+
const data = await ctx.use(SidebarLoader);
|
|
90
|
+
return <Sidebar data={data} />;
|
|
91
|
+
};
|
|
92
|
+
SidebarSlot.use = () => [
|
|
93
|
+
loader(SidebarLoader),
|
|
94
|
+
loading(<DefaultSidebarSkeleton />),
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
parallel({ "@sidebar": SidebarSlot }, () => [
|
|
98
|
+
// Replaces the default skeleton; SidebarLoader from handler.use still runs.
|
|
99
|
+
loading(<SiteSpecificSidebarSkeleton />),
|
|
100
|
+
]);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Composable parallel slots (the main pay-off)
|
|
104
|
+
|
|
105
|
+
The parallel slot site is where `handler.use` shines. A slot handler that owns its data/loading lets a layout declare **just** the slot names — every loader, skeleton, and revalidation contract travels with the slot itself.
|
|
106
|
+
|
|
107
|
+
### Without `handler.use` (every caller wires it up)
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
layout(<DashboardLayout />, () => [
|
|
111
|
+
parallel({ "@cart": CartSummary }, () => [
|
|
112
|
+
loader(CartLoader),
|
|
113
|
+
loading(<CartSkeleton />),
|
|
114
|
+
revalidate(revalidateCartData),
|
|
115
|
+
]),
|
|
116
|
+
parallel({ "@notifs": NotificationPanel }, () => [
|
|
117
|
+
loader(NotificationsLoader),
|
|
118
|
+
loading(<NotifsSkeleton />),
|
|
119
|
+
revalidate(revalidateNotifs),
|
|
120
|
+
]),
|
|
121
|
+
path("/dashboard", DashboardIndex, { name: "dashboard.index" }),
|
|
122
|
+
]);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Every layout that wants `@cart` must repeat the same loader/loading/revalidate triplet.
|
|
126
|
+
|
|
127
|
+
### With `handler.use` (slot owns its dependencies)
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const CartSummary: Handler = async (ctx) => {
|
|
131
|
+
const cart = await ctx.use(CartLoader);
|
|
132
|
+
return <CartSummaryView cart={cart} />;
|
|
133
|
+
};
|
|
134
|
+
CartSummary.use = () => [
|
|
135
|
+
loader(CartLoader),
|
|
136
|
+
loading(<CartSkeleton />),
|
|
137
|
+
revalidate(revalidateCartData),
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
const NotificationPanel: Handler = async (ctx) => {
|
|
141
|
+
const items = await ctx.use(NotificationsLoader);
|
|
142
|
+
return <NotificationsView items={items} />;
|
|
143
|
+
};
|
|
144
|
+
NotificationPanel.use = () => [
|
|
145
|
+
loader(NotificationsLoader),
|
|
146
|
+
loading(<NotifsSkeleton />),
|
|
147
|
+
revalidate(revalidateNotifs),
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
// Mount sites become declarative — no per-call data wiring.
|
|
151
|
+
layout(<DashboardLayout />, () => [
|
|
152
|
+
parallel({ "@cart": CartSummary, "@notifs": NotificationPanel }),
|
|
153
|
+
path("/dashboard", DashboardIndex, { name: "dashboard.index" }),
|
|
154
|
+
]);
|
|
155
|
+
|
|
156
|
+
layout(<AccountLayout />, () => [
|
|
157
|
+
// Same slot, same defaults, zero re-wiring.
|
|
158
|
+
parallel({ "@cart": CartSummary }),
|
|
159
|
+
path("/account", AccountIndex, { name: "account.index" }),
|
|
160
|
+
]);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Each slot handler is now a portable, self-contained unit. Different layouts can use the same slot without copying data plumbing.
|
|
164
|
+
|
|
165
|
+
### Streaming behavior is per-slot
|
|
166
|
+
|
|
167
|
+
A slot's `loading()` (whether from `handler.use` or explicit) makes that slot an independent streaming unit — its loader does not block the parent layout. Two slot handlers with their own loading skeletons stream independently.
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
parallel({
|
|
171
|
+
"@cart": CartSummary, // handler.use loading() → streams independently
|
|
172
|
+
"@cartBadge": CartBadge, // no loading() anywhere → awaited before paint
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Two scopes for explicit `use` at the mount site: shared (broadcast) and slot-local
|
|
177
|
+
|
|
178
|
+
`parallel()` accepts an explicit `use()` callback that **broadcasts** to every slot in the call ([dsl-helpers.ts](../../src/route-definition/dsl-helpers.ts)). That's the right behavior for the items the parallel allow-list permits and that accumulate (`loader`, `revalidate`, `errorBoundary`, `notFoundBoundary`, `transition`) — every slot gets them. (Note: `middleware` is not allowed inside `parallel()`; see the allowed-types table above.)
|
|
179
|
+
|
|
180
|
+
For single-assignment items like `loading()`, broadcasting overwrites every slot's `handler.use` default. Pass a **slot descriptor** `{ handler, use }` instead: items in the descriptor's `use` apply only to that slot.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
parallel({
|
|
184
|
+
"@meta": MetaSlot,
|
|
185
|
+
"@sidebar": {
|
|
186
|
+
handler: SidebarSlot,
|
|
187
|
+
use: () => [loading(<SidebarSkeleton />)], // ← only @sidebar
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Per-slot merge order is **handler.use → shared use → slot-local use** (narrowest scope wins for last-write-wins items like `loading()`):
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
parallel(
|
|
196
|
+
{
|
|
197
|
+
"@cart": {
|
|
198
|
+
handler: Cart,
|
|
199
|
+
use: () => [loading(<CartSkeleton />)], // wins for @cart
|
|
200
|
+
},
|
|
201
|
+
"@notifs": Notifs, // gets <BroadcastSkeleton />
|
|
202
|
+
},
|
|
203
|
+
() => [
|
|
204
|
+
loader(SharedAnalyticsLoader), // accumulates on every slot
|
|
205
|
+
loading(<BroadcastSkeleton />), // applies to slots without slot-local
|
|
206
|
+
],
|
|
207
|
+
);
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Use the descriptor's `use` for `loading(false)` too — opting one slot out of streaming without affecting siblings:
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
parallel(
|
|
214
|
+
{
|
|
215
|
+
"@cart": { handler: Cart, use: () => [loading(false)] }, // @cart awaits
|
|
216
|
+
"@notifs": Notifs, // @notifs still streams with broadcast skeleton
|
|
217
|
+
},
|
|
218
|
+
() => [loading(<BroadcastSkeleton />)],
|
|
219
|
+
);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Rule of thumb: shared `use` is for items that legitimately apply to every slot. Slot-local `use` is for per-slot precision — especially `loading()` and `loading(false)`.
|
|
223
|
+
|
|
224
|
+
### Replacing a whole slot from a parent's `handler.use`
|
|
225
|
+
|
|
226
|
+
A handler can publish a default `parallel({...})` set via its `.use`, and the mount site can replace any individual slot by re-declaring it. Last `parallel()` per slot name wins (see `skills/parallel` § Slot Override Semantics).
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
const ProductPage: Handler<"/product/:slug"> = (ctx) => (
|
|
230
|
+
<article>
|
|
231
|
+
<ProductHero slug={ctx.params.slug} />
|
|
232
|
+
<ParallelOutlet name="@related" />
|
|
233
|
+
<ParallelOutlet name="@reviews" />
|
|
234
|
+
</article>
|
|
235
|
+
);
|
|
236
|
+
ProductPage.use = () => [
|
|
237
|
+
parallel({
|
|
238
|
+
"@related": DefaultRelatedProducts,
|
|
239
|
+
"@reviews": DefaultReviews,
|
|
240
|
+
}),
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
244
|
+
// Override @related only; @reviews keeps the default from handler.use.
|
|
245
|
+
parallel({ "@related": SiteSpecificRelated }),
|
|
246
|
+
]);
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Other mount sites
|
|
250
|
+
|
|
251
|
+
### Pages (`path()`)
|
|
252
|
+
|
|
253
|
+
Page handlers can carry middleware, loaders, error boundaries, parallel slots, etc. — anything from the `path` row of the table above.
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
const CheckoutPage: Handler<"/checkout"> = async (ctx) => { /* … */ };
|
|
257
|
+
CheckoutPage.use = () => [
|
|
258
|
+
middleware(requireAuth),
|
|
259
|
+
loader(CartLoader),
|
|
260
|
+
errorBoundary(<CheckoutError />),
|
|
261
|
+
notFoundBoundary(<CheckoutNotFound />),
|
|
262
|
+
];
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Layouts (`layout()`)
|
|
266
|
+
|
|
267
|
+
Layout handlers can carry middleware that runs for every child route, plus default parallels, includes, etc.
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
const AdminLayout: Handler = (ctx) => {
|
|
271
|
+
const user = ctx.get(CurrentUser);
|
|
272
|
+
return <Admin user={user} />;
|
|
273
|
+
};
|
|
274
|
+
AdminLayout.use = () => [
|
|
275
|
+
middleware(requireAdmin),
|
|
276
|
+
parallel({ "@adminNotifs": AdminNotifsSlot }),
|
|
277
|
+
];
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Intercepts (`intercept()`)
|
|
281
|
+
|
|
282
|
+
Intercept handlers can carry their own middleware chain, loaders, and even nested layouts/routes for the modal shell.
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
const QuickViewModal: Handler = async (ctx) => {
|
|
286
|
+
const product = await ctx.use(ProductLoader);
|
|
287
|
+
return <QuickView product={product} />;
|
|
288
|
+
};
|
|
289
|
+
QuickViewModal.use = () => [
|
|
290
|
+
loader(ProductLoader),
|
|
291
|
+
loading(<QuickViewSkeleton />),
|
|
292
|
+
layout(<ModalChrome />),
|
|
293
|
+
];
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## `loading()` is a single-assignment item — scope it correctly
|
|
297
|
+
|
|
298
|
+
Most `use` items accumulate when merged: `handler.use` `middleware()` runs _and_ explicit `middleware()` runs; both `loader()` registrations apply. `loading()` is different — it mutates `entry.loading` directly, last call wins ([dsl-helpers.ts `loadingFn`](../../src/route-definition/dsl-helpers.ts)).
|
|
299
|
+
|
|
300
|
+
For pages, layouts, and intercepts that's straightforward: explicit `loading()` at the mount site replaces any `loading()` from `handler.use`. The merge order is `handler.use → explicit`, so the explicit one is the last writer and wins.
|
|
301
|
+
|
|
302
|
+
For parallel slots, the shared `parallel(..., () => [...])` callback is **broadcast** to every slot in the call. A single `loading()` placed there lands on every slot, overwriting each slot's `handler.use` default. To scope `loading()` to one slot, use the **slot descriptor** form:
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
const Cart: Handler = async (ctx) => { /* … */ };
|
|
306
|
+
Cart.use = () => [loader(CartLoader), loading(<CartSkeleton />)];
|
|
307
|
+
|
|
308
|
+
const Notifs: Handler = async (ctx) => { /* … */ };
|
|
309
|
+
Notifs.use = () => [loader(NotifsLoader), loading(<NotifsSkeleton />)];
|
|
310
|
+
|
|
311
|
+
// ✅ @cart gets a custom skeleton; @notifs keeps its handler.use default.
|
|
312
|
+
parallel({
|
|
313
|
+
"@cart": {
|
|
314
|
+
handler: Cart,
|
|
315
|
+
use: () => [loading(<CustomCartSkeleton />)],
|
|
316
|
+
},
|
|
317
|
+
"@notifs": Notifs,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// ✅ Opt one slot out of streaming while siblings still stream the broadcast.
|
|
321
|
+
parallel(
|
|
322
|
+
{
|
|
323
|
+
"@cart": { handler: Cart, use: () => [loading(false)] },
|
|
324
|
+
"@notifs": Notifs,
|
|
325
|
+
},
|
|
326
|
+
() => [loading(<BroadcastSkeleton />)],
|
|
327
|
+
);
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Per-slot merge order is **handler.use → shared use → slot-local use**. Slot-local is the narrowest scope, so it wins for last-write-wins items like `loading()`. Items that accumulate within the parallel allow-list (`loader`, `revalidate`, `errorBoundary`, `notFoundBoundary`, `transition`) compose across all three layers regardless.
|
|
331
|
+
|
|
332
|
+
Other things to keep in mind about `loading()`:
|
|
333
|
+
|
|
334
|
+
- Any `loading()` (regardless of source) makes the segment a streaming unit. A handler that includes `loading()` in its `.use` opts every mount site into streaming by default. To opt back out, pass `loading(false)` at the mount site (`loading: false` handling in [match-middleware/segment-resolution.ts](../../src/router/match-middleware/segment-resolution.ts)) — use the slot descriptor form for parallel slots so the opt-out doesn't broadcast.
|
|
335
|
+
|
|
336
|
+
Rule of thumb: only put `loading()` in `handler.use` if you genuinely want every mount site to stream by default. Use the slot descriptor's `use` for any per-slot intent at a `parallel()` call.
|
|
337
|
+
|
|
338
|
+
## Edge cases & gotchas
|
|
339
|
+
|
|
340
|
+
- **ReactNode handlers cannot have `.use`.** A bare JSX element passed as a handler (e.g., `path("/about", <About />)`) has no function to attach properties to. Pass a function or branded definition instead.
|
|
341
|
+
- **Branded handlers** — `Static()`, `Prerender()`, and `Passthrough()` are positional constructors (not object-arg). Construct first, then attach `.use` to the returned definition:
|
|
342
|
+
|
|
343
|
+
```typescript
|
|
344
|
+
const ProductPage = Prerender(async (ctx) => {
|
|
345
|
+
const product = await fetchProduct(ctx.params.slug);
|
|
346
|
+
return <ProductView product={product} />;
|
|
347
|
+
});
|
|
348
|
+
ProductPage.use = () => [loader(ProductLoader)];
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
- **Items can be flat or nested arrays.** `handler.use()` results are flattened with `.flat(3)` before validation, so factory helpers that return arrays inline work the same as in regular `use()` callbacks.
|
|
352
|
+
- **Validation runs at registration / first match**, not at handler definition. A handler doesn't know its mount site at definition time — the same handler used in a `path()` and an `intercept()` is validated against each mount's allowed-types set when registered.
|
|
353
|
+
- **No silent shadowing.** If a disallowed item slips through (e.g., a layout factory returning `cache()` from a slot's `handler.use`), the runtime throws with the offending type and mount site named.
|
|
354
|
+
|
|
355
|
+
## Cross-references
|
|
356
|
+
|
|
357
|
+
- `skills/route` — `path()` mount site basics
|
|
358
|
+
- `skills/layout` — `layout()` mount site basics
|
|
359
|
+
- `skills/parallel` — parallel slot semantics, slot override rules, streaming behavior
|
|
360
|
+
- `skills/intercept` — intercept mount site basics
|
|
361
|
+
- `skills/loader` — defining `createLoader` and reading via `ctx.use()`
|
|
362
|
+
- `skills/middleware` — middleware semantics and ordering
|