@rangojs/router 0.0.0-experimental.18.d80c2f4.20260228 → 0.0.0-experimental.1b930379
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 +296 -29
- package/dist/bin/rango.js +355 -47
- package/dist/vite/index.js +1947 -1561
- package/package.json +3 -6
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +9 -5
- package/skills/caching/SKILL.md +8 -8
- package/skills/document-cache/SKILL.md +10 -10
- package/skills/hooks/SKILL.md +221 -48
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +83 -4
- package/skills/layout/SKILL.md +62 -2
- package/skills/loader/SKILL.md +362 -35
- package/skills/middleware/SKILL.md +124 -44
- package/skills/parallel/SKILL.md +58 -3
- package/skills/prerender/SKILL.md +189 -19
- package/skills/rango/SKILL.md +1 -2
- package/skills/response-routes/SKILL.md +11 -10
- package/skills/route/SKILL.md +47 -7
- package/skills/router-setup/SKILL.md +103 -15
- package/skills/theme/SKILL.md +5 -4
- package/skills/typesafety/SKILL.md +130 -71
- package/skills/use-cache/SKILL.md +16 -2
- package/src/__internal.ts +93 -1
- package/src/bin/rango.ts +56 -19
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/event-controller.ts +53 -48
- package/src/browser/history-state.ts +80 -0
- package/src/browser/intercept-utils.ts +1 -1
- package/src/browser/link-interceptor.ts +23 -3
- package/src/browser/merge-segment-loaders.ts +9 -2
- package/src/browser/navigation-bridge.ts +163 -444
- package/src/browser/navigation-client.ts +99 -89
- package/src/browser/navigation-store.ts +4 -33
- package/src/browser/navigation-transaction.ts +295 -0
- package/src/browser/partial-update.ts +103 -147
- package/src/browser/prefetch/cache.ts +154 -0
- package/src/browser/prefetch/fetch.ts +135 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +48 -0
- package/src/browser/prefetch/queue.ts +88 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +165 -68
- package/src/browser/react/NavigationProvider.tsx +35 -0
- package/src/browser/react/context.ts +6 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +11 -11
- package/src/browser/react/location-state-shared.ts +29 -11
- package/src/browser/react/location-state.ts +6 -4
- 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 +23 -45
- package/src/browser/react/use-client-cache.ts +5 -3
- package/src/browser/react/use-handle.ts +21 -64
- package/src/browser/react/use-navigation.ts +15 -62
- 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 +75 -93
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +57 -21
- package/src/browser/scroll-restoration.ts +10 -7
- package/src/browser/server-action-bridge.ts +462 -405
- package/src/browser/types.ts +81 -35
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +38 -13
- package/src/build/generate-route-types.ts +4 -0
- package/src/build/index.ts +1 -0
- package/src/build/route-trie.ts +19 -3
- package/src/build/route-types/codegen.ts +13 -4
- package/src/build/route-types/include-resolution.ts +13 -0
- package/src/build/route-types/per-module-writer.ts +15 -3
- package/src/build/route-types/router-processing.ts +170 -18
- package/src/build/runtime-discovery.ts +13 -1
- 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 +136 -123
- package/src/cache/cache-scope.ts +76 -83
- package/src/cache/cf/cf-cache-store.ts +12 -7
- package/src/cache/document-cache.ts +93 -69
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/index.ts +0 -15
- package/src/cache/memory-segment-store.ts +43 -69
- package/src/cache/profile-registry.ts +43 -8
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +140 -117
- package/src/cache/taint.ts +30 -3
- package/src/cache/types.ts +1 -115
- package/src/client.rsc.tsx +2 -1
- package/src/client.tsx +64 -81
- package/src/errors.ts +6 -1
- package/src/handle.ts +1 -1
- package/src/handles/MetaTags.tsx +5 -2
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/host/cookie-handler.ts +11 -5
- package/src/host/index.ts +0 -3
- package/src/host/router.ts +34 -17
- package/src/host/types.ts +14 -6
- package/src/href-client.ts +3 -1
- package/src/index.rsc.ts +47 -35
- package/src/index.ts +62 -65
- package/src/loader.rsc.ts +14 -35
- package/src/loader.ts +10 -2
- package/src/prerender/store.ts +60 -18
- package/src/prerender.ts +76 -18
- package/src/reverse.ts +11 -7
- package/src/root-error-boundary.tsx +30 -26
- package/src/route-definition/dsl-helpers.ts +9 -6
- package/src/route-definition/index.ts +0 -3
- package/src/route-definition/redirect.ts +15 -3
- package/src/route-map-builder.ts +38 -2
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +7 -0
- package/src/router/content-negotiation.ts +1 -1
- package/src/router/debug-manifest.ts +16 -3
- package/src/router/error-handling.ts +2 -2
- package/src/router/handler-context.ts +145 -38
- package/src/router/intercept-resolution.ts +6 -4
- package/src/router/lazy-includes.ts +4 -0
- package/src/router/loader-resolution.ts +13 -6
- package/src/router/logging.ts +100 -3
- package/src/router/manifest.ts +32 -3
- package/src/router/match-api.ts +85 -86
- package/src/router/match-context.ts +4 -2
- package/src/router/match-handlers.ts +185 -11
- package/src/router/match-middleware/background-revalidation.ts +65 -85
- package/src/router/match-middleware/cache-lookup.ts +78 -10
- package/src/router/match-middleware/cache-store.ts +3 -1
- package/src/router/match-pipelines.ts +8 -43
- package/src/router/match-result.ts +0 -9
- package/src/router/metrics.ts +233 -13
- package/src/router/middleware-types.ts +76 -64
- package/src/router/middleware.ts +267 -141
- package/src/router/pattern-matching.ts +61 -10
- package/src/router/prerender-match.ts +48 -6
- package/src/router/preview-match.ts +10 -1
- package/src/router/revalidation.ts +87 -8
- package/src/router/router-context.ts +15 -0
- package/src/router/router-interfaces.ts +185 -48
- package/src/router/router-options.ts +237 -6
- package/src/router/router-registry.ts +5 -2
- package/src/router/segment-resolution/fresh.ts +169 -244
- package/src/router/segment-resolution/helpers.ts +263 -0
- package/src/router/segment-resolution/loader-cache.ts +102 -98
- package/src/router/segment-resolution/revalidation.ts +394 -273
- package/src/router/segment-resolution/static-store.ts +2 -2
- package/src/router/segment-resolution.ts +1 -3
- package/src/router/segment-wrappers.ts +3 -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 +20 -2
- package/src/router/types.ts +7 -1
- package/src/router.ts +252 -32
- package/src/rsc/handler-context.ts +13 -2
- package/src/rsc/handler.ts +511 -426
- package/src/rsc/helpers.ts +139 -5
- package/src/rsc/index.ts +0 -20
- package/src/rsc/loader-fetch.ts +97 -48
- package/src/rsc/manifest-init.ts +3 -2
- package/src/rsc/nonce.ts +14 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +245 -19
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +67 -54
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +195 -84
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +26 -3
- package/src/search-params.ts +38 -23
- package/src/server/context.ts +61 -7
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +11 -6
- package/src/server/handle-store.ts +84 -12
- package/src/server/loader-registry.ts +11 -46
- package/src/server/request-context.ts +314 -41
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +68 -25
- package/src/static-handler.ts +7 -0
- package/src/theme/ThemeProvider.tsx +6 -1
- package/src/theme/index.ts +4 -18
- package/src/theme/theme-context.ts +1 -28
- package/src/theme/theme-script.ts +2 -1
- package/src/types/cache-types.ts +6 -1
- package/src/types/error-types.ts +3 -0
- package/src/types/global-namespace.ts +40 -11
- package/src/types/handler-context.ts +122 -43
- package/src/types/index.ts +1 -4
- package/src/types/loader-types.ts +25 -42
- package/src/types/route-config.ts +17 -36
- package/src/types/route-entry.ts +28 -0
- package/src/types/segments.ts +0 -5
- package/src/urls/include-helper.ts +49 -8
- package/src/urls/index.ts +1 -0
- package/src/urls/path-helper-types.ts +30 -12
- package/src/urls/path-helper.ts +17 -2
- package/src/urls/pattern-types.ts +21 -1
- package/src/urls/response-types.ts +32 -11
- package/src/urls/type-extraction.ts +23 -15
- package/src/use-loader.tsx +75 -103
- package/src/vite/discovery/bundle-postprocess.ts +32 -52
- package/src/vite/discovery/discover-routers.ts +52 -26
- package/src/vite/discovery/prerender-collection.ts +58 -41
- package/src/vite/discovery/route-types-writer.ts +7 -7
- package/src/vite/discovery/state.ts +7 -7
- package/src/vite/discovery/virtual-module-codegen.ts +5 -2
- package/src/vite/index.ts +10 -51
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +3 -3
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -21
- package/src/vite/plugins/expose-internal-ids.ts +4 -3
- package/src/vite/plugins/refresh-cmd.ts +65 -0
- package/src/vite/plugins/use-cache-transform.ts +91 -3
- package/src/vite/plugins/version-plugin.ts +188 -18
- package/src/vite/rango.ts +61 -36
- package/src/vite/router-discovery.ts +173 -100
- package/src/vite/utils/package-resolution.ts +24 -28
- package/src/vite/utils/prerender-utils.ts +81 -0
- package/src/vite/utils/shared-utils.ts +19 -9
- package/CLAUDE.md +0 -43
- package/skills/testing/SKILL.md +0 -226
- package/src/browser/lru-cache.ts +0 -61
- package/src/browser/request-controller.ts +0 -164
- package/src/cache/memory-store.ts +0 -253
- package/src/fetchable-loader-action.ts +0 -30
- package/src/href-context.ts +0 -33
- package/src/loader-action-shared.ts +0 -104
- package/src/route-definition/route-function.ts +0 -119
- package/src/router.gen.ts +0 -6
- package/src/static-handler.gen.ts +0 -5
- package/src/urls.gen.ts +0 -8
package/AGENTS.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @rangojs/router
|
|
2
|
+
|
|
3
|
+
A file-system based React Server Components router.
|
|
4
|
+
|
|
5
|
+
Run `/rango` to understand the API. Detailed guides for each feature are in the `skills/` directory (e.g. `node_modules/@rangojs/router/skills/loader`, `skills/caching`, `skills/middleware`, etc.).
|
package/README.md
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
# @rangojs/router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Named-route RSC router with structural composability and type-safe partial rendering for Vite.
|
|
4
4
|
|
|
5
5
|
> **Experimental:** This package is under active development. APIs may change between releases. Install with `@experimental` tag.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
- **Composable URL patterns** — Django-style `urls()` DSL with `path`, `layout`, `include`
|
|
10
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`
|
|
11
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
|
|
12
14
|
- **Layouts & nesting** — Nested layouts with `<Outlet />` and parallel routes
|
|
13
15
|
- **Segment-level caching** — `cache()` DSL with TTL/SWR and pluggable cache stores
|
|
14
16
|
- **Middleware** — Route-level middleware with cookie and header access
|
|
@@ -16,8 +18,15 @@ Django-inspired RSC router with type-safe partial rendering for Vite.
|
|
|
16
18
|
- **Theme support** — Light/dark mode with FOUC prevention and system detection
|
|
17
19
|
- **Host routing** — Multi-app routing by domain/subdomain via `@rangojs/router/host`
|
|
18
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"`
|
|
19
22
|
- **CLI codegen** — `rango generate` for route type generation
|
|
20
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
|
+
|
|
21
30
|
## Installation
|
|
22
31
|
|
|
23
32
|
```bash
|
|
@@ -36,6 +45,30 @@ For Cloudflare Workers:
|
|
|
36
45
|
npm install @cloudflare/vite-plugin
|
|
37
46
|
```
|
|
38
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
|
+
|
|
39
72
|
## Quick Start
|
|
40
73
|
|
|
41
74
|
### Vite Config
|
|
@@ -53,23 +86,30 @@ export default defineConfig({
|
|
|
53
86
|
|
|
54
87
|
### Router
|
|
55
88
|
|
|
89
|
+
This file is a server/RSC module and should import router construction APIs from
|
|
90
|
+
`@rangojs/router`.
|
|
91
|
+
|
|
56
92
|
```tsx
|
|
57
93
|
// src/router.tsx
|
|
58
94
|
import { createRouter, urls } from "@rangojs/router";
|
|
59
95
|
import { Document } from "./document";
|
|
60
96
|
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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" }),
|
|
67
105
|
]);
|
|
68
106
|
|
|
69
107
|
export const router = createRouter({ document: Document }).routes(urlpatterns);
|
|
70
108
|
|
|
71
109
|
// Export typed reverse function for URL generation by route name
|
|
72
110
|
export const reverse = router.reverse;
|
|
111
|
+
|
|
112
|
+
// reverse("blog.post", { slug: "hello-world" }) -> "/blog/hello-world"
|
|
73
113
|
```
|
|
74
114
|
|
|
75
115
|
### Document
|
|
@@ -95,7 +135,15 @@ export function Document({ children }: { children: ReactNode }) {
|
|
|
95
135
|
|
|
96
136
|
## Defining Routes
|
|
97
137
|
|
|
98
|
-
|
|
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
|
|
99
147
|
|
|
100
148
|
```tsx
|
|
101
149
|
import { urls } from "@rangojs/router";
|
|
@@ -108,6 +156,99 @@ const urlpatterns = urls(({ path }) => [
|
|
|
108
156
|
]);
|
|
109
157
|
```
|
|
110
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
|
+
|
|
111
252
|
### Typed Handlers
|
|
112
253
|
|
|
113
254
|
Route handlers receive a typed context with params, search params, and `reverse()`:
|
|
@@ -122,6 +263,30 @@ export const ProductPage: Handler<"product"> = (ctx) => {
|
|
|
122
263
|
};
|
|
123
264
|
```
|
|
124
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
|
+
|
|
125
290
|
### Search Params
|
|
126
291
|
|
|
127
292
|
Define a search schema on the route for type-safe search parameters:
|
|
@@ -141,6 +306,44 @@ const SearchPage: Handler<"search"> = (ctx) => {
|
|
|
141
306
|
};
|
|
142
307
|
```
|
|
143
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
|
+
|
|
144
347
|
### Response Routes
|
|
145
348
|
|
|
146
349
|
Define API endpoints that bypass the RSC pipeline:
|
|
@@ -248,10 +451,10 @@ import { useLoader } from "@rangojs/router/client";
|
|
|
248
451
|
import { BlogSidebarLoader } from "./loaders/blog";
|
|
249
452
|
|
|
250
453
|
function BlogSidebar() {
|
|
251
|
-
const {
|
|
454
|
+
const { data } = useLoader(BlogSidebarLoader);
|
|
252
455
|
return (
|
|
253
456
|
<ul>
|
|
254
|
-
{posts.map((p) => (
|
|
457
|
+
{data.posts.map((p) => (
|
|
255
458
|
<li key={p.slug}>{p.title}</li>
|
|
256
459
|
))}
|
|
257
460
|
</ul>
|
|
@@ -313,7 +516,7 @@ function Nav() {
|
|
|
313
516
|
return (
|
|
314
517
|
<nav>
|
|
315
518
|
<Link to={href("/")}>Home</Link>
|
|
316
|
-
<Link to={href("/blog")} prefetch="
|
|
519
|
+
<Link to={href("/blog")} prefetch="adaptive">
|
|
317
520
|
Blog
|
|
318
521
|
</Link>
|
|
319
522
|
<Link to={href("/about")}>About</Link>
|
|
@@ -324,20 +527,21 @@ function Nav() {
|
|
|
324
527
|
|
|
325
528
|
`href()` validates that the path matches a registered route pattern at compile time (e.g. `/blog/my-post` matches `/blog/:slug`).
|
|
326
529
|
|
|
327
|
-
### Navigation
|
|
530
|
+
### Navigation Hooks
|
|
328
531
|
|
|
329
532
|
```tsx
|
|
330
533
|
"use client";
|
|
331
|
-
import { useNavigation } from "@rangojs/router/client";
|
|
534
|
+
import { useNavigation, useRouter } from "@rangojs/router/client";
|
|
332
535
|
|
|
333
536
|
function SearchForm() {
|
|
334
|
-
const
|
|
537
|
+
const router = useRouter();
|
|
538
|
+
const nav = useNavigation();
|
|
335
539
|
|
|
336
540
|
function handleSubmit(query: string) {
|
|
337
|
-
|
|
541
|
+
router.push(`/search?q=${encodeURIComponent(query)}`);
|
|
338
542
|
}
|
|
339
543
|
|
|
340
|
-
return <form onSubmit={...}>{
|
|
544
|
+
return <form onSubmit={...}>{nav.state !== "idle" && <Spinner />}</form>;
|
|
341
545
|
}
|
|
342
546
|
```
|
|
343
547
|
|
|
@@ -385,6 +589,20 @@ export const urlpatterns = urls(({ path, include }) => [
|
|
|
385
589
|
|
|
386
590
|
Included route names are prefixed with the include name: `reverse("api.health")`, `reverse("api.products")`.
|
|
387
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
|
+
|
|
388
606
|
## Middleware
|
|
389
607
|
|
|
390
608
|
```tsx
|
|
@@ -511,6 +729,23 @@ export const ProductPage = Prerender(
|
|
|
511
729
|
|
|
512
730
|
With `passthrough: true`, known params are served from the build-time cache and unknown params fall through to live rendering.
|
|
513
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
|
+
|
|
514
749
|
## Theme
|
|
515
750
|
|
|
516
751
|
### Router Configuration
|
|
@@ -561,7 +796,7 @@ hostRouter.fallback().map(() => import("./apps/site/handler.js"));
|
|
|
561
796
|
|
|
562
797
|
export default {
|
|
563
798
|
async fetch(request, env, ctx) {
|
|
564
|
-
return hostRouter.match(request, {
|
|
799
|
+
return hostRouter.match(request, { env, ctx });
|
|
565
800
|
},
|
|
566
801
|
};
|
|
567
802
|
```
|
|
@@ -605,20 +840,52 @@ Auto-detects file type:
|
|
|
605
840
|
|
|
606
841
|
## Type Safety
|
|
607
842
|
|
|
608
|
-
The Vite plugin automatically generates a `router.named-routes.gen.ts` file that globally registers
|
|
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.
|
|
609
870
|
|
|
610
871
|
## Subpath Exports
|
|
611
872
|
|
|
612
|
-
| Export | Description
|
|
613
|
-
| ------------------------ |
|
|
614
|
-
| `@rangojs/router` |
|
|
615
|
-
| `@rangojs/router/client` | Client: `Link`, `Outlet`, `href`, `useNavigation`, `useLoader`, `MetaTags`
|
|
616
|
-
| `@rangojs/router/cache` | Cache: `CFCacheStore`, `MemorySegmentCacheStore`, `createDocumentCacheMiddleware`
|
|
617
|
-
| `@rangojs/router/theme` | Theme: `useTheme`, `ThemeProvider`, `ThemeScript`
|
|
618
|
-
| `@rangojs/router/host` | Host routing: `createHostRouter`, `defineHosts`
|
|
619
|
-
| `@rangojs/router/vite` | Vite plugin: `rango()`
|
|
620
|
-
| `@rangojs/router/server`
|
|
621
|
-
| `@rangojs/router/
|
|
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.
|
|
622
889
|
|
|
623
890
|
## Examples
|
|
624
891
|
|