@rangojs/router 0.0.0-experimental.79 → 0.0.0-experimental.7c7e4327
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 +8 -4
- package/README.md +301 -797
- package/dist/bin/rango.js +603 -145
- package/dist/testing/vitest.js +82 -0
- package/dist/vite/index.js +3750 -1160
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +96 -24
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +85 -6
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +228 -33
- package/skills/caching/SKILL.md +336 -19
- package/skills/catalog.json +271 -0
- package/skills/comparison/SKILL.md +50 -0
- package/skills/comparison/agents/openai.yaml +4 -0
- package/skills/comparison/references/framework-comparison.md +837 -0
- package/skills/composability/SKILL.md +110 -4
- package/skills/css/SKILL.md +76 -0
- package/skills/debug-manifest/SKILL.md +5 -3
- package/skills/defer-hydration/SKILL.md +235 -0
- package/skills/document-cache/SKILL.md +87 -56
- package/skills/fonts/SKILL.md +1 -1
- package/skills/handler-use/SKILL.md +12 -10
- package/skills/hooks/SKILL.md +73 -691
- package/skills/hooks/data.md +273 -0
- package/skills/hooks/handle-and-actions.md +103 -0
- package/skills/hooks/navigation.md +110 -0
- package/skills/hooks/outlets.md +41 -0
- package/skills/hooks/state.md +228 -0
- package/skills/hooks/urls.md +135 -0
- package/skills/host-router/SKILL.md +129 -27
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +75 -19
- package/skills/layout/SKILL.md +40 -19
- package/skills/links/SKILL.md +247 -17
- package/skills/loader/SKILL.md +248 -10
- package/skills/middleware/SKILL.md +25 -13
- package/skills/migrate-nextjs/SKILL.md +205 -20
- package/skills/migrate-react-router/SKILL.md +59 -670
- package/skills/migrate-react-router/cloudflare-workers.md +129 -0
- package/skills/migrate-react-router/component-migration.md +196 -0
- package/skills/migrate-react-router/data-and-actions.md +225 -0
- package/skills/migrate-react-router/route-mapping.md +271 -0
- package/skills/mime-routes/SKILL.md +29 -2
- package/skills/observability/SKILL.md +202 -0
- package/skills/parallel/SKILL.md +40 -10
- package/skills/ppr/SKILL.md +616 -0
- package/skills/prerender/SKILL.md +72 -60
- package/skills/rango/SKILL.md +318 -26
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +138 -49
- package/skills/route/SKILL.md +117 -9
- package/skills/router-setup/SKILL.md +44 -9
- package/skills/scripts/SKILL.md +179 -0
- package/skills/server-actions/SKILL.md +776 -0
- package/skills/shell-manifest/SKILL.md +185 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/tailwind/SKILL.md +28 -4
- package/skills/testing/SKILL.md +130 -0
- 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 +131 -0
- package/skills/testing/loader.md +128 -0
- package/skills/testing/middleware.md +99 -0
- package/skills/testing/render-handler.md +122 -0
- package/skills/testing/response-routes.md +95 -0
- package/skills/testing/reverse-and-types.md +85 -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/theme/SKILL.md +1 -1
- package/skills/typesafety/SKILL.md +45 -626
- package/skills/typesafety/env-and-bindings.md +254 -0
- package/skills/typesafety/generated-files-and-cli.md +335 -0
- package/skills/typesafety/params-and-search.md +153 -0
- package/skills/typesafety/route-types.md +209 -0
- package/skills/use-cache/SKILL.md +74 -15
- package/skills/vercel/SKILL.md +128 -0
- package/skills/view-transitions/SKILL.md +337 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/__internal.ts +0 -65
- 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/connection-warmup.ts +134 -0
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/event-controller.ts +252 -158
- 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/logging.ts +28 -0
- package/src/browser/merge-segment-loaders.ts +6 -4
- package/src/browser/navigation-bridge.ts +94 -25
- package/src/browser/navigation-client.ts +144 -79
- package/src/browser/navigation-store-handle.ts +38 -0
- package/src/browser/navigation-store.ts +161 -73
- package/src/browser/navigation-transaction.ts +9 -59
- package/src/browser/network-error-handler.ts +34 -7
- package/src/browser/partial-update.ts +183 -144
- package/src/browser/prefetch/cache.ts +242 -77
- package/src/browser/prefetch/fetch.ts +325 -69
- package/src/browser/prefetch/queue.ts +61 -12
- package/src/browser/rango-state.ts +158 -76
- package/src/browser/react/Link.tsx +58 -20
- package/src/browser/react/NavigationProvider.tsx +202 -120
- package/src/browser/react/ScrollRestoration.tsx +10 -6
- 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 +17 -14
- 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 +25 -3
- 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 +99 -8
- package/src/browser/rsc-router.tsx +145 -28
- package/src/browser/scroll-restoration.ts +37 -22
- package/src/browser/segment-reconciler.ts +31 -21
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +236 -65
- package/src/browser/types.ts +102 -9
- package/src/browser/validate-redirect-origin.ts +43 -16
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +203 -154
- package/src/build/generate-route-types.ts +3 -1
- package/src/build/index.ts +11 -3
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +152 -21
- 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 +456 -62
- 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 +128 -51
- package/src/build/route-types/scan-filter.ts +1 -1
- package/src/build/route-types/source-scan.ts +216 -0
- package/src/build/runtime-discovery.ts +13 -21
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-key-utils.ts +58 -13
- package/src/cache/cache-policy.ts +108 -34
- package/src/cache/cache-runtime.ts +421 -58
- package/src/cache/cache-scope.ts +187 -96
- package/src/cache/cache-tag.ts +149 -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 +2202 -372
- 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 +6 -16
- package/src/cache/document-cache.ts +126 -41
- package/src/cache/handle-snapshot.ts +70 -0
- package/src/cache/index.ts +23 -20
- package/src/cache/memory-segment-store.ts +243 -37
- package/src/cache/profile-registry.ts +46 -31
- package/src/cache/read-through-swr.ts +56 -12
- package/src/cache/segment-codec.ts +13 -21
- package/src/cache/shell-snapshot.ts +417 -0
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/types.ts +180 -99
- package/src/cache/vercel/index.ts +11 -0
- package/src/cache/vercel/vercel-cache-store.ts +1127 -0
- package/src/client.rsc.tsx +41 -21
- package/src/client.tsx +33 -61
- package/src/cloudflare/index.ts +11 -0
- package/src/cloudflare/tracing.ts +108 -0
- package/src/component-utils.ts +19 -0
- package/src/components/DefaultDocument.tsx +8 -2
- package/src/context-var.ts +18 -6
- package/src/decode-loader-results.ts +52 -0
- package/src/defer.ts +185 -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 +67 -37
- package/src/handles/MetaTags.tsx +24 -53
- package/src/handles/Scripts.tsx +183 -0
- package/src/handles/breadcrumbs.ts +35 -8
- package/src/handles/deferred-resolution.ts +127 -0
- package/src/handles/is-thenable.ts +18 -0
- package/src/handles/meta.ts +14 -40
- package/src/handles/script.ts +244 -0
- package/src/host/cookie-handler.ts +9 -60
- package/src/host/errors.ts +13 -22
- package/src/host/index.ts +9 -2
- 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 +97 -12
- package/src/index.ts +98 -14
- 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 +32 -37
- package/src/prerender.ts +78 -10
- package/src/redirect-origin.ts +114 -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 -39
- package/src/root-error-boundary.tsx +1 -19
- package/src/route-content-wrapper.tsx +19 -77
- package/src/route-definition/dsl-helpers.ts +304 -309
- package/src/route-definition/helper-factories.ts +28 -140
- package/src/route-definition/helpers-types.ts +87 -59
- package/src/route-definition/index.ts +1 -2
- package/src/route-definition/redirect.ts +44 -11
- package/src/route-definition/resolve-handler-use.ts +12 -1
- package/src/route-definition/use-item-types.ts +29 -0
- package/src/route-map-builder.ts +41 -20
- package/src/route-types.ts +19 -46
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +73 -25
- package/src/router/error-handling.ts +45 -18
- package/src/router/find-match.ts +129 -30
- package/src/router/handler-context.ts +27 -42
- package/src/router/instrument.ts +355 -0
- package/src/router/intercept-resolution.ts +39 -20
- package/src/router/lazy-includes.ts +82 -59
- package/src/router/loader-resolution.ts +167 -72
- package/src/router/logging.ts +0 -6
- package/src/router/manifest.ts +74 -40
- package/src/router/match-api.ts +80 -55
- package/src/router/match-context.ts +0 -22
- package/src/router/match-handlers.ts +211 -165
- package/src/router/match-middleware/background-revalidation.ts +40 -24
- package/src/router/match-middleware/cache-lookup.ts +159 -285
- package/src/router/match-middleware/cache-store.ts +64 -52
- package/src/router/match-middleware/intercept-resolution.ts +0 -22
- package/src/router/match-middleware/segment-resolution.ts +0 -22
- package/src/router/match-pipelines.ts +1 -42
- package/src/router/match-result.ts +69 -79
- package/src/router/metrics.ts +0 -34
- package/src/router/middleware-types.ts +7 -134
- package/src/router/middleware.ts +298 -172
- package/src/router/navigation-snapshot.ts +7 -56
- package/src/router/params-util.ts +23 -0
- package/src/router/parse-pattern.ts +115 -0
- package/src/router/pattern-matching.ts +181 -150
- package/src/router/prefetch-cache-ttl.ts +51 -0
- package/src/router/prefetch-limits.ts +37 -0
- package/src/router/prerender-match.ts +112 -67
- package/src/router/preview-match.ts +6 -2
- package/src/router/request-classification.ts +50 -69
- package/src/router/revalidation.ts +123 -73
- package/src/router/route-snapshot.ts +14 -3
- package/src/router/router-context.ts +6 -29
- package/src/router/router-interfaces.ts +115 -36
- package/src/router/router-options.ts +166 -5
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +131 -86
- package/src/router/segment-resolution/helpers.ts +86 -6
- package/src/router/segment-resolution/loader-cache.ts +139 -39
- package/src/router/segment-resolution/loader-mask.ts +67 -0
- package/src/router/segment-resolution/loader-snapshot.ts +251 -0
- package/src/router/segment-resolution/revalidation.ts +272 -320
- 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 +56 -0
- package/src/router/segment-resolution.ts +5 -1
- package/src/router/segment-wrappers.ts +6 -5
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/substitute-pattern-params.ts +75 -0
- package/src/router/telemetry-otel.ts +160 -200
- package/src/router/telemetry.ts +105 -20
- package/src/router/timeout.ts +0 -20
- package/src/router/tracing.ts +215 -0
- package/src/router/trie-matching.ts +171 -59
- package/src/router/types.ts +9 -63
- package/src/router/url-params.ts +57 -0
- package/src/router.ts +157 -71
- package/src/rsc/full-payload.ts +70 -0
- package/src/rsc/handler-context.ts +3 -2
- package/src/rsc/handler.ts +291 -217
- package/src/rsc/helpers.ts +168 -46
- package/src/rsc/index.ts +2 -5
- package/src/rsc/json-route-result.ts +38 -0
- package/src/rsc/loader-fetch.ts +114 -38
- package/src/rsc/manifest-init.ts +29 -42
- package/src/rsc/nonce.ts +10 -1
- package/src/rsc/origin-guard.ts +39 -25
- package/src/rsc/progressive-enhancement.ts +124 -13
- package/src/rsc/redirect-guard.ts +100 -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 +421 -76
- package/src/rsc/runtime-warnings.ts +23 -10
- package/src/rsc/server-action.ts +282 -116
- package/src/rsc/shell-capture.ts +1158 -0
- package/src/rsc/shell-serve.ts +150 -0
- package/src/rsc/ssr-setup.ts +16 -0
- package/src/rsc/transition-gate.ts +89 -0
- package/src/rsc/types.ts +53 -5
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +35 -30
- package/src/segment-loader-promise.ts +49 -4
- package/src/segment-system.tsx +350 -149
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +208 -51
- package/src/server/cookie-parse.ts +32 -0
- package/src/server/cookie-store.ts +152 -5
- package/src/server/handle-store.ts +21 -38
- package/src/server/loader-registry.ts +33 -42
- package/src/server/request-context.ts +395 -176
- package/src/ssr/index.tsx +458 -178
- package/src/ssr/ssr-root.tsx +228 -0
- package/src/static-handler.ts +10 -13
- package/src/testing/cache-status.ts +162 -0
- package/src/testing/collect-handle.ts +46 -0
- package/src/testing/dispatch.ts +813 -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 +199 -0
- package/src/testing/index.ts +105 -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 +357 -0
- package/src/testing/render-route.tsx +584 -0
- package/src/testing/run-loader.ts +385 -0
- package/src/testing/run-middleware.ts +205 -0
- package/src/testing/run-transition-when.ts +164 -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 +56 -84
- package/src/theme/ThemeScript.tsx +7 -9
- package/src/theme/constants.ts +52 -13
- package/src/theme/index.ts +0 -7
- 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 +13 -4
- package/src/types/error-types.ts +30 -90
- package/src/types/global-namespace.ts +54 -41
- package/src/types/handler-context.ts +110 -62
- package/src/types/index.ts +3 -10
- package/src/types/loader-types.ts +11 -9
- package/src/types/request-scope.ts +112 -0
- package/src/types/route-config.ts +20 -52
- package/src/types/route-entry.ts +0 -6
- package/src/types/segments.ts +135 -14
- package/src/urls/include-helper.ts +19 -64
- package/src/urls/include-provider.ts +71 -0
- package/src/urls/index.ts +2 -11
- package/src/urls/path-helper-types.ts +63 -17
- package/src/urls/path-helper.ts +22 -106
- package/src/urls/pattern-types.ts +72 -19
- package/src/urls/response-types.ts +22 -29
- package/src/urls/type-extraction.ts +98 -154
- package/src/urls/urls-function.ts +1 -19
- package/src/use-loader.tsx +292 -107
- package/src/vercel/index.ts +11 -0
- package/src/vercel/tracing.ts +88 -0
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +8 -7
- package/src/vite/discovery/dev-prerender-cache.ts +117 -0
- package/src/vite/discovery/discover-routers.ts +127 -86
- package/src/vite/discovery/discovery-errors.ts +255 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +96 -68
- 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 +45 -1
- package/src/vite/discovery/virtual-module-codegen.ts +14 -34
- package/src/vite/index.ts +4 -0
- package/src/vite/inject-client-debug.ts +88 -0
- package/src/vite/plugin-types.ts +210 -10
- package/src/vite/plugins/cjs-to-esm.ts +16 -19
- package/src/vite/plugins/client-ref-dedup.ts +16 -11
- 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 +48 -95
- package/src/vite/plugins/expose-id-utils.ts +88 -55
- package/src/vite/plugins/expose-ids/export-analysis.ts +101 -34
- package/src/vite/plugins/expose-ids/handler-transform.ts +11 -90
- 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 +505 -486
- package/src/vite/plugins/performance-tracks.ts +26 -25
- package/src/vite/plugins/refresh-cmd.ts +1 -1
- package/src/vite/plugins/use-cache-transform.ts +73 -83
- package/src/vite/plugins/vercel-output.ts +384 -0
- package/src/vite/plugins/version-injector.ts +40 -29
- package/src/vite/plugins/version-plugin.ts +37 -40
- package/src/vite/plugins/virtual-entries.ts +138 -27
- package/src/vite/rango.ts +236 -138
- package/src/vite/router-discovery.ts +927 -136
- package/src/vite/utils/ast-handler-extract.ts +26 -35
- package/src/vite/utils/banner.ts +1 -1
- 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 +71 -43
- package/src/vite/utils/shared-utils.ts +142 -43
- 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 -7
- package/src/network-error-thrower.tsx +0 -23
- package/src/router/middleware-cookies.ts +0 -55
package/README.md
CHANGED
|
@@ -1,78 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Rango
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A code-first, type-safe React Server Components router. Django-inspired:
|
|
4
|
+
routes are expressed in one visible tree, URLs are built from names, and
|
|
5
|
+
everything past the core is opt-in.
|
|
4
6
|
|
|
5
|
-
> **Experimental:** This package is under active development. APIs may change
|
|
7
|
+
> **Experimental:** This package is under active development. APIs may change
|
|
8
|
+
> between releases. Install with `@experimental` tag.
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
This page is a tour: it builds one small shop and meets the entire core API
|
|
11
|
+
along the way — about six primitives. Everything else is opt-in and linked at
|
|
12
|
+
the end. For the design rationale behind these APIs, read
|
|
13
|
+
[Why Rango](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/why-rango.md); this page shows how it feels, that page
|
|
14
|
+
argues why it's right.
|
|
8
15
|
|
|
9
|
-
|
|
10
|
-
- **Structural composability** — Attach routes, loaders, middleware, handles, caching, prerendering, and static generation without hiding the route tree
|
|
11
|
-
- **Composable URL patterns** — Django-style `urls()` DSL with `path`, `layout`, `include`
|
|
12
|
-
- **Data loaders** — `createLoader()` with automatic streaming and Suspense integration
|
|
13
|
-
- **Live data layer** — Pre-render or cache the UI shell while loaders stay live by default at request time
|
|
14
|
-
- **Layouts & nesting** — Nested layouts with `<Outlet />` and parallel routes
|
|
15
|
-
- **Segment-level caching** — `cache()` DSL with TTL/SWR and pluggable cache stores
|
|
16
|
-
- **Middleware** — Route-level middleware with cookie and header access
|
|
17
|
-
- **Pre-rendering** — `Prerender()` and `Static()` handlers for build-time rendering
|
|
18
|
-
- **Theme support** — Light/dark mode with FOUC prevention and system detection
|
|
19
|
-
- **Host routing** — Multi-app routing by domain/subdomain via `@rangojs/router/host`
|
|
20
|
-
- **Response routes** — `path.json()`, `path.text()`, `path.xml()` for API endpoints
|
|
21
|
-
- **Trailing slash control** — Per-route canonical URLs with `"never"`, `"always"`, or `"ignore"`
|
|
22
|
-
- **CLI codegen** — `rango generate` for route type generation
|
|
23
|
-
|
|
24
|
-
## Design Docs
|
|
25
|
-
|
|
26
|
-
- [Execution model](./docs/internal/execution-model.md)
|
|
27
|
-
- [Semantic change checklist](./docs/internal/semantic-change-checklist.md)
|
|
28
|
-
- [Stability roadmap](./docs/internal/stability-roadmap.md)
|
|
29
|
-
|
|
30
|
-
## Installation
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
npm install @rangojs/router@experimental
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Peer dependencies:
|
|
16
|
+
## Install
|
|
37
17
|
|
|
38
18
|
```bash
|
|
39
|
-
npm install react @vitejs/plugin-rsc
|
|
19
|
+
npm install @rangojs/router@experimental react @vitejs/plugin-rsc
|
|
40
20
|
```
|
|
41
21
|
|
|
42
|
-
For Cloudflare Workers:
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
npm install @cloudflare/vite-plugin
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## Import Paths
|
|
49
|
-
|
|
50
|
-
Use these import paths consistently:
|
|
51
|
-
|
|
52
|
-
- `@rangojs/router` — server/RSC router APIs, route DSL, `createRouter`, `urls`, `redirect`, `Prerender`, `Static`, shared types
|
|
53
|
-
- `@rangojs/router/client` — hooks and components such as `Link`, `Outlet`, `href`, `useNavigation`, `useLoader`, `useAction`, `useLocationState`
|
|
54
|
-
- `@rangojs/router/cache` — public cache APIs such as `CFCacheStore`, `MemorySegmentCacheStore`, `createDocumentCacheMiddleware`
|
|
55
|
-
- `@rangojs/router/host`, `@rangojs/router/theme`, `@rangojs/router/vite` — specialized public subpaths
|
|
56
|
-
- `@rangojs/router/rsc`, `@rangojs/router/ssr` — advanced server-only integration subpaths for custom request/HTML pipelines
|
|
57
|
-
|
|
58
|
-
Use only subpaths that are explicitly exported from the package. Avoid deep imports such as `@rangojs/router/cache/cf`.
|
|
59
|
-
|
|
60
|
-
`@rangojs/router` is conditionally resolved. Server-only root APIs such as
|
|
61
|
-
`createRouter()`, `urls()`, `redirect()`, `Prerender()`, and `cookies()` rely on
|
|
62
|
-
the `react-server` export condition and are meant to run in router definitions,
|
|
63
|
-
handlers, and other RSC/server modules. Outside that environment the root entry
|
|
64
|
-
falls back to stub implementations that throw guidance errors.
|
|
65
|
-
|
|
66
|
-
If you hit a root-entrypoint stub error:
|
|
67
|
-
|
|
68
|
-
- hooks and components like `Link`, `Outlet`, `useLoader`, `useNavigation`, and `MetaTags` belong in `@rangojs/router/client`
|
|
69
|
-
- cache APIs like `CFCacheStore` and `createDocumentCacheMiddleware` belong in `@rangojs/router/cache`
|
|
70
|
-
- host-router APIs belong in `@rangojs/router/host`
|
|
71
|
-
|
|
72
|
-
## Quick Start
|
|
73
|
-
|
|
74
|
-
### Vite Config
|
|
75
|
-
|
|
76
22
|
```ts
|
|
77
23
|
// vite.config.ts
|
|
78
24
|
import react from "@vitejs/plugin-react";
|
|
@@ -84,873 +30,431 @@ export default defineConfig({
|
|
|
84
30
|
});
|
|
85
31
|
```
|
|
86
32
|
|
|
87
|
-
|
|
33
|
+
The `cloudflare` preset targets Cloudflare Workers (add
|
|
34
|
+
`@cloudflare/vite-plugin`); the `vercel` preset emits a ready-to-deploy
|
|
35
|
+
`.vercel/output` (Build Output API) from a plain `vite build` — see the
|
|
36
|
+
[`/vercel` skill](./skills/vercel/SKILL.md); omit `preset` for the default
|
|
37
|
+
Node setup.
|
|
88
38
|
|
|
89
|
-
|
|
90
|
-
`@rangojs/router`.
|
|
39
|
+
## Using the skills with your coding agent
|
|
91
40
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
41
|
+
This package ships 43 agent skills in `node_modules/@rangojs/router/skills/` —
|
|
42
|
+
task-focused guides written for LLM coding agents. Start at
|
|
43
|
+
`skills/rango/SKILL.md` (the mental model + catalog); a machine-readable index
|
|
44
|
+
is at `skills/catalog.json`.
|
|
95
45
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
46
|
+
- **Claude Code**: point it at the skills (e.g. "read
|
|
47
|
+
node_modules/@rangojs/router/skills/rango/SKILL.md before routing work"), or
|
|
48
|
+
copy/symlink the directories you use into your project's `.claude/skills/`.
|
|
49
|
+
- **Other agents (Cursor, Codex CLI, Gemini CLI, ...)**: these harnesses
|
|
50
|
+
auto-discover skills from `.agents/skills/` in your project (or
|
|
51
|
+
`~/.agents/skills/`) — copy or symlink the skill directories you use from
|
|
52
|
+
`node_modules/@rangojs/router/skills/<name>` into `.agents/skills/<name>`.
|
|
53
|
+
The files are plain markdown; cross-references like `/loader` name sibling
|
|
54
|
+
skill directories.
|
|
100
55
|
|
|
101
|
-
|
|
102
|
-
// reverse("home") -> "/"
|
|
103
|
-
```
|
|
56
|
+
## 1. Pages
|
|
104
57
|
|
|
105
|
-
|
|
58
|
+
A router is a tree. `path()` places a page, `layout()` wraps children,
|
|
59
|
+
`{ name }` gives a route an identity:
|
|
106
60
|
|
|
107
61
|
```tsx
|
|
62
|
+
// src/router.tsx
|
|
108
63
|
import { createRouter, urls } from "@rangojs/router";
|
|
109
|
-
import {
|
|
64
|
+
import { Document } from "./document";
|
|
65
|
+
import { ShopLayout } from "./layouts/shop";
|
|
66
|
+
import { HomePage } from "./routes/home";
|
|
67
|
+
import { ProductPage } from "./routes/product";
|
|
110
68
|
|
|
111
|
-
const urlpatterns = urls(({ path,
|
|
112
|
-
|
|
113
|
-
|
|
69
|
+
const urlpatterns = urls(({ path, layout }) => [
|
|
70
|
+
layout(<ShopLayout />, () => [
|
|
71
|
+
path("/", HomePage, { name: "home" }),
|
|
72
|
+
path("/products/:slug", ProductPage, { name: "product" }),
|
|
73
|
+
]),
|
|
114
74
|
]);
|
|
115
75
|
|
|
116
|
-
export const router = createRouter().routes(urlpatterns);
|
|
117
|
-
// reverse("blog.post", { slug: "hello-world" }) -> "/blog/hello-world"
|
|
76
|
+
export const router = createRouter({ document: Document }).routes(urlpatterns);
|
|
118
77
|
```
|
|
119
78
|
|
|
120
|
-
|
|
79
|
+
```tsx
|
|
80
|
+
// src/layouts/shop.tsx
|
|
81
|
+
import { Outlet } from "@rangojs/router/client";
|
|
82
|
+
|
|
83
|
+
export function ShopLayout() {
|
|
84
|
+
return (
|
|
85
|
+
<div>
|
|
86
|
+
<nav>Shop</nav>
|
|
87
|
+
<main>
|
|
88
|
+
<Outlet /> {/* child routes render here */}
|
|
89
|
+
</main>
|
|
90
|
+
</div>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
121
94
|
|
|
122
95
|
```tsx
|
|
123
96
|
// src/document.tsx
|
|
124
97
|
"use client";
|
|
125
98
|
|
|
126
99
|
import type { ReactNode } from "react";
|
|
127
|
-
import { MetaTags } from "@rangojs/router/client";
|
|
100
|
+
import { MetaTags, Scripts } from "@rangojs/router/client";
|
|
128
101
|
|
|
129
102
|
export function Document({ children }: { children: ReactNode }) {
|
|
130
103
|
return (
|
|
131
104
|
<html lang="en">
|
|
132
105
|
<head>
|
|
133
106
|
<MetaTags />
|
|
107
|
+
<Scripts />
|
|
134
108
|
</head>
|
|
135
|
-
<body>
|
|
109
|
+
<body>
|
|
110
|
+
<Scripts position="body" />
|
|
111
|
+
{children}
|
|
112
|
+
</body>
|
|
136
113
|
</html>
|
|
137
114
|
);
|
|
138
115
|
}
|
|
139
116
|
```
|
|
140
117
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
Rango is a named-route router first.
|
|
144
|
-
|
|
145
|
-
Paths define where a route lives. Names define how the app refers to it.
|
|
118
|
+
(The built-in `DefaultDocument` already wires all of this — a custom document
|
|
119
|
+
is optional.)
|
|
146
120
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
As an app grows, routes can pull in external handlers, loaders, middleware, handles, cache policy, intercepts, prerendering, and static generation while keeping the route tree visible at the composition site.
|
|
150
|
-
|
|
151
|
-
### Named Routes
|
|
152
|
-
|
|
153
|
-
```tsx
|
|
154
|
-
import { urls } from "@rangojs/router";
|
|
155
|
-
|
|
156
|
-
const urlpatterns = urls(({ path }) => [
|
|
157
|
-
path("/", HomePage, { name: "home" }),
|
|
158
|
-
path("/product/:slug", ProductPage, { name: "product" }),
|
|
159
|
-
path("/search/:query?", SearchPage, { name: "search" }),
|
|
160
|
-
path("/files/*", FilesPage, { name: "files" }),
|
|
161
|
-
]);
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
Use `reverse()` as the default way to link to routes:
|
|
165
|
-
|
|
166
|
-
```tsx
|
|
167
|
-
router.reverse("product", { slug: "widget" }); // "/product/widget"
|
|
168
|
-
router.reverse("search", undefined, { q: "rsc" }); // "/search?q=rsc"
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Composable URL Modules
|
|
172
|
-
|
|
173
|
-
Local route names compose cleanly with `include(..., { name })`:
|
|
174
|
-
|
|
175
|
-
```tsx
|
|
176
|
-
import { urls } from "@rangojs/router";
|
|
177
|
-
|
|
178
|
-
export const blogPatterns = urls(({ path }) => [
|
|
179
|
-
path("/", BlogIndexPage, { name: "index" }),
|
|
180
|
-
path("/:slug", BlogPostPage, { name: "post" }),
|
|
181
|
-
]);
|
|
182
|
-
|
|
183
|
-
export const urlpatterns = urls(({ path, include }) => [
|
|
184
|
-
path("/", HomePage, { name: "home" }),
|
|
185
|
-
include("/blog", blogPatterns, { name: "blog" }),
|
|
186
|
-
]);
|
|
187
|
-
|
|
188
|
-
router.reverse("blog.index"); // "/blog"
|
|
189
|
-
router.reverse("blog.post", { slug: "hello-world" }); // "/blog/hello-world"
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
This is the core composition model:
|
|
193
|
-
|
|
194
|
-
- Paths stay local to the module that defines them
|
|
195
|
-
- Names become stable references across the app
|
|
196
|
-
- `include()` scales those names without forcing raw path-string coupling
|
|
197
|
-
|
|
198
|
-
### Structural Composability
|
|
199
|
-
|
|
200
|
-
Rango avoids the usual tradeoff between modularity and visibility.
|
|
201
|
-
|
|
202
|
-
You can extract route behavior into separate files or packages and still keep one readable route definition that shows the structure of the app.
|
|
203
|
-
|
|
204
|
-
```tsx
|
|
205
|
-
import { urls } from "@rangojs/router";
|
|
206
|
-
import { ProductPage } from "./routes/product";
|
|
207
|
-
import { ProductLoader } from "./loaders/product";
|
|
208
|
-
import { productMiddleware } from "./middleware/product";
|
|
209
|
-
import { productRevalidate } from "./revalidation/product";
|
|
210
|
-
|
|
211
|
-
const shopPatterns = urls(({ path, loader, middleware, revalidate, cache }) => [
|
|
212
|
-
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
213
|
-
middleware(productMiddleware),
|
|
214
|
-
loader(ProductLoader),
|
|
215
|
-
revalidate(productRevalidate),
|
|
216
|
-
cache({ ttl: 300 }),
|
|
217
|
-
]),
|
|
218
|
-
]);
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
The route tree stays explicit even when behavior is modular.
|
|
222
|
-
|
|
223
|
-
This applies to:
|
|
224
|
-
|
|
225
|
-
- external route modules mounted with `include()`
|
|
226
|
-
- imported loaders, middleware, and handles attached at the route site
|
|
227
|
-
- prerendering and static generation attached without turning the route tree opaque
|
|
228
|
-
|
|
229
|
-
### Loaders As the Live Data Layer
|
|
230
|
-
|
|
231
|
-
Rango separates app structure from app data.
|
|
232
|
-
|
|
233
|
-
Routes, layouts, and pre-rendered segments can be static or cached, while
|
|
234
|
-
loaders stay live by default and re-resolve at request time.
|
|
235
|
-
|
|
236
|
-
This means you can pre-render or cache the shell of a page without freezing its
|
|
237
|
-
data.
|
|
238
|
-
|
|
239
|
-
- `cache()` caches route structure and rendered UI segments
|
|
240
|
-
- `Prerender()` skips loaders at build time
|
|
241
|
-
- `loader()` provides fresh request-time data
|
|
242
|
-
- individual loaders can opt into caching explicitly when needed
|
|
243
|
-
|
|
244
|
-
```tsx
|
|
245
|
-
import { urls, Prerender } from "@rangojs/router";
|
|
246
|
-
import { ArticleLoader } from "./loaders/article";
|
|
247
|
-
|
|
248
|
-
const docsPatterns = urls(({ path, loader }) => [
|
|
249
|
-
path("/docs/:slug", Prerender(DocsArticle), { name: "docs.article" }, () => [
|
|
250
|
-
loader(ArticleLoader), // fresh by default
|
|
251
|
-
]),
|
|
252
|
-
]);
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
Pre-render the page, keep the data live.
|
|
256
|
-
|
|
257
|
-
### Typed Handlers
|
|
258
|
-
|
|
259
|
-
Route handlers receive a typed context with params, search params, and `reverse()`:
|
|
121
|
+
A handler is a function of `ctx`. Typing it by route name gives typed params
|
|
122
|
+
— the Vite plugin generates the route map automatically, nothing to register:
|
|
260
123
|
|
|
261
124
|
```tsx
|
|
125
|
+
// src/routes/product.tsx
|
|
262
126
|
import type { Handler } from "@rangojs/router";
|
|
263
127
|
|
|
264
128
|
export const ProductPage: Handler<"product"> = (ctx) => {
|
|
265
|
-
|
|
266
|
-
const homeUrl = ctx.reverse("home"); // type-safe URL by route name
|
|
267
|
-
return <h1>Product: {slug}</h1>;
|
|
268
|
-
};
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
### Choosing a Handler Style
|
|
272
|
-
|
|
273
|
-
All handler typing styles are supported, but they solve different problems:
|
|
274
|
-
|
|
275
|
-
- `Handler<"product">` — default for named app routes
|
|
276
|
-
- `Handler<".post", ScopedRouteMap<"blog">>` — best for reusable included modules
|
|
277
|
-
- `Handler<"/blog/:slug">` — good for unnamed or local-only extracted handlers
|
|
278
|
-
- `Handler<{ slug: string }>` — escape hatch for advanced or decoupled cases
|
|
279
|
-
|
|
280
|
-
Example of a scoped local name inside a mounted module:
|
|
281
|
-
|
|
282
|
-
```tsx
|
|
283
|
-
import type { Handler } from "@rangojs/router";
|
|
284
|
-
import type { ScopedRouteMap } from "@rangojs/router/__internal";
|
|
285
|
-
|
|
286
|
-
type BlogRoutes = ScopedRouteMap<"blog">;
|
|
287
|
-
|
|
288
|
-
export const BlogPostPage: Handler<".post", BlogRoutes> = (ctx) => {
|
|
289
|
-
return <a href={ctx.reverse(".index")}>Back to blog</a>;
|
|
290
|
-
};
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
See [`../../docs/named-routes.md`](../../docs/named-routes.md) for the recommended mental model.
|
|
294
|
-
|
|
295
|
-
### Search Params
|
|
296
|
-
|
|
297
|
-
Define a search schema on the route for type-safe search parameters:
|
|
298
|
-
|
|
299
|
-
```tsx
|
|
300
|
-
const urlpatterns = urls(({ path }) => [
|
|
301
|
-
path("/search", SearchPage, {
|
|
302
|
-
name: "search",
|
|
303
|
-
search: { q: "string", page: "number?", sort: "string?" },
|
|
304
|
-
}),
|
|
305
|
-
]);
|
|
306
|
-
|
|
307
|
-
// Handler receives typed search params via ctx.search
|
|
308
|
-
const SearchPage: Handler<"search"> = (ctx) => {
|
|
309
|
-
const { q, page, sort } = ctx.search;
|
|
310
|
-
// q: string, page: number | undefined, sort: string | undefined
|
|
129
|
+
return <h1>{ctx.params.slug}</h1>; // slug: string, from the pattern
|
|
311
130
|
};
|
|
312
131
|
```
|
|
313
132
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
Trailing slash behavior is a current `path()` feature.
|
|
317
|
-
|
|
318
|
-
Set it per route with `trailingSlash`:
|
|
133
|
+
And because routes have names, URLs are built, never hand-written:
|
|
319
134
|
|
|
320
135
|
```tsx
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
name: "about",
|
|
324
|
-
trailingSlash: "never",
|
|
325
|
-
}),
|
|
326
|
-
path("/docs/", DocsPage, {
|
|
327
|
-
name: "docs",
|
|
328
|
-
trailingSlash: "always",
|
|
329
|
-
}),
|
|
330
|
-
path("/webhook", WebhookHandler, {
|
|
331
|
-
name: "webhook",
|
|
332
|
-
trailingSlash: "ignore",
|
|
333
|
-
}),
|
|
334
|
-
]);
|
|
136
|
+
const url = ctx.reverse("product", { slug: "espresso-cup" });
|
|
137
|
+
// "/products/espresso-cup" — name and params compile-time checked
|
|
335
138
|
```
|
|
336
139
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
- `"ignore"` — matches both forms without redirect
|
|
342
|
-
|
|
343
|
-
Default behavior when `trailingSlash` is omitted:
|
|
140
|
+
Rename `/products/:slug` to `/shop/:slug` in the one place it's defined and
|
|
141
|
+
every link, redirect, and prefetch follows. In client components, `href()`
|
|
142
|
+
validates static paths against the registered patterns:
|
|
143
|
+
`<Link to={href("/")}>Home</Link>`.
|
|
344
144
|
|
|
345
|
-
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
145
|
+
The tree is also lazy-first, which is the shape serverless cold starts want.
|
|
146
|
+
`include()` mounts a whole route module under a prefix — and with the async
|
|
147
|
+
form, `include("/shop", () => import("./shop"))`, the group is code-split:
|
|
148
|
+
its module doesn't load or run until a request matches it, a group nobody
|
|
149
|
+
visits never evaluates at all, and warm requests run zero route handlers.
|
|
150
|
+
Boot cost stays flat as the app grows — one module body at startup, not one
|
|
151
|
+
per group — while matching stays an `O(path length)` prefix trie, identical
|
|
152
|
+
in dev and production. None of this is assumed: the trie is benchmarked
|
|
153
|
+
in-repo against multi-thousand-route manifests, and the lazy guarantees are
|
|
154
|
+
pinned by run-count tests (see
|
|
155
|
+
[matching & lazy discovery](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/internal/matching-and-lazy-discovery.md)).
|
|
156
|
+
Grow the tree without watching the boot time.
|
|
349
157
|
|
|
350
|
-
|
|
158
|
+
That's a working site. Everything below adds to this app.
|
|
351
159
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
Define API endpoints that bypass the RSC pipeline:
|
|
355
|
-
|
|
356
|
-
```tsx
|
|
357
|
-
const urlpatterns = urls(({ path }) => [
|
|
358
|
-
path.json("/api/health", () => ({ status: "ok" }), { name: "health" }),
|
|
359
|
-
path.text("/robots.txt", () => "User-agent: *\nAllow: /", { name: "robots" }),
|
|
360
|
-
path.xml("/feed.xml", () => "<rss>...</rss>", { name: "feed" }),
|
|
361
|
-
]);
|
|
362
|
-
```
|
|
363
|
-
|
|
364
|
-
Response types available: `path.json()`, `path.text()`, `path.html()`, `path.xml()`, `path.image()`, `path.stream()`, `path.any()`.
|
|
365
|
-
|
|
366
|
-
## Layouts & Nesting
|
|
367
|
-
|
|
368
|
-
### Layouts with Outlet
|
|
369
|
-
|
|
370
|
-
```tsx
|
|
371
|
-
import { urls } from "@rangojs/router";
|
|
372
|
-
|
|
373
|
-
const urlpatterns = urls(({ path, layout }) => [
|
|
374
|
-
layout(<MainLayout />, () => [
|
|
375
|
-
path("/", HomePage, { name: "home" }),
|
|
376
|
-
path("/about", AboutPage, { name: "about" }),
|
|
377
|
-
]),
|
|
378
|
-
]);
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
```tsx
|
|
382
|
-
"use client";
|
|
383
|
-
import { Outlet } from "@rangojs/router/client";
|
|
384
|
-
|
|
385
|
-
function MainLayout() {
|
|
386
|
-
return (
|
|
387
|
-
<div>
|
|
388
|
-
<nav>...</nav>
|
|
389
|
-
<Outlet />
|
|
390
|
-
</div>
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
### Loading Skeletons
|
|
396
|
-
|
|
397
|
-
```tsx
|
|
398
|
-
const urlpatterns = urls(({ path, loading }) => [
|
|
399
|
-
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
400
|
-
loading(<ProductSkeleton />),
|
|
401
|
-
]),
|
|
402
|
-
]);
|
|
403
|
-
```
|
|
160
|
+
## 2. Data
|
|
404
161
|
|
|
405
|
-
|
|
162
|
+
The product page needs data. A handler is an async server component — fetch
|
|
163
|
+
where you render:
|
|
406
164
|
|
|
407
165
|
```tsx
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
path("/blog", BlogIndexPage, { name: "blog" }),
|
|
415
|
-
path("/blog/:slug", BlogPostPage, { name: "blogPost" }),
|
|
416
|
-
]),
|
|
417
|
-
]);
|
|
166
|
+
// src/routes/product.tsx
|
|
167
|
+
export const ProductPage: Handler<"product"> = async (ctx) => {
|
|
168
|
+
const product = await db.products.find(ctx.params.slug);
|
|
169
|
+
ctx.use(Meta)({ title: product.name }); // metadata where the data is
|
|
170
|
+
return <ProductView product={product} />;
|
|
171
|
+
};
|
|
418
172
|
```
|
|
419
173
|
|
|
420
|
-
|
|
174
|
+
That's the default data path. React Router and Remix split data into a
|
|
175
|
+
loader beside the component because components couldn't fetch; RSC collapses
|
|
176
|
+
the split, and Rango doesn't reintroduce it. (That `ctx.use(Meta)` line is
|
|
177
|
+
also the whole metadata story: push tags where the data already is, layouts
|
|
178
|
+
set title templates, deeper segments override — no separate metadata export,
|
|
179
|
+
no second fetch.)
|
|
421
180
|
|
|
422
|
-
|
|
181
|
+
Loaders enter when data needs a life of its own. First case: a **client
|
|
182
|
+
component** needs server data — the stock badge is interactive, but the
|
|
183
|
+
stock lives in the database:
|
|
423
184
|
|
|
424
185
|
```tsx
|
|
186
|
+
// src/loaders/stock.ts
|
|
425
187
|
import { createLoader } from "@rangojs/router";
|
|
426
188
|
|
|
427
|
-
export const
|
|
428
|
-
|
|
429
|
-
return
|
|
189
|
+
export const StockLoader = createLoader(async (ctx) => {
|
|
190
|
+
"use server";
|
|
191
|
+
return db.stockFor(ctx.params.slug);
|
|
430
192
|
});
|
|
431
193
|
```
|
|
432
194
|
|
|
433
|
-
### Using in Server Components (Handlers)
|
|
434
|
-
|
|
435
195
|
```tsx
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const { posts } = await ctx.use(BlogSidebarLoader);
|
|
441
|
-
return (
|
|
442
|
-
<ul>
|
|
443
|
-
{posts.map((p) => (
|
|
444
|
-
<li key={p.slug}>{p.title}</li>
|
|
445
|
-
))}
|
|
446
|
-
</ul>
|
|
447
|
-
);
|
|
448
|
-
}
|
|
196
|
+
path("/products/:slug", ProductPage, { name: "product" }, () => [
|
|
197
|
+
loader(StockLoader),
|
|
198
|
+
loading(<ProductSkeleton />),
|
|
199
|
+
]),
|
|
449
200
|
```
|
|
450
201
|
|
|
451
|
-
### Using in Client Components
|
|
452
|
-
|
|
453
202
|
```tsx
|
|
203
|
+
// src/components/stock-badge.tsx
|
|
454
204
|
"use client";
|
|
455
205
|
import { useLoader } from "@rangojs/router/client";
|
|
456
|
-
import {
|
|
206
|
+
import { StockLoader } from "../loaders/stock";
|
|
457
207
|
|
|
458
|
-
function
|
|
459
|
-
const { data } = useLoader(
|
|
460
|
-
return
|
|
461
|
-
<ul>
|
|
462
|
-
{data.posts.map((p) => (
|
|
463
|
-
<li key={p.slug}>{p.title}</li>
|
|
464
|
-
))}
|
|
465
|
-
</ul>
|
|
466
|
-
);
|
|
208
|
+
export function StockBadge() {
|
|
209
|
+
const { data } = useLoader(StockLoader);
|
|
210
|
+
return <span>{data.inStock ? "In stock" : "Sold out"}</span>;
|
|
467
211
|
}
|
|
468
212
|
```
|
|
469
213
|
|
|
470
|
-
|
|
214
|
+
Loaders run in parallel with the handler and stream; `loading()` opts the
|
|
215
|
+
segment into skeleton-then-stream. Without it, document requests arrive
|
|
216
|
+
**ready** — the HTML ships with data in place; the skeleton is a per-segment
|
|
217
|
+
choice, not the first impression.
|
|
471
218
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
]);
|
|
478
|
-
```
|
|
219
|
+
The rule of thumb: fetch in the **handler** when the data belongs to the
|
|
220
|
+
rendered page — it will be frozen with the shell if you cache it (step 4).
|
|
221
|
+
Put data in a **loader** when it must outlive the shell: shared with client
|
|
222
|
+
components, fresh on every hit even when the segment is cached, refetchable
|
|
223
|
+
from the client, or revalidated on its own after actions.
|
|
479
224
|
|
|
480
|
-
##
|
|
225
|
+
## 3. Mutations
|
|
481
226
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
227
|
+
Users add to cart. A server action is a plain `"use server"` function; the
|
|
228
|
+
form posts to it with standard React 19 hooks — and it works without
|
|
229
|
+
JavaScript:
|
|
485
230
|
|
|
486
231
|
```tsx
|
|
487
|
-
|
|
488
|
-
|
|
232
|
+
// src/actions/cart.ts
|
|
233
|
+
"use server";
|
|
489
234
|
|
|
490
|
-
function
|
|
491
|
-
|
|
492
|
-
<nav>
|
|
493
|
-
<Link to={reverse("home")}>Home</Link>
|
|
494
|
-
<Link to={reverse("blogPost", { slug: "my-post" })}>My Post</Link>
|
|
495
|
-
<Link to={reverse("about")}>About</Link>
|
|
496
|
-
</nav>
|
|
497
|
-
);
|
|
235
|
+
export async function addToCart(productId: string) {
|
|
236
|
+
await db.cart.insert({ productId });
|
|
498
237
|
}
|
|
499
238
|
```
|
|
500
239
|
|
|
501
|
-
`reverse()` is type-safe — route names and required params are checked at compile time. Included routes use dotted names: `reverse("api.health")`.
|
|
502
|
-
|
|
503
|
-
Handlers also have `ctx.reverse()` directly on the context:
|
|
504
|
-
|
|
505
|
-
```tsx
|
|
506
|
-
const BlogPostPage: Handler<"blogPost"> = (ctx) => {
|
|
507
|
-
const backUrl = ctx.reverse("blog");
|
|
508
|
-
return <Link to={backUrl}>Back to blog</Link>;
|
|
509
|
-
};
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
### `href()` for Path Validation (Client Components)
|
|
513
|
-
|
|
514
|
-
In client components, use `href()` for compile-time path validation:
|
|
515
|
-
|
|
516
240
|
```tsx
|
|
241
|
+
// src/components/add-to-cart.tsx
|
|
517
242
|
"use client";
|
|
518
|
-
import {
|
|
243
|
+
import { useActionState } from "react";
|
|
244
|
+
import { addToCart } from "../actions/cart";
|
|
519
245
|
|
|
520
|
-
function
|
|
246
|
+
export function AddToCart({ productId }: { productId: string }) {
|
|
247
|
+
const [, action, pending] = useActionState(() => addToCart(productId), null);
|
|
521
248
|
return (
|
|
522
|
-
<
|
|
523
|
-
<
|
|
524
|
-
|
|
525
|
-
Blog
|
|
526
|
-
</Link>
|
|
527
|
-
<Link to={href("/about")}>About</Link>
|
|
528
|
-
</nav>
|
|
249
|
+
<form action={action}>
|
|
250
|
+
<button disabled={pending}>{pending ? "Adding…" : "Add to cart"}</button>
|
|
251
|
+
</form>
|
|
529
252
|
);
|
|
530
253
|
}
|
|
531
254
|
```
|
|
532
255
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
256
|
+
After an action, route segments and loaders re-render by default so the UI
|
|
257
|
+
reflects the new state. `revalidate()` narrows that to the segments that
|
|
258
|
+
actually own the data — matched by action **reference**, so renames are
|
|
259
|
+
compile errors, not stale predicates:
|
|
536
260
|
|
|
537
261
|
```tsx
|
|
538
|
-
|
|
539
|
-
import { useNavigation, useRouter } from "@rangojs/router/client";
|
|
540
|
-
|
|
541
|
-
function SearchForm() {
|
|
542
|
-
const router = useRouter();
|
|
543
|
-
const nav = useNavigation();
|
|
262
|
+
import * as CartActions from "./actions/cart";
|
|
544
263
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
}
|
|
551
|
-
```
|
|
552
|
-
|
|
553
|
-
### Scroll Restoration
|
|
554
|
-
|
|
555
|
-
```tsx
|
|
556
|
-
"use client";
|
|
557
|
-
import { ScrollRestoration } from "@rangojs/router/client";
|
|
558
|
-
|
|
559
|
-
function Document({ children }) {
|
|
560
|
-
return (
|
|
561
|
-
<html>
|
|
562
|
-
<body>
|
|
563
|
-
{children}
|
|
564
|
-
<ScrollRestoration />
|
|
565
|
-
</body>
|
|
566
|
-
</html>
|
|
567
|
-
);
|
|
568
|
-
}
|
|
569
|
-
```
|
|
570
|
-
|
|
571
|
-
## Includes (Composable Modules)
|
|
572
|
-
|
|
573
|
-
Split URL patterns into composable modules with `include()`:
|
|
574
|
-
|
|
575
|
-
```tsx
|
|
576
|
-
// src/api/urls.tsx
|
|
577
|
-
import { urls } from "@rangojs/router";
|
|
578
|
-
|
|
579
|
-
export const apiPatterns = urls(({ path }) => [
|
|
580
|
-
path.json("/health", () => ({ status: "ok" }), { name: "health" }),
|
|
581
|
-
path.json("/products", getProducts, { name: "products" }),
|
|
582
|
-
]);
|
|
583
|
-
|
|
584
|
-
// src/urls.tsx
|
|
585
|
-
import { urls } from "@rangojs/router";
|
|
586
|
-
import { apiPatterns } from "./api/urls";
|
|
587
|
-
|
|
588
|
-
export const urlpatterns = urls(({ path, include }) => [
|
|
589
|
-
path("/", HomePage, { name: "home" }),
|
|
590
|
-
include("/api", apiPatterns, { name: "api" }),
|
|
591
|
-
// Mounts apiPatterns under /api: /api/health, /api/products
|
|
592
|
-
]);
|
|
264
|
+
path("/cart", CartPage, { name: "cart" }, () => [
|
|
265
|
+
loader(CartLoader, () => [
|
|
266
|
+
revalidate((ctx) => ctx.isAction(CartActions) || undefined),
|
|
267
|
+
]),
|
|
268
|
+
]),
|
|
593
269
|
```
|
|
594
270
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
The `name` option controls how child route names appear globally:
|
|
600
|
-
|
|
601
|
-
| Form | Child names | Generated types | Reverse resolution |
|
|
602
|
-
| ---------------------------------- | ------------------- | ---------------------- | -------------------------------------------------------------------- |
|
|
603
|
-
| `include("/x", p, { name: "ns" })` | `ns.child` | Exported as `ns.child` | `reverse("ns.child")` globally, `reverse(".child")` inside |
|
|
604
|
-
| `include("/x", p, { name: "" })` | `child` (flattened) | Exported as-is | `reverse("child")` globally, `reverse(".child")` inside (root-scope) |
|
|
605
|
-
| `include("/x", p)` | Private scope | Not exported | `reverse(".child")` inside only |
|
|
606
|
-
|
|
607
|
-
Without a `name`, included routes are local to the mounted module. They still match requests and render normally, but their names are hidden from the generated route map and cannot be reversed globally. Use `{ name: "" }` to merge children into the parent namespace without adding a prefix.
|
|
608
|
-
|
|
609
|
-
**`{ name: "" }` is flattening, not isolation.** Flattened routes behave as if defined inline at the include site — dot-local reverse (`.name`) can reach any sibling route at root scope, including routes from other `{ name: "" }` mounts. If you need module-level isolation, omit the `name` option or use a namespace.
|
|
610
|
-
|
|
611
|
-
## Middleware
|
|
612
|
-
|
|
613
|
-
```tsx
|
|
614
|
-
const urlpatterns = urls(({ path, middleware }) => [
|
|
615
|
-
middleware(
|
|
616
|
-
async (ctx, next) => {
|
|
617
|
-
const start = Date.now();
|
|
618
|
-
const response = await next();
|
|
619
|
-
console.log(
|
|
620
|
-
`${ctx.request.method} ${ctx.url.pathname} ${Date.now() - start}ms`,
|
|
621
|
-
);
|
|
622
|
-
return response;
|
|
623
|
-
},
|
|
624
|
-
() => [path("/dashboard", DashboardPage, { name: "dashboard" })],
|
|
625
|
-
),
|
|
626
|
-
]);
|
|
627
|
-
```
|
|
271
|
+
Notice what you didn't write: no API endpoint, no fetch wrapper, and no
|
|
272
|
+
client-cache invalidation call. Actions invalidate the client-side caches
|
|
273
|
+
(history entries, prefetches, HTTP cache key) automatically — a no-op action
|
|
274
|
+
can opt out per invocation with `keepClientCache()`.
|
|
628
275
|
|
|
629
|
-
##
|
|
276
|
+
## 4. Speed
|
|
630
277
|
|
|
631
|
-
|
|
278
|
+
Production traffic. Wrap a segment in `cache()` and the rendered shell —
|
|
279
|
+
including everything the handler fetched — is stored, while every loader on
|
|
280
|
+
it keeps running fresh on each hit. This is where the handler-vs-loader
|
|
281
|
+
choice from step 2 pays off: handler data freezes with the shell, the
|
|
282
|
+
`StockLoader` stays live. Cached shell, live data, one line:
|
|
632
283
|
|
|
633
284
|
```tsx
|
|
634
|
-
const urlpatterns = urls(({ path, cache }) => [
|
|
635
|
-
|
|
636
|
-
path("/
|
|
637
|
-
|
|
285
|
+
const urlpatterns = urls(({ path, layout, loader, loading, cache }) => [
|
|
286
|
+
layout(<ShopLayout />, () => [
|
|
287
|
+
path("/", HomePage, { name: "home" }),
|
|
288
|
+
cache({ ttl: 600, swr: 3600, tags: ["products"] }, () => [
|
|
289
|
+
path("/products/:slug", ProductPage, { name: "product" }, () => [
|
|
290
|
+
loader(StockLoader), // never cached: re-runs on every hit
|
|
291
|
+
loading(<ProductSkeleton />),
|
|
292
|
+
]),
|
|
293
|
+
]),
|
|
638
294
|
]),
|
|
639
295
|
]);
|
|
640
296
|
```
|
|
641
297
|
|
|
642
|
-
|
|
298
|
+
Wire a store once on the router (`MemorySegmentCacheStore` for dev,
|
|
299
|
+
`CFCacheStore` for Cloudflare — see the [`/caching` skill](./skills/caching/SKILL.md)),
|
|
300
|
+
and bust by tag from the mutation that changes the data:
|
|
643
301
|
|
|
644
302
|
```tsx
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
createDocumentCacheMiddleware,
|
|
649
|
-
} from "@rangojs/router/cache";
|
|
650
|
-
|
|
651
|
-
export const router = createRouter({
|
|
652
|
-
document: Document,
|
|
653
|
-
cache: (env) => ({
|
|
654
|
-
store: new CFCacheStore({
|
|
655
|
-
defaults: { ttl: 60, swr: 300 },
|
|
656
|
-
ctx: env.ctx,
|
|
657
|
-
}),
|
|
658
|
-
}),
|
|
659
|
-
})
|
|
660
|
-
.use(createDocumentCacheMiddleware())
|
|
661
|
-
.routes(urlpatterns);
|
|
662
|
-
```
|
|
663
|
-
|
|
664
|
-
Available cache stores:
|
|
665
|
-
|
|
666
|
-
- `CFCacheStore` — Cloudflare edge cache (production)
|
|
667
|
-
- `MemorySegmentCacheStore` — In-memory cache (development/testing)
|
|
668
|
-
|
|
669
|
-
## Pre-rendering
|
|
670
|
-
|
|
671
|
-
Pre-rendering generates route segments at build time. The worker handles all requests — there are no static files served from assets.
|
|
303
|
+
// src/actions/products.ts
|
|
304
|
+
"use server";
|
|
305
|
+
import { updateTag } from "@rangojs/router";
|
|
672
306
|
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
```tsx
|
|
678
|
-
import { Static } from "@rangojs/router";
|
|
679
|
-
|
|
680
|
-
export const AboutPage = Static(async () => {
|
|
681
|
-
return <article>...</article>;
|
|
682
|
-
});
|
|
683
|
-
|
|
684
|
-
export const DocsNav = Static(async () => {
|
|
685
|
-
const items = await readDocsNavItems();
|
|
686
|
-
return (
|
|
687
|
-
<nav>
|
|
688
|
-
{items.map((i) => (
|
|
689
|
-
<a key={i.slug} href={i.slug}>
|
|
690
|
-
{i.title}
|
|
691
|
-
</a>
|
|
692
|
-
))}
|
|
693
|
-
</nav>
|
|
694
|
-
);
|
|
695
|
-
});
|
|
307
|
+
export async function renameProduct(id: string, name: string) {
|
|
308
|
+
await db.products.rename(id, name);
|
|
309
|
+
await updateTag("products"); // awaitable, read-your-own-writes
|
|
310
|
+
}
|
|
696
311
|
```
|
|
697
312
|
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
Use `Prerender()` for route-scoped pre-rendering. With params, provide `getParams` first, handler second:
|
|
313
|
+
Navigation speed is a `Link` prop away:
|
|
701
314
|
|
|
702
315
|
```tsx
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
async () => {
|
|
707
|
-
const slugs = await getAllBlogSlugs();
|
|
708
|
-
return slugs.map((slug) => ({ slug }));
|
|
709
|
-
},
|
|
710
|
-
async (ctx) => {
|
|
711
|
-
const post = await getPost(ctx.params.slug);
|
|
712
|
-
return <article>{post.content}</article>;
|
|
713
|
-
},
|
|
714
|
-
);
|
|
316
|
+
<Link to={url} prefetch="viewport">
|
|
317
|
+
{product.name}
|
|
318
|
+
</Link>
|
|
715
319
|
```
|
|
716
320
|
|
|
717
|
-
|
|
321
|
+
A fully-prefetched navigation commits a **finished page** — no skeleton, no
|
|
322
|
+
loading flash — and staying correct is the router's job: every action
|
|
323
|
+
invalidates the prefetch caches by default, so a prefetched page can't show
|
|
324
|
+
pre-mutation data.
|
|
718
325
|
|
|
719
|
-
|
|
326
|
+
To move the shell's cost to build time entirely, `Prerender()` bakes it while
|
|
327
|
+
loaders stay live at runtime — same mental model, earlier cache write. See
|
|
328
|
+
the [`/prerender` skill](./skills/prerender/SKILL.md).
|
|
720
329
|
|
|
721
|
-
|
|
722
|
-
import { Prerender, Passthrough } from "@rangojs/router";
|
|
723
|
-
|
|
724
|
-
export const ProductPageDef = Prerender(
|
|
725
|
-
async () => {
|
|
726
|
-
const featured = await db.getFeaturedProducts();
|
|
727
|
-
return featured.map((p) => ({ id: p.id }));
|
|
728
|
-
},
|
|
729
|
-
async (ctx) => {
|
|
730
|
-
const product = await db.getProduct(ctx.params.id);
|
|
731
|
-
return <Product data={product} />;
|
|
732
|
-
},
|
|
733
|
-
);
|
|
734
|
-
|
|
735
|
-
// In route definition:
|
|
736
|
-
path(
|
|
737
|
-
"/products/:id",
|
|
738
|
-
Passthrough(ProductPageDef, async (ctx) => {
|
|
739
|
-
const product = await ctx.env.DB.getProduct(ctx.params.id);
|
|
740
|
-
return <Product data={product} />;
|
|
741
|
-
}),
|
|
742
|
-
);
|
|
743
|
-
```
|
|
330
|
+
## 5. An API, when you need one
|
|
744
331
|
|
|
745
|
-
|
|
332
|
+
Response routes live in the same tree — `path.json()`, `path.text()`,
|
|
333
|
+
`path.xml()`, `path.image()`, `path.stream()`:
|
|
746
334
|
|
|
747
335
|
```tsx
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
},
|
|
753
|
-
async (ctx) => {
|
|
754
|
-
const product = await db.getProduct(ctx.params.id);
|
|
755
|
-
if (!product.published) return ctx.passthrough();
|
|
756
|
-
return <Product data={product} />;
|
|
757
|
-
},
|
|
758
|
-
);
|
|
336
|
+
path("/products/:slug", ProductPage, { name: "product" }),
|
|
337
|
+
path.json("/products/:slug", (ctx) => db.products.find(ctx.params.slug), {
|
|
338
|
+
name: "productJson",
|
|
339
|
+
}),
|
|
759
340
|
```
|
|
760
341
|
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
342
|
+
Same URL: browsers get the page, API clients get JSON, negotiated by
|
|
343
|
+
`Accept` header in the route trie. Handlers return bare values; errors
|
|
344
|
+
serialize as RFC 9457 `application/problem+json`. The payload type is
|
|
345
|
+
inferred from the handler — no codegen:
|
|
764
346
|
|
|
765
347
|
```ts
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
)
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
348
|
+
type Product = RouteResponse<typeof urlpatterns, "productJson">;
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
See the [`/api-client` skill](./skills/api-client/SKILL.md) for a small typed
|
|
352
|
+
client over these endpoints.
|
|
353
|
+
|
|
354
|
+
## Everything else, when you need it
|
|
355
|
+
|
|
356
|
+
That was the core: `path`/`layout`/`include`, names, loaders, actions +
|
|
357
|
+
`revalidate`, `cache`, response routes. The rest is opt-in — reach for it
|
|
358
|
+
when the requirement appears:
|
|
359
|
+
|
|
360
|
+
| I need to… | Skill |
|
|
361
|
+
| ----------------------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
362
|
+
| guard or shape requests (auth, headers) | [`/middleware`](./skills/middleware/SKILL.md) |
|
|
363
|
+
| multi-column layouts, independent slots | [`/parallel`](./skills/parallel/SKILL.md) |
|
|
364
|
+
| open a route as a modal on soft navigation | [`/intercept`](./skills/intercept/SKILL.md) |
|
|
365
|
+
| compose route modules / sub-apps | [`/route`](./skills/route/SKILL.md), [`/composability`](./skills/composability/SKILL.md) |
|
|
366
|
+
| cache a single function or component | [`/use-cache`](./skills/use-cache/SKILL.md), [`/cache-guide`](./skills/cache-guide/SKILL.md) |
|
|
367
|
+
| feed live loaders from a cached shell | [`/shell-manifest`](./skills/shell-manifest/SKILL.md) |
|
|
368
|
+
| edge caching with Cache-Control | [`/document-cache`](./skills/document-cache/SKILL.md) |
|
|
369
|
+
| light/dark mode without FOUC | [`/theme`](./skills/theme/SKILL.md) |
|
|
370
|
+
| analytics / third-party scripts with CSP nonce | [`/scripts`](./skills/scripts/SKILL.md) |
|
|
371
|
+
| locale routing | [`/i18n`](./skills/i18n/SKILL.md) |
|
|
372
|
+
| SSE and WebSockets | [`/streams-and-websockets`](./skills/streams-and-websockets/SKILL.md) |
|
|
373
|
+
| multi-app routing by domain | [`/host-router`](./skills/host-router/SKILL.md) |
|
|
374
|
+
| animate navigations | [`/view-transitions`](./skills/view-transitions/SKILL.md) |
|
|
375
|
+
| test loaders, middleware, handlers, Flight | [`/testing`](./skills/testing/SKILL.md) |
|
|
376
|
+
| see where request time goes | [`/observability`](./skills/observability/SKILL.md) |
|
|
377
|
+
| deploy to Vercel (cache store, tracing, output) | [`/vercel`](./skills/vercel/SKILL.md) |
|
|
378
|
+
| compare Rango with Next.js / TanStack / Waku | [`/comparison`](./skills/comparison/SKILL.md) |
|
|
379
|
+
|
|
380
|
+
The [`/rango` skill](./skills/rango/SKILL.md) is the full catalog and the
|
|
381
|
+
mental model that ties it together.
|
|
382
|
+
|
|
383
|
+
## Reference
|
|
384
|
+
|
|
385
|
+
### Imports and subpaths
|
|
386
|
+
|
|
387
|
+
| Export | Description |
|
|
388
|
+
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
389
|
+
| `@rangojs/router` | Server/RSC core and shared types: `createRouter`, `urls`, `createLoader`, `Handler`, `Prerender`, `Meta` |
|
|
390
|
+
| `@rangojs/router/client` | Client: `Link`, `Outlet`, `href`, `useNavigation`, `useLoader`, `MetaTags` |
|
|
391
|
+
| `@rangojs/router/cache` | Cache: `CFCacheStore`, `VercelCacheStore`, `MemorySegmentCacheStore`, `createDocumentCacheMiddleware` |
|
|
392
|
+
| `@rangojs/router/theme` | Theme: `useTheme`, `ThemeProvider`, `ThemeScript` |
|
|
393
|
+
| `@rangojs/router/host` | Host routing: `createHostRouter`, `defineHosts`, `isNoRouteMatchError` |
|
|
394
|
+
| `@rangojs/router/vercel` | Vercel: `createVercelTracing` (phase spans via `@vercel/otel`'s global tracer) |
|
|
395
|
+
| `@rangojs/router/vite` | Vite plugin: `rango()` |
|
|
396
|
+
| `@rangojs/router/testing` | Consumer testing primitives: `runLoader`, `runMiddleware`, `dispatch` (plus `/testing/dom`, `/testing/flight`, `/testing/e2e`) |
|
|
397
|
+
| `@rangojs/router/rsc` | Advanced server pipeline APIs: `createRSCHandler`, request-context access |
|
|
398
|
+
| `@rangojs/router/ssr` | Advanced SSR bridge APIs: `createSSRHandler` |
|
|
399
|
+
|
|
400
|
+
Use only subpaths that are explicitly exported; avoid deep imports.
|
|
401
|
+
|
|
402
|
+
The root entry is conditionally resolved: server-only APIs (`createRouter`,
|
|
403
|
+
`urls`, `redirect`, `Prerender`, `cookies`) run under the `react-server`
|
|
404
|
+
condition and throw guidance errors elsewhere. If you hit a root-entrypoint
|
|
405
|
+
stub error: hooks and components (`Link`, `Outlet`, `useLoader`, `MetaTags`)
|
|
406
|
+
live in `@rangojs/router/client`; cache APIs in `@rangojs/router/cache`;
|
|
407
|
+
host APIs in `@rangojs/router/host`.
|
|
408
|
+
|
|
409
|
+
### Type safety
|
|
410
|
+
|
|
411
|
+
The Vite plugin generates `router.named-routes.gen.ts` automatically (on dev
|
|
412
|
+
startup, HMR, and builds), registering route names, params, and search
|
|
413
|
+
schemas globally via `Rango.GeneratedRouteMap`. That powers `Handler<"name">`,
|
|
414
|
+
`ctx.reverse()`, and `RouteParams<"name">` with no manual registration.
|
|
415
|
+
|
|
416
|
+
For response-aware and path-based utilities (`href()`, `Rango.Path`,
|
|
417
|
+
`RouteResponse`), augment `Rango.RegisteredRoutes` once:
|
|
790
418
|
|
|
791
419
|
```ts
|
|
792
|
-
// Custom factory
|
|
793
|
-
rango({
|
|
794
|
-
buildEnv: async (ctx) => {
|
|
795
|
-
const { getPlatformProxy } = await import("wrangler");
|
|
796
|
-
const proxy = await getPlatformProxy();
|
|
797
|
-
return { env: proxy.env, dispose: proxy.dispose };
|
|
798
|
-
},
|
|
799
|
-
});
|
|
800
|
-
|
|
801
|
-
// Plain object (Node.js)
|
|
802
|
-
rango({ buildEnv: { DATABASE_URL: process.env.DATABASE_URL } });
|
|
803
|
-
```
|
|
804
|
-
|
|
805
|
-
Build-time env applies to both production builds and dev on-demand prerender. Without `buildEnv`, accessing `ctx.env` in a Prerender handler throws with a clear error.
|
|
806
|
-
|
|
807
|
-
## Theme
|
|
808
|
-
|
|
809
|
-
### Router Configuration
|
|
810
|
-
|
|
811
|
-
```tsx
|
|
812
|
-
export const router = createRouter({
|
|
813
|
-
document: Document,
|
|
814
|
-
theme: {
|
|
815
|
-
defaultTheme: "light",
|
|
816
|
-
themes: ["light", "dark", "system"],
|
|
817
|
-
attribute: "class",
|
|
818
|
-
enableSystem: true,
|
|
819
|
-
},
|
|
820
|
-
}).routes(urlpatterns);
|
|
821
|
-
```
|
|
822
|
-
|
|
823
|
-
### Theme Toggle
|
|
824
|
-
|
|
825
|
-
```tsx
|
|
826
|
-
"use client";
|
|
827
|
-
import { useTheme } from "@rangojs/router/theme";
|
|
828
|
-
|
|
829
|
-
function ThemeToggle() {
|
|
830
|
-
const { theme, setTheme, themes } = useTheme();
|
|
831
|
-
return (
|
|
832
|
-
<select value={theme} onChange={(e) => setTheme(e.target.value)}>
|
|
833
|
-
{themes.map((t) => (
|
|
834
|
-
<option key={t}>{t}</option>
|
|
835
|
-
))}
|
|
836
|
-
</select>
|
|
837
|
-
);
|
|
838
|
-
}
|
|
839
|
-
```
|
|
840
|
-
|
|
841
|
-
## Host Routing
|
|
842
|
-
|
|
843
|
-
Route requests to different apps based on domain/subdomain patterns using `@rangojs/router/host`:
|
|
844
|
-
|
|
845
|
-
```tsx
|
|
846
|
-
// worker.rsc.tsx
|
|
847
|
-
import { createHostRouter } from "@rangojs/router/host";
|
|
848
|
-
|
|
849
|
-
const hostRouter = createHostRouter();
|
|
850
|
-
|
|
851
|
-
hostRouter.host(["*.localhost"]).map(() => import("./apps/admin/handler.js"));
|
|
852
|
-
hostRouter.host(["localhost"]).map(() => import("./apps/site/handler.js"));
|
|
853
|
-
hostRouter.fallback().map(() => import("./apps/site/handler.js"));
|
|
854
|
-
|
|
855
|
-
export default {
|
|
856
|
-
async fetch(request, env, ctx) {
|
|
857
|
-
return hostRouter.match(request, { env, ctx });
|
|
858
|
-
},
|
|
859
|
-
};
|
|
860
|
-
```
|
|
861
|
-
|
|
862
|
-
Each sub-app has its own `createRouter()` and `urls()`. The host router lazily imports the matched app's handler. Patterns are matched in registration order — register more specific patterns (subdomains) before catch-alls.
|
|
863
|
-
|
|
864
|
-
## Meta Tags
|
|
865
|
-
|
|
866
|
-
Accumulate meta tags across route segments using the built-in `Meta` handle:
|
|
867
|
-
|
|
868
|
-
```tsx
|
|
869
|
-
import { Meta } from "@rangojs/router";
|
|
870
|
-
import type { HandlerContext } from "@rangojs/router";
|
|
871
|
-
|
|
872
|
-
export function BlogPostPage(ctx: HandlerContext) {
|
|
873
|
-
const meta = ctx.use(Meta);
|
|
874
|
-
meta({ title: "My Blog Post" });
|
|
875
|
-
meta({ name: "description", content: "A great blog post" });
|
|
876
|
-
meta({ property: "og:title", content: "My Blog Post" });
|
|
877
|
-
|
|
878
|
-
return <article>...</article>;
|
|
879
|
-
}
|
|
880
|
-
```
|
|
881
|
-
|
|
882
|
-
Render collected tags in the document with `<MetaTags />` from `@rangojs/router/client`.
|
|
883
|
-
|
|
884
|
-
## CLI: `rango generate`
|
|
885
|
-
|
|
886
|
-
Route types are generated automatically by the Vite plugin. The CLI is a manual fallback for generating types outside the dev server (e.g. in CI or for IDE support before first `pnpm dev`):
|
|
887
|
-
|
|
888
|
-
```bash
|
|
889
|
-
npx rango generate src/router.tsx
|
|
890
|
-
npx rango generate src/ # recursive scan
|
|
891
|
-
npx rango generate src/urls.tsx src/api/ # mix files and directories
|
|
892
|
-
```
|
|
893
|
-
|
|
894
|
-
Auto-detects file type:
|
|
895
|
-
|
|
896
|
-
- Files with `createRouter` → `*.named-routes.gen.ts` with global route map
|
|
897
|
-
- Files with `urls()` → `*.gen.ts` with per-module route names, params, and search types
|
|
898
|
-
|
|
899
|
-
## Type Safety
|
|
900
|
-
|
|
901
|
-
The Vite plugin automatically generates a `router.named-routes.gen.ts` file that globally registers route names, patterns, and search schemas via `RSCRouter.GeneratedRouteMap`. This powers server-side named-route typing such as `Handler<"name">`, `ctx.reverse()`, `getRequestContext().reverse()`, and `RouteParams<"name">` without any manual route registration. The gen file is updated on dev server startup, HMR, and production builds.
|
|
902
|
-
|
|
903
|
-
Use the generated map by default. Augment `RSCRouter.RegisteredRoutes` only when you need the richer `typeof router.routeMap` shape globally, especially for response-aware and path-based utilities.
|
|
904
|
-
|
|
905
|
-
```typescript
|
|
906
420
|
// router.tsx
|
|
907
421
|
const router = createRouter<AppBindings>({}).routes(urlpatterns);
|
|
908
422
|
|
|
909
423
|
declare global {
|
|
910
|
-
namespace
|
|
424
|
+
namespace Rango {
|
|
911
425
|
interface Env extends AppEnv {}
|
|
912
|
-
interface Vars extends AppVars {}
|
|
913
426
|
interface RegisteredRoutes extends typeof router.routeMap {}
|
|
914
427
|
}
|
|
915
428
|
}
|
|
916
429
|
```
|
|
917
430
|
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
- `GeneratedRouteMap` (auto-generated) — use for server-side named-route typing: `Handler<"name">`, `ctx.reverse()`, `Prerender<"name">`
|
|
921
|
-
- `typeof router.routeMap` — use when you need route entries with response metadata
|
|
922
|
-
- `RegisteredRoutes` (manual augmentation) — use to expose `typeof router.routeMap` globally for `href()`, `PathResponse`, `ValidPaths`, and other path/response-aware utilities
|
|
431
|
+
See the [`/typesafety` skill](./skills/typesafety/SKILL.md) for the full
|
|
432
|
+
surface breakdown.
|
|
923
433
|
|
|
924
|
-
|
|
925
|
-
`ctx.reverse()` by default. If you want type-safe local names for a specific
|
|
926
|
-
module, use `scopedReverse<typeof localPatterns>(ctx.reverse)` or
|
|
927
|
-
`scopedReverse<routes>(ctx.reverse)` with a generated local route type.
|
|
434
|
+
### CLI
|
|
928
435
|
|
|
929
|
-
|
|
436
|
+
Route types are generated by the Vite plugin; the CLI is the manual fallback
|
|
437
|
+
for CI or pre-first-run IDE support:
|
|
930
438
|
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
| `@rangojs/router/cache` | Cache: `CFCacheStore`, `MemorySegmentCacheStore`, `createDocumentCacheMiddleware` |
|
|
936
|
-
| `@rangojs/router/theme` | Theme: `useTheme`, `ThemeProvider`, `ThemeScript` |
|
|
937
|
-
| `@rangojs/router/host` | Host routing: `createHostRouter`, `defineHosts` |
|
|
938
|
-
| `@rangojs/router/vite` | Vite plugin: `rango()` |
|
|
939
|
-
| `@rangojs/router/rsc` | Advanced server pipeline APIs: `createRSCHandler`, request-context access |
|
|
940
|
-
| `@rangojs/router/ssr` | Advanced SSR bridge APIs: `createSSRHandler` |
|
|
941
|
-
| `@rangojs/router/server` | Internal build/runtime utilities for advanced integrations |
|
|
942
|
-
| `@rangojs/router/build` | Build utilities |
|
|
439
|
+
```bash
|
|
440
|
+
npx rango generate src/router.tsx # global named-route map
|
|
441
|
+
npx rango generate src/ # recursive scan
|
|
442
|
+
```
|
|
943
443
|
|
|
944
|
-
|
|
945
|
-
or components, import from `@rangojs/router/client`; if you need cache or host
|
|
946
|
-
APIs, use their dedicated subpaths.
|
|
444
|
+
### Examples
|
|
947
445
|
|
|
948
|
-
|
|
446
|
+
- [`e2e/mini`](https://github.com/ivogt/vite-rsc/tree/main/packages/rangojs-router/e2e/mini) — single-file demo app
|
|
447
|
+
- [`cloudflare-basic`](https://github.com/ivogt/vite-rsc/tree/main/tests/cloudflare-basic) — Cloudflare Workers with caching, loaders, theme, and pre-rendering
|
|
448
|
+
- [`cloudflare-multi-router`](https://github.com/ivogt/vite-rsc/tree/main/examples/cloudflare-multi-router) — multi-app host routing
|
|
449
|
+
- [`vercel-basic`](https://github.com/ivogt/vite-rsc/tree/main/examples/vercel-basic) — Vercel deployment with `preset: "vercel"`, `VercelCacheStore`, and OTel tracing
|
|
450
|
+
- [`vercel-multi-router`](https://github.com/ivogt/vite-rsc/tree/main/examples/vercel-multi-router) — multi-app host routing on Vercel (single function, routed by Host header)
|
|
949
451
|
|
|
950
|
-
|
|
452
|
+
### Going deeper
|
|
951
453
|
|
|
952
|
-
- [
|
|
953
|
-
- [
|
|
454
|
+
- [Why Rango](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/why-rango.md) — the design rationale, claim by claim
|
|
455
|
+
- [Framework comparison](./skills/comparison/references/framework-comparison.md) — Rango vs Next.js App Router, TanStack Start, and Waku, capability by capability
|
|
456
|
+
- [Docs index](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/README.md) — architecture, caching, prerender, testing
|
|
457
|
+
- [Execution model](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/internal/execution-model.md) — the runtime contract
|
|
954
458
|
|
|
955
459
|
## License
|
|
956
460
|
|