@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
|
@@ -19,6 +19,8 @@ import type {
|
|
|
19
19
|
ResolvedRouteMap,
|
|
20
20
|
} from "./route-config.js";
|
|
21
21
|
import type { LoaderDefinition } from "./loader-types.js";
|
|
22
|
+
import type { UseItems, HandlerUseItem } from "../route-types.js";
|
|
23
|
+
import type { RequestScope } from "./request-scope.js";
|
|
22
24
|
|
|
23
25
|
// Re-export MiddlewareFn for internal/advanced use
|
|
24
26
|
export type { MiddlewareFn } from "../router/middleware.js";
|
|
@@ -135,7 +137,7 @@ export type Handler<
|
|
|
135
137
|
| Record<string, any> = {},
|
|
136
138
|
TRouteMap extends {} = DefaultHandlerRouteMap,
|
|
137
139
|
TEnv = DefaultEnv,
|
|
138
|
-
> = (
|
|
140
|
+
> = ((
|
|
139
141
|
ctx: HandlerContext<
|
|
140
142
|
T extends `.${infer Local}`
|
|
141
143
|
? Local extends keyof TRouteMap
|
|
@@ -160,7 +162,10 @@ export type Handler<
|
|
|
160
162
|
: ExtractSearchFromEntry<DefaultHandlerRouteMap, T>,
|
|
161
163
|
TRouteMap extends DefaultHandlerRouteMap ? never : TRouteMap
|
|
162
164
|
>,
|
|
163
|
-
) => ReactNode | Promise<ReactNode> | Response | Promise<Response
|
|
165
|
+
) => ReactNode | Promise<ReactNode> | Response | Promise<Response>) & {
|
|
166
|
+
/** Composable default DSL items merged when the handler is mounted. */
|
|
167
|
+
use?: () => UseItems<HandlerUseItem>;
|
|
168
|
+
};
|
|
164
169
|
|
|
165
170
|
/**
|
|
166
171
|
* Context passed to handlers (Hono-inspired type-safe context)
|
|
@@ -170,7 +175,7 @@ export type Handler<
|
|
|
170
175
|
* - Cleaned route URL (`url`, `searchParams`, `pathname` — no `_rsc*` params)
|
|
171
176
|
* - Original request (`request` — raw transport URL, headers, method, body)
|
|
172
177
|
* - Platform bindings (env.DB, env.KV, env.SECRETS)
|
|
173
|
-
* - Middleware variables (
|
|
178
|
+
* - Middleware variables (`get("user")`, `get("permissions")`)
|
|
174
179
|
* - Getter/setter for variables (get('user'), set('user', ...))
|
|
175
180
|
*
|
|
176
181
|
* @example
|
|
@@ -178,8 +183,7 @@ export type Handler<
|
|
|
178
183
|
* const handler = (ctx: HandlerContext<{ slug: string }, AppEnv>) => {
|
|
179
184
|
* ctx.params.slug // Route param (string)
|
|
180
185
|
* ctx.env.DB // Binding (D1Database)
|
|
181
|
-
* ctx.
|
|
182
|
-
* ctx.get('user') // Alternative getter
|
|
186
|
+
* ctx.get('user') // Variable (User | undefined)
|
|
183
187
|
* ctx.set('user', {...}) // Setter
|
|
184
188
|
* ctx.url // Clean URL (no _rsc* params)
|
|
185
189
|
* ctx.searchParams // Clean params (no _rsc* params)
|
|
@@ -192,7 +196,7 @@ export type HandlerContext<
|
|
|
192
196
|
TEnv = DefaultEnv,
|
|
193
197
|
TSearch extends SearchSchema = {},
|
|
194
198
|
TRouteMap = never,
|
|
195
|
-
> = {
|
|
199
|
+
> = RequestScope<TEnv> & {
|
|
196
200
|
/**
|
|
197
201
|
* Route parameters extracted from the URL pattern.
|
|
198
202
|
* Type-safe when using Handler<"/path/:param"> or Handler<{ param: string }>.
|
|
@@ -207,43 +211,19 @@ export type HandlerContext<
|
|
|
207
211
|
*/
|
|
208
212
|
build: boolean;
|
|
209
213
|
/**
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* for cases where you need original headers, method, or body.
|
|
214
|
-
*/
|
|
215
|
-
request: Request;
|
|
216
|
-
/**
|
|
217
|
-
* Query parameters from the URL (system params like `_rsc*` are filtered).
|
|
218
|
-
* Always a standard URLSearchParams instance.
|
|
214
|
+
* True when running in Vite dev mode, false during production build or
|
|
215
|
+
* live request rendering. Use this to branch on runtime mode without
|
|
216
|
+
* changing build semantics (e.g., skip expensive operations in dev).
|
|
219
217
|
*/
|
|
220
|
-
|
|
218
|
+
dev: boolean;
|
|
221
219
|
/**
|
|
222
220
|
* Typed search parameters parsed from URL query string via the route's
|
|
223
221
|
* search schema. Empty object when no schema is defined.
|
|
224
222
|
*/
|
|
225
223
|
search: {} extends TSearch ? {} : ResolveSearchSchema<TSearch>;
|
|
226
|
-
/**
|
|
227
|
-
* The pathname portion of the request URL.
|
|
228
|
-
*/
|
|
229
|
-
pathname: string;
|
|
230
|
-
/**
|
|
231
|
-
* The full URL object (with system params filtered).
|
|
232
|
-
*/
|
|
233
|
-
url: URL;
|
|
234
|
-
/**
|
|
235
|
-
* Platform bindings (DB, KV, secrets, etc.).
|
|
236
|
-
* Access resources like `ctx.env.DB`, `ctx.env.KV`.
|
|
237
|
-
*/
|
|
238
|
-
env: TEnv;
|
|
239
|
-
/**
|
|
240
|
-
* Middleware-injected variables.
|
|
241
|
-
* Access values like `ctx.var.user`, `ctx.var.permissions`.
|
|
242
|
-
*/
|
|
243
|
-
var: DefaultVars;
|
|
244
224
|
/**
|
|
245
225
|
* Type-safe getter for middleware variables.
|
|
246
|
-
*
|
|
226
|
+
* Preferred way to read middleware-injected variables.
|
|
247
227
|
*
|
|
248
228
|
* @example
|
|
249
229
|
* ```typescript
|
|
@@ -264,24 +244,18 @@ export type HandlerContext<
|
|
|
264
244
|
* ```
|
|
265
245
|
*/
|
|
266
246
|
set: {
|
|
267
|
-
<T>(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
* return <ProductPage />;
|
|
278
|
-
* });
|
|
279
|
-
* ```
|
|
280
|
-
*/
|
|
281
|
-
res: Response;
|
|
247
|
+
<T>(
|
|
248
|
+
contextVar: ContextVar<T>,
|
|
249
|
+
value: T,
|
|
250
|
+
options?: { cache?: boolean },
|
|
251
|
+
): void;
|
|
252
|
+
} & (<K extends keyof DefaultVars>(
|
|
253
|
+
key: K,
|
|
254
|
+
value: DefaultVars[K],
|
|
255
|
+
options?: { cache?: boolean },
|
|
256
|
+
) => void);
|
|
282
257
|
/**
|
|
283
|
-
*
|
|
284
|
-
* Headers set here are merged into the final response.
|
|
258
|
+
* Response headers. Headers set here are merged into the final response.
|
|
285
259
|
*
|
|
286
260
|
* @example
|
|
287
261
|
* ```typescript
|
|
@@ -295,8 +269,15 @@ export type HandlerContext<
|
|
|
295
269
|
/**
|
|
296
270
|
* Access loader data or push handle data.
|
|
297
271
|
*
|
|
272
|
+
* Available in route handlers, layout handlers, middleware, server actions,
|
|
273
|
+
* and server components rendered within the request context.
|
|
274
|
+
*
|
|
298
275
|
* For loaders: Returns a promise that resolves to the loader data.
|
|
299
276
|
* Loaders are executed in parallel and memoized per request.
|
|
277
|
+
* Prefer DSL `loader()` + client `useLoader()` over `ctx.use(Loader)` —
|
|
278
|
+
* DSL loaders are always fresh and cache-safe. Use `ctx.use(Loader)` only
|
|
279
|
+
* when you need loader data in the handler itself (e.g., to set context
|
|
280
|
+
* variables or make routing decisions).
|
|
300
281
|
*
|
|
301
282
|
* For handles: Returns a push function to add data for this segment.
|
|
302
283
|
* Handle data accumulates across all matched route segments.
|
|
@@ -304,10 +285,11 @@ export type HandlerContext<
|
|
|
304
285
|
*
|
|
305
286
|
* @example
|
|
306
287
|
* ```typescript
|
|
307
|
-
* // Loader
|
|
308
|
-
* route("
|
|
309
|
-
* const
|
|
310
|
-
*
|
|
288
|
+
* // Loader escape hatch — use when handler needs the data directly
|
|
289
|
+
* route("product", async (ctx) => {
|
|
290
|
+
* const { product } = await ctx.use(ProductLoader);
|
|
291
|
+
* ctx.set(Product, product); // make available to children
|
|
292
|
+
* return <ProductPage />;
|
|
311
293
|
* });
|
|
312
294
|
*
|
|
313
295
|
* // Handle usage - direct value
|
|
@@ -436,6 +418,10 @@ export type InternalHandlerContext<
|
|
|
436
418
|
TEnv = DefaultEnv,
|
|
437
419
|
TSearch extends SearchSchema = {},
|
|
438
420
|
> = HandlerContext<TParams, TEnv, TSearch> & {
|
|
421
|
+
/** @internal Stub response for collecting headers/cookies. */
|
|
422
|
+
res: Response;
|
|
423
|
+
/** @internal Shared variable backing store for ctx.get()/ctx.set(). */
|
|
424
|
+
_variables: Record<string, any>;
|
|
439
425
|
/** Prerender-only control flow helper, attached when the runtime context supports it. */
|
|
440
426
|
passthrough?: () => unknown;
|
|
441
427
|
/** Current segment ID for handle data attribution. */
|
|
@@ -523,30 +509,112 @@ export type RevalidateParams<TParams = GenericParams, TEnv = any> = Parameters<
|
|
|
523
509
|
* })
|
|
524
510
|
* ```
|
|
525
511
|
*/
|
|
512
|
+
/**
|
|
513
|
+
* Revalidation function called during client-side navigation to decide whether
|
|
514
|
+
* a segment (layout, route, parallel slot, or loader) should be re-rendered.
|
|
515
|
+
*
|
|
516
|
+
* Return `true` to re-render, `false` to skip (keep client's current version),
|
|
517
|
+
* or `{ defaultShouldRevalidate: boolean }` to override the default for
|
|
518
|
+
* downstream segments.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```ts
|
|
522
|
+
* // Re-render only when a cart action happened or browser signals staleness
|
|
523
|
+
* revalidate(({ actionId, stale }) =>
|
|
524
|
+
* actionId?.includes("cart") || stale || false
|
|
525
|
+
* )
|
|
526
|
+
*
|
|
527
|
+
* // Always re-render when params change (default behavior made explicit)
|
|
528
|
+
* revalidate(({ defaultShouldRevalidate }) => defaultShouldRevalidate)
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
526
531
|
export type ShouldRevalidateFn<TParams = GenericParams, TEnv = any> = (args: {
|
|
532
|
+
/** Route params from the page being navigated away from. */
|
|
527
533
|
currentParams: TParams;
|
|
534
|
+
/** Full URL of the page being navigated away from. */
|
|
528
535
|
currentUrl: URL;
|
|
536
|
+
/** Route params for the navigation target. */
|
|
529
537
|
nextParams: TParams;
|
|
538
|
+
/** Full URL of the navigation target. */
|
|
530
539
|
nextUrl: URL;
|
|
540
|
+
/**
|
|
541
|
+
* The router's default revalidation decision for this segment.
|
|
542
|
+
* `true` when params changed or the segment is new to the client.
|
|
543
|
+
* Return this when you want default behavior plus your own conditions.
|
|
544
|
+
*/
|
|
531
545
|
defaultShouldRevalidate: boolean;
|
|
546
|
+
/** Full handler context — access to `ctx.use()`, `ctx.env`, `ctx.params`, etc. */
|
|
532
547
|
context: HandlerContext<TParams, TEnv>;
|
|
533
|
-
|
|
548
|
+
|
|
549
|
+
// ── Segment metadata (which segment is being evaluated) ──────────────
|
|
550
|
+
|
|
551
|
+
/** The type of segment being revalidated. */
|
|
534
552
|
segmentType: "layout" | "route" | "parallel";
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
553
|
+
/** Layout name (e.g., `"root"`, `"shop"`, `"auth"`). Only set for layout segments. */
|
|
554
|
+
layoutName?: string;
|
|
555
|
+
/** Slot name (e.g., `"@sidebar"`, `"@modal"`). Only set for parallel segments. */
|
|
556
|
+
slotName?: string;
|
|
557
|
+
|
|
558
|
+
// ── Action context (populated when revalidation is triggered by a server action) ──
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Identifier of the server action that triggered revalidation.
|
|
562
|
+
* `undefined` during normal navigation (no action involved).
|
|
563
|
+
*
|
|
564
|
+
* Format: `"src/<path>#<exportName>"` — the file path is the source path
|
|
565
|
+
* relative to the project root, followed by `#` and the exported function name.
|
|
566
|
+
*
|
|
567
|
+
* This is stable and can be used for path-based matching to revalidate
|
|
568
|
+
* when any action in a module or directory fires:
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* ```ts
|
|
572
|
+
* // Match a specific action
|
|
573
|
+
* revalidate(({ actionId }) => actionId === "src/actions/cart.ts#addToCart")
|
|
574
|
+
*
|
|
575
|
+
* // Match any action in the cart module
|
|
576
|
+
* revalidate(({ actionId }) => actionId?.includes("cart") ?? false)
|
|
577
|
+
*
|
|
578
|
+
* // Match any action under src/apps/store/actions/
|
|
579
|
+
* revalidate(({ actionId }) => actionId?.startsWith("src/apps/store/actions/") ?? false)
|
|
580
|
+
* ```
|
|
581
|
+
*/
|
|
582
|
+
actionId?: string;
|
|
583
|
+
/** URL where the action was executed (the page the user was on when they triggered the action). */
|
|
584
|
+
actionUrl?: URL;
|
|
585
|
+
/** Return value from the action execution. Can be used to conditionally revalidate based on the action's outcome. */
|
|
586
|
+
actionResult?: any;
|
|
587
|
+
/** FormData from the action request body. Only set for form-based actions (not inline `"use server"` actions). */
|
|
588
|
+
formData?: FormData;
|
|
589
|
+
/** HTTP method: `"GET"` for navigation, `"POST"` for server actions. */
|
|
590
|
+
method?: string;
|
|
591
|
+
|
|
592
|
+
// ── Route identity ───────────────────────────────────────────────────
|
|
593
|
+
|
|
594
|
+
/** Route name of the navigation target. Alias for `toRouteName`. */
|
|
595
|
+
routeName?: DefaultRouteName;
|
|
596
|
+
/**
|
|
597
|
+
* Route name being navigated away from.
|
|
598
|
+
* `undefined` for unnamed internal routes (those without a `name` option).
|
|
599
|
+
*/
|
|
600
|
+
fromRouteName?: DefaultRouteName;
|
|
601
|
+
/**
|
|
602
|
+
* Route name being navigated to.
|
|
603
|
+
* `undefined` for unnamed internal routes (those without a `name` option).
|
|
604
|
+
*/
|
|
605
|
+
toRouteName?: DefaultRouteName;
|
|
606
|
+
|
|
607
|
+
// ── Staleness signal ─────────────────────────────────────────────────
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* `true` when the browser signals that data may be stale — typically because
|
|
611
|
+
* a server action was executed in this or another tab (`_rsc_stale` header).
|
|
612
|
+
*
|
|
613
|
+
* This is NOT segment cache staleness (loaders are never segment-cached).
|
|
614
|
+
* Use this to decide whether loader data should be re-fetched after an
|
|
615
|
+
* action that may have mutated backend state.
|
|
616
|
+
*/
|
|
617
|
+
stale?: boolean;
|
|
550
618
|
}) => boolean | { defaultShouldRevalidate: boolean };
|
|
551
619
|
|
|
552
620
|
// MiddlewareFn is imported from "../router/middleware.js" and re-exported
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import type { ContextVar } from "../context-var.js";
|
|
2
|
+
import type { Handle } from "../handle.js";
|
|
2
3
|
import type { MiddlewareFn } from "../router/middleware.js";
|
|
3
4
|
import type { ScopedReverseFunction } from "../reverse.js";
|
|
4
5
|
import type { SearchSchema, ResolveSearchSchema } from "../search-params.js";
|
|
6
|
+
import type { UseItems, LoaderUseItem } from "../route-types.js";
|
|
5
7
|
import type {
|
|
6
8
|
DefaultEnv,
|
|
7
9
|
DefaultReverseRouteMap,
|
|
8
10
|
DefaultVars,
|
|
9
11
|
} from "./global-namespace.js";
|
|
12
|
+
import type { RequestScope } from "./request-scope.js";
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Context passed to loader functions during execution
|
|
@@ -38,7 +41,7 @@ export type LoaderContext<
|
|
|
38
41
|
TEnv = DefaultEnv,
|
|
39
42
|
TBody = unknown,
|
|
40
43
|
TSearch extends SearchSchema = {},
|
|
41
|
-
> = {
|
|
44
|
+
> = RequestScope<TEnv> & {
|
|
42
45
|
params: TParams;
|
|
43
46
|
/**
|
|
44
47
|
* Route params extracted from the URL pattern match (server-side only).
|
|
@@ -47,22 +50,43 @@ export type LoaderContext<
|
|
|
47
50
|
* resource scoping.
|
|
48
51
|
*/
|
|
49
52
|
routeParams: Record<string, string>;
|
|
50
|
-
request: Request;
|
|
51
|
-
searchParams: URLSearchParams;
|
|
52
53
|
search: {} extends TSearch ? {} : ResolveSearchSchema<TSearch>;
|
|
53
|
-
pathname: string;
|
|
54
|
-
url: URL;
|
|
55
|
-
env: TEnv;
|
|
56
|
-
var: DefaultVars;
|
|
57
54
|
get: {
|
|
58
55
|
<T>(contextVar: ContextVar<T>): T | undefined;
|
|
59
56
|
} & (<K extends keyof DefaultVars>(key: K) => DefaultVars[K]);
|
|
60
57
|
/**
|
|
61
|
-
* Access another loader's data
|
|
58
|
+
* Access another loader's data, or read handle data after rendered().
|
|
59
|
+
*
|
|
60
|
+
* For loaders: returns a promise (loaders run in parallel).
|
|
61
|
+
* For handles: returns collected data (only after `await ctx.rendered()`).
|
|
62
|
+
*/
|
|
63
|
+
use: {
|
|
64
|
+
<T, TLoaderParams = any>(
|
|
65
|
+
loader: LoaderDefinition<T, TLoaderParams>,
|
|
66
|
+
): Promise<T>;
|
|
67
|
+
<TData, TAccumulated = TData[]>(
|
|
68
|
+
handle: Handle<TData, TAccumulated>,
|
|
69
|
+
): TAccumulated;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* **Experimental.** Wait for all non-loader segments to settle.
|
|
73
|
+
*
|
|
74
|
+
* After the returned promise resolves, handle data is available via
|
|
75
|
+
* `ctx.use(handle)`. Only supported in DSL loaders on non-streaming
|
|
76
|
+
* trees (no `loading()`). Throws if called from a handler-invoked
|
|
77
|
+
* loader or when the tree uses streaming.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const PricesLoader = createLoader(async (ctx) => {
|
|
82
|
+
* "use server";
|
|
83
|
+
* await ctx.rendered();
|
|
84
|
+
* const products = ctx.use(Products); // reads handle data
|
|
85
|
+
* return pricing.getLive(products.map(p => p.id));
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
62
88
|
*/
|
|
63
|
-
|
|
64
|
-
loader: LoaderDefinition<T, TLoaderParams>,
|
|
65
|
-
) => Promise<T>;
|
|
89
|
+
rendered: () => Promise<void>;
|
|
66
90
|
/**
|
|
67
91
|
* HTTP method (GET, POST, PUT, PATCH, DELETE)
|
|
68
92
|
* Available when loader is called via load({ method: "POST", ... })
|
|
@@ -166,11 +190,11 @@ export type LoadOptions =
|
|
|
166
190
|
* return await db.products.findBySlug(slug);
|
|
167
191
|
* });
|
|
168
192
|
*
|
|
169
|
-
* //
|
|
170
|
-
* const
|
|
193
|
+
* // Client usage (preferred — cache-safe, always fresh)
|
|
194
|
+
* const { data } = useLoader(CartLoader);
|
|
171
195
|
*
|
|
172
|
-
* //
|
|
173
|
-
* const cart =
|
|
196
|
+
* // Server escape hatch (handler needs data directly)
|
|
197
|
+
* const cart = await ctx.use(CartLoader);
|
|
174
198
|
* ```
|
|
175
199
|
*/
|
|
176
200
|
export type LoaderDefinition<
|
|
@@ -180,4 +204,6 @@ export type LoaderDefinition<
|
|
|
180
204
|
__brand: "loader";
|
|
181
205
|
$$id: string; // Injected by Vite plugin (exposeInternalIds) - unique identifier
|
|
182
206
|
fn?: LoaderFn<T, TParams, any>; // Optional - server-side only, stored in registry for RSC
|
|
207
|
+
/** Composable default DSL items merged when the loader is mounted. */
|
|
208
|
+
use?: () => UseItems<LoaderUseItem>;
|
|
183
209
|
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RequestScope: the fields every user-facing context shares.
|
|
3
|
+
*
|
|
4
|
+
* A handler, middleware, loader, response handler, and the ALS-bound
|
|
5
|
+
* RequestContext are all different phases of the same request, and they
|
|
6
|
+
* all carry the same set of request-scoped capabilities: the raw Request,
|
|
7
|
+
* the parsed URL pair (`url` is cleaned of internal `_rsc*` params,
|
|
8
|
+
* `originalUrl` retains them), pathname/searchParams, platform bindings
|
|
9
|
+
* (`env`), and two escape hatches for work that outlives the response
|
|
10
|
+
* (`waitUntil`) or needs the raw Cloudflare runtime object
|
|
11
|
+
* (`executionContext`).
|
|
12
|
+
*
|
|
13
|
+
* Each public context type intersects `RequestScope<TEnv>` with its own
|
|
14
|
+
* phase-specific fields (e.g. `params`/`reverse` on HandlerContext,
|
|
15
|
+
* `headers`/`header()` on MiddlewareContext). That keeps platform surface
|
|
16
|
+
* in one place and lets the next runtime escape hatch we need land in
|
|
17
|
+
* one file instead of four.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { DefaultEnv } from "./global-namespace.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Minimal subset of Cloudflare Workers' ExecutionContext that the router
|
|
24
|
+
* uses. Defined locally so the package does not depend on
|
|
25
|
+
* `@cloudflare/workers-types`. Consumers that want the full type can cast.
|
|
26
|
+
*
|
|
27
|
+
* On non-Cloudflare runtimes (Node, dev server, tests), this is undefined
|
|
28
|
+
* — portable apps should prefer `ctx.waitUntil(...)`, which degrades
|
|
29
|
+
* gracefully. `ctx.executionContext` is the escape hatch for libraries
|
|
30
|
+
* (MCP, Durable Object routing, etc.) that type their arguments as the
|
|
31
|
+
* raw ExecutionContext.
|
|
32
|
+
*/
|
|
33
|
+
export interface ExecutionContext {
|
|
34
|
+
waitUntil(promise: Promise<any>): void;
|
|
35
|
+
passThroughOnException(): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Fallback `waitUntil` body used when no Cloudflare `ExecutionContext`
|
|
40
|
+
* is available (Node, dev, tests). Runs the work fire-and-forget and
|
|
41
|
+
* logs errors so they don't silently swallow.
|
|
42
|
+
*
|
|
43
|
+
* Exported so every `waitUntil` call site degrades identically instead
|
|
44
|
+
* of inventing its own fallback policy.
|
|
45
|
+
*/
|
|
46
|
+
export function fireAndForgetWaitUntil(fn: () => Promise<void>): void {
|
|
47
|
+
fn().catch((err) =>
|
|
48
|
+
console.error("[waitUntil] Background task failed:", err),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Fields present on every user-facing request context.
|
|
54
|
+
*
|
|
55
|
+
* @template TEnv - Platform bindings type (Cloudflare env, etc.).
|
|
56
|
+
*/
|
|
57
|
+
export interface RequestScope<TEnv = DefaultEnv> {
|
|
58
|
+
/**
|
|
59
|
+
* The original incoming Request object (transport URL intact).
|
|
60
|
+
* Use `url` / `searchParams` for application logic — those have
|
|
61
|
+
* internal `_rsc*` params stripped. `request` preserves the raw URL
|
|
62
|
+
* when you need original headers, method, or body.
|
|
63
|
+
*/
|
|
64
|
+
request: Request;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The request URL with internal `_rsc*` transport params stripped.
|
|
68
|
+
* Use this for routing, link generation, and display.
|
|
69
|
+
*/
|
|
70
|
+
url: URL;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The original request URL with all parameters intact, including
|
|
74
|
+
* internal `_rsc*` transport params. Use `url` for application logic
|
|
75
|
+
* — this is only needed for advanced cases like debugging or custom
|
|
76
|
+
* cache keying.
|
|
77
|
+
*/
|
|
78
|
+
originalUrl: URL;
|
|
79
|
+
|
|
80
|
+
/** URL pathname (same as `url.pathname`). */
|
|
81
|
+
pathname: string;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Query parameters from the URL (system params like `_rsc*` are
|
|
85
|
+
* filtered). Always a standard `URLSearchParams` instance.
|
|
86
|
+
*/
|
|
87
|
+
searchParams: URLSearchParams;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Platform bindings (DB, KV, secrets, etc.). On Cloudflare Workers
|
|
91
|
+
* these are the `env` object passed to the Worker's `fetch()` handler.
|
|
92
|
+
*/
|
|
93
|
+
env: TEnv;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Schedule work to run after the response is sent.
|
|
97
|
+
* On Cloudflare Workers, delegates to `executionContext.waitUntil()`.
|
|
98
|
+
* On Node / dev / tests, runs as fire-and-forget with error logging.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* ctx.waitUntil(async () => {
|
|
103
|
+
* await cacheStore.set(key, data, ttl);
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
waitUntil(fn: () => Promise<void>): void;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Raw Cloudflare Workers `ExecutionContext`, when running on a
|
|
111
|
+
* Cloudflare-compatible runtime. Undefined elsewhere.
|
|
112
|
+
*
|
|
113
|
+
* Escape hatch for libraries that type their arguments as
|
|
114
|
+
* `ExecutionContext` (MCP `fetch`, `routeAgentRequest`, etc.).
|
|
115
|
+
* For the common "do work after the response" case, prefer
|
|
116
|
+
* `ctx.waitUntil(...)` — it is platform-neutral.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* path.any("/mcp", (ctx) =>
|
|
121
|
+
* emailMcp.fetch(ctx.request, ctx.env, ctx.executionContext!),
|
|
122
|
+
* );
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
executionContext?: ExecutionContext;
|
|
126
|
+
}
|
|
@@ -24,17 +24,26 @@ type ParseConstraint<T extends string> =
|
|
|
24
24
|
* - :param(a|b)? -> { name: "param", optional: true, type: "a" | "b" }
|
|
25
25
|
*/
|
|
26
26
|
type ExtractParamInfo<T extends string> =
|
|
27
|
-
// Optional + constrained: :param(a|b)?
|
|
28
|
-
T extends `${infer Name}(${infer Constraint})
|
|
27
|
+
// Optional + constrained (with optional suffix): :param(a|b)?suffix
|
|
28
|
+
T extends `${infer Name}(${infer Constraint})?${string}`
|
|
29
29
|
? { name: Name; optional: true; type: ParseConstraint<Constraint> }
|
|
30
|
-
: // Constrained
|
|
31
|
-
T extends `${infer Name}(${infer Constraint})`
|
|
30
|
+
: // Constrained (with optional suffix): :param(a|b)suffix
|
|
31
|
+
T extends `${infer Name}(${infer Constraint})${string}`
|
|
32
32
|
? { name: Name; optional: false; type: ParseConstraint<Constraint> }
|
|
33
|
-
: // Optional
|
|
34
|
-
T extends `${infer Name}
|
|
33
|
+
: // Optional (with optional suffix): :param?suffix
|
|
34
|
+
T extends `${infer Name}?${string}`
|
|
35
35
|
? { name: Name; optional: true; type: string }
|
|
36
|
-
: //
|
|
37
|
-
|
|
36
|
+
: // Param with dot-suffix: :param.html
|
|
37
|
+
T extends `${infer Name}.${string}`
|
|
38
|
+
? { name: Name; optional: false; type: string }
|
|
39
|
+
: // Param with dash-suffix: :param-slug
|
|
40
|
+
T extends `${infer Name}-${string}`
|
|
41
|
+
? { name: Name; optional: false; type: string }
|
|
42
|
+
: // Param with tilde-suffix: :param~v2
|
|
43
|
+
T extends `${infer Name}~${string}`
|
|
44
|
+
? { name: Name; optional: false; type: string }
|
|
45
|
+
: // Required: :param (no suffix)
|
|
46
|
+
{ name: T; optional: false; type: string };
|
|
38
47
|
|
|
39
48
|
/**
|
|
40
49
|
* Build param object from info
|
package/src/types/route-entry.ts
CHANGED
|
@@ -8,10 +8,21 @@ export interface LazyIncludeContext {
|
|
|
8
8
|
urlPrefix: string;
|
|
9
9
|
namePrefix: string | undefined;
|
|
10
10
|
parent: unknown; // EntryData - avoid circular import
|
|
11
|
+
/** Counter snapshot from pattern extraction for consistent shortCode indices */
|
|
12
|
+
counters?: Record<string, number>;
|
|
11
13
|
cacheProfiles?: Record<
|
|
12
14
|
string,
|
|
13
15
|
import("../cache/profile-registry.js").CacheProfile
|
|
14
16
|
>;
|
|
17
|
+
/** Root scope flag for dot-local reverse resolution */
|
|
18
|
+
rootScoped?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Positional include scope token composed from the parent scope plus this
|
|
21
|
+
* include's sibling index (`${parentScope}I${idx}`). Applied to direct-
|
|
22
|
+
* descendant shortCodes during lazy evaluation so routes inside the
|
|
23
|
+
* include cannot collide with siblings declared outside it.
|
|
24
|
+
*/
|
|
25
|
+
includeScope?: string;
|
|
15
26
|
}
|
|
16
27
|
|
|
17
28
|
/**
|
|
@@ -55,6 +66,13 @@ export interface RouteEntry<TEnv = any> {
|
|
|
55
66
|
| Promise<() => Array<AllUseItems>>;
|
|
56
67
|
mountIndex: number;
|
|
57
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Router ID that owns this entry. Used to namespace the manifest cache
|
|
71
|
+
* so multi-router setups (host routing) don't share cached EntryData
|
|
72
|
+
* across routers with overlapping mountIndex + routeKey combinations.
|
|
73
|
+
*/
|
|
74
|
+
routerId?: string;
|
|
75
|
+
|
|
58
76
|
/**
|
|
59
77
|
* Route keys in this entry that have pre-render handlers.
|
|
60
78
|
* Used by the non-trie match path to set the `pr` flag.
|
|
@@ -62,7 +80,7 @@ export interface RouteEntry<TEnv = any> {
|
|
|
62
80
|
prerenderRouteKeys?: Set<string>;
|
|
63
81
|
|
|
64
82
|
/**
|
|
65
|
-
* Route keys in this entry that
|
|
83
|
+
* Route keys in this entry that are wrapped with `Passthrough()`.
|
|
66
84
|
* Used by the non-trie match path to set the `pt` flag.
|
|
67
85
|
*/
|
|
68
86
|
passthroughRouteKeys?: Set<string>;
|
package/src/types/segments.ts
CHANGED
|
@@ -50,7 +50,9 @@ export interface ResolvedSegment {
|
|
|
50
50
|
parallelName?: string; // For parallels: the parallel group name (used to match with revalidations)
|
|
51
51
|
// Loader-specific fields
|
|
52
52
|
loaderId?: string; // For loaders: the loader $$id identifier
|
|
53
|
+
_inherited?: boolean; // For inherited loaders: dedup marker for buildMatchResult
|
|
53
54
|
loaderData?: any; // For loaders: the resolved data from loader execution
|
|
55
|
+
parallelLoading?: ReactNode; // For parallel-owned loaders: the parallel's loading fallback
|
|
54
56
|
// Intercept loader fields (for streaming loader data in parallel segments)
|
|
55
57
|
loaderDataPromise?: Promise<any[]> | any[]; // Loader data promise or resolved array
|
|
56
58
|
loaderIds?: string[]; // IDs ($$id) of loaders for this segment
|
|
@@ -124,11 +126,6 @@ export interface MatchResult {
|
|
|
124
126
|
* Used by ctx.reverse() for local name resolution.
|
|
125
127
|
*/
|
|
126
128
|
routeName?: string;
|
|
127
|
-
/**
|
|
128
|
-
* Server-Timing header value (only present when debugPerformance is enabled)
|
|
129
|
-
* Can be added to response headers for DevTools integration
|
|
130
|
-
*/
|
|
131
|
-
serverTiming?: string;
|
|
132
129
|
/**
|
|
133
130
|
* State of named slots for this route match
|
|
134
131
|
* Key is slot name (e.g., "@modal"), value is slot state
|