@rangojs/router 0.0.0-experimental.19 → 0.0.0-experimental.1c0bdfad
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 +17 -0
- package/README.md +291 -61
- package/dist/bin/rango.js +544 -143
- package/dist/testing/vitest.js +82 -0
- package/dist/vite/index.js +3744 -1329
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +67 -13
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +312 -0
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +247 -23
- package/skills/caching/SKILL.md +322 -19
- package/skills/composability/SKILL.md +27 -2
- package/skills/css/SKILL.md +76 -0
- package/skills/debug-manifest/SKILL.md +4 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +282 -60
- package/skills/host-router/SKILL.md +278 -0
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +50 -6
- package/skills/layout/SKILL.md +35 -9
- package/skills/links/SKILL.md +249 -17
- package/skills/loader/SKILL.md +297 -31
- package/skills/middleware/SKILL.md +52 -13
- package/skills/migrate-nextjs/SKILL.md +584 -0
- package/skills/migrate-react-router/SKILL.md +771 -0
- package/skills/mime-routes/SKILL.md +28 -1
- package/skills/observability/SKILL.md +172 -0
- package/skills/parallel/SKILL.md +203 -7
- package/skills/prerender/SKILL.md +155 -111
- package/skills/rango/SKILL.md +251 -23
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +123 -48
- package/skills/route/SKILL.md +104 -9
- package/skills/router-setup/SKILL.md +124 -11
- package/skills/scripts/SKILL.md +179 -0
- package/skills/server-actions/SKILL.md +775 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/tailwind/SKILL.md +27 -3
- package/skills/testing/SKILL.md +125 -222
- package/skills/testing/bindings.md +103 -0
- package/skills/testing/cache-prerender.md +127 -0
- package/skills/testing/client-components.md +124 -0
- package/skills/testing/e2e-parity.md +125 -0
- package/skills/testing/flight.md +91 -0
- package/skills/testing/handles.md +129 -0
- package/skills/testing/loader.md +128 -0
- package/skills/testing/middleware.md +99 -0
- package/skills/testing/render-handler.md +121 -0
- package/skills/testing/response-routes.md +95 -0
- package/skills/testing/reverse-and-types.md +84 -0
- package/skills/testing/server-actions.md +107 -0
- package/skills/testing/server-tree.md +128 -0
- package/skills/testing/setup.md +123 -0
- package/skills/typesafety/SKILL.md +357 -52
- package/skills/use-cache/SKILL.md +46 -14
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/__internal.ts +67 -40
- package/src/bin/rango.ts +18 -0
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/action-fence.ts +47 -0
- package/src/browser/app-shell.ts +39 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/connection-warmup.ts +134 -0
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/event-controller.ts +197 -150
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/invalidate-client-cache.ts +52 -0
- package/src/browser/link-interceptor.ts +4 -0
- package/src/browser/navigation-bridge.ts +200 -30
- package/src/browser/navigation-client.ts +217 -58
- package/src/browser/navigation-store-handle.ts +38 -0
- package/src/browser/navigation-store.ts +76 -67
- package/src/browser/navigation-transaction.ts +18 -66
- package/src/browser/network-error-handler.ts +34 -7
- package/src/browser/partial-update.ts +187 -112
- package/src/browser/prefetch/cache.ts +312 -30
- package/src/browser/prefetch/fetch.ts +344 -47
- package/src/browser/prefetch/policy.ts +6 -0
- package/src/browser/prefetch/queue.ts +126 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +158 -76
- package/src/browser/react/Link.tsx +125 -18
- package/src/browser/react/NavigationProvider.tsx +135 -120
- package/src/browser/react/ScrollRestoration.tsx +10 -6
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/filter-segment-order.ts +66 -7
- package/src/browser/react/index.ts +0 -48
- package/src/browser/react/location-state-shared.ts +178 -8
- package/src/browser/react/location-state.ts +39 -14
- package/src/browser/react/use-action.ts +6 -15
- package/src/browser/react/use-handle.ts +23 -69
- package/src/browser/react/use-href.tsx +8 -1
- package/src/browser/react/use-link-status.ts +33 -8
- package/src/browser/react/use-navigation.ts +32 -7
- package/src/browser/react/use-params.ts +20 -10
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +46 -11
- package/src/browser/react/use-search-params.ts +0 -5
- package/src/browser/react/use-segments.ts +11 -21
- package/src/browser/response-adapter.ts +80 -5
- package/src/browser/rsc-router.tsx +226 -75
- package/src/browser/scroll-restoration.ts +54 -42
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +619 -442
- package/src/browser/types.ts +115 -11
- package/src/browser/validate-redirect-origin.ts +43 -16
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +65 -40
- package/src/build/generate-route-types.ts +7 -1
- package/src/build/index.ts +8 -2
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +182 -37
- package/src/build/route-types/ast-route-extraction.ts +15 -8
- package/src/build/route-types/codegen.ts +16 -5
- package/src/build/route-types/include-resolution.ts +125 -24
- package/src/build/route-types/param-extraction.ts +6 -3
- package/src/build/route-types/per-module-writer.ts +22 -6
- package/src/build/route-types/router-processing.ts +392 -106
- package/src/build/route-types/scan-filter.ts +9 -2
- package/src/build/route-types/source-scan.ts +216 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-key-utils.ts +29 -13
- package/src/cache/cache-policy.ts +108 -34
- package/src/cache/cache-runtime.ts +214 -48
- package/src/cache/cache-scope.ts +236 -89
- package/src/cache/cache-tag.ts +103 -0
- package/src/cache/cf/cf-base64.ts +33 -0
- package/src/cache/cf/cf-cache-constants.ts +127 -0
- package/src/cache/cf/cf-cache-store.ts +2224 -171
- package/src/cache/cf/cf-cache-types.ts +349 -0
- package/src/cache/cf/cf-kv-utils.ts +46 -0
- package/src/cache/cf/cf-tag-marker-memo.ts +105 -0
- package/src/cache/cf/index.ts +11 -17
- package/src/cache/document-cache.ts +89 -27
- package/src/cache/handle-snapshot.ts +70 -0
- package/src/cache/index.ts +11 -20
- package/src/cache/memory-segment-store.ts +136 -37
- package/src/cache/profile-registry.ts +31 -31
- package/src/cache/read-through-swr.ts +41 -11
- package/src/cache/segment-codec.ts +9 -17
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/taint.ts +55 -0
- package/src/cache/types.ts +37 -100
- package/src/client.rsc.tsx +45 -21
- package/src/client.tsx +120 -336
- package/src/cloudflare/index.ts +11 -0
- package/src/cloudflare/tracing.ts +109 -0
- package/src/component-utils.ts +19 -0
- package/src/components/DefaultDocument.tsx +8 -2
- package/src/context-var.ts +84 -2
- package/src/debug.ts +2 -2
- package/src/decode-loader-results.ts +52 -0
- package/src/defer.ts +196 -0
- package/src/deps/ssr.ts +0 -1
- package/src/encode-kv.ts +49 -0
- package/src/errors.ts +30 -4
- package/src/escape-script.ts +52 -0
- package/src/handle.ts +70 -22
- package/src/handles/MetaTags.tsx +56 -19
- package/src/handles/Scripts.tsx +183 -0
- package/src/handles/breadcrumbs.ts +95 -0
- package/src/handles/is-thenable.ts +19 -0
- package/src/handles/meta.ts +51 -40
- package/src/handles/script.ts +244 -0
- package/src/host/cookie-handler.ts +9 -60
- package/src/host/errors.ts +0 -24
- package/src/host/index.ts +8 -5
- package/src/host/pattern-matcher.ts +23 -52
- package/src/host/router.ts +107 -99
- package/src/host/testing.ts +40 -27
- package/src/host/types.ts +37 -4
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +137 -22
- package/src/index.rsc.ts +79 -29
- package/src/index.ts +149 -65
- package/src/internal-debug.ts +11 -10
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +20 -13
- package/src/loader.ts +12 -11
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/outlet-provider.tsx +1 -5
- package/src/prerender/param-hash.ts +16 -16
- package/src/prerender/store.ts +63 -26
- package/src/prerender.ts +198 -82
- package/src/redirect-origin.ts +100 -0
- package/src/regex-escape.ts +8 -0
- package/src/render-error-thrower.tsx +20 -0
- package/src/response-utils.ts +62 -0
- package/src/reverse.ts +65 -15
- package/src/root-error-boundary.tsx +1 -19
- package/src/route-content-wrapper.tsx +7 -72
- package/src/route-definition/dsl-helpers.ts +469 -276
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +113 -37
- package/src/route-definition/index.ts +3 -3
- package/src/route-definition/redirect.ts +53 -12
- package/src/route-definition/resolve-handler-use.ts +161 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-map-builder.ts +7 -17
- package/src/route-types.ts +37 -41
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +164 -17
- package/src/router/error-handling.ts +45 -18
- package/src/router/find-match.ts +45 -22
- package/src/router/handler-context.ts +110 -39
- package/src/router/instrument.ts +350 -0
- package/src/router/intercept-resolution.ts +50 -24
- package/src/router/lazy-includes.ts +19 -53
- package/src/router/loader-resolution.ts +274 -56
- package/src/router/logging.ts +5 -8
- package/src/router/manifest.ts +49 -45
- package/src/router/match-api.ts +121 -205
- package/src/router/match-context.ts +0 -22
- package/src/router/match-handlers.ts +58 -58
- package/src/router/match-middleware/background-revalidation.ts +33 -6
- package/src/router/match-middleware/cache-lookup.ts +214 -263
- package/src/router/match-middleware/cache-store.ts +73 -33
- package/src/router/match-middleware/intercept-resolution.ts +8 -28
- package/src/router/match-middleware/segment-resolution.ts +52 -18
- package/src/router/match-pipelines.ts +1 -42
- package/src/router/match-result.ts +104 -49
- package/src/router/metrics.ts +217 -26
- package/src/router/middleware-types.ts +24 -110
- package/src/router/middleware.ts +384 -197
- package/src/router/navigation-snapshot.ts +131 -0
- package/src/router/params-util.ts +23 -0
- package/src/router/pattern-matching.ts +148 -91
- package/src/router/prefetch-cache-ttl.ts +51 -0
- package/src/router/prerender-match.ts +199 -56
- package/src/router/preview-match.ts +32 -102
- package/src/router/request-classification.ts +276 -0
- package/src/router/revalidation.ts +144 -74
- package/src/router/route-snapshot.ts +244 -0
- package/src/router/router-context.ts +8 -28
- package/src/router/router-interfaces.ts +129 -36
- package/src/router/router-options.ts +185 -23
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +281 -76
- package/src/router/segment-resolution/helpers.ts +116 -31
- package/src/router/segment-resolution/loader-cache.ts +63 -37
- package/src/router/segment-resolution/revalidation.ts +493 -391
- package/src/router/segment-resolution/static-store.ts +19 -5
- package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/segment-resolution.ts +5 -1
- package/src/router/segment-wrappers.ts +8 -5
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry-otel.ts +161 -199
- package/src/router/telemetry.ts +96 -19
- package/src/router/timeout.ts +0 -20
- package/src/router/tracing.ts +206 -0
- package/src/router/trie-matching.ts +180 -58
- package/src/router/types.ts +10 -63
- package/src/router/url-params.ts +44 -0
- package/src/router.ts +182 -54
- package/src/rsc/handler-context.ts +3 -2
- package/src/rsc/handler.ts +702 -460
- package/src/rsc/helpers.ts +168 -46
- package/src/rsc/index.ts +2 -25
- package/src/rsc/json-route-result.ts +38 -0
- package/src/rsc/loader-fetch.ts +127 -31
- package/src/rsc/manifest-init.ts +33 -42
- package/src/rsc/origin-guard.ts +39 -25
- package/src/rsc/progressive-enhancement.ts +98 -19
- package/src/rsc/redirect-guard.ts +99 -0
- package/src/rsc/response-cache-serve.ts +238 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +99 -189
- package/src/rsc/rsc-rendering.ts +126 -106
- package/src/rsc/runtime-warnings.ts +23 -10
- package/src/rsc/server-action.ts +269 -114
- package/src/rsc/ssr-setup.ts +144 -0
- package/src/rsc/types.ts +34 -6
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +49 -41
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +149 -0
- package/src/segment-system.tsx +281 -129
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +317 -63
- package/src/server/cookie-parse.ts +32 -0
- package/src/server/cookie-store.ts +80 -5
- package/src/server/handle-store.ts +40 -38
- package/src/server/loader-registry.ts +26 -46
- package/src/server/request-context.ts +425 -177
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +25 -16
- package/src/static-handler.ts +27 -18
- package/src/testing/cache-status.ts +162 -0
- package/src/testing/collect-handle.ts +40 -0
- package/src/testing/dispatch.ts +701 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +188 -0
- package/src/testing/e2e/index.ts +128 -0
- package/src/testing/e2e/matchers.ts +35 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +387 -0
- package/src/testing/e2e/server.ts +195 -0
- package/src/testing/flight-matchers.ts +97 -0
- package/src/testing/flight-normalize.ts +11 -0
- package/src/testing/flight-runtime.d.ts +57 -0
- package/src/testing/flight-tree.ts +682 -0
- package/src/testing/flight.entry.ts +52 -0
- package/src/testing/flight.ts +257 -0
- package/src/testing/generated-routes.ts +183 -0
- package/src/testing/index.ts +99 -0
- package/src/testing/internal/context.ts +371 -0
- package/src/testing/internal/flight-client-globals.ts +30 -0
- package/src/testing/internal/seed-vars.ts +54 -0
- package/src/testing/render-handler.ts +343 -0
- package/src/testing/render-route.tsx +581 -0
- package/src/testing/run-loader.ts +385 -0
- package/src/testing/run-middleware.ts +205 -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 +305 -0
- package/src/theme/ThemeProvider.tsx +20 -58
- package/src/theme/ThemeScript.tsx +7 -9
- package/src/theme/constants.ts +52 -13
- package/src/theme/index.ts +3 -19
- package/src/theme/theme-context.ts +1 -5
- package/src/theme/theme-script.ts +22 -21
- package/src/theme/use-theme.ts +0 -3
- package/src/types/boundaries.ts +0 -35
- package/src/types/cache-types.ts +17 -8
- package/src/types/error-types.ts +30 -90
- package/src/types/global-namespace.ts +54 -41
- package/src/types/handler-context.ts +236 -88
- package/src/types/index.ts +1 -10
- package/src/types/loader-types.ts +44 -15
- package/src/types/request-scope.ts +112 -0
- package/src/types/route-config.ts +10 -45
- package/src/types/route-entry.ts +19 -7
- package/src/types/segments.ts +37 -19
- package/src/urls/include-helper.ts +33 -70
- package/src/urls/index.ts +1 -11
- package/src/urls/path-helper-types.ts +58 -11
- package/src/urls/path-helper.ts +57 -111
- package/src/urls/pattern-types.ts +48 -19
- package/src/urls/response-types.ts +25 -22
- package/src/urls/type-extraction.ts +58 -139
- package/src/urls/urls-function.ts +1 -18
- package/src/use-loader.tsx +346 -89
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +64 -91
- package/src/vite/discovery/discover-routers.ts +147 -88
- 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 +247 -145
- 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 +61 -13
- package/src/vite/discovery/virtual-module-codegen.ts +14 -34
- package/src/vite/index.ts +10 -3
- package/src/vite/inject-client-debug.ts +36 -0
- package/src/vite/plugin-types.ts +155 -65
- package/src/vite/plugins/cjs-to-esm.ts +16 -19
- package/src/vite/plugins/client-ref-dedup.ts +120 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -15
- 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 +194 -0
- package/src/vite/plugins/expose-action-id.ts +49 -98
- package/src/vite/plugins/expose-id-utils.ts +96 -51
- package/src/vite/plugins/expose-ids/export-analysis.ts +101 -34
- package/src/vite/plugins/expose-ids/handler-transform.ts +15 -64
- package/src/vite/plugins/expose-ids/loader-transform.ts +14 -24
- package/src/vite/plugins/expose-ids/router-transform.ts +118 -29
- package/src/vite/plugins/expose-internal-ids.ts +553 -317
- package/src/vite/plugins/performance-tracks.ts +89 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/use-cache-transform.ts +73 -83
- package/src/vite/plugins/version-injector.ts +21 -25
- package/src/vite/plugins/version-plugin.ts +46 -37
- package/src/vite/plugins/virtual-entries.ts +13 -18
- package/src/vite/rango.ts +241 -287
- package/src/vite/router-discovery.ts +956 -149
- package/src/vite/utils/ast-handler-extract.ts +26 -35
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/bundle-analysis.ts +10 -15
- package/src/vite/utils/client-chunks.ts +184 -0
- package/src/vite/utils/directive-prologue.ts +40 -0
- package/src/vite/utils/forward-user-plugins.ts +171 -0
- package/src/vite/utils/manifest-utils.ts +4 -59
- package/src/vite/utils/package-resolution.ts +20 -52
- package/src/vite/utils/prerender-utils.ts +141 -34
- package/src/vite/utils/shared-utils.ts +92 -42
- package/CLAUDE.md +0 -5
- package/src/browser/action-response-classifier.ts +0 -99
- package/src/browser/react/use-client-cache.ts +0 -58
- package/src/browser/shallow.ts +0 -40
- package/src/handles/index.ts +0 -6
- package/src/network-error-thrower.tsx +0 -23
- package/src/route-definition/route-function.ts +0 -119
- package/src/router/middleware-cookies.ts +0 -55
|
@@ -32,12 +32,34 @@ is only needed when you want the richer `typeof router.routeMap` shape
|
|
|
32
32
|
available globally.
|
|
33
33
|
|
|
34
34
|
- `GeneratedRouteMap` — auto-registered by `router.named-routes.gen.ts`
|
|
35
|
-
Use for `Handler<"name"
|
|
36
|
-
|
|
35
|
+
Use for `Handler<"name">` (type annotation), `Prerender<"name">(...)` (function
|
|
36
|
+
call with type arg for param inference), server `ctx.reverse()`, and
|
|
37
|
+
named-route param/search inference.
|
|
37
38
|
- `typeof router.routeMap` — the real merged route map from your router
|
|
38
39
|
instance, including response-route metadata such as `{ path, response }`.
|
|
39
40
|
- `RegisteredRoutes` — manual global hook for exposing `typeof router.routeMap`
|
|
40
|
-
to utilities
|
|
41
|
+
to global utilities that need the exact router-builder map, especially
|
|
42
|
+
`Rango.PathResponse`.
|
|
43
|
+
|
|
44
|
+
### Generated Route Type Surfaces
|
|
45
|
+
|
|
46
|
+
There are three distinct typing surfaces. They are **not** interchangeable —
|
|
47
|
+
pick the one that matches what you need to type:
|
|
48
|
+
|
|
49
|
+
| Surface | Source | Scope | Gives | Does not give |
|
|
50
|
+
| ------------------- | ---------------------------------------- | ------ | ---------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
51
|
+
| `GeneratedRouteMap` | `router.named-routes.gen.ts` (auto) | global | route names, path params, search schemas | response/MIME payloads |
|
|
52
|
+
| `routes` | per-module `*.gen.ts` (`rango generate`) | local | local names, params, search | the global app map |
|
|
53
|
+
| `RegisteredRoutes` | manual `extends typeof router.routeMap` | global | paths, params, **response payloads** | the `Handler`/`Prerender` default (those read `GeneratedRouteMap` to avoid a `router.tsx` cycle) |
|
|
54
|
+
|
|
55
|
+
Key consequence: `href()` and the ambient `Rango.Path` type are typed from
|
|
56
|
+
whichever map is present — they prefer `RegisteredRoutes` when you wire it, otherwise fall back to
|
|
57
|
+
the auto-generated `GeneratedRouteMap`, so **`rango generate` alone gives you
|
|
58
|
+
path-checked `href()`** with no manual augmentation. Response and MIME payload
|
|
59
|
+
inference is the exception: it comes only from `typeof router.routeMap` (via
|
|
60
|
+
`RegisteredRoutes`), because `GeneratedRouteMap` carries paths + search but no
|
|
61
|
+
payloads — so `Rango.PathResponse` resolves to `never` until you wire
|
|
62
|
+
`RegisteredRoutes`.
|
|
41
63
|
|
|
42
64
|
Recommended setup:
|
|
43
65
|
|
|
@@ -50,7 +72,7 @@ import type { AppBindings, AppVars } from "./env";
|
|
|
50
72
|
export const router = createRouter<AppBindings>({}).routes(urlpatterns);
|
|
51
73
|
|
|
52
74
|
declare global {
|
|
53
|
-
namespace
|
|
75
|
+
namespace Rango {
|
|
54
76
|
interface Env extends AppBindings {}
|
|
55
77
|
interface Vars extends AppVars {}
|
|
56
78
|
interface RegisteredRoutes extends typeof router.routeMap {}
|
|
@@ -58,6 +80,54 @@ declare global {
|
|
|
58
80
|
}
|
|
59
81
|
```
|
|
60
82
|
|
|
83
|
+
### Single-App Setup Checklist
|
|
84
|
+
|
|
85
|
+
For one app, keep the ambient types, generated named-routes file, and router
|
|
86
|
+
instance in the same TypeScript program:
|
|
87
|
+
|
|
88
|
+
```jsonc
|
|
89
|
+
// tsconfig.json
|
|
90
|
+
{
|
|
91
|
+
"compilerOptions": {
|
|
92
|
+
"strict": true,
|
|
93
|
+
"moduleResolution": "bundler",
|
|
94
|
+
"jsx": "react-jsx",
|
|
95
|
+
"noEmit": true,
|
|
96
|
+
},
|
|
97
|
+
"include": ["src"],
|
|
98
|
+
"files": ["src/router.tsx"],
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Then generate the route types from the router file:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npx rango generate src/router.tsx
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
This creates `src/router.named-routes.gen.ts`, which augments
|
|
109
|
+
`Rango.GeneratedRouteMap`. Keep that generated file committed with the router
|
|
110
|
+
source. The `files` entry keeps `router.tsx` in the program even when nothing
|
|
111
|
+
imports it directly, so `Rango.Env`, `Rango.Vars`, and optional
|
|
112
|
+
`Rango.RegisteredRoutes` augmentation are visible to handlers, loaders, actions,
|
|
113
|
+
and client helpers.
|
|
114
|
+
|
|
115
|
+
### Named Routes, `$$routeNames`, And `router.routeMap`
|
|
116
|
+
|
|
117
|
+
There are two runtime/type surfaces with similar names:
|
|
118
|
+
|
|
119
|
+
- `router.named-routes.gen.ts` exports `NamedRoutes` and augments
|
|
120
|
+
`Rango.GeneratedRouteMap`. The Vite plugin imports that file internally and
|
|
121
|
+
injects it as `$$routeNames` so `router.reverse` has the static route-name map.
|
|
122
|
+
App code should not pass or import `$$routeNames` directly.
|
|
123
|
+
- `router.routeMap` is the public router instance property for type extraction.
|
|
124
|
+
Use `typeof router.routeMap` when augmenting `Rango.RegisteredRoutes` for
|
|
125
|
+
global response payload helpers such as `Rango.PathResponse`.
|
|
126
|
+
|
|
127
|
+
Do not document or use a public `router.routeNames` API unless one is
|
|
128
|
+
intentionally added. Today, the public extraction surface is `router.routeMap`;
|
|
129
|
+
the generated file and `$$routeNames` are build machinery.
|
|
130
|
+
|
|
61
131
|
## Route Definition with Type-Safe Names
|
|
62
132
|
|
|
63
133
|
```typescript
|
|
@@ -127,17 +197,107 @@ function ShopNav() {
|
|
|
127
197
|
}
|
|
128
198
|
```
|
|
129
199
|
|
|
130
|
-
`href()` and
|
|
131
|
-
|
|
200
|
+
`href()` and the `Rango.Path` type read from `RegisteredRoutes` when you augment
|
|
201
|
+
it, otherwise from the auto-generated `GeneratedRouteMap` — so `rango generate`
|
|
202
|
+
alone type-checks `href()` paths with no manual augmentation. The augmentation
|
|
203
|
+
below is only needed for **`Rango.PathResponse`** (response-payload inference), which
|
|
204
|
+
`GeneratedRouteMap` cannot provide:
|
|
132
205
|
|
|
133
206
|
```typescript
|
|
134
207
|
declare global {
|
|
135
|
-
namespace
|
|
208
|
+
namespace Rango {
|
|
136
209
|
interface RegisteredRoutes extends typeof router.routeMap {}
|
|
137
210
|
}
|
|
138
211
|
}
|
|
139
212
|
```
|
|
140
213
|
|
|
214
|
+
For wrapper helpers, type the path parameter as `Rango.Path`. It is ambient (no
|
|
215
|
+
import) and shares `href()`'s compile-time path checking, so a wrapper stays in
|
|
216
|
+
sync with your routes automatically:
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import { href } from "@rangojs/router/client";
|
|
220
|
+
|
|
221
|
+
export const appHref = (path: Rango.Path): string => href(path);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
For response-route payloads, `Rango.PathResponse<T>` is the ambient lookup. It
|
|
225
|
+
accepts a route _pattern_ **or** a concrete path, so it also serves as the return
|
|
226
|
+
type of a typed `fetch` wrapper. It only resolves once `RegisteredRoutes` carries
|
|
227
|
+
response metadata:
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import { href } from "@rangojs/router/client";
|
|
231
|
+
|
|
232
|
+
type Product = Rango.PathResponse<"/api/products/:id">; // by pattern
|
|
233
|
+
type Same = Rango.PathResponse<"/api/products/42">; // by concrete path
|
|
234
|
+
|
|
235
|
+
// Response inferred from the concrete path passed in:
|
|
236
|
+
async function get<T extends Rango.Path>(
|
|
237
|
+
path: T,
|
|
238
|
+
): Promise<Rango.PathResponse<T>> {
|
|
239
|
+
return fetch(href(path)).then((r) => r.json());
|
|
240
|
+
}
|
|
241
|
+
const product = await get("/api/products/42"); // Product (bare value)
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Pattern keys (`/:id`) match exactly; a concrete path under a _nested_ dynamic
|
|
245
|
+
route can match several patterns and union their responses.
|
|
246
|
+
|
|
247
|
+
`Rango.PathResponse` describes the JSON **wire** shape, not the handler's raw
|
|
248
|
+
return. A `path.json()` handler returning `{ createdAt: Date }` resolves here to
|
|
249
|
+
`{ createdAt: string }` (bare value), matching what `r.json()` yields. This
|
|
250
|
+
is applied via the ambient `Rango.JsonSerialize<T>` transform (`Date -> string`,
|
|
251
|
+
honors `toJSON()`, drops functions/`undefined`, `bigint -> never`). A separate
|
|
252
|
+
`Rango.FlightSerialize<T>` models the higher-fidelity RSC Flight boundary
|
|
253
|
+
(loaders / RSC props, where `Date` is preserved) — do **not** use it for
|
|
254
|
+
`path.json()`.
|
|
255
|
+
|
|
256
|
+
### Overriding serialization globally
|
|
257
|
+
|
|
258
|
+
For your own types, the zero-config way to control the JSON wire shape is a
|
|
259
|
+
`toJSON()` method — `Rango.JsonSerialize` honors it, and it matches the runtime
|
|
260
|
+
exactly (`JSON.stringify` calls `toJSON()`):
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
class Money {
|
|
264
|
+
constructor(private cents: number) {}
|
|
265
|
+
toJSON(): number {
|
|
266
|
+
return this.cents;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Rango.JsonSerialize<Money> is number; Rango.PathResponse reflects it.
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
To override a transform for types you **don't** own (or for the Flight boundary,
|
|
273
|
+
which has no `toJSON()`), augment its override slot. Because `Rango.JsonSerialize`
|
|
274
|
+
/ `Rango.FlightSerialize` are type _aliases_ (TS can't merge those), you provide a
|
|
275
|
+
single member that is your **complete** transform, delegating to the built-in for
|
|
276
|
+
the cases you don't change:
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
declare global {
|
|
280
|
+
namespace Rango {
|
|
281
|
+
interface JsonSerializeOverride<T> {
|
|
282
|
+
app: T extends Decimal ? string : Rango.JsonSerializeBuiltin<T>;
|
|
283
|
+
}
|
|
284
|
+
interface FlightSerializeOverride<T> {
|
|
285
|
+
app: T extends Money ? number : Rango.FlightSerializeBuiltin<T>;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// Rango.JsonSerialize<Decimal> -> string; Rango.FlightSerialize<Money> -> number;
|
|
290
|
+
// everything else stays on the built-in, recursively (nested fields too).
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Rules: provide **exactly one** member (the slot is read as
|
|
294
|
+
`Override<T>[keyof Override<T>]`, so multiple members union and conflict).
|
|
295
|
+
Overrides win over `toJSON()` and apply at every nesting level. Caveat for JSON:
|
|
296
|
+
the `path.json()` runtime is plain `JSON.stringify`, which only honors `toJSON()`,
|
|
297
|
+
so a `JsonSerializeOverride` that disagrees with what the runtime emits will lie —
|
|
298
|
+
prefer `toJSON()` for your own types and use the slot only for types you can't
|
|
299
|
+
modify.
|
|
300
|
+
|
|
141
301
|
See `/links` for full URL generation guide.
|
|
142
302
|
|
|
143
303
|
## Environment Type Setup
|
|
@@ -155,7 +315,7 @@ export interface AppBindings {
|
|
|
155
315
|
AI: Ai;
|
|
156
316
|
}
|
|
157
317
|
|
|
158
|
-
// Variables set by middleware — declared via
|
|
318
|
+
// Variables set by middleware — declared via global namespace augmentation
|
|
159
319
|
export interface AppVariables {
|
|
160
320
|
user?: { id: string; email: string; role: string };
|
|
161
321
|
requestId?: string;
|
|
@@ -175,7 +335,7 @@ const router = createRouter<AppBindings>({
|
|
|
175
335
|
|
|
176
336
|
// Register bindings and variables globally for implicit typing
|
|
177
337
|
declare global {
|
|
178
|
-
namespace
|
|
338
|
+
namespace Rango {
|
|
179
339
|
interface Env extends AppBindings {}
|
|
180
340
|
interface Vars extends AppVariables {}
|
|
181
341
|
}
|
|
@@ -196,7 +356,7 @@ export const authMiddleware: Middleware = async (ctx, next) => {
|
|
|
196
356
|
// loaders - typed context
|
|
197
357
|
export const UserLoader = createLoader(async (ctx) => {
|
|
198
358
|
const db = ctx.env.DB; // D1Database (plain bindings)
|
|
199
|
-
const userId = ctx.get("user")?.id; // from
|
|
359
|
+
const userId = ctx.get("user")?.id; // from Rango.Vars
|
|
200
360
|
return db.prepare("SELECT * FROM users WHERE id = ?").bind(userId).first();
|
|
201
361
|
});
|
|
202
362
|
```
|
|
@@ -208,7 +368,7 @@ Register environment types globally for implicit typing:
|
|
|
208
368
|
```typescript
|
|
209
369
|
// router.tsx
|
|
210
370
|
declare global {
|
|
211
|
-
namespace
|
|
371
|
+
namespace Rango {
|
|
212
372
|
interface Env extends AppBindings {}
|
|
213
373
|
interface Vars extends AppVariables {}
|
|
214
374
|
}
|
|
@@ -220,8 +380,8 @@ Now handlers have typed context without explicit imports:
|
|
|
220
380
|
```typescript
|
|
221
381
|
// In loaders
|
|
222
382
|
export const DashboardLoader = createLoader(async (ctx) => {
|
|
223
|
-
// ctx.env.DB is typed from global
|
|
224
|
-
// ctx.get("user") is typed from global
|
|
383
|
+
// ctx.env.DB is typed from global Rango.Env
|
|
384
|
+
// ctx.get("user") is typed from global Rango.Vars
|
|
225
385
|
const user = ctx.get("user");
|
|
226
386
|
return { user };
|
|
227
387
|
});
|
|
@@ -259,15 +419,21 @@ This avoids circular references because `Handler` defaults to `GeneratedRouteMap
|
|
|
259
419
|
(from `router.named-routes.gen.ts`) instead of `RegisteredRoutes` (which depends on `router.tsx`).
|
|
260
420
|
|
|
261
421
|
You can also pass an explicit route map for per-module isolation (opt-in,
|
|
262
|
-
after running `npx rango generate`)
|
|
422
|
+
after running `npx rango generate`). With a local map, the route name is
|
|
423
|
+
**dot-prefixed** so params and search resolve from `routes`, not the global map:
|
|
263
424
|
|
|
264
425
|
```typescript
|
|
265
426
|
import type { Handler } from "@rangojs/router";
|
|
266
427
|
import type { routes } from "./urls.gen.js";
|
|
267
428
|
|
|
268
|
-
export const SearchPage: Handler<"search", routes> = (ctx) => { ... };
|
|
429
|
+
export const SearchPage: Handler<".search", routes> = (ctx) => { ... };
|
|
269
430
|
```
|
|
270
431
|
|
|
432
|
+
Note the difference: `Handler<"search">` (no dot) resolves against the global
|
|
433
|
+
`GeneratedRouteMap`; `Handler<".search", routes>` resolves against the local
|
|
434
|
+
`routes` map. Mixing them — `Handler<"search", routes>` — silently ignores
|
|
435
|
+
`routes` for param/search inference and only uses it for local `ctx.reverse(".x")`.
|
|
436
|
+
|
|
271
437
|
Supported types: `"string"`, `"number"`, `"boolean"`, with `?` suffix for optional.
|
|
272
438
|
Values are automatically coerced from query string (e.g., `"2"` becomes `2` for numbers).
|
|
273
439
|
Routes without a `search` schema keep the standard `URLSearchParams` behavior.
|
|
@@ -281,12 +447,18 @@ import type { RouteSearchParams, RouteParams } from "@rangojs/router";
|
|
|
281
447
|
|
|
282
448
|
// RouteSearchParams<"name"> resolves the search schema to a typed object
|
|
283
449
|
type SP = RouteSearchParams<"search">;
|
|
284
|
-
// { q: string; page?: number; sort?: string }
|
|
450
|
+
// { q: string | undefined; page?: number; sort?: string }
|
|
285
451
|
|
|
286
452
|
// RouteParams<"name"> resolves URL params from the route pattern
|
|
287
453
|
type P = RouteParams<"blogPost">;
|
|
288
454
|
// { slug: string }
|
|
289
455
|
|
|
456
|
+
// Optional URL params (`:slug?`) resolve to `string | undefined`
|
|
457
|
+
// because absent segments are omitted from `ctx.params` at runtime.
|
|
458
|
+
type C = RouteParams<"checkout">;
|
|
459
|
+
// { step?: string }
|
|
460
|
+
// → ctx.params.step is `string | undefined`; use `?? "default"` to coalesce.
|
|
461
|
+
|
|
290
462
|
// Use in component props
|
|
291
463
|
interface SearchResultsProps {
|
|
292
464
|
params: RouteSearchParams<"search">;
|
|
@@ -319,6 +491,19 @@ export const NamedRoutes = {
|
|
|
319
491
|
} as const;
|
|
320
492
|
```
|
|
321
493
|
|
|
494
|
+
You never open a `.gen.ts` by hand. Treat the generated types as call-site
|
|
495
|
+
honesty checks, not modules to read:
|
|
496
|
+
|
|
497
|
+
- **Do not import `router.named-routes.gen.ts` directly**, and don't reach for
|
|
498
|
+
`Rango.GeneratedRouteMap`. It is the whole-app manifest, auto-wired
|
|
499
|
+
globally — `Handler<"name">` and `ctx.reverse("name")` already see it.
|
|
500
|
+
- **Per-module `*.gen.ts` imports are fine** — they are the opt-in local-route
|
|
501
|
+
pattern for `useReverse(routes)` and explicit local handler typing
|
|
502
|
+
(`Handler<".name", routes>`). See `/links`.
|
|
503
|
+
|
|
504
|
+
If a type error points at a generated map instead of your call site, that's a
|
|
505
|
+
smell — fix the call site (or regenerate), never edit the generated file.
|
|
506
|
+
|
|
322
507
|
## Loader Type Safety
|
|
323
508
|
|
|
324
509
|
Loaders have typed return values:
|
|
@@ -334,7 +519,7 @@ export const ProductLoader = createLoader(async (ctx) => {
|
|
|
334
519
|
});
|
|
335
520
|
|
|
336
521
|
// In server component - type is inferred
|
|
337
|
-
import { useLoader } from "@rangojs/router";
|
|
522
|
+
import { useLoader } from "@rangojs/router/client";
|
|
338
523
|
|
|
339
524
|
async function ProductPage() {
|
|
340
525
|
const product = await useLoader(ProductLoader);
|
|
@@ -369,8 +554,18 @@ interface PaginationData {
|
|
|
369
554
|
perPage: number;
|
|
370
555
|
}
|
|
371
556
|
export const Pagination = createVar<PaginationData>();
|
|
557
|
+
|
|
558
|
+
// Non-cacheable var — reading inside cache() or "use cache" throws at runtime
|
|
559
|
+
const Session = createVar<SessionData>({ cache: false });
|
|
372
560
|
```
|
|
373
561
|
|
|
562
|
+
`createVar` accepts an optional options object. The `cache` option (default
|
|
563
|
+
`true`) controls whether the var's values can be read inside cache scopes.
|
|
564
|
+
Write-level escalation is also supported: `ctx.set(Var, value, { cache: false })`
|
|
565
|
+
marks a specific write as non-cacheable even if the var itself is cacheable.
|
|
566
|
+
"Least cacheable wins" — if either says `cache: false`, the value throws on
|
|
567
|
+
read inside `cache()` or `"use cache"`.
|
|
568
|
+
|
|
374
569
|
### Producer (handler or middleware)
|
|
375
570
|
|
|
376
571
|
```typescript
|
|
@@ -398,9 +593,9 @@ export function PaginationLayout(ctx: any) {
|
|
|
398
593
|
}
|
|
399
594
|
```
|
|
400
595
|
|
|
401
|
-
### Why not just use
|
|
596
|
+
### Why not just use Rango.Vars?
|
|
402
597
|
|
|
403
|
-
`
|
|
598
|
+
`Rango.Vars` (via global namespace augmentation) provides app-global typing for
|
|
404
599
|
`ctx.get("key")` / `ctx.set("key", value)`. It works for middleware state
|
|
405
600
|
shared app-wide. `createVar<T>()` is for route-local or feature-scoped
|
|
406
601
|
context -- the producer and consumer import the same token, creating a
|
|
@@ -414,26 +609,30 @@ Both approaches coexist: `ctx.get("user")` (global via Vars) and
|
|
|
414
609
|
Handles have typed data:
|
|
415
610
|
|
|
416
611
|
```typescript
|
|
417
|
-
//
|
|
418
|
-
import {
|
|
419
|
-
|
|
420
|
-
//
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
// In client - typed array
|
|
612
|
+
// Built-in Breadcrumbs handle — import from "@rangojs/router"
|
|
613
|
+
import { Breadcrumbs } from "@rangojs/router";
|
|
614
|
+
// Type: Handle<BreadcrumbItem, BreadcrumbItem[]>
|
|
615
|
+
// BreadcrumbItem: { label: string; href: string; content?: ReactNode | Promise<ReactNode> }
|
|
616
|
+
|
|
617
|
+
// In route handler — push is fully typed
|
|
618
|
+
path("/shop/product/:slug", (ctx) => {
|
|
619
|
+
const breadcrumb = ctx.use(Breadcrumbs);
|
|
620
|
+
breadcrumb({ label: "Products", href: "/shop/products" });
|
|
621
|
+
return <ProductPage />;
|
|
622
|
+
}, { name: "product" });
|
|
623
|
+
|
|
624
|
+
// In client — typed array
|
|
625
|
+
import { useHandle, Breadcrumbs } from "@rangojs/router/client";
|
|
433
626
|
function BreadcrumbNav() {
|
|
434
627
|
const crumbs = useHandle(Breadcrumbs);
|
|
435
|
-
// crumbs:
|
|
628
|
+
// crumbs: BreadcrumbItem[]
|
|
436
629
|
}
|
|
630
|
+
|
|
631
|
+
// Custom handles also work the same way
|
|
632
|
+
import { createHandle } from "@rangojs/router";
|
|
633
|
+
export const PageTitle = createHandle<string, string>(
|
|
634
|
+
(segments) => segments.flat().at(-1) ?? "Default Title"
|
|
635
|
+
);
|
|
437
636
|
```
|
|
438
637
|
|
|
439
638
|
## Ref Prop Type Safety (Loaders & Handles)
|
|
@@ -447,14 +646,14 @@ export const ProductLoader = createLoader(async (ctx) => {
|
|
|
447
646
|
return { product: await fetchProduct(ctx.params.slug) };
|
|
448
647
|
});
|
|
449
648
|
|
|
450
|
-
//
|
|
451
|
-
|
|
649
|
+
// Built-in Breadcrumbs — or any custom handle created with createHandle()
|
|
650
|
+
```
|
|
452
651
|
|
|
652
|
+
```tsx
|
|
453
653
|
// Client component — typeof infers all generics
|
|
454
|
-
|
|
455
|
-
import { useLoader, useHandle } from "@rangojs/router/client";
|
|
654
|
+
"use client";
|
|
655
|
+
import { useLoader, useHandle, type Breadcrumbs } from "@rangojs/router/client";
|
|
456
656
|
import type { ProductLoader } from "../loaders";
|
|
457
|
-
import type { Breadcrumbs } from "../handles";
|
|
458
657
|
|
|
459
658
|
function MyComponent({
|
|
460
659
|
loader,
|
|
@@ -473,6 +672,42 @@ RSC Flight serialization calls `toJSON()` on both loaders and handles,
|
|
|
473
672
|
sending only `{ __brand, $$id }` to the client. The hooks recover the
|
|
474
673
|
full functionality from module-level registries.
|
|
475
674
|
|
|
675
|
+
## Stable identity: `path#export`
|
|
676
|
+
|
|
677
|
+
Loaders, handles, cached functions (`functionId`), and server actions
|
|
678
|
+
(`actionId`) all share one identity scheme: `{modulePath}#{exportName}`,
|
|
679
|
+
injected at build by the `exposeInternalIds` and `exposeActionId` Vite plugins.
|
|
680
|
+
This is also the identity React server actions carry across the Flight boundary,
|
|
681
|
+
which is why a `revalidate()` predicate sees an action as a `path#export` string:
|
|
682
|
+
|
|
683
|
+
```typescript
|
|
684
|
+
revalidate(
|
|
685
|
+
({ actionId }) => actionId === "src/actions/cart.ts#addToCart" || undefined,
|
|
686
|
+
);
|
|
687
|
+
```
|
|
688
|
+
|
|
689
|
+
`actionId` is the only stable reference React exposes across the Flight boundary,
|
|
690
|
+
so it stays as the floor and escape hatch. The hand-written-string surface
|
|
691
|
+
(`actionId?.includes("cart.ts#")`) is brittle: a renamed action or moved file
|
|
692
|
+
silently stops matching with no compile error. Prefer **`ctx.isAction()`** in a
|
|
693
|
+
revalidate predicate — it resolves the action's id from an imported reference, so
|
|
694
|
+
a rename is a type error in one place instead of silent drift:
|
|
695
|
+
|
|
696
|
+
```ts
|
|
697
|
+
import { addToCart, removeFromCart } from "./actions/cart";
|
|
698
|
+
import * as CartActions from "./actions/cart";
|
|
699
|
+
|
|
700
|
+
revalidate((ctx) => ctx.isAction(addToCart) || undefined); // one action
|
|
701
|
+
revalidate((ctx) => ctx.isAction(addToCart, removeFromCart) || undefined); // several
|
|
702
|
+
revalidate((ctx) => ctx.isAction(CartActions) || undefined); // any action in the module
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
`ctx.isAction()` (only available on the revalidate predicate's context) returns a
|
|
706
|
+
raw boolean — combine with `|| undefined` for the "revalidate on match, else
|
|
707
|
+
defer" intent. It resolves the reference the same way the router derives
|
|
708
|
+
`actionId` (`$id` in production, `$$id` in dev), so matching
|
|
709
|
+
works in both modes. `actionId` stays available for advanced cases.
|
|
710
|
+
|
|
476
711
|
## Location State Type Safety
|
|
477
712
|
|
|
478
713
|
```typescript
|
|
@@ -508,9 +743,37 @@ function ProductHeader() {
|
|
|
508
743
|
|
|
509
744
|
## Multi-Project tsconfig Setup
|
|
510
745
|
|
|
511
|
-
For monorepos or multi-app setups,
|
|
512
|
-
|
|
513
|
-
|
|
746
|
+
For monorepos or multi-app setups, each app should have its own TypeScript
|
|
747
|
+
program. Do not typecheck two Rango apps with different `Rango.Env`,
|
|
748
|
+
`Rango.Vars`, or `Rango.RegisteredRoutes` declarations in one tsconfig, because
|
|
749
|
+
ambient global interfaces merge across the whole program.
|
|
750
|
+
|
|
751
|
+
### Multiple routers in one program
|
|
752
|
+
|
|
753
|
+
`Rango.GeneratedRouteMap` is a **single global interface**. Each router's
|
|
754
|
+
generated `router.named-routes.gen.ts` augments it, so two routers in the **same
|
|
755
|
+
TS program** that define overlapping route names (e.g. both have a `home`) make
|
|
756
|
+
the augmentations collide:
|
|
757
|
+
|
|
758
|
+
```text
|
|
759
|
+
Interface 'GeneratedRouteMap' cannot simultaneously extend ...
|
|
760
|
+
Named property 'home' ... are not identical.
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
This is the multi-router / host-router case. Resolve it by:
|
|
764
|
+
|
|
765
|
+
- **Separate TS programs** — give each router its own tsconfig (as below) so only
|
|
766
|
+
one generated map is in scope per program. Recommended.
|
|
767
|
+
- **Unique route-name prefixes** — name routes per router (`appA.home`,
|
|
768
|
+
`appB.home`) so the merged global map has no duplicate keys.
|
|
769
|
+
|
|
770
|
+
A single global generated map is a single-router convenience; global named-route
|
|
771
|
+
typing across multiple routers in one program is not supported today (it would
|
|
772
|
+
need per-router scoping in the generated map).
|
|
773
|
+
|
|
774
|
+
Use a shared base tsconfig for common compiler options, then make every app
|
|
775
|
+
tsconfig include its own source tree, its own `router.tsx`, and the generated
|
|
776
|
+
`router.named-routes.gen.ts` that lives beside that router.
|
|
514
777
|
|
|
515
778
|
```jsonc
|
|
516
779
|
// tsconfig.base.json (root)
|
|
@@ -549,10 +812,49 @@ global type declarations (like `RSCRouter.Env`).
|
|
|
549
812
|
}
|
|
550
813
|
```
|
|
551
814
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
815
|
+
Run generation per app:
|
|
816
|
+
|
|
817
|
+
```bash
|
|
818
|
+
npx rango generate apps/shop/src/router.tsx
|
|
819
|
+
npx rango generate apps/blog/src/router.tsx
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
If an app has multiple tsconfigs (`tsconfig.app.json`, `tsconfig.test.json`,
|
|
823
|
+
`tsconfig.worker.json`), every tsconfig that typechecks Rango handlers,
|
|
824
|
+
components, loaders, actions, or client navigation must see the same app-local
|
|
825
|
+
type surfaces:
|
|
826
|
+
|
|
827
|
+
```jsonc
|
|
828
|
+
// apps/shop/tsconfig.test.json
|
|
829
|
+
{
|
|
830
|
+
"extends": "./tsconfig.json",
|
|
831
|
+
"include": ["src", "tests"],
|
|
832
|
+
"files": ["src/router.tsx"],
|
|
833
|
+
}
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
The `files` array ensures `router.tsx` is always included even if nothing
|
|
837
|
+
directly imports it. The generated `router.named-routes.gen.ts` is normally
|
|
838
|
+
covered by `include: ["src"]`; if a tsconfig uses a narrow `include`, add the
|
|
839
|
+
generated file explicitly. Each app gets its own typed environment and named
|
|
840
|
+
route map without interfering with other apps.
|
|
841
|
+
|
|
842
|
+
For response and MIME payload lookup in each app, augment `RegisteredRoutes`
|
|
843
|
+
inside that app's router file:
|
|
844
|
+
|
|
845
|
+
```typescript
|
|
846
|
+
// apps/shop/src/router.tsx
|
|
847
|
+
export const router = createRouter<ShopEnv>({ document: Document }).routes(
|
|
848
|
+
urlpatterns,
|
|
849
|
+
);
|
|
850
|
+
|
|
851
|
+
declare global {
|
|
852
|
+
namespace Rango {
|
|
853
|
+
interface Env extends ShopEnv {}
|
|
854
|
+
interface RegisteredRoutes extends typeof router.routeMap {}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
```
|
|
556
858
|
|
|
557
859
|
## Complete Type-Safe Setup
|
|
558
860
|
|
|
@@ -588,7 +890,7 @@ const router = createRouter<AppBindings>({
|
|
|
588
890
|
|
|
589
891
|
// Register bindings and variables globally for implicit typing
|
|
590
892
|
declare global {
|
|
591
|
-
namespace
|
|
893
|
+
namespace Rango {
|
|
592
894
|
interface Env extends AppBindings {}
|
|
593
895
|
interface Vars extends AppVariables {}
|
|
594
896
|
}
|
|
@@ -599,13 +901,16 @@ export default router;
|
|
|
599
901
|
|
|
600
902
|
// 4. Run `npx rango generate src/router.tsx` to generate
|
|
601
903
|
// router.named-routes.gen.ts (auto-registers GeneratedRouteMap globally).
|
|
602
|
-
// No manual RegisteredRoutes declaration needed
|
|
904
|
+
// No manual RegisteredRoutes declaration is needed for named-route handlers,
|
|
905
|
+
// ctx.reverse, prerender, href(), or Rango.Path. Add `RegisteredRoutes
|
|
906
|
+
// extends typeof router.routeMap` when global response payload helpers such
|
|
907
|
+
// as Rango.PathResponse need the richer router.routeMap metadata.
|
|
603
908
|
|
|
604
909
|
// 5. loaders/*.ts - Type-safe loaders
|
|
605
910
|
export const ProductLoader = createLoader(async (ctx) => {
|
|
606
911
|
// ctx.params: { slug: string }
|
|
607
|
-
// ctx.get("user"): User | undefined (from
|
|
608
|
-
// ctx.env.DB: D1Database (plain bindings from
|
|
912
|
+
// ctx.get("user"): User | undefined (from Rango.Vars)
|
|
913
|
+
// ctx.env.DB: D1Database (plain bindings from Rango.Env)
|
|
609
914
|
return { product: await fetchProduct(ctx.params.slug) };
|
|
610
915
|
});
|
|
611
916
|
|