@rangojs/router 0.0.0-experimental.0f44aca1
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 +5 -0
- package/README.md +899 -0
- package/dist/bin/rango.js +1601 -0
- package/dist/vite/index.js +5214 -0
- package/package.json +176 -0
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +262 -0
- package/skills/caching/SKILL.md +220 -0
- package/skills/composability/SKILL.md +172 -0
- package/skills/debug-manifest/SKILL.md +112 -0
- package/skills/document-cache/SKILL.md +182 -0
- package/skills/fonts/SKILL.md +167 -0
- package/skills/hooks/SKILL.md +704 -0
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +313 -0
- package/skills/layout/SKILL.md +310 -0
- package/skills/links/SKILL.md +239 -0
- package/skills/loader/SKILL.md +596 -0
- package/skills/middleware/SKILL.md +339 -0
- package/skills/mime-routes/SKILL.md +128 -0
- package/skills/parallel/SKILL.md +305 -0
- package/skills/prerender/SKILL.md +643 -0
- package/skills/rango/SKILL.md +118 -0
- package/skills/response-routes/SKILL.md +411 -0
- package/skills/route/SKILL.md +385 -0
- package/skills/router-setup/SKILL.md +439 -0
- package/skills/tailwind/SKILL.md +129 -0
- package/skills/theme/SKILL.md +79 -0
- package/skills/typesafety/SKILL.md +623 -0
- package/skills/use-cache/SKILL.md +324 -0
- package/src/__internal.ts +273 -0
- package/src/bin/rango.ts +321 -0
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/action-response-classifier.ts +99 -0
- package/src/browser/event-controller.ts +899 -0
- package/src/browser/history-state.ts +80 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/link-interceptor.ts +141 -0
- package/src/browser/logging.ts +55 -0
- package/src/browser/merge-segment-loaders.ts +134 -0
- package/src/browser/navigation-bridge.ts +645 -0
- package/src/browser/navigation-client.ts +215 -0
- package/src/browser/navigation-store.ts +806 -0
- package/src/browser/navigation-transaction.ts +295 -0
- package/src/browser/network-error-handler.ts +61 -0
- package/src/browser/partial-update.ts +550 -0
- package/src/browser/prefetch/cache.ts +146 -0
- package/src/browser/prefetch/fetch.ts +135 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +42 -0
- package/src/browser/prefetch/queue.ts +88 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +360 -0
- package/src/browser/react/NavigationProvider.tsx +386 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +59 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +162 -0
- package/src/browser/react/location-state.ts +107 -0
- package/src/browser/react/mount-context.ts +37 -0
- package/src/browser/react/nonce-context.ts +23 -0
- package/src/browser/react/shallow-equal.ts +27 -0
- package/src/browser/react/use-action.ts +218 -0
- package/src/browser/react/use-client-cache.ts +58 -0
- package/src/browser/react/use-handle.ts +162 -0
- package/src/browser/react/use-href.tsx +40 -0
- package/src/browser/react/use-link-status.ts +135 -0
- package/src/browser/react/use-mount.ts +31 -0
- package/src/browser/react/use-navigation.ts +99 -0
- package/src/browser/react/use-params.ts +65 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-router.ts +63 -0
- package/src/browser/react/use-search-params.ts +56 -0
- package/src/browser/react/use-segments.ts +171 -0
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +431 -0
- package/src/browser/scroll-restoration.ts +400 -0
- package/src/browser/segment-reconciler.ts +216 -0
- package/src/browser/segment-structure-assert.ts +83 -0
- package/src/browser/server-action-bridge.ts +667 -0
- package/src/browser/shallow.ts +40 -0
- package/src/browser/types.ts +538 -0
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +438 -0
- package/src/build/generate-route-types.ts +36 -0
- package/src/build/index.ts +35 -0
- package/src/build/route-trie.ts +265 -0
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +98 -0
- package/src/build/route-types/codegen.ts +102 -0
- package/src/build/route-types/include-resolution.ts +411 -0
- package/src/build/route-types/param-extraction.ts +48 -0
- package/src/build/route-types/per-module-writer.ts +128 -0
- package/src/build/route-types/router-processing.ts +469 -0
- package/src/build/route-types/scan-filter.ts +78 -0
- package/src/build/runtime-discovery.ts +231 -0
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-key-utils.ts +44 -0
- package/src/cache/cache-policy.ts +125 -0
- package/src/cache/cache-runtime.ts +338 -0
- package/src/cache/cache-scope.ts +382 -0
- package/src/cache/cf/cf-cache-store.ts +540 -0
- package/src/cache/cf/index.ts +25 -0
- package/src/cache/document-cache.ts +369 -0
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +41 -0
- package/src/cache/index.ts +43 -0
- package/src/cache/memory-segment-store.ts +328 -0
- package/src/cache/profile-registry.ts +73 -0
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +256 -0
- package/src/cache/taint.ts +98 -0
- package/src/cache/types.ts +342 -0
- package/src/client.rsc.tsx +85 -0
- package/src/client.tsx +601 -0
- package/src/component-utils.ts +76 -0
- package/src/components/DefaultDocument.tsx +27 -0
- package/src/context-var.ts +86 -0
- package/src/debug.ts +243 -0
- package/src/default-error-boundary.tsx +88 -0
- package/src/deps/browser.ts +8 -0
- package/src/deps/html-stream-client.ts +2 -0
- package/src/deps/html-stream-server.ts +2 -0
- package/src/deps/rsc.ts +10 -0
- package/src/deps/ssr.ts +2 -0
- package/src/errors.ts +365 -0
- package/src/handle.ts +135 -0
- package/src/handles/MetaTags.tsx +246 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +7 -0
- package/src/handles/meta.ts +264 -0
- package/src/host/cookie-handler.ts +165 -0
- package/src/host/errors.ts +97 -0
- package/src/host/index.ts +53 -0
- package/src/host/pattern-matcher.ts +214 -0
- package/src/host/router.ts +352 -0
- package/src/host/testing.ts +79 -0
- package/src/host/types.ts +146 -0
- package/src/host/utils.ts +25 -0
- package/src/href-client.ts +222 -0
- package/src/index.rsc.ts +233 -0
- package/src/index.ts +277 -0
- package/src/internal-debug.ts +11 -0
- package/src/loader.rsc.ts +89 -0
- package/src/loader.ts +64 -0
- package/src/network-error-thrower.tsx +23 -0
- package/src/outlet-context.ts +15 -0
- package/src/outlet-provider.tsx +45 -0
- package/src/prerender/param-hash.ts +37 -0
- package/src/prerender/store.ts +185 -0
- package/src/prerender.ts +463 -0
- package/src/reverse.ts +330 -0
- package/src/root-error-boundary.tsx +289 -0
- package/src/route-content-wrapper.tsx +196 -0
- package/src/route-definition/dsl-helpers.ts +934 -0
- package/src/route-definition/helper-factories.ts +200 -0
- package/src/route-definition/helpers-types.ts +430 -0
- package/src/route-definition/index.ts +52 -0
- package/src/route-definition/redirect.ts +93 -0
- package/src/route-definition.ts +1 -0
- package/src/route-map-builder.ts +275 -0
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +259 -0
- package/src/router/content-negotiation.ts +116 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/find-match.ts +158 -0
- package/src/router/handler-context.ts +451 -0
- package/src/router/intercept-resolution.ts +395 -0
- package/src/router/lazy-includes.ts +234 -0
- package/src/router/loader-resolution.ts +420 -0
- package/src/router/logging.ts +248 -0
- package/src/router/manifest.ts +267 -0
- package/src/router/match-api.ts +620 -0
- package/src/router/match-context.ts +266 -0
- package/src/router/match-handlers.ts +440 -0
- package/src/router/match-middleware/background-revalidation.ts +223 -0
- package/src/router/match-middleware/cache-lookup.ts +634 -0
- package/src/router/match-middleware/cache-store.ts +295 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +306 -0
- package/src/router/match-middleware/segment-resolution.ts +192 -0
- package/src/router/match-pipelines.ts +179 -0
- package/src/router/match-result.ts +219 -0
- package/src/router/metrics.ts +282 -0
- package/src/router/middleware-cookies.ts +55 -0
- package/src/router/middleware-types.ts +222 -0
- package/src/router/middleware.ts +748 -0
- package/src/router/pattern-matching.ts +563 -0
- package/src/router/prerender-match.ts +402 -0
- package/src/router/preview-match.ts +170 -0
- package/src/router/revalidation.ts +289 -0
- package/src/router/router-context.ts +316 -0
- package/src/router/router-interfaces.ts +452 -0
- package/src/router/router-options.ts +592 -0
- package/src/router/router-registry.ts +24 -0
- package/src/router/segment-resolution/fresh.ts +570 -0
- package/src/router/segment-resolution/helpers.ts +263 -0
- package/src/router/segment-resolution/loader-cache.ts +198 -0
- package/src/router/segment-resolution/revalidation.ts +1239 -0
- package/src/router/segment-resolution/static-store.ts +67 -0
- package/src/router/segment-resolution.ts +21 -0
- package/src/router/segment-wrappers.ts +289 -0
- package/src/router/telemetry-otel.ts +299 -0
- package/src/router/telemetry.ts +300 -0
- package/src/router/timeout.ts +148 -0
- package/src/router/trie-matching.ts +239 -0
- package/src/router/types.ts +170 -0
- package/src/router.ts +1002 -0
- package/src/rsc/handler-context.ts +45 -0
- package/src/rsc/handler.ts +1089 -0
- package/src/rsc/helpers.ts +198 -0
- package/src/rsc/index.ts +36 -0
- package/src/rsc/loader-fetch.ts +209 -0
- package/src/rsc/manifest-init.ts +86 -0
- package/src/rsc/nonce.ts +32 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +379 -0
- package/src/rsc/response-error.ts +37 -0
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +235 -0
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +348 -0
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +263 -0
- package/src/search-params.ts +230 -0
- package/src/segment-system.tsx +454 -0
- package/src/server/context.ts +591 -0
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +37 -0
- package/src/server/handle-store.ts +308 -0
- package/src/server/loader-registry.ts +133 -0
- package/src/server/request-context.ts +914 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +51 -0
- package/src/ssr/index.tsx +365 -0
- package/src/static-handler.ts +114 -0
- package/src/theme/ThemeProvider.tsx +297 -0
- package/src/theme/ThemeScript.tsx +61 -0
- package/src/theme/constants.ts +62 -0
- package/src/theme/index.ts +48 -0
- package/src/theme/theme-context.ts +44 -0
- package/src/theme/theme-script.ts +155 -0
- package/src/theme/types.ts +182 -0
- package/src/theme/use-theme.ts +44 -0
- package/src/types/boundaries.ts +158 -0
- package/src/types/cache-types.ts +198 -0
- package/src/types/error-types.ts +192 -0
- package/src/types/global-namespace.ts +100 -0
- package/src/types/handler-context.ts +687 -0
- package/src/types/index.ts +88 -0
- package/src/types/loader-types.ts +183 -0
- package/src/types/route-config.ts +170 -0
- package/src/types/route-entry.ts +102 -0
- package/src/types/segments.ts +148 -0
- package/src/types.ts +1 -0
- package/src/urls/include-helper.ts +197 -0
- package/src/urls/index.ts +53 -0
- package/src/urls/path-helper-types.ts +339 -0
- package/src/urls/path-helper.ts +329 -0
- package/src/urls/pattern-types.ts +95 -0
- package/src/urls/response-types.ts +106 -0
- package/src/urls/type-extraction.ts +372 -0
- package/src/urls/urls-function.ts +98 -0
- package/src/urls.ts +1 -0
- package/src/use-loader.tsx +354 -0
- package/src/vite/discovery/bundle-postprocess.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +344 -0
- package/src/vite/discovery/prerender-collection.ts +385 -0
- package/src/vite/discovery/route-types-writer.ts +258 -0
- package/src/vite/discovery/self-gen-tracking.ts +47 -0
- package/src/vite/discovery/state.ts +110 -0
- package/src/vite/discovery/virtual-module-codegen.ts +203 -0
- package/src/vite/index.ts +16 -0
- package/src/vite/plugin-types.ts +131 -0
- package/src/vite/plugins/cjs-to-esm.ts +93 -0
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +105 -0
- package/src/vite/plugins/expose-action-id.ts +365 -0
- package/src/vite/plugins/expose-id-utils.ts +287 -0
- package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +179 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +569 -0
- package/src/vite/plugins/refresh-cmd.ts +65 -0
- package/src/vite/plugins/use-cache-transform.ts +323 -0
- package/src/vite/plugins/version-injector.ts +83 -0
- package/src/vite/plugins/version-plugin.ts +254 -0
- package/src/vite/plugins/version.d.ts +12 -0
- package/src/vite/plugins/virtual-entries.ts +123 -0
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +510 -0
- package/src/vite/router-discovery.ts +785 -0
- package/src/vite/utils/ast-handler-extract.ts +517 -0
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +137 -0
- package/src/vite/utils/manifest-utils.ts +70 -0
- package/src/vite/utils/package-resolution.ts +121 -0
- package/src/vite/utils/prerender-utils.ts +189 -0
- package/src/vite/utils/shared-utils.ts +169 -0
package/README.md
ADDED
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
# @rangojs/router
|
|
2
|
+
|
|
3
|
+
Named-route RSC router with structural composability and type-safe partial rendering for Vite.
|
|
4
|
+
|
|
5
|
+
> **Experimental:** This package is under active development. APIs may change between releases. Install with `@experimental` tag.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Named routes** — `reverse("blogPost", { slug })` for type-safe URL generation (Django-style)
|
|
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:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install react @vitejs/plugin-rsc
|
|
40
|
+
```
|
|
41
|
+
|
|
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
|
+
```ts
|
|
77
|
+
// vite.config.ts
|
|
78
|
+
import react from "@vitejs/plugin-react";
|
|
79
|
+
import { defineConfig } from "vite";
|
|
80
|
+
import { rango } from "@rangojs/router/vite";
|
|
81
|
+
|
|
82
|
+
export default defineConfig({
|
|
83
|
+
plugins: [react(), rango({ preset: "cloudflare" })],
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Router
|
|
88
|
+
|
|
89
|
+
This file is a server/RSC module and should import router construction APIs from
|
|
90
|
+
`@rangojs/router`.
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
// src/router.tsx
|
|
94
|
+
import { createRouter, urls } from "@rangojs/router";
|
|
95
|
+
import { Document } from "./document";
|
|
96
|
+
|
|
97
|
+
const blogPatterns = urls(({ path }) => [
|
|
98
|
+
path("/", BlogIndexPage, { name: "index" }),
|
|
99
|
+
path("/:slug", BlogPostPage, { name: "post" }),
|
|
100
|
+
]);
|
|
101
|
+
|
|
102
|
+
const urlpatterns = urls(({ path, include }) => [
|
|
103
|
+
path("/", HomePage, { name: "home" }),
|
|
104
|
+
include("/blog", blogPatterns, { name: "blog" }),
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
export const router = createRouter({ document: Document }).routes(urlpatterns);
|
|
108
|
+
|
|
109
|
+
// Export typed reverse function for URL generation by route name
|
|
110
|
+
export const reverse = router.reverse;
|
|
111
|
+
|
|
112
|
+
// reverse("blog.post", { slug: "hello-world" }) -> "/blog/hello-world"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Document
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
// src/document.tsx
|
|
119
|
+
"use client";
|
|
120
|
+
|
|
121
|
+
import type { ReactNode } from "react";
|
|
122
|
+
import { MetaTags } from "@rangojs/router/client";
|
|
123
|
+
|
|
124
|
+
export function Document({ children }: { children: ReactNode }) {
|
|
125
|
+
return (
|
|
126
|
+
<html lang="en">
|
|
127
|
+
<head>
|
|
128
|
+
<MetaTags />
|
|
129
|
+
</head>
|
|
130
|
+
<body>{children}</body>
|
|
131
|
+
</html>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Defining Routes
|
|
137
|
+
|
|
138
|
+
Rango is a named-route router first.
|
|
139
|
+
|
|
140
|
+
Paths define where a route lives. Names define how the app refers to it.
|
|
141
|
+
|
|
142
|
+
It is also structurally composable.
|
|
143
|
+
|
|
144
|
+
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.
|
|
145
|
+
|
|
146
|
+
### Named Routes
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
import { urls } from "@rangojs/router";
|
|
150
|
+
|
|
151
|
+
const urlpatterns = urls(({ path }) => [
|
|
152
|
+
path("/", HomePage, { name: "home" }),
|
|
153
|
+
path("/product/:slug", ProductPage, { name: "product" }),
|
|
154
|
+
path("/search/:query?", SearchPage, { name: "search" }),
|
|
155
|
+
path("/files/*", FilesPage, { name: "files" }),
|
|
156
|
+
]);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Use `reverse()` as the default way to link to routes:
|
|
160
|
+
|
|
161
|
+
```tsx
|
|
162
|
+
router.reverse("product", { slug: "widget" }); // "/product/widget"
|
|
163
|
+
router.reverse("search", undefined, { q: "rsc" }); // "/search?q=rsc"
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Composable URL Modules
|
|
167
|
+
|
|
168
|
+
Local route names compose cleanly with `include(..., { name })`:
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
import { urls } from "@rangojs/router";
|
|
172
|
+
|
|
173
|
+
export const blogPatterns = urls(({ path }) => [
|
|
174
|
+
path("/", BlogIndexPage, { name: "index" }),
|
|
175
|
+
path("/:slug", BlogPostPage, { name: "post" }),
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
export const urlpatterns = urls(({ path, include }) => [
|
|
179
|
+
path("/", HomePage, { name: "home" }),
|
|
180
|
+
include("/blog", blogPatterns, { name: "blog" }),
|
|
181
|
+
]);
|
|
182
|
+
|
|
183
|
+
router.reverse("blog.index"); // "/blog"
|
|
184
|
+
router.reverse("blog.post", { slug: "hello-world" }); // "/blog/hello-world"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
This is the core composition model:
|
|
188
|
+
|
|
189
|
+
- Paths stay local to the module that defines them
|
|
190
|
+
- Names become stable references across the app
|
|
191
|
+
- `include()` scales those names without forcing raw path-string coupling
|
|
192
|
+
|
|
193
|
+
### Structural Composability
|
|
194
|
+
|
|
195
|
+
Rango avoids the usual tradeoff between modularity and visibility.
|
|
196
|
+
|
|
197
|
+
You can extract route behavior into separate files or packages and still keep one readable route definition that shows the structure of the app.
|
|
198
|
+
|
|
199
|
+
```tsx
|
|
200
|
+
import { urls } from "@rangojs/router";
|
|
201
|
+
import { ProductPage } from "./routes/product";
|
|
202
|
+
import { ProductLoader } from "./loaders/product";
|
|
203
|
+
import { productMiddleware } from "./middleware/product";
|
|
204
|
+
import { productRevalidate } from "./revalidation/product";
|
|
205
|
+
|
|
206
|
+
const shopPatterns = urls(({ path, loader, middleware, revalidate, cache }) => [
|
|
207
|
+
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
208
|
+
middleware(productMiddleware),
|
|
209
|
+
loader(ProductLoader),
|
|
210
|
+
revalidate(productRevalidate),
|
|
211
|
+
cache({ ttl: 300 }),
|
|
212
|
+
]),
|
|
213
|
+
]);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The route tree stays explicit even when behavior is modular.
|
|
217
|
+
|
|
218
|
+
This applies to:
|
|
219
|
+
|
|
220
|
+
- external route modules mounted with `include()`
|
|
221
|
+
- imported loaders, middleware, and handles attached at the route site
|
|
222
|
+
- prerendering and static generation attached without turning the route tree opaque
|
|
223
|
+
|
|
224
|
+
### Loaders As the Live Data Layer
|
|
225
|
+
|
|
226
|
+
Rango separates app structure from app data.
|
|
227
|
+
|
|
228
|
+
Routes, layouts, and pre-rendered segments can be static or cached, while
|
|
229
|
+
loaders stay live by default and re-resolve at request time.
|
|
230
|
+
|
|
231
|
+
This means you can pre-render or cache the shell of a page without freezing its
|
|
232
|
+
data.
|
|
233
|
+
|
|
234
|
+
- `cache()` caches route structure and rendered UI segments
|
|
235
|
+
- `Prerender()` skips loaders at build time
|
|
236
|
+
- `loader()` provides fresh request-time data
|
|
237
|
+
- individual loaders can opt into caching explicitly when needed
|
|
238
|
+
|
|
239
|
+
```tsx
|
|
240
|
+
import { urls, Prerender } from "@rangojs/router";
|
|
241
|
+
import { ArticleLoader } from "./loaders/article";
|
|
242
|
+
|
|
243
|
+
const docsPatterns = urls(({ path, loader }) => [
|
|
244
|
+
path("/docs/:slug", Prerender(DocsArticle), { name: "docs.article" }, () => [
|
|
245
|
+
loader(ArticleLoader), // fresh by default
|
|
246
|
+
]),
|
|
247
|
+
]);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Pre-render the page, keep the data live.
|
|
251
|
+
|
|
252
|
+
### Typed Handlers
|
|
253
|
+
|
|
254
|
+
Route handlers receive a typed context with params, search params, and `reverse()`:
|
|
255
|
+
|
|
256
|
+
```tsx
|
|
257
|
+
import type { Handler } from "@rangojs/router";
|
|
258
|
+
|
|
259
|
+
export const ProductPage: Handler<"product"> = (ctx) => {
|
|
260
|
+
const { slug } = ctx.params; // typed from pattern
|
|
261
|
+
const homeUrl = ctx.reverse("home"); // type-safe URL by route name
|
|
262
|
+
return <h1>Product: {slug}</h1>;
|
|
263
|
+
};
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Choosing a Handler Style
|
|
267
|
+
|
|
268
|
+
All handler typing styles are supported, but they solve different problems:
|
|
269
|
+
|
|
270
|
+
- `Handler<"product">` — default for named app routes
|
|
271
|
+
- `Handler<".post", ScopedRouteMap<"blog">>` — best for reusable included modules
|
|
272
|
+
- `Handler<"/blog/:slug">` — good for unnamed or local-only extracted handlers
|
|
273
|
+
- `Handler<{ slug: string }>` — escape hatch for advanced or decoupled cases
|
|
274
|
+
|
|
275
|
+
Example of a scoped local name inside a mounted module:
|
|
276
|
+
|
|
277
|
+
```tsx
|
|
278
|
+
import type { Handler } from "@rangojs/router";
|
|
279
|
+
import type { ScopedRouteMap } from "@rangojs/router/__internal";
|
|
280
|
+
|
|
281
|
+
type BlogRoutes = ScopedRouteMap<"blog">;
|
|
282
|
+
|
|
283
|
+
export const BlogPostPage: Handler<".post", BlogRoutes> = (ctx) => {
|
|
284
|
+
return <a href={ctx.reverse(".index")}>Back to blog</a>;
|
|
285
|
+
};
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
See [`../../docs/named-routes.md`](../../docs/named-routes.md) for the recommended mental model.
|
|
289
|
+
|
|
290
|
+
### Search Params
|
|
291
|
+
|
|
292
|
+
Define a search schema on the route for type-safe search parameters:
|
|
293
|
+
|
|
294
|
+
```tsx
|
|
295
|
+
const urlpatterns = urls(({ path }) => [
|
|
296
|
+
path("/search", SearchPage, {
|
|
297
|
+
name: "search",
|
|
298
|
+
search: { q: "string", page: "number?", sort: "string?" },
|
|
299
|
+
}),
|
|
300
|
+
]);
|
|
301
|
+
|
|
302
|
+
// Handler receives typed search params via ctx.search
|
|
303
|
+
const SearchPage: Handler<"search"> = (ctx) => {
|
|
304
|
+
const { q, page, sort } = ctx.search;
|
|
305
|
+
// q: string, page: number | undefined, sort: string | undefined
|
|
306
|
+
};
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Trailing Slash Handling
|
|
310
|
+
|
|
311
|
+
Trailing slash behavior is a current `path()` feature.
|
|
312
|
+
|
|
313
|
+
Set it per route with `trailingSlash`:
|
|
314
|
+
|
|
315
|
+
```tsx
|
|
316
|
+
const urlpatterns = urls(({ path }) => [
|
|
317
|
+
path("/about", AboutPage, {
|
|
318
|
+
name: "about",
|
|
319
|
+
trailingSlash: "never",
|
|
320
|
+
}),
|
|
321
|
+
path("/docs/", DocsPage, {
|
|
322
|
+
name: "docs",
|
|
323
|
+
trailingSlash: "always",
|
|
324
|
+
}),
|
|
325
|
+
path("/webhook", WebhookHandler, {
|
|
326
|
+
name: "webhook",
|
|
327
|
+
trailingSlash: "ignore",
|
|
328
|
+
}),
|
|
329
|
+
]);
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Modes:
|
|
333
|
+
|
|
334
|
+
- `"never"` — canonical URL has no trailing slash, redirects `/about/` to `/about`
|
|
335
|
+
- `"always"` — canonical URL has a trailing slash, redirects `/docs` to `/docs/`
|
|
336
|
+
- `"ignore"` — matches both forms without redirect
|
|
337
|
+
|
|
338
|
+
Default behavior when `trailingSlash` is omitted:
|
|
339
|
+
|
|
340
|
+
- There is no separate global default mode
|
|
341
|
+
- If the pattern is defined without a trailing slash, the canonical URL is the no-slash form
|
|
342
|
+
- If the pattern is defined with a trailing slash, the canonical URL is the slash form
|
|
343
|
+
- The router redirects to the canonical form based on the pattern you defined
|
|
344
|
+
|
|
345
|
+
The recommended public API is the per-route `path(..., { trailingSlash })` option. Use `"ignore"` sparingly, especially on content pages, because `/x` and `/x/` are distinct URLs.
|
|
346
|
+
|
|
347
|
+
### Response Routes
|
|
348
|
+
|
|
349
|
+
Define API endpoints that bypass the RSC pipeline:
|
|
350
|
+
|
|
351
|
+
```tsx
|
|
352
|
+
const urlpatterns = urls(({ path }) => [
|
|
353
|
+
path.json("/api/health", () => ({ status: "ok" }), { name: "health" }),
|
|
354
|
+
path.text("/robots.txt", () => "User-agent: *\nAllow: /", { name: "robots" }),
|
|
355
|
+
path.xml("/feed.xml", () => "<rss>...</rss>", { name: "feed" }),
|
|
356
|
+
]);
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Response types available: `path.json()`, `path.text()`, `path.html()`, `path.xml()`, `path.image()`, `path.stream()`, `path.any()`.
|
|
360
|
+
|
|
361
|
+
## Layouts & Nesting
|
|
362
|
+
|
|
363
|
+
### Layouts with Outlet
|
|
364
|
+
|
|
365
|
+
```tsx
|
|
366
|
+
import { urls } from "@rangojs/router";
|
|
367
|
+
|
|
368
|
+
const urlpatterns = urls(({ path, layout }) => [
|
|
369
|
+
layout(<MainLayout />, () => [
|
|
370
|
+
path("/", HomePage, { name: "home" }),
|
|
371
|
+
path("/about", AboutPage, { name: "about" }),
|
|
372
|
+
]),
|
|
373
|
+
]);
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
```tsx
|
|
377
|
+
"use client";
|
|
378
|
+
import { Outlet } from "@rangojs/router/client";
|
|
379
|
+
|
|
380
|
+
function MainLayout() {
|
|
381
|
+
return (
|
|
382
|
+
<div>
|
|
383
|
+
<nav>...</nav>
|
|
384
|
+
<Outlet />
|
|
385
|
+
</div>
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Loading Skeletons
|
|
391
|
+
|
|
392
|
+
```tsx
|
|
393
|
+
const urlpatterns = urls(({ path, loading }) => [
|
|
394
|
+
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
395
|
+
loading(<ProductSkeleton />),
|
|
396
|
+
]),
|
|
397
|
+
]);
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Parallel Routes
|
|
401
|
+
|
|
402
|
+
```tsx
|
|
403
|
+
const urlpatterns = urls(({ path, layout, parallel, loader, loading }) => [
|
|
404
|
+
layout(BlogLayout, () => [
|
|
405
|
+
parallel({ "@sidebar": BlogSidebarHandler }, () => [
|
|
406
|
+
loader(BlogSidebarLoader),
|
|
407
|
+
loading(<SidebarSkeleton />),
|
|
408
|
+
]),
|
|
409
|
+
path("/blog", BlogIndexPage, { name: "blog" }),
|
|
410
|
+
path("/blog/:slug", BlogPostPage, { name: "blogPost" }),
|
|
411
|
+
]),
|
|
412
|
+
]);
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
## Data Loaders
|
|
416
|
+
|
|
417
|
+
### Creating a Loader
|
|
418
|
+
|
|
419
|
+
```tsx
|
|
420
|
+
import { createLoader } from "@rangojs/router";
|
|
421
|
+
|
|
422
|
+
export const BlogSidebarLoader = createLoader(async (ctx) => {
|
|
423
|
+
const posts = await db.getRecentPosts();
|
|
424
|
+
return { posts, loadedAt: new Date().toISOString() };
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Using in Server Components (Handlers)
|
|
429
|
+
|
|
430
|
+
```tsx
|
|
431
|
+
import type { HandlerContext } from "@rangojs/router";
|
|
432
|
+
import { BlogSidebarLoader } from "./loaders/blog";
|
|
433
|
+
|
|
434
|
+
async function BlogSidebarHandler(ctx: HandlerContext) {
|
|
435
|
+
const { posts } = await ctx.use(BlogSidebarLoader);
|
|
436
|
+
return (
|
|
437
|
+
<ul>
|
|
438
|
+
{posts.map((p) => (
|
|
439
|
+
<li key={p.slug}>{p.title}</li>
|
|
440
|
+
))}
|
|
441
|
+
</ul>
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
### Using in Client Components
|
|
447
|
+
|
|
448
|
+
```tsx
|
|
449
|
+
"use client";
|
|
450
|
+
import { useLoader } from "@rangojs/router/client";
|
|
451
|
+
import { BlogSidebarLoader } from "./loaders/blog";
|
|
452
|
+
|
|
453
|
+
function BlogSidebar() {
|
|
454
|
+
const { data } = useLoader(BlogSidebarLoader);
|
|
455
|
+
return (
|
|
456
|
+
<ul>
|
|
457
|
+
{data.posts.map((p) => (
|
|
458
|
+
<li key={p.slug}>{p.title}</li>
|
|
459
|
+
))}
|
|
460
|
+
</ul>
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
### Attaching Loaders to Routes
|
|
466
|
+
|
|
467
|
+
```tsx
|
|
468
|
+
const urlpatterns = urls(({ path, loader }) => [
|
|
469
|
+
path("/blog", BlogIndexPage, { name: "blog" }, () => [
|
|
470
|
+
loader(BlogSidebarLoader),
|
|
471
|
+
]),
|
|
472
|
+
]);
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
## Navigation & Links
|
|
476
|
+
|
|
477
|
+
### Named Routes with `reverse()` (Server Components)
|
|
478
|
+
|
|
479
|
+
In server components, use `reverse()` to generate URLs by route name:
|
|
480
|
+
|
|
481
|
+
```tsx
|
|
482
|
+
import { Link } from "@rangojs/router/client";
|
|
483
|
+
import { reverse } from "./router";
|
|
484
|
+
|
|
485
|
+
function BlogIndex() {
|
|
486
|
+
return (
|
|
487
|
+
<nav>
|
|
488
|
+
<Link to={reverse("home")}>Home</Link>
|
|
489
|
+
<Link to={reverse("blogPost", { slug: "my-post" })}>My Post</Link>
|
|
490
|
+
<Link to={reverse("about")}>About</Link>
|
|
491
|
+
</nav>
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
`reverse()` is type-safe — route names and required params are checked at compile time. Included routes use dotted names: `reverse("api.health")`.
|
|
497
|
+
|
|
498
|
+
Handlers also have `ctx.reverse()` directly on the context:
|
|
499
|
+
|
|
500
|
+
```tsx
|
|
501
|
+
const BlogPostPage: Handler<"blogPost"> = (ctx) => {
|
|
502
|
+
const backUrl = ctx.reverse("blog");
|
|
503
|
+
return <Link to={backUrl}>Back to blog</Link>;
|
|
504
|
+
};
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### `href()` for Path Validation (Client Components)
|
|
508
|
+
|
|
509
|
+
In client components, use `href()` for compile-time path validation:
|
|
510
|
+
|
|
511
|
+
```tsx
|
|
512
|
+
"use client";
|
|
513
|
+
import { Link, href } from "@rangojs/router/client";
|
|
514
|
+
|
|
515
|
+
function Nav() {
|
|
516
|
+
return (
|
|
517
|
+
<nav>
|
|
518
|
+
<Link to={href("/")}>Home</Link>
|
|
519
|
+
<Link to={href("/blog")} prefetch="adaptive">
|
|
520
|
+
Blog
|
|
521
|
+
</Link>
|
|
522
|
+
<Link to={href("/about")}>About</Link>
|
|
523
|
+
</nav>
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
`href()` validates that the path matches a registered route pattern at compile time (e.g. `/blog/my-post` matches `/blog/:slug`).
|
|
529
|
+
|
|
530
|
+
### Navigation Hooks
|
|
531
|
+
|
|
532
|
+
```tsx
|
|
533
|
+
"use client";
|
|
534
|
+
import { useNavigation, useRouter } from "@rangojs/router/client";
|
|
535
|
+
|
|
536
|
+
function SearchForm() {
|
|
537
|
+
const router = useRouter();
|
|
538
|
+
const nav = useNavigation();
|
|
539
|
+
|
|
540
|
+
function handleSubmit(query: string) {
|
|
541
|
+
router.push(`/search?q=${encodeURIComponent(query)}`);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return <form onSubmit={...}>{nav.state !== "idle" && <Spinner />}</form>;
|
|
545
|
+
}
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
### Scroll Restoration
|
|
549
|
+
|
|
550
|
+
```tsx
|
|
551
|
+
"use client";
|
|
552
|
+
import { ScrollRestoration } from "@rangojs/router/client";
|
|
553
|
+
|
|
554
|
+
function Document({ children }) {
|
|
555
|
+
return (
|
|
556
|
+
<html>
|
|
557
|
+
<body>
|
|
558
|
+
{children}
|
|
559
|
+
<ScrollRestoration />
|
|
560
|
+
</body>
|
|
561
|
+
</html>
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
## Includes (Composable Modules)
|
|
567
|
+
|
|
568
|
+
Split URL patterns into composable modules with `include()`:
|
|
569
|
+
|
|
570
|
+
```tsx
|
|
571
|
+
// src/api/urls.tsx
|
|
572
|
+
import { urls } from "@rangojs/router";
|
|
573
|
+
|
|
574
|
+
export const apiPatterns = urls(({ path }) => [
|
|
575
|
+
path.json("/health", () => ({ status: "ok" }), { name: "health" }),
|
|
576
|
+
path.json("/products", getProducts, { name: "products" }),
|
|
577
|
+
]);
|
|
578
|
+
|
|
579
|
+
// src/urls.tsx
|
|
580
|
+
import { urls } from "@rangojs/router";
|
|
581
|
+
import { apiPatterns } from "./api/urls";
|
|
582
|
+
|
|
583
|
+
export const urlpatterns = urls(({ path, include }) => [
|
|
584
|
+
path("/", HomePage, { name: "home" }),
|
|
585
|
+
include("/api", apiPatterns, { name: "api" }),
|
|
586
|
+
// Mounts apiPatterns under /api: /api/health, /api/products
|
|
587
|
+
]);
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
Included route names are prefixed with the include name: `reverse("api.health")`, `reverse("api.products")`.
|
|
591
|
+
|
|
592
|
+
### Include name scoping
|
|
593
|
+
|
|
594
|
+
The `name` option controls how child route names appear globally:
|
|
595
|
+
|
|
596
|
+
| Form | Child names | Generated types | Reverse resolution |
|
|
597
|
+
| ---------------------------------- | ------------------- | ---------------------- | -------------------------------------------------------------------- |
|
|
598
|
+
| `include("/x", p, { name: "ns" })` | `ns.child` | Exported as `ns.child` | `reverse("ns.child")` globally, `reverse(".child")` inside |
|
|
599
|
+
| `include("/x", p, { name: "" })` | `child` (flattened) | Exported as-is | `reverse("child")` globally, `reverse(".child")` inside (root-scope) |
|
|
600
|
+
| `include("/x", p)` | Private scope | Not exported | `reverse(".child")` inside only |
|
|
601
|
+
|
|
602
|
+
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.
|
|
603
|
+
|
|
604
|
+
**`{ 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.
|
|
605
|
+
|
|
606
|
+
## Middleware
|
|
607
|
+
|
|
608
|
+
```tsx
|
|
609
|
+
const urlpatterns = urls(({ path, middleware }) => [
|
|
610
|
+
middleware(
|
|
611
|
+
async (ctx, next) => {
|
|
612
|
+
const start = Date.now();
|
|
613
|
+
const response = await next();
|
|
614
|
+
console.log(
|
|
615
|
+
`${ctx.request.method} ${ctx.url.pathname} ${Date.now() - start}ms`,
|
|
616
|
+
);
|
|
617
|
+
return response;
|
|
618
|
+
},
|
|
619
|
+
() => [path("/dashboard", DashboardPage, { name: "dashboard" })],
|
|
620
|
+
),
|
|
621
|
+
]);
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
## Caching
|
|
625
|
+
|
|
626
|
+
### Route-Level Caching
|
|
627
|
+
|
|
628
|
+
```tsx
|
|
629
|
+
const urlpatterns = urls(({ path, cache }) => [
|
|
630
|
+
cache({ ttl: 60, swr: 300 }, () => [
|
|
631
|
+
path("/blog", BlogIndexPage, { name: "blog" }),
|
|
632
|
+
path("/blog/:slug", BlogPostPage, { name: "blogPost" }),
|
|
633
|
+
]),
|
|
634
|
+
]);
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
### Cache Store Configuration
|
|
638
|
+
|
|
639
|
+
```tsx
|
|
640
|
+
import { createRouter } from "@rangojs/router";
|
|
641
|
+
import {
|
|
642
|
+
CFCacheStore,
|
|
643
|
+
createDocumentCacheMiddleware,
|
|
644
|
+
} from "@rangojs/router/cache";
|
|
645
|
+
|
|
646
|
+
export const router = createRouter({
|
|
647
|
+
document: Document,
|
|
648
|
+
cache: (env) => ({
|
|
649
|
+
store: new CFCacheStore({
|
|
650
|
+
defaults: { ttl: 60, swr: 300 },
|
|
651
|
+
ctx: env.ctx,
|
|
652
|
+
}),
|
|
653
|
+
}),
|
|
654
|
+
})
|
|
655
|
+
.use(createDocumentCacheMiddleware())
|
|
656
|
+
.routes(urlpatterns);
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
Available cache stores:
|
|
660
|
+
|
|
661
|
+
- `CFCacheStore` — Cloudflare edge cache (production)
|
|
662
|
+
- `MemorySegmentCacheStore` — In-memory cache (development/testing)
|
|
663
|
+
|
|
664
|
+
## Pre-rendering
|
|
665
|
+
|
|
666
|
+
Pre-rendering generates route segments at build time. The worker handles all requests — there are no static files served from assets.
|
|
667
|
+
|
|
668
|
+
### Static Segments
|
|
669
|
+
|
|
670
|
+
Use `Static()` for segments rendered once at build time (no params). Works on `path()`, `layout()`, and `parallel()`:
|
|
671
|
+
|
|
672
|
+
```tsx
|
|
673
|
+
import { Static } from "@rangojs/router";
|
|
674
|
+
|
|
675
|
+
export const AboutPage = Static(async () => {
|
|
676
|
+
return <article>...</article>;
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
export const DocsNav = Static(async () => {
|
|
680
|
+
const items = await readDocsNavItems();
|
|
681
|
+
return (
|
|
682
|
+
<nav>
|
|
683
|
+
{items.map((i) => (
|
|
684
|
+
<a key={i.slug} href={i.slug}>
|
|
685
|
+
{i.title}
|
|
686
|
+
</a>
|
|
687
|
+
))}
|
|
688
|
+
</nav>
|
|
689
|
+
);
|
|
690
|
+
});
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
### Dynamic Routes with Prerender
|
|
694
|
+
|
|
695
|
+
Use `Prerender()` for route-scoped pre-rendering. With params, provide `getParams` first, handler second:
|
|
696
|
+
|
|
697
|
+
```tsx
|
|
698
|
+
import { Prerender } from "@rangojs/router";
|
|
699
|
+
|
|
700
|
+
export const BlogPost = Prerender(
|
|
701
|
+
async () => {
|
|
702
|
+
const slugs = await getAllBlogSlugs();
|
|
703
|
+
return slugs.map((slug) => ({ slug }));
|
|
704
|
+
},
|
|
705
|
+
async (ctx) => {
|
|
706
|
+
const post = await getPost(ctx.params.slug);
|
|
707
|
+
return <article>{post.content}</article>;
|
|
708
|
+
},
|
|
709
|
+
);
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
### Passthrough for Unknown Params
|
|
713
|
+
|
|
714
|
+
```tsx
|
|
715
|
+
import { Prerender } from "@rangojs/router";
|
|
716
|
+
|
|
717
|
+
export const ProductPage = Prerender(
|
|
718
|
+
async () => {
|
|
719
|
+
const featured = await db.getFeaturedProducts();
|
|
720
|
+
return featured.map((p) => ({ id: p.id }));
|
|
721
|
+
},
|
|
722
|
+
async (ctx) => {
|
|
723
|
+
const product = await db.getProduct(ctx.params.id);
|
|
724
|
+
return <Product data={product} />;
|
|
725
|
+
},
|
|
726
|
+
{ passthrough: true },
|
|
727
|
+
);
|
|
728
|
+
```
|
|
729
|
+
|
|
730
|
+
With `passthrough: true`, known params are served from the build-time cache and unknown params fall through to live rendering.
|
|
731
|
+
|
|
732
|
+
Handlers can also skip individual param sets with `ctx.passthrough()`, deferring them to the live handler at runtime:
|
|
733
|
+
|
|
734
|
+
```tsx
|
|
735
|
+
export const ProductPage = Prerender(
|
|
736
|
+
async () => {
|
|
737
|
+
const all = await db.getAllProducts();
|
|
738
|
+
return all.map((p) => ({ id: p.id }));
|
|
739
|
+
},
|
|
740
|
+
async (ctx) => {
|
|
741
|
+
const product = await db.getProduct(ctx.params.id);
|
|
742
|
+
if (!product.published) return ctx.passthrough();
|
|
743
|
+
return <Product data={product} />;
|
|
744
|
+
},
|
|
745
|
+
{ passthrough: true },
|
|
746
|
+
);
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
## Theme
|
|
750
|
+
|
|
751
|
+
### Router Configuration
|
|
752
|
+
|
|
753
|
+
```tsx
|
|
754
|
+
export const router = createRouter({
|
|
755
|
+
document: Document,
|
|
756
|
+
theme: {
|
|
757
|
+
defaultTheme: "light",
|
|
758
|
+
themes: ["light", "dark", "system"],
|
|
759
|
+
attribute: "class",
|
|
760
|
+
enableSystem: true,
|
|
761
|
+
},
|
|
762
|
+
}).routes(urlpatterns);
|
|
763
|
+
```
|
|
764
|
+
|
|
765
|
+
### Theme Toggle
|
|
766
|
+
|
|
767
|
+
```tsx
|
|
768
|
+
"use client";
|
|
769
|
+
import { useTheme } from "@rangojs/router/theme";
|
|
770
|
+
|
|
771
|
+
function ThemeToggle() {
|
|
772
|
+
const { theme, setTheme, themes } = useTheme();
|
|
773
|
+
return (
|
|
774
|
+
<select value={theme} onChange={(e) => setTheme(e.target.value)}>
|
|
775
|
+
{themes.map((t) => (
|
|
776
|
+
<option key={t}>{t}</option>
|
|
777
|
+
))}
|
|
778
|
+
</select>
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
## Host Routing
|
|
784
|
+
|
|
785
|
+
Route requests to different apps based on domain/subdomain patterns using `@rangojs/router/host`:
|
|
786
|
+
|
|
787
|
+
```tsx
|
|
788
|
+
// worker.rsc.tsx
|
|
789
|
+
import { createHostRouter } from "@rangojs/router/host";
|
|
790
|
+
|
|
791
|
+
const hostRouter = createHostRouter();
|
|
792
|
+
|
|
793
|
+
hostRouter.host(["*.localhost"]).map(() => import("./apps/admin/handler.js"));
|
|
794
|
+
hostRouter.host(["localhost"]).map(() => import("./apps/site/handler.js"));
|
|
795
|
+
hostRouter.fallback().map(() => import("./apps/site/handler.js"));
|
|
796
|
+
|
|
797
|
+
export default {
|
|
798
|
+
async fetch(request, env, ctx) {
|
|
799
|
+
return hostRouter.match(request, { env, ctx });
|
|
800
|
+
},
|
|
801
|
+
};
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
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.
|
|
805
|
+
|
|
806
|
+
## Meta Tags
|
|
807
|
+
|
|
808
|
+
Accumulate meta tags across route segments using the built-in `Meta` handle:
|
|
809
|
+
|
|
810
|
+
```tsx
|
|
811
|
+
import { Meta } from "@rangojs/router";
|
|
812
|
+
import type { HandlerContext } from "@rangojs/router";
|
|
813
|
+
|
|
814
|
+
export function BlogPostPage(ctx: HandlerContext) {
|
|
815
|
+
const meta = ctx.use(Meta);
|
|
816
|
+
meta({ title: "My Blog Post" });
|
|
817
|
+
meta({ name: "description", content: "A great blog post" });
|
|
818
|
+
meta({ property: "og:title", content: "My Blog Post" });
|
|
819
|
+
|
|
820
|
+
return <article>...</article>;
|
|
821
|
+
}
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
Render collected tags in the document with `<MetaTags />` from `@rangojs/router/client`.
|
|
825
|
+
|
|
826
|
+
## CLI: `rango generate`
|
|
827
|
+
|
|
828
|
+
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`):
|
|
829
|
+
|
|
830
|
+
```bash
|
|
831
|
+
npx rango generate src/router.tsx
|
|
832
|
+
npx rango generate src/ # recursive scan
|
|
833
|
+
npx rango generate src/urls.tsx src/api/ # mix files and directories
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
Auto-detects file type:
|
|
837
|
+
|
|
838
|
+
- Files with `createRouter` → `*.named-routes.gen.ts` with global route map
|
|
839
|
+
- Files with `urls()` → `*.gen.ts` with per-module route names, params, and search types
|
|
840
|
+
|
|
841
|
+
## Type Safety
|
|
842
|
+
|
|
843
|
+
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.
|
|
844
|
+
|
|
845
|
+
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.
|
|
846
|
+
|
|
847
|
+
```typescript
|
|
848
|
+
// router.tsx
|
|
849
|
+
const router = createRouter<AppBindings>({}).routes(urlpatterns);
|
|
850
|
+
|
|
851
|
+
declare global {
|
|
852
|
+
namespace RSCRouter {
|
|
853
|
+
interface Env extends AppEnv {}
|
|
854
|
+
interface Vars extends AppVars {}
|
|
855
|
+
interface RegisteredRoutes extends typeof router.routeMap {}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
Quick rule of thumb:
|
|
861
|
+
|
|
862
|
+
- `GeneratedRouteMap` (auto-generated) — use for server-side named-route typing: `Handler<"name">`, `ctx.reverse()`, `Prerender<"name">`
|
|
863
|
+
- `typeof router.routeMap` — use when you need route entries with response metadata
|
|
864
|
+
- `RegisteredRoutes` (manual augmentation) — use to expose `typeof router.routeMap` globally for `href()`, `PathResponse`, `ValidPaths`, and other path/response-aware utilities
|
|
865
|
+
|
|
866
|
+
For extracted reusable loaders or middleware, prefer global dotted names on
|
|
867
|
+
`ctx.reverse()` by default. If you want type-safe local names for a specific
|
|
868
|
+
module, use `scopedReverse<typeof localPatterns>(ctx.reverse)` or
|
|
869
|
+
`scopedReverse<routes>(ctx.reverse)` with a generated local route type.
|
|
870
|
+
|
|
871
|
+
## Subpath Exports
|
|
872
|
+
|
|
873
|
+
| Export | Description |
|
|
874
|
+
| ------------------------ | -------------------------------------------------------------------------------------------------------- |
|
|
875
|
+
| `@rangojs/router` | Server/RSC core and shared types: `createRouter`, `urls`, `createLoader`, `Handler`, `Prerender`, `Meta` |
|
|
876
|
+
| `@rangojs/router/client` | Client: `Link`, `Outlet`, `href`, `useNavigation`, `useLoader`, `MetaTags` |
|
|
877
|
+
| `@rangojs/router/cache` | Cache: `CFCacheStore`, `MemorySegmentCacheStore`, `createDocumentCacheMiddleware` |
|
|
878
|
+
| `@rangojs/router/theme` | Theme: `useTheme`, `ThemeProvider`, `ThemeScript` |
|
|
879
|
+
| `@rangojs/router/host` | Host routing: `createHostRouter`, `defineHosts` |
|
|
880
|
+
| `@rangojs/router/vite` | Vite plugin: `rango()` |
|
|
881
|
+
| `@rangojs/router/rsc` | Advanced server pipeline APIs: `createRSCHandler`, request-context access |
|
|
882
|
+
| `@rangojs/router/ssr` | Advanced SSR bridge APIs: `createSSRHandler` |
|
|
883
|
+
| `@rangojs/router/server` | Internal build/runtime utilities for advanced integrations |
|
|
884
|
+
| `@rangojs/router/build` | Build utilities |
|
|
885
|
+
|
|
886
|
+
The root entrypoint is not a generic client/runtime barrel. If you need hooks
|
|
887
|
+
or components, import from `@rangojs/router/client`; if you need cache or host
|
|
888
|
+
APIs, use their dedicated subpaths.
|
|
889
|
+
|
|
890
|
+
## Examples
|
|
891
|
+
|
|
892
|
+
See the `examples/` directory for full working applications:
|
|
893
|
+
|
|
894
|
+
- [`cloudflare-basic`](../../examples/cloudflare-basic) — Cloudflare Workers with caching, loaders, theme, and pre-rendering
|
|
895
|
+
- [`cloudflare-multi-router`](../../examples/cloudflare-multi-router) — Multi-app host routing
|
|
896
|
+
|
|
897
|
+
## License
|
|
898
|
+
|
|
899
|
+
MIT
|