@rangojs/router 0.0.0-experimental.88a3b2f7 → 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/README.md +50 -20
- package/dist/vite/index.js +647 -176
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +7 -5
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/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 +88 -16
- package/skills/loader/SKILL.md +35 -2
- package/skills/middleware/SKILL.md +32 -3
- package/skills/migrate-nextjs/SKILL.md +560 -0
- package/skills/migrate-react-router/SKILL.md +765 -0
- package/skills/parallel/SKILL.md +59 -0
- package/skills/rango/SKILL.md +24 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +24 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +3 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/navigation-bridge.ts +72 -4
- package/src/browser/navigation-client.ts +64 -13
- package/src/browser/navigation-store.ts +25 -1
- package/src/browser/partial-update.ts +34 -3
- package/src/browser/prefetch/cache.ts +129 -21
- package/src/browser/prefetch/fetch.ts +148 -16
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +30 -2
- package/src/browser/react/NavigationProvider.tsx +50 -11
- 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 +8 -1
- package/src/browser/rsc-router.tsx +34 -6
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/types.ts +13 -0
- package/src/build/route-trie.ts +50 -24
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/client.tsx +84 -230
- package/src/index.rsc.ts +3 -0
- package/src/index.ts +44 -9
- package/src/outlet-context.ts +1 -1
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +7 -3
- package/src/route-definition/dsl-helpers.ts +180 -24
- package/src/route-definition/helpers-types.ts +61 -14
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-types.ts +7 -0
- package/src/router/handler-context.ts +24 -4
- package/src/router/lazy-includes.ts +6 -6
- package/src/router/loader-resolution.ts +73 -46
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +3 -3
- package/src/router/match-middleware/cache-lookup.ts +10 -5
- package/src/router/match-middleware/segment-resolution.ts +1 -1
- package/src/router/match-result.ts +82 -4
- package/src/router/middleware-types.ts +2 -22
- package/src/router/middleware.ts +32 -4
- package/src/router/pattern-matching.ts +60 -9
- package/src/router/segment-resolution/fresh.ts +52 -0
- package/src/router/segment-resolution/revalidation.ts +69 -1
- package/src/router/trie-matching.ts +10 -4
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +1 -2
- package/src/rsc/handler.ts +21 -9
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/progressive-enhancement.ts +12 -2
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +12 -1
- package/src/rsc/server-action.ts +8 -0
- package/src/rsc/types.ts +1 -0
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +11 -61
- package/src/server/context.ts +26 -3
- package/src/server/handle-store.ts +19 -0
- package/src/server/request-context.ts +64 -56
- package/src/types/handler-context.ts +2 -34
- package/src/types/loader-types.ts +5 -6
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +1 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +34 -5
- package/src/urls/response-types.ts +2 -10
- package/src/use-loader.tsx +77 -5
- package/src/vite/debug.ts +55 -0
- package/src/vite/discovery/prerender-collection.ts +124 -83
- 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-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 +4 -6
- package/src/vite/rango.ts +49 -14
- package/src/vite/router-discovery.ts +186 -26
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +20 -6
|
@@ -20,6 +20,7 @@ import type {
|
|
|
20
20
|
} from "./route-config.js";
|
|
21
21
|
import type { LoaderDefinition } from "./loader-types.js";
|
|
22
22
|
import type { UseItems, HandlerUseItem } from "../route-types.js";
|
|
23
|
+
import type { RequestScope } from "./request-scope.js";
|
|
23
24
|
|
|
24
25
|
// Re-export MiddlewareFn for internal/advanced use
|
|
25
26
|
export type { MiddlewareFn } from "../router/middleware.js";
|
|
@@ -195,7 +196,7 @@ export type HandlerContext<
|
|
|
195
196
|
TEnv = DefaultEnv,
|
|
196
197
|
TSearch extends SearchSchema = {},
|
|
197
198
|
TRouteMap = never,
|
|
198
|
-
> = {
|
|
199
|
+
> = RequestScope<TEnv> & {
|
|
199
200
|
/**
|
|
200
201
|
* Route parameters extracted from the URL pattern.
|
|
201
202
|
* Type-safe when using Handler<"/path/:param"> or Handler<{ param: string }>.
|
|
@@ -215,44 +216,11 @@ export type HandlerContext<
|
|
|
215
216
|
* changing build semantics (e.g., skip expensive operations in dev).
|
|
216
217
|
*/
|
|
217
218
|
dev: boolean;
|
|
218
|
-
/**
|
|
219
|
-
* The original incoming Request object (transport URL intact).
|
|
220
|
-
* Use `ctx.url` / `ctx.searchParams` for application logic — those have
|
|
221
|
-
* internal `_rsc*` params stripped. `ctx.request` preserves the raw URL
|
|
222
|
-
* for cases where you need original headers, method, or body.
|
|
223
|
-
*/
|
|
224
|
-
request: Request;
|
|
225
|
-
/**
|
|
226
|
-
* Query parameters from the URL (system params like `_rsc*` are filtered).
|
|
227
|
-
* Always a standard URLSearchParams instance.
|
|
228
|
-
*/
|
|
229
|
-
searchParams: URLSearchParams;
|
|
230
219
|
/**
|
|
231
220
|
* Typed search parameters parsed from URL query string via the route's
|
|
232
221
|
* search schema. Empty object when no schema is defined.
|
|
233
222
|
*/
|
|
234
223
|
search: {} extends TSearch ? {} : ResolveSearchSchema<TSearch>;
|
|
235
|
-
/**
|
|
236
|
-
* The pathname portion of the request URL.
|
|
237
|
-
*/
|
|
238
|
-
pathname: string;
|
|
239
|
-
/**
|
|
240
|
-
* The full URL object (with internal `_rsc*` params stripped).
|
|
241
|
-
* Use this for application logic — routing, link generation, display.
|
|
242
|
-
*/
|
|
243
|
-
url: URL;
|
|
244
|
-
/**
|
|
245
|
-
* The original request URL with all parameters intact, including
|
|
246
|
-
* internal `_rsc*` transport params. Use `ctx.url` for application
|
|
247
|
-
* logic — this is only needed for advanced cases like debugging
|
|
248
|
-
* or custom cache keying.
|
|
249
|
-
*/
|
|
250
|
-
originalUrl: URL;
|
|
251
|
-
/**
|
|
252
|
-
* Platform bindings (DB, KV, secrets, etc.).
|
|
253
|
-
* Access resources like `ctx.env.DB`, `ctx.env.KV`.
|
|
254
|
-
*/
|
|
255
|
-
env: TEnv;
|
|
256
224
|
/**
|
|
257
225
|
* Type-safe getter for middleware variables.
|
|
258
226
|
* Preferred way to read middleware-injected variables.
|
|
@@ -3,11 +3,13 @@ import type { Handle } from "../handle.js";
|
|
|
3
3
|
import type { MiddlewareFn } from "../router/middleware.js";
|
|
4
4
|
import type { ScopedReverseFunction } from "../reverse.js";
|
|
5
5
|
import type { SearchSchema, ResolveSearchSchema } from "../search-params.js";
|
|
6
|
+
import type { UseItems, LoaderUseItem } from "../route-types.js";
|
|
6
7
|
import type {
|
|
7
8
|
DefaultEnv,
|
|
8
9
|
DefaultReverseRouteMap,
|
|
9
10
|
DefaultVars,
|
|
10
11
|
} from "./global-namespace.js";
|
|
12
|
+
import type { RequestScope } from "./request-scope.js";
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Context passed to loader functions during execution
|
|
@@ -39,7 +41,7 @@ export type LoaderContext<
|
|
|
39
41
|
TEnv = DefaultEnv,
|
|
40
42
|
TBody = unknown,
|
|
41
43
|
TSearch extends SearchSchema = {},
|
|
42
|
-
> = {
|
|
44
|
+
> = RequestScope<TEnv> & {
|
|
43
45
|
params: TParams;
|
|
44
46
|
/**
|
|
45
47
|
* Route params extracted from the URL pattern match (server-side only).
|
|
@@ -48,12 +50,7 @@ export type LoaderContext<
|
|
|
48
50
|
* resource scoping.
|
|
49
51
|
*/
|
|
50
52
|
routeParams: Record<string, string>;
|
|
51
|
-
request: Request;
|
|
52
|
-
searchParams: URLSearchParams;
|
|
53
53
|
search: {} extends TSearch ? {} : ResolveSearchSchema<TSearch>;
|
|
54
|
-
pathname: string;
|
|
55
|
-
url: URL;
|
|
56
|
-
env: TEnv;
|
|
57
54
|
get: {
|
|
58
55
|
<T>(contextVar: ContextVar<T>): T | undefined;
|
|
59
56
|
} & (<K extends keyof DefaultVars>(key: K) => DefaultVars[K]);
|
|
@@ -207,4 +204,6 @@ export type LoaderDefinition<
|
|
|
207
204
|
__brand: "loader";
|
|
208
205
|
$$id: string; // Injected by Vite plugin (exposeInternalIds) - unique identifier
|
|
209
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>;
|
|
210
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
|
+
}
|
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
|
/**
|
package/src/types/segments.ts
CHANGED
|
@@ -50,12 +50,12 @@ 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
|
|
54
55
|
parallelLoading?: ReactNode; // For parallel-owned loaders: the parallel's loading fallback
|
|
55
56
|
// Intercept loader fields (for streaming loader data in parallel segments)
|
|
56
57
|
loaderDataPromise?: Promise<any[]> | any[]; // Loader data promise or resolved array
|
|
57
58
|
loaderIds?: string[]; // IDs ($$id) of loaders for this segment
|
|
58
|
-
parallelLoaderSources?: any[]; // Internal: preserves stable aggregate promise across renders
|
|
59
59
|
// Error-specific fields
|
|
60
60
|
error?: ErrorInfo; // For error segments: the error information
|
|
61
61
|
// NotFound-specific fields
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
runWithPrefixes,
|
|
5
5
|
getUrlPrefix,
|
|
6
6
|
getNamePrefix,
|
|
7
|
-
getRootScoped,
|
|
8
7
|
} from "../server/context";
|
|
9
8
|
import {
|
|
10
9
|
INTERNAL_INCLUDE_SCOPE_PREFIX,
|
|
@@ -149,22 +148,32 @@ export function createIncludeHelper<TEnv>(): IncludeFn<TEnv> {
|
|
|
149
148
|
});
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
//
|
|
153
|
-
//
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
//
|
|
158
|
-
//
|
|
159
|
-
//
|
|
160
|
-
// and
|
|
161
|
-
|
|
151
|
+
// Allocate an include-scope token for this include() call. The token is
|
|
152
|
+
// appended to the parent's shortCode prefix whenever the include's
|
|
153
|
+
// direct-descendant shortCodes are generated (see getShortCode in
|
|
154
|
+
// context.ts), partitioning the parent's counter namespace so routes
|
|
155
|
+
// inside an include cannot collide with siblings declared outside it.
|
|
156
|
+
//
|
|
157
|
+
// Scopes compose: a nested include inside an outer include with scope
|
|
158
|
+
// "I0" allocates against the `${parent.shortCode}I0_include` counter
|
|
159
|
+
// and produces scope "I0I0", "I0I1", etc.
|
|
160
|
+
const parentScope = ctx.includeScope ?? "";
|
|
161
|
+
let includeScope = parentScope;
|
|
162
162
|
if (capturedParent?.shortCode) {
|
|
163
|
-
const
|
|
164
|
-
ctx.counters[
|
|
165
|
-
ctx.counters[
|
|
163
|
+
const includeCounterKey = `${capturedParent.shortCode}${parentScope}_include`;
|
|
164
|
+
ctx.counters[includeCounterKey] ??= 0;
|
|
165
|
+
const includeIdx = ctx.counters[includeCounterKey];
|
|
166
|
+
ctx.counters[includeCounterKey] = includeIdx + 1;
|
|
167
|
+
includeScope = `${parentScope}I${includeIdx}`;
|
|
166
168
|
}
|
|
167
169
|
|
|
170
|
+
// Snapshot parent's counters AFTER allocating the include scope so lazy
|
|
171
|
+
// manifest generation starts with the same counter state this include
|
|
172
|
+
// observed — its descendants still get fresh per-scope counters because
|
|
173
|
+
// they key off `${parent.shortCode}${includeScope}_*` (not shared with
|
|
174
|
+
// siblings outside the include).
|
|
175
|
+
const capturedCounters = { ...ctx.counters };
|
|
176
|
+
|
|
168
177
|
// Compute rootScoped at capture time, mirroring the logic in runWithPrefixes.
|
|
169
178
|
// This ensures lazy evaluation restores the correct scope state.
|
|
170
179
|
const parentRootScoped = ctx.rootScoped;
|
|
@@ -191,6 +200,7 @@ export function createIncludeHelper<TEnv>(): IncludeFn<TEnv> {
|
|
|
191
200
|
counters: capturedCounters,
|
|
192
201
|
cacheProfiles: ctx.cacheProfiles,
|
|
193
202
|
rootScoped: capturedRootScoped,
|
|
203
|
+
includeScope,
|
|
194
204
|
},
|
|
195
205
|
} as IncludeItem;
|
|
196
206
|
};
|
|
@@ -233,12 +233,27 @@ export type PathHelpers<TEnv> = {
|
|
|
233
233
|
include: IncludeFn<TEnv>;
|
|
234
234
|
|
|
235
235
|
/**
|
|
236
|
-
* Define parallel routes that render simultaneously in named slots
|
|
236
|
+
* Define parallel routes that render simultaneously in named slots.
|
|
237
|
+
*
|
|
238
|
+
* A slot value can be a Handler / ReactNode / StaticHandlerDefinition
|
|
239
|
+
* (legacy form, broadcast use applies to every slot) or a slot descriptor
|
|
240
|
+
* `{ handler, use? }` whose `use` is scoped to that slot only. Per-slot
|
|
241
|
+
* merge order is `handler.use` → shared `use` → slot-local `use`, with
|
|
242
|
+
* narrowest scope winning for last-write-wins items like `loading()`.
|
|
237
243
|
*/
|
|
238
244
|
parallel: <
|
|
239
245
|
TSlots extends Record<
|
|
240
246
|
`@${string}`,
|
|
241
|
-
Handler<any, any, TEnv>
|
|
247
|
+
| Handler<any, any, TEnv>
|
|
248
|
+
| ReactNode
|
|
249
|
+
| StaticHandlerDefinition
|
|
250
|
+
| {
|
|
251
|
+
handler:
|
|
252
|
+
| Handler<any, any, TEnv>
|
|
253
|
+
| ReactNode
|
|
254
|
+
| StaticHandlerDefinition;
|
|
255
|
+
use?: () => ParallelUseItem[];
|
|
256
|
+
}
|
|
242
257
|
>,
|
|
243
258
|
>(
|
|
244
259
|
slots: TSlots,
|
|
@@ -264,9 +279,20 @@ export type PathHelpers<TEnv> = {
|
|
|
264
279
|
) => InterceptItem;
|
|
265
280
|
|
|
266
281
|
/**
|
|
267
|
-
* Attach middleware to the current route/layout
|
|
282
|
+
* Attach middleware to the current route/layout, or wrap child segments
|
|
268
283
|
*/
|
|
269
|
-
middleware:
|
|
284
|
+
middleware: {
|
|
285
|
+
(fn: MiddlewareFn<TEnv>): MiddlewareItem;
|
|
286
|
+
(
|
|
287
|
+
fn: MiddlewareFn<TEnv>,
|
|
288
|
+
children: () => UseItems<LayoutUseItem>,
|
|
289
|
+
): MiddlewareItem;
|
|
290
|
+
(fns: MiddlewareFn<TEnv>[]): MiddlewareItem;
|
|
291
|
+
(
|
|
292
|
+
fns: MiddlewareFn<TEnv>[],
|
|
293
|
+
children: () => UseItems<LayoutUseItem>,
|
|
294
|
+
): MiddlewareItem;
|
|
295
|
+
};
|
|
270
296
|
|
|
271
297
|
/**
|
|
272
298
|
* Control when a segment should revalidate during navigation
|
|
@@ -284,7 +310,10 @@ export type PathHelpers<TEnv> = {
|
|
|
284
310
|
/**
|
|
285
311
|
* Attach a loading component to the current route/layout
|
|
286
312
|
*/
|
|
287
|
-
loading: (
|
|
313
|
+
loading: (
|
|
314
|
+
component: ReactNode | (() => ReactNode),
|
|
315
|
+
options?: { ssr?: boolean },
|
|
316
|
+
) => LoadingItem;
|
|
288
317
|
|
|
289
318
|
/**
|
|
290
319
|
* Attach an error boundary to catch errors in this segment
|
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
DefaultVars,
|
|
6
6
|
} from "../types/global-namespace.js";
|
|
7
7
|
import type { UseItems, ResponseRouteUseItem } from "../route-types.js";
|
|
8
|
+
import type { RequestScope } from "../types/request-scope.js";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Reverse function for response handler contexts.
|
|
@@ -93,19 +94,10 @@ export type TextResponseHandler<
|
|
|
93
94
|
export interface ResponseHandlerContext<
|
|
94
95
|
TParams = Record<string, string>,
|
|
95
96
|
TEnv = any,
|
|
96
|
-
> {
|
|
97
|
-
request: Request;
|
|
97
|
+
> extends RequestScope<TEnv> {
|
|
98
98
|
params: TParams;
|
|
99
99
|
/** @internal Phantom property for params type invariance. Prevents mounting handlers on wrong routes. */
|
|
100
100
|
readonly _paramCheck?: (params: TParams) => TParams;
|
|
101
|
-
/** Platform bindings (DB, KV, secrets, etc.). */
|
|
102
|
-
env: TEnv;
|
|
103
|
-
/** Query parameters from the URL (system params like `_rsc*` are filtered). */
|
|
104
|
-
searchParams: URLSearchParams;
|
|
105
|
-
/** The full URL object (with system params filtered). */
|
|
106
|
-
url: URL;
|
|
107
|
-
/** The pathname portion of the request URL. */
|
|
108
|
-
pathname: string;
|
|
109
101
|
reverse: ResponseReverseFunction;
|
|
110
102
|
/** Read a variable set by middleware via ctx.set(key, value) or ctx.set(ContextVar, value). */
|
|
111
103
|
get: {
|
package/src/use-loader.tsx
CHANGED
|
@@ -1,9 +1,71 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
isValidElement,
|
|
5
|
+
startTransition,
|
|
6
|
+
useCallback,
|
|
7
|
+
useContext,
|
|
8
|
+
useEffect,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
type ReactNode,
|
|
13
|
+
} from "react";
|
|
4
14
|
import { OutletContext, type OutletContextValue } from "./outlet-context.js";
|
|
5
15
|
import type { LoaderDefinition, LoadOptions } from "./types.js";
|
|
6
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Extract a specific loader's data from a content ReactNode.
|
|
19
|
+
*
|
|
20
|
+
* When a route registers loaders via loader(), the resolved data lives in
|
|
21
|
+
* the route's OutletProvider (rendered as <Outlet /> content). Parallel
|
|
22
|
+
* slots are siblings of <Outlet />, so they can't find it by walking
|
|
23
|
+
* the parent context chain. This helper traverses wrapper elements
|
|
24
|
+
* (MountContextProvider, ViewTransition, etc.) to reach the OutletProvider
|
|
25
|
+
* and extract the loader data directly.
|
|
26
|
+
*/
|
|
27
|
+
const NOT_FOUND = Symbol("not-found");
|
|
28
|
+
|
|
29
|
+
function extractContentLoaderData(
|
|
30
|
+
node: ReactNode,
|
|
31
|
+
loaderId: string,
|
|
32
|
+
): unknown | typeof NOT_FOUND {
|
|
33
|
+
if (!isValidElement(node)) return NOT_FOUND;
|
|
34
|
+
const props = node.props as Record<string, any> | undefined;
|
|
35
|
+
if (!props) return NOT_FOUND;
|
|
36
|
+
|
|
37
|
+
// Direct OutletProvider with loaderData
|
|
38
|
+
if (props.loaderData && loaderId in props.loaderData) {
|
|
39
|
+
return props.loaderData[loaderId];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// LoaderBoundary: loaderIds + loaderDataPromise (already resolved array).
|
|
43
|
+
// When the segment has loading(), loaderData is resolved inside
|
|
44
|
+
// LoaderBoundary via use(). If the promise was pre-awaited (forceAwait
|
|
45
|
+
// or isAction), the prop is a raw array we can index into.
|
|
46
|
+
if (
|
|
47
|
+
props.loaderIds &&
|
|
48
|
+
Array.isArray(props.loaderIds) &&
|
|
49
|
+
props.loaderDataPromise &&
|
|
50
|
+
!(props.loaderDataPromise instanceof Promise)
|
|
51
|
+
) {
|
|
52
|
+
const idx = (props.loaderIds as string[]).indexOf(loaderId);
|
|
53
|
+
if (idx !== -1) {
|
|
54
|
+
const data = (props.loaderDataPromise as any[])[idx];
|
|
55
|
+
// loaderDataPromise entries may be { ok, data } result objects
|
|
56
|
+
if (data && typeof data === "object" && "ok" in data) {
|
|
57
|
+
return data.ok ? data.data : NOT_FOUND;
|
|
58
|
+
}
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Traverse into wrapper elements (MountContextProvider, ViewTransition,
|
|
64
|
+
// Suspense wrappers, etc.)
|
|
65
|
+
if (props.children) return extractContentLoaderData(props.children, loaderId);
|
|
66
|
+
return NOT_FOUND;
|
|
67
|
+
}
|
|
68
|
+
|
|
7
69
|
/**
|
|
8
70
|
* Payload returned by loader RSC requests
|
|
9
71
|
*/
|
|
@@ -71,19 +133,27 @@ function useLoaderInternal<T>(
|
|
|
71
133
|
const context = useContext(OutletContext);
|
|
72
134
|
|
|
73
135
|
// Get data from context (SSR/navigation)
|
|
74
|
-
const
|
|
136
|
+
const contextData = useMemo((): T | undefined => {
|
|
75
137
|
let current: OutletContextValue | null | undefined = context;
|
|
76
138
|
while (current) {
|
|
77
139
|
if (current.loaderData && loader.$$id in current.loaderData) {
|
|
78
140
|
return current.loaderData[loader.$$id] as T;
|
|
79
141
|
}
|
|
142
|
+
// Check content element — the route's OutletProvider is rendered as
|
|
143
|
+
// <Outlet /> content (a child), so its loaderData isn't in the parent
|
|
144
|
+
// chain. Parallel slots need to reach into it to find route-level loaders.
|
|
145
|
+
const contentData = extractContentLoaderData(
|
|
146
|
+
current.content,
|
|
147
|
+
loader.$$id,
|
|
148
|
+
);
|
|
149
|
+
if (contentData !== NOT_FOUND) {
|
|
150
|
+
return contentData as T;
|
|
151
|
+
}
|
|
80
152
|
current = current.parent;
|
|
81
153
|
}
|
|
82
154
|
return undefined;
|
|
83
155
|
}, [context, loader.$$id]);
|
|
84
156
|
|
|
85
|
-
const contextData = getContextData();
|
|
86
|
-
|
|
87
157
|
// Local state for fetched data (from load() calls)
|
|
88
158
|
const [fetchedData, setFetchedData] = useState<T | undefined>(undefined);
|
|
89
159
|
const [isLoading, setIsLoading] = useState(false);
|
|
@@ -215,7 +285,9 @@ function useLoaderInternal<T>(
|
|
|
215
285
|
|
|
216
286
|
const result = payload.loaderResult;
|
|
217
287
|
if (requestId === requestIdRef.current) {
|
|
218
|
-
|
|
288
|
+
startTransition(() => {
|
|
289
|
+
setFetchedData(result);
|
|
290
|
+
});
|
|
219
291
|
}
|
|
220
292
|
return result;
|
|
221
293
|
} catch (e) {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Debug logging for the Rango Vite plugin.
|
|
3
|
+
*
|
|
4
|
+
* Thin wrapper over the `debug` package (the same one Vite uses for its
|
|
5
|
+
* own `vite:*` namespaces). Enable via `DEBUG=rango:*` or `vite --debug rango`.
|
|
6
|
+
*
|
|
7
|
+
* Returns `undefined` when the namespace is not enabled, so call sites can
|
|
8
|
+
* guard expensive diagnostics with a simple truthiness check:
|
|
9
|
+
*
|
|
10
|
+
* const debug = createRangoDebugger("rango:routes");
|
|
11
|
+
* if (debug) debug("built manifest (%d routes) in %dms", n, ms);
|
|
12
|
+
*
|
|
13
|
+
* Back-compat: INTERNAL_RANGO_DEBUG=1 still enables all rango namespaces
|
|
14
|
+
* (sets DEBUG=rango:* for the current process if nothing is already set).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import debugFactory from "debug";
|
|
18
|
+
|
|
19
|
+
// Back-compat: the legacy INTERNAL_RANGO_DEBUG env var enabled per-site
|
|
20
|
+
// console.logs in this plugin. Map it to `rango:*` so those call sites can
|
|
21
|
+
// be migrated to the `debug` pipeline without breaking existing setups.
|
|
22
|
+
// Uses debug.enable() rather than mutating process.env because the `debug`
|
|
23
|
+
// package already snapshotted DEBUG when it was imported above.
|
|
24
|
+
if (process.env.INTERNAL_RANGO_DEBUG) {
|
|
25
|
+
const existing = debugFactory.disable();
|
|
26
|
+
debugFactory.enable(existing ? `${existing},rango:*` : "rango:*");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Debugger = (formatter: string, ...args: unknown[]) => void;
|
|
30
|
+
|
|
31
|
+
export function createRangoDebugger(namespace: string): Debugger | undefined {
|
|
32
|
+
const instance = debugFactory(namespace);
|
|
33
|
+
return instance.enabled ? (instance as unknown as Debugger) : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Measure a sync or async block and log its duration via `debug`. No-ops
|
|
38
|
+
* (still runs `fn`) when the namespace is disabled, so production cost is a
|
|
39
|
+
* single `.enabled` check per call.
|
|
40
|
+
*
|
|
41
|
+
* await timed(debug, "discover routers", () => discoverRouters(state));
|
|
42
|
+
*/
|
|
43
|
+
export async function timed<T>(
|
|
44
|
+
debug: Debugger | undefined,
|
|
45
|
+
label: string,
|
|
46
|
+
fn: () => T | Promise<T>,
|
|
47
|
+
): Promise<T> {
|
|
48
|
+
if (!debug) return await fn();
|
|
49
|
+
const start = performance.now();
|
|
50
|
+
try {
|
|
51
|
+
return await fn();
|
|
52
|
+
} finally {
|
|
53
|
+
debug("%s (%sms)", label, (performance.now() - start).toFixed(1));
|
|
54
|
+
}
|
|
55
|
+
}
|