@rangojs/router 0.0.0-experimental.b02a2fec → 0.0.0-experimental.b30bbf02
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/README.md +112 -17
- package/dist/vite/index.js +1338 -462
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +7 -5
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +33 -20
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +90 -16
- package/skills/loader/SKILL.md +70 -3
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/parallel/SKILL.md +66 -0
- package/skills/rango/SKILL.md +25 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +24 -0
- package/skills/server-actions/SKILL.md +739 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +3 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/event-controller.ts +44 -4
- package/src/browser/navigation-bridge.ts +71 -5
- package/src/browser/navigation-client.ts +64 -13
- package/src/browser/navigation-store.ts +25 -1
- package/src/browser/partial-update.ts +34 -3
- package/src/browser/prefetch/cache.ts +129 -21
- package/src/browser/prefetch/fetch.ts +148 -16
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +30 -2
- package/src/browser/react/NavigationProvider.tsx +70 -18
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +11 -1
- package/src/browser/react/use-router.ts +8 -1
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/rsc-router.tsx +34 -6
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/types.ts +19 -0
- package/src/build/route-trie.ts +50 -24
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/client.tsx +82 -174
- package/src/index.rsc.ts +3 -0
- package/src/index.ts +40 -9
- package/src/outlet-context.ts +1 -1
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +7 -3
- package/src/route-definition/dsl-helpers.ts +175 -23
- package/src/route-definition/helpers-types.ts +63 -14
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-types.ts +7 -0
- package/src/router/handler-context.ts +24 -4
- package/src/router/lazy-includes.ts +6 -6
- package/src/router/loader-resolution.ts +3 -0
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +4 -3
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-result.ts +21 -2
- package/src/router/middleware-types.ts +2 -22
- package/src/router/middleware.ts +54 -7
- package/src/router/pattern-matching.ts +87 -17
- package/src/router/revalidation.ts +15 -1
- package/src/router/segment-resolution/fresh.ts +8 -0
- package/src/router/segment-resolution/revalidation.ts +128 -100
- package/src/router/trie-matching.ts +18 -13
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +1 -2
- package/src/rsc/handler.ts +8 -4
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +10 -0
- package/src/rsc/server-action.ts +4 -0
- package/src/rsc/types.ts +6 -0
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +11 -61
- package/src/server/context.ts +26 -3
- package/src/server/request-context.ts +10 -42
- package/src/ssr/index.tsx +5 -1
- package/src/types/handler-context.ts +12 -39
- package/src/types/loader-types.ts +5 -6
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +17 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +30 -4
- package/src/urls/response-types.ts +2 -10
- package/src/vite/debug.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +31 -3
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +48 -1
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/plugins/cjs-to-esm.ts +5 -0
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +16 -4
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +52 -28
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +516 -486
- package/src/vite/plugins/performance-tracks.ts +17 -9
- package/src/vite/plugins/use-cache-transform.ts +56 -43
- package/src/vite/plugins/version-injector.ts +37 -11
- package/src/vite/rango.ts +49 -14
- package/src/vite/router-discovery.ts +558 -53
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +20 -6
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ Named-route RSC router with structural composability and type-safe partial rende
|
|
|
10
10
|
- **Structural composability** — Attach routes, loaders, middleware, handles, caching, prerendering, and static generation without hiding the route tree
|
|
11
11
|
- **Composable URL patterns** — Django-style `urls()` DSL with `path`, `layout`, `include`
|
|
12
12
|
- **Data loaders** — `createLoader()` with automatic streaming and Suspense integration
|
|
13
|
+
- **Server actions** — `"use server"` mutations with `useActionState`, `useOptimistic`, and per-segment + per-loader `revalidate()` rules
|
|
13
14
|
- **Live data layer** — Pre-render or cache the UI shell while loaders stay live by default at request time
|
|
14
15
|
- **Layouts & nesting** — Nested layouts with `<Outlet />` and parallel routes
|
|
15
16
|
- **Segment-level caching** — `cache()` DSL with TTL/SWR and pluggable cache stores
|
|
@@ -161,13 +162,18 @@ const urlpatterns = urls(({ path }) => [
|
|
|
161
162
|
]);
|
|
162
163
|
```
|
|
163
164
|
|
|
164
|
-
Use `reverse()` as the default way to link to routes:
|
|
165
|
+
Use `ctx.reverse()` from handler context as the default way to link to routes from server code:
|
|
165
166
|
|
|
166
167
|
```tsx
|
|
167
|
-
|
|
168
|
-
|
|
168
|
+
const ProductPage: Handler<"product"> = (ctx) => {
|
|
169
|
+
const url = ctx.reverse("product", { slug: "widget" }); // "/product/widget"
|
|
170
|
+
const searchUrl = ctx.reverse("search", undefined, { q: "rsc" }); // "/search?q=rsc"
|
|
171
|
+
return <Link to={url}>Widget</Link>;
|
|
172
|
+
};
|
|
169
173
|
```
|
|
170
174
|
|
|
175
|
+
`router.reverse()` (exported from the router module) is the same function without a handler context, useful in scripts or tests. In request code, prefer `ctx.reverse()` — it auto-fills mount params from the current match.
|
|
176
|
+
|
|
171
177
|
### Composable URL Modules
|
|
172
178
|
|
|
173
179
|
Local route names compose cleanly with `include(..., { name })`:
|
|
@@ -477,41 +483,130 @@ const urlpatterns = urls(({ path, loader }) => [
|
|
|
477
483
|
]);
|
|
478
484
|
```
|
|
479
485
|
|
|
480
|
-
##
|
|
486
|
+
## Server Actions
|
|
481
487
|
|
|
482
|
-
|
|
488
|
+
Server actions are React's RSC mutation primitive. Define them with the
|
|
489
|
+
`"use server"` directive — Rango uses standard React 19 hooks
|
|
490
|
+
(`useActionState`, `useFormStatus`, `useOptimistic`) with no framework wrapper.
|
|
483
491
|
|
|
484
|
-
|
|
492
|
+
```tsx
|
|
493
|
+
// app/actions/cart.ts
|
|
494
|
+
"use server";
|
|
495
|
+
|
|
496
|
+
import { getRequestContext } from "@rangojs/router";
|
|
497
|
+
|
|
498
|
+
export async function addToCart(productId: string): Promise<void> {
|
|
499
|
+
const ctx = getRequestContext();
|
|
500
|
+
const userId = ctx.get("user").id;
|
|
501
|
+
await db.cart.insert({ userId, productId });
|
|
502
|
+
}
|
|
503
|
+
```
|
|
485
504
|
|
|
486
505
|
```tsx
|
|
487
|
-
|
|
488
|
-
|
|
506
|
+
// Client form with progressive enhancement + pending state
|
|
507
|
+
"use client";
|
|
508
|
+
import { useActionState } from "react";
|
|
509
|
+
import { saveProfile } from "../actions/profile";
|
|
489
510
|
|
|
490
|
-
function
|
|
511
|
+
export function ProfileForm() {
|
|
512
|
+
const [state, action, pending] = useActionState(saveProfile, null);
|
|
491
513
|
return (
|
|
492
|
-
<
|
|
493
|
-
<
|
|
494
|
-
<
|
|
495
|
-
<
|
|
496
|
-
</
|
|
514
|
+
<form action={action}>
|
|
515
|
+
<input name="name" defaultValue={state?.values?.name} />
|
|
516
|
+
{state?.errors?.name && <p role="alert">{state.errors.name}</p>}
|
|
517
|
+
<button disabled={pending}>{pending ? "Saving…" : "Save"}</button>
|
|
518
|
+
</form>
|
|
497
519
|
);
|
|
498
520
|
}
|
|
499
521
|
```
|
|
500
522
|
|
|
501
|
-
|
|
523
|
+
After an action runs, matched route segments (path/layout/parallel/intercept)
|
|
524
|
+
and loaders can re-render/re-resolve so the UI reflects the new state.
|
|
525
|
+
Attach a `revalidate(({ actionId }) => ...)` rule on any segment or loader
|
|
526
|
+
that owns data the action touched:
|
|
502
527
|
|
|
503
|
-
|
|
528
|
+
```tsx
|
|
529
|
+
urls(({ path, loader, revalidate }) => [
|
|
530
|
+
// Segment-level: re-render the cart page handler after cart actions.
|
|
531
|
+
// Nest loaders that belong to this route inside the same path() so the
|
|
532
|
+
// segment owns its data dependencies.
|
|
533
|
+
path("/cart", CartPage, { name: "cart" }, () => [
|
|
534
|
+
revalidate(
|
|
535
|
+
({ actionId }) => actionId?.startsWith("src/actions/cart.ts#") ?? false,
|
|
536
|
+
),
|
|
537
|
+
loader(CartLoader, () => [
|
|
538
|
+
revalidate(
|
|
539
|
+
({ actionId }) => actionId?.startsWith("src/actions/cart.ts#") ?? false,
|
|
540
|
+
),
|
|
541
|
+
]),
|
|
542
|
+
]),
|
|
543
|
+
]);
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
For the full guide — validation with Zod, error handling, file uploads,
|
|
547
|
+
`useOptimistic`, redirects, and progressive enhancement — see the
|
|
548
|
+
`/server-actions` skill.
|
|
549
|
+
|
|
550
|
+
## Navigation & Links
|
|
551
|
+
|
|
552
|
+
### Named Routes with `ctx.reverse()` (Server)
|
|
553
|
+
|
|
554
|
+
In server components and handlers, use `ctx.reverse()` to generate URLs by route name. This is the default — it is typed, auto-fills mount params from the current match, and resolves both local (`.name`) and absolute (`name.sub`) names:
|
|
504
555
|
|
|
505
556
|
```tsx
|
|
557
|
+
import { Link } from "@rangojs/router/client";
|
|
558
|
+
import type { Handler } from "@rangojs/router";
|
|
559
|
+
|
|
506
560
|
const BlogPostPage: Handler<"blogPost"> = (ctx) => {
|
|
507
561
|
const backUrl = ctx.reverse("blog");
|
|
508
562
|
return <Link to={backUrl}>Back to blog</Link>;
|
|
509
563
|
};
|
|
510
564
|
```
|
|
511
565
|
|
|
566
|
+
`reverse()` is type-safe — route names and required params are checked at compile time. Included routes use dotted names: `ctx.reverse("api.health")`.
|
|
567
|
+
|
|
568
|
+
For scripts, tests, or other code without a handler context, import the router-level `reverse`:
|
|
569
|
+
|
|
570
|
+
```tsx
|
|
571
|
+
import { reverse } from "./router";
|
|
572
|
+
reverse("blogPost", { slug: "my-post" });
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
### Client Components
|
|
576
|
+
|
|
577
|
+
**`reverse()` is server-only.** It depends on the route manifest and handler context — neither is available in the browser bundle. Client components receive URLs as props, loader data, or server-action return values:
|
|
578
|
+
|
|
579
|
+
```tsx
|
|
580
|
+
// server
|
|
581
|
+
function BlogIndex(ctx: HandlerContext) {
|
|
582
|
+
return (
|
|
583
|
+
<Nav
|
|
584
|
+
home={ctx.reverse("home")}
|
|
585
|
+
post={ctx.reverse("blogPost", { slug: "my-post" })}
|
|
586
|
+
/>
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
```tsx
|
|
592
|
+
"use client";
|
|
593
|
+
import { Link } from "@rangojs/router/client";
|
|
594
|
+
|
|
595
|
+
export function Nav({ home, post }: { home: string; post: string }) {
|
|
596
|
+
return (
|
|
597
|
+
<nav>
|
|
598
|
+
<Link to={home}>Home</Link>
|
|
599
|
+
<Link to={post}>My Post</Link>
|
|
600
|
+
</nav>
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
For client-side navigation to static paths (no named-route lookup), use `href()` — see below. For URLs tied to named routes, always generate on the server and pass the string in.
|
|
606
|
+
|
|
512
607
|
### `href()` for Path Validation (Client Components)
|
|
513
608
|
|
|
514
|
-
In client components, use `href()` for compile-time path validation:
|
|
609
|
+
In client components, use `href()` for compile-time path validation on static path strings:
|
|
515
610
|
|
|
516
611
|
```tsx
|
|
517
612
|
"use client";
|