@rangojs/router 0.0.0-experimental.20 → 0.0.0-experimental.204030a9
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 +242 -55
- package/dist/bin/rango.js +277 -99
- package/dist/vite/index.js +2929 -1132
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +68 -21
- package/skills/breadcrumbs/SKILL.md +252 -0
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +243 -21
- package/skills/caching/SKILL.md +159 -10
- package/skills/composability/SKILL.md +27 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +262 -51
- package/skills/host-router/SKILL.md +243 -0
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +46 -4
- package/skills/layout/SKILL.md +28 -7
- package/skills/links/SKILL.md +249 -17
- package/skills/loader/SKILL.md +291 -31
- package/skills/middleware/SKILL.md +49 -12
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +197 -6
- package/skills/prerender/SKILL.md +125 -102
- package/skills/rango/SKILL.md +242 -23
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +66 -9
- package/skills/route/SKILL.md +91 -8
- package/skills/router-setup/SKILL.md +98 -8
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/testing/SKILL.md +511 -188
- package/skills/typesafety/SKILL.md +354 -50
- package/skills/use-cache/SKILL.md +34 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +117 -0
- package/src/__internal.ts +92 -0
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +91 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/link-interceptor.ts +4 -0
- package/src/browser/navigation-bridge.ts +183 -18
- package/src/browser/navigation-client.ts +187 -57
- package/src/browser/navigation-store.ts +75 -17
- package/src/browser/navigation-transaction.ts +21 -37
- package/src/browser/partial-update.ts +143 -40
- 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 +110 -33
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +23 -64
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +43 -10
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +25 -0
- package/src/browser/rsc-router.tsx +200 -75
- package/src/browser/scroll-restoration.ts +46 -39
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +31 -36
- package/src/browser/types.ts +81 -5
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +65 -40
- package/src/build/generate-route-types.ts +5 -0
- package/src/build/index.ts +2 -0
- package/src/build/route-trie.ts +69 -26
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +9 -2
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +278 -88
- package/src/build/route-types/scan-filter.ts +9 -2
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +76 -49
- package/src/cache/cf/cf-cache-store.ts +501 -18
- 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 +5 -1
- package/src/client.tsx +95 -284
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -1
- package/src/handle.ts +65 -12
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/host/index.ts +2 -5
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -20
- package/src/index.rsc.ts +15 -40
- package/src/index.ts +92 -76
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +2 -5
- package/src/loader.ts +3 -10
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +57 -15
- package/src/prerender.ts +141 -80
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -15
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +435 -260
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +110 -34
- 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-definition/use-item-types.ts +32 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +37 -41
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +113 -1
- package/src/router/error-handling.ts +1 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +105 -39
- package/src/router/intercept-resolution.ts +15 -22
- package/src/router/lazy-includes.ts +12 -9
- package/src/router/loader-resolution.ts +175 -23
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +129 -193
- package/src/router/match-handlers.ts +63 -20
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +136 -106
- package/src/router/match-middleware/cache-store.ts +54 -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 +124 -18
- package/src/router/metrics.ts +239 -14
- package/src/router/middleware-types.ts +61 -31
- package/src/router/middleware.ts +226 -124
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +118 -19
- package/src/router/prerender-match.ts +114 -10
- package/src/router/preview-match.ts +32 -102
- package/src/router/request-classification.ts +286 -0
- package/src/router/revalidation.ts +85 -9
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +91 -29
- package/src/router/router-options.ts +89 -19
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +240 -23
- 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 +483 -289
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry.ts +99 -0
- package/src/router/trie-matching.ts +38 -15
- package/src/router/types.ts +9 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +120 -32
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +524 -370
- package/src/rsc/helpers.ts +91 -43
- package/src/rsc/index.ts +1 -21
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/origin-guard.ts +28 -10
- package/src/rsc/progressive-enhancement.ts +39 -10
- package/src/rsc/response-route-handler.ts +46 -53
- package/src/rsc/rsc-rendering.ts +69 -89
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +39 -47
- package/src/rsc/ssr-setup.ts +144 -0
- package/src/rsc/types.ts +19 -3
- package/src/search-params.ts +20 -17
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +219 -67
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +285 -63
- package/src/server/cookie-store.ts +28 -4
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +228 -65
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +9 -1
- package/src/static-handler.ts +19 -7
- package/src/testing/cache-status.ts +166 -0
- package/src/testing/collect-handle.ts +63 -0
- package/src/testing/dispatch.ts +440 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +154 -0
- package/src/testing/e2e/index.ts +149 -0
- package/src/testing/e2e/matchers.ts +51 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +306 -0
- package/src/testing/e2e/server.ts +183 -0
- package/src/testing/flight-matchers.ts +104 -0
- package/src/testing/flight-runtime.d.ts +21 -0
- package/src/testing/flight.entry.ts +22 -0
- package/src/testing/flight.ts +182 -0
- package/src/testing/generated-routes.ts +223 -0
- package/src/testing/index.ts +98 -0
- package/src/testing/internal/context.ts +151 -0
- package/src/testing/render-route.tsx +536 -0
- package/src/testing/run-loader.ts +296 -0
- package/src/testing/run-middleware.ts +170 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
- package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
- package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
- package/src/testing/vitest-stubs/version.ts +5 -0
- package/src/testing/vitest.ts +112 -0
- package/src/theme/index.ts +4 -13
- package/src/types/cache-types.ts +4 -4
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +197 -79
- package/src/types/index.ts +1 -0
- 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 +37 -6
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +0 -3
- package/src/urls/path-helper-types.ts +50 -9
- package/src/urls/path-helper.ts +63 -63
- package/src/urls/pattern-types.ts +48 -19
- package/src/urls/response-types.ts +25 -22
- package/src/urls/type-extraction.ts +26 -116
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +487 -44
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +63 -91
- package/src/vite/discovery/discover-routers.ts +106 -53
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +222 -107
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +50 -13
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +10 -3
- package/src/vite/plugin-types.ts +111 -72
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- 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 +55 -33
- package/src/vite/plugins/expose-id-utils.ts +24 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +12 -35
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +544 -317
- package/src/vite/plugins/performance-tracks.ts +92 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +72 -3
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +265 -226
- package/src/vite/router-discovery.ts +924 -137
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +21 -5
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +98 -5
- package/src/vite/utils/shared-utils.ts +109 -27
- package/src/browser/action-response-classifier.ts +0 -99
- package/src/route-definition/route-function.ts +0 -119
package/src/types/cache-types.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* during cache key generation (before middleware runs).
|
|
6
6
|
*
|
|
7
7
|
* Note: While the full RequestContext is passed, middleware-set variables
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* read via `ctx.get()` may not be populated yet since cache lookup happens
|
|
9
|
+
* before middleware execution.
|
|
10
10
|
*/
|
|
11
11
|
export type { RequestContext as CacheContext } from "../server/request-context.js";
|
|
12
12
|
|
|
@@ -101,7 +101,7 @@ export interface CacheOptions<TEnv = unknown> {
|
|
|
101
101
|
* Return false to skip cache for this request (always fetch fresh).
|
|
102
102
|
*
|
|
103
103
|
* Has access to full RequestContext including env, request, params, cookies, etc.
|
|
104
|
-
* Note: Middleware-set variables
|
|
104
|
+
* Note: Middleware-set variables read via `ctx.get()` may not be populated yet.
|
|
105
105
|
*
|
|
106
106
|
* @example
|
|
107
107
|
* ```typescript
|
|
@@ -123,7 +123,7 @@ export interface CacheOptions<TEnv = unknown> {
|
|
|
123
123
|
* Bypasses default key generation AND store's keyGenerator.
|
|
124
124
|
*
|
|
125
125
|
* Has access to full RequestContext including env, request, params, cookies, etc.
|
|
126
|
-
* Note: Middleware-set variables
|
|
126
|
+
* Note: Middleware-set variables read via `ctx.get()` may not be populated yet.
|
|
127
127
|
*
|
|
128
128
|
* @example
|
|
129
129
|
* ```typescript
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* ```typescript
|
|
8
8
|
* // In env.ts or env.d.ts
|
|
9
9
|
* declare global {
|
|
10
|
-
* namespace
|
|
10
|
+
* namespace Rango {
|
|
11
11
|
* interface Env extends AppBindings {}
|
|
12
12
|
* interface Vars extends AppVariables {}
|
|
13
13
|
* }
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
20
|
declare global {
|
|
21
|
-
namespace
|
|
21
|
+
namespace Rango {
|
|
22
22
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
23
23
|
interface Env {
|
|
24
24
|
// Empty by default - users augment with their bindings (e.g., { DB: D1Database })
|
|
@@ -44,13 +44,25 @@ declare global {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* Route map for path-validation surfaces (`href`, `ValidPaths`, `PathResponse`).
|
|
48
|
+
*
|
|
49
|
+
* Resolution order:
|
|
50
|
+
* 1. `RegisteredRoutes` — manual `extends typeof router.routeMap`; richest map,
|
|
51
|
+
* the only one carrying response-route payload metadata.
|
|
52
|
+
* 2. `GeneratedRouteMap` — auto-wired by `router.named-routes.gen.ts`; path +
|
|
53
|
+
* search only. Lets `rango generate` alone enable `href()`/`ValidPaths` path
|
|
54
|
+
* checking without a manual `RegisteredRoutes` declaration.
|
|
55
|
+
* 3. `Record<string, string>` — permissive fallback when nothing is registered.
|
|
56
|
+
*
|
|
57
|
+
* Referencing `GeneratedRouteMap` here is cycle-safe: it is declared in the
|
|
58
|
+
* standalone gen file with no imports, unlike `RegisteredRoutes extends typeof
|
|
59
|
+
* router.routeMap`.
|
|
50
60
|
*/
|
|
51
|
-
export type GetRegisteredRoutes = keyof
|
|
52
|
-
?
|
|
53
|
-
|
|
61
|
+
export type GetRegisteredRoutes = keyof Rango.RegisteredRoutes extends never
|
|
62
|
+
? keyof Rango.GeneratedRouteMap extends never
|
|
63
|
+
? Record<string, string>
|
|
64
|
+
: Rango.GeneratedRouteMap
|
|
65
|
+
: Rango.RegisteredRoutes;
|
|
54
66
|
|
|
55
67
|
/**
|
|
56
68
|
* Default route map for reverse() surfaces.
|
|
@@ -58,12 +70,11 @@ export type GetRegisteredRoutes = keyof RSCRouter.RegisteredRoutes extends never
|
|
|
58
70
|
* cycles, but falls back to RegisteredRoutes for manual augmentation and then to
|
|
59
71
|
* a permissive record when no route types are available.
|
|
60
72
|
*/
|
|
61
|
-
export type DefaultReverseRouteMap =
|
|
62
|
-
keyof
|
|
63
|
-
?
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
: RSCRouter.GeneratedRouteMap;
|
|
73
|
+
export type DefaultReverseRouteMap = keyof Rango.GeneratedRouteMap extends never
|
|
74
|
+
? keyof Rango.RegisteredRoutes extends never
|
|
75
|
+
? Record<string, string>
|
|
76
|
+
: Rango.RegisteredRoutes
|
|
77
|
+
: Rango.GeneratedRouteMap;
|
|
67
78
|
|
|
68
79
|
/**
|
|
69
80
|
* Default route map for Handler type.
|
|
@@ -71,30 +82,32 @@ export type DefaultReverseRouteMap =
|
|
|
71
82
|
* circular dependencies: router.tsx -> urls.tsx -> handler.tsx -> RegisteredRoutes -> router.tsx.
|
|
72
83
|
* GeneratedRouteMap is declared in a standalone gen file with no imports.
|
|
73
84
|
*/
|
|
74
|
-
export type DefaultHandlerRouteMap =
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
: RSCRouter.GeneratedRouteMap;
|
|
85
|
+
export type DefaultHandlerRouteMap = keyof Rango.GeneratedRouteMap extends never
|
|
86
|
+
? {}
|
|
87
|
+
: Rango.GeneratedRouteMap;
|
|
78
88
|
|
|
79
89
|
/**
|
|
80
|
-
* Default environment type - uses global augmentation if available
|
|
90
|
+
* Default environment type - uses global augmentation if available.
|
|
91
|
+
*
|
|
92
|
+
* Falls back to `unknown` (not `any`) when `Rango.Env` is not augmented, so
|
|
93
|
+
* an app that forgets to register its bindings gets a compile error on
|
|
94
|
+
* `ctx.env.SOMETHING` instead of silently losing all env type-checking. Augment
|
|
95
|
+
* `Rango.Env` to type `ctx.env`.
|
|
81
96
|
*/
|
|
82
|
-
export type DefaultEnv = keyof
|
|
83
|
-
? any
|
|
84
|
-
: RSCRouter.Env;
|
|
97
|
+
export type DefaultEnv = keyof Rango.Env extends never ? unknown : Rango.Env;
|
|
85
98
|
|
|
86
99
|
/**
|
|
87
100
|
* Default variables type - uses global augmentation if available, Record<string, any> otherwise
|
|
88
101
|
*/
|
|
89
|
-
export type DefaultVars = keyof
|
|
102
|
+
export type DefaultVars = keyof Rango.Vars extends never
|
|
90
103
|
? Record<string, any>
|
|
91
|
-
:
|
|
104
|
+
: Rango.Vars;
|
|
92
105
|
|
|
93
106
|
/**
|
|
94
107
|
* Default route name type for public `routeName` on contexts.
|
|
95
108
|
* When GeneratedRouteMap is augmented, narrows to the known route names.
|
|
96
109
|
* Otherwise falls back to `string` for untyped usage.
|
|
97
110
|
*/
|
|
98
|
-
export type DefaultRouteName = keyof
|
|
111
|
+
export type DefaultRouteName = keyof Rango.GeneratedRouteMap extends never
|
|
99
112
|
? string
|
|
100
|
-
: keyof
|
|
113
|
+
: keyof Rango.GeneratedRouteMap & string;
|
|
@@ -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";
|
|
@@ -41,7 +43,7 @@ export type { MiddlewareFn } from "../router/middleware.js";
|
|
|
41
43
|
*/
|
|
42
44
|
export type ScopedRouteMap<
|
|
43
45
|
TPrefix extends string,
|
|
44
|
-
TMap =
|
|
46
|
+
TMap = Rango.GeneratedRouteMap,
|
|
45
47
|
> = {
|
|
46
48
|
[K in keyof TMap as K extends `${TPrefix}.${infer Rest}`
|
|
47
49
|
? Rest
|
|
@@ -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
|
+
* 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).
|
|
214
217
|
*/
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Query parameters from the URL (system params like `_rsc*` are filtered).
|
|
218
|
-
* Always a standard URLSearchParams instance.
|
|
219
|
-
*/
|
|
220
|
-
searchParams: URLSearchParams;
|
|
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
|
-
|
|
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);
|
|
269
257
|
/**
|
|
270
|
-
*
|
|
271
|
-
* Headers set here are merged into the final response.
|
|
272
|
-
*
|
|
273
|
-
* @example
|
|
274
|
-
* ```typescript
|
|
275
|
-
* route("product", (ctx) => {
|
|
276
|
-
* ctx.res.headers.set("Cache-Control", "s-maxage=60");
|
|
277
|
-
* return <ProductPage />;
|
|
278
|
-
* });
|
|
279
|
-
* ```
|
|
280
|
-
*/
|
|
281
|
-
res: Response;
|
|
282
|
-
/**
|
|
283
|
-
* Shorthand for ctx.res.headers - response headers.
|
|
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. */
|
|
@@ -485,13 +471,16 @@ export type RevalidateParams<TParams = GenericParams, TEnv = any> = Parameters<
|
|
|
485
471
|
* **Return Types:**
|
|
486
472
|
* - `boolean` - Hard decision: immediately returns this value (short-circuits)
|
|
487
473
|
* - `{ defaultShouldRevalidate: boolean }` - Soft decision: updates suggestion for next revalidator
|
|
474
|
+
* - `void` / `null` / `undefined` - Defer to the current suggestion (no opinion); the
|
|
475
|
+
* loop continues to the next revalidator without changing the running default
|
|
488
476
|
*
|
|
489
477
|
* **Execution Flow:**
|
|
490
478
|
* 1. Start with built-in `defaultShouldRevalidate` (true if params changed)
|
|
491
479
|
* 2. Execute global revalidators first, then route-specific
|
|
492
480
|
* 3. Hard decision (boolean): stop immediately and use that value
|
|
493
481
|
* 4. Soft decision (object): update suggestion and continue to next revalidator
|
|
494
|
-
* 5.
|
|
482
|
+
* 5. Defer (`void` / `null` / `undefined`): leave suggestion unchanged and continue
|
|
483
|
+
* 6. If no hard decision was returned: use the final running suggestion
|
|
495
484
|
*
|
|
496
485
|
* @param args.currentParams - Previous route params (generic by default, can be narrowed)
|
|
497
486
|
* @param args.currentUrl - Previous URL
|
|
@@ -503,7 +492,8 @@ export type RevalidateParams<TParams = GenericParams, TEnv = any> = Parameters<
|
|
|
503
492
|
* @param args.formData - Form data from action (future support)
|
|
504
493
|
* @param args.formMethod - HTTP method from action (future support)
|
|
505
494
|
*
|
|
506
|
-
* @returns Hard decision (boolean)
|
|
495
|
+
* @returns Hard decision (boolean), soft suggestion (object), or defer
|
|
496
|
+
* (`void` / `null` / `undefined`) to keep the running suggestion as-is.
|
|
507
497
|
*
|
|
508
498
|
* @example
|
|
509
499
|
* ```typescript
|
|
@@ -523,31 +513,159 @@ export type RevalidateParams<TParams = GenericParams, TEnv = any> = Parameters<
|
|
|
523
513
|
* })
|
|
524
514
|
* ```
|
|
525
515
|
*/
|
|
516
|
+
/**
|
|
517
|
+
* A reference to a server action, used by `isAction()` in a revalidate predicate.
|
|
518
|
+
*
|
|
519
|
+
* Either a directly imported action (`import { addToCart }`) or a namespace
|
|
520
|
+
* import of an action module (`import * as CartActions`). Matching resolves the
|
|
521
|
+
* action's build-injected id (`path#export`) — the same identity the router uses
|
|
522
|
+
* for `actionId` — so a renamed or moved action breaks at compile time instead
|
|
523
|
+
* of silently failing to match.
|
|
524
|
+
*/
|
|
525
|
+
export type ActionRef =
|
|
526
|
+
| ((...args: never[]) => unknown)
|
|
527
|
+
| Record<string, unknown>;
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Revalidation function called during client-side navigation to decide whether
|
|
531
|
+
* a segment (layout, route, parallel slot, or loader) should be re-rendered.
|
|
532
|
+
*
|
|
533
|
+
* Return `true` to re-render, `false` to skip (keep client's current version),
|
|
534
|
+
* `{ defaultShouldRevalidate: boolean }` to update the running suggestion for
|
|
535
|
+
* downstream revalidators, or nothing (`void` / `null` / `undefined`) to defer
|
|
536
|
+
* to the current suggestion without changing it.
|
|
537
|
+
*
|
|
538
|
+
* @example
|
|
539
|
+
* ```ts
|
|
540
|
+
* // Re-render when a cart action happened or the browser signals staleness;
|
|
541
|
+
* // defer otherwise (|| undefined) so the segment default still applies
|
|
542
|
+
* revalidate(({ actionId, stale }) =>
|
|
543
|
+
* actionId?.includes("cart") || stale || undefined
|
|
544
|
+
* )
|
|
545
|
+
*
|
|
546
|
+
* // Always re-render when params change (default behavior made explicit)
|
|
547
|
+
* revalidate(({ defaultShouldRevalidate }) => defaultShouldRevalidate)
|
|
548
|
+
* ```
|
|
549
|
+
*/
|
|
526
550
|
export type ShouldRevalidateFn<TParams = GenericParams, TEnv = any> = (args: {
|
|
551
|
+
/** Route params from the page being navigated away from. */
|
|
527
552
|
currentParams: TParams;
|
|
553
|
+
/** Full URL of the page being navigated away from. */
|
|
528
554
|
currentUrl: URL;
|
|
555
|
+
/** Route params for the navigation target. */
|
|
529
556
|
nextParams: TParams;
|
|
557
|
+
/** Full URL of the navigation target. */
|
|
530
558
|
nextUrl: URL;
|
|
559
|
+
/**
|
|
560
|
+
* The router's default revalidation decision for this segment.
|
|
561
|
+
* `true` when params changed or the segment is new to the client.
|
|
562
|
+
* Return this when you want default behavior plus your own conditions.
|
|
563
|
+
*/
|
|
531
564
|
defaultShouldRevalidate: boolean;
|
|
565
|
+
/** Full handler context — access to `ctx.use()`, `ctx.env`, `ctx.params`, etc. */
|
|
532
566
|
context: HandlerContext<TParams, TEnv>;
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
567
|
+
|
|
568
|
+
// ── Segment metadata (which segment is being evaluated) ──────────────
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* The type of segment being revalidated. `"loader"` is passed to revalidate
|
|
572
|
+
* functions attached to a `loader(Fn, () => [revalidate(...)])` registration.
|
|
573
|
+
*/
|
|
574
|
+
segmentType: "layout" | "route" | "parallel" | "loader";
|
|
575
|
+
/** Layout name (e.g., `"root"`, `"shop"`, `"auth"`). Only set for layout segments. */
|
|
576
|
+
layoutName?: string;
|
|
577
|
+
/** Slot name (e.g., `"@sidebar"`, `"@modal"`). Only set for parallel segments. */
|
|
578
|
+
slotName?: string;
|
|
579
|
+
|
|
580
|
+
// ── Action context (populated when revalidation is triggered by a server action) ──
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Identifier of the server action that triggered revalidation.
|
|
584
|
+
* `undefined` during normal navigation (no action involved).
|
|
585
|
+
*
|
|
586
|
+
* Format: `"src/<path>#<exportName>"` — the file path is the source path
|
|
587
|
+
* relative to the project root, followed by `#` and the exported function name.
|
|
588
|
+
*
|
|
589
|
+
* This is stable and can be used for path-based matching to revalidate
|
|
590
|
+
* when any action in a module or directory fires. Prefer `|| undefined`
|
|
591
|
+
* (defer to the segment default / downstream revalidators) over `?? false`
|
|
592
|
+
* (hard short-circuit that suppresses the default and ends the chain):
|
|
593
|
+
*
|
|
594
|
+
* @example
|
|
595
|
+
* ```ts
|
|
596
|
+
* // Match a specific action
|
|
597
|
+
* revalidate(({ actionId }) => actionId === "src/actions/cart.ts#addToCart" || undefined)
|
|
598
|
+
*
|
|
599
|
+
* // Match any action in the cart module
|
|
600
|
+
* revalidate(({ actionId }) => actionId?.includes("cart") || undefined)
|
|
601
|
+
*
|
|
602
|
+
* // Match any action under src/apps/store/actions/
|
|
603
|
+
* revalidate(({ actionId }) => actionId?.startsWith("src/apps/store/actions/") || undefined)
|
|
604
|
+
* ```
|
|
605
|
+
*/
|
|
606
|
+
actionId?: string;
|
|
607
|
+
/**
|
|
608
|
+
* Typed, rename-safe action matching. Returns `true` when the action that
|
|
609
|
+
* triggered this revalidation is one of the given references — or, for a
|
|
610
|
+
* namespace import (`import * as CartActions`), any export of that module —
|
|
611
|
+
* and `false` otherwise (including plain navigation with no action).
|
|
612
|
+
*
|
|
613
|
+
* Prefer this over hand-written `actionId` substring matches: it resolves the
|
|
614
|
+
* action's stable `path#export` id from the imported reference, so a rename is
|
|
615
|
+
* a type error in one place instead of silent drift across consumers. It
|
|
616
|
+
* resolves the reference the same way the action boundary derives `actionId`
|
|
617
|
+
* (`$id ?? $$id`), so it matches in both dev and production.
|
|
618
|
+
*
|
|
619
|
+
* Returns a raw boolean, so for the common "revalidate on match, else defer"
|
|
620
|
+
* intent combine with `|| undefined`:
|
|
621
|
+
*
|
|
622
|
+
* @example
|
|
623
|
+
* ```ts
|
|
624
|
+
* import { addToCart, removeFromCart } from "./actions/cart";
|
|
625
|
+
* import * as CartActions from "./actions/cart";
|
|
626
|
+
*
|
|
627
|
+
* revalidate((ctx) => ctx.isAction(addToCart) || undefined); // one action
|
|
628
|
+
* revalidate((ctx) => ctx.isAction(addToCart, removeFromCart) || undefined); // several
|
|
629
|
+
* revalidate((ctx) => ctx.isAction(CartActions) || undefined); // any in the module
|
|
630
|
+
* ```
|
|
631
|
+
*/
|
|
632
|
+
isAction: (...actions: ActionRef[]) => boolean;
|
|
633
|
+
/** URL where the action was executed (the page the user was on when they triggered the action). */
|
|
634
|
+
actionUrl?: URL;
|
|
635
|
+
/** Return value from the action execution. Can be used to conditionally revalidate based on the action's outcome. */
|
|
636
|
+
actionResult?: any;
|
|
637
|
+
/** FormData from the action request body. Only set for form-based actions (not inline `"use server"` actions). */
|
|
638
|
+
formData?: FormData;
|
|
639
|
+
/** HTTP method: `"GET"` for navigation, `"POST"` for server actions. */
|
|
640
|
+
method?: string;
|
|
641
|
+
|
|
642
|
+
// ── Route identity ───────────────────────────────────────────────────
|
|
643
|
+
|
|
644
|
+
/** Route name of the navigation target. Alias for `toRouteName`. */
|
|
645
|
+
routeName?: DefaultRouteName;
|
|
646
|
+
/**
|
|
647
|
+
* Route name being navigated away from.
|
|
648
|
+
* `undefined` for unnamed internal routes (those without a `name` option).
|
|
649
|
+
*/
|
|
650
|
+
fromRouteName?: DefaultRouteName;
|
|
651
|
+
/**
|
|
652
|
+
* Route name being navigated to.
|
|
653
|
+
* `undefined` for unnamed internal routes (those without a `name` option).
|
|
654
|
+
*/
|
|
655
|
+
toRouteName?: DefaultRouteName;
|
|
656
|
+
|
|
657
|
+
// ── Staleness signal ─────────────────────────────────────────────────
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* `true` when the browser signals that data may be stale — typically because
|
|
661
|
+
* a server action was executed in this or another tab (`_rsc_stale` header).
|
|
662
|
+
*
|
|
663
|
+
* This is NOT segment cache staleness (loaders are never segment-cached).
|
|
664
|
+
* Use this to decide whether loader data should be re-fetched after an
|
|
665
|
+
* action that may have mutated backend state.
|
|
666
|
+
*/
|
|
667
|
+
stale?: boolean;
|
|
668
|
+
}) => boolean | { defaultShouldRevalidate: boolean } | null | void;
|
|
551
669
|
|
|
552
670
|
// MiddlewareFn is imported from "../router/middleware.js" and re-exported
|
|
553
671
|
|
|
@@ -652,7 +770,7 @@ export type Revalidate<
|
|
|
652
770
|
* Middleware function with typed params and environment
|
|
653
771
|
*
|
|
654
772
|
* @template TParams - Params object (defaults to generic)
|
|
655
|
-
* @template TEnv - Environment type (defaults to global
|
|
773
|
+
* @template TEnv - Environment type (defaults to global Rango.Env)
|
|
656
774
|
*
|
|
657
775
|
* Note: Middleware cannot directly use route names for params typing because
|
|
658
776
|
* middleware is defined during router setup, before RegisteredRoutes is populated.
|
|
@@ -660,7 +778,7 @@ export type Revalidate<
|
|
|
660
778
|
*
|
|
661
779
|
* @example
|
|
662
780
|
* ```typescript
|
|
663
|
-
* // Basic middleware (uses global
|
|
781
|
+
* // Basic middleware (uses global Rango.Env via module augmentation)
|
|
664
782
|
* const middleware: Middleware = async (ctx, next) => {
|
|
665
783
|
* ctx.set("user", { id: "123" }); // Type-safe!
|
|
666
784
|
* await next();
|
package/src/types/index.ts
CHANGED
|
@@ -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
|
};
|