@rangojs/router 0.0.0-experimental.2
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/CLAUDE.md +7 -0
- package/README.md +19 -0
- package/dist/vite/index.js +1298 -0
- package/package.json +140 -0
- package/skills/caching/SKILL.md +319 -0
- package/skills/document-cache/SKILL.md +152 -0
- package/skills/hooks/SKILL.md +359 -0
- package/skills/intercept/SKILL.md +292 -0
- package/skills/layout/SKILL.md +216 -0
- package/skills/loader/SKILL.md +365 -0
- package/skills/middleware/SKILL.md +442 -0
- package/skills/parallel/SKILL.md +255 -0
- package/skills/route/SKILL.md +141 -0
- package/skills/router-setup/SKILL.md +403 -0
- package/skills/theme/SKILL.md +54 -0
- package/skills/typesafety/SKILL.md +352 -0
- package/src/__mocks__/version.ts +6 -0
- package/src/__tests__/component-utils.test.ts +76 -0
- package/src/__tests__/route-definition.test.ts +63 -0
- package/src/__tests__/urls.test.tsx +436 -0
- package/src/browser/event-controller.ts +876 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/link-interceptor.ts +121 -0
- package/src/browser/lru-cache.ts +69 -0
- package/src/browser/merge-segment-loaders.ts +126 -0
- package/src/browser/navigation-bridge.ts +893 -0
- package/src/browser/navigation-client.ts +162 -0
- package/src/browser/navigation-store.ts +823 -0
- package/src/browser/partial-update.ts +559 -0
- package/src/browser/react/Link.tsx +248 -0
- package/src/browser/react/NavigationProvider.tsx +275 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +53 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +120 -0
- package/src/browser/react/location-state.ts +62 -0
- package/src/browser/react/use-action.ts +240 -0
- package/src/browser/react/use-client-cache.ts +56 -0
- package/src/browser/react/use-handle.ts +178 -0
- package/src/browser/react/use-href.tsx +208 -0
- package/src/browser/react/use-link-status.ts +134 -0
- package/src/browser/react/use-navigation.ts +150 -0
- package/src/browser/react/use-segments.ts +188 -0
- package/src/browser/request-controller.ts +164 -0
- package/src/browser/rsc-router.tsx +353 -0
- package/src/browser/scroll-restoration.ts +324 -0
- package/src/browser/server-action-bridge.ts +747 -0
- package/src/browser/shallow.ts +35 -0
- package/src/browser/types.ts +464 -0
- package/src/cache/__tests__/document-cache.test.ts +522 -0
- package/src/cache/__tests__/memory-segment-store.test.ts +487 -0
- package/src/cache/__tests__/memory-store.test.ts +484 -0
- package/src/cache/cache-scope.ts +565 -0
- package/src/cache/cf/__tests__/cf-cache-store.test.ts +428 -0
- package/src/cache/cf/cf-cache-store.ts +428 -0
- package/src/cache/cf/index.ts +19 -0
- package/src/cache/document-cache.ts +340 -0
- package/src/cache/index.ts +58 -0
- package/src/cache/memory-segment-store.ts +150 -0
- package/src/cache/memory-store.ts +253 -0
- package/src/cache/types.ts +387 -0
- package/src/client.rsc.tsx +88 -0
- package/src/client.tsx +621 -0
- package/src/component-utils.ts +76 -0
- package/src/components/DefaultDocument.tsx +23 -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 +259 -0
- package/src/handle.ts +120 -0
- package/src/handles/MetaTags.tsx +193 -0
- package/src/handles/index.ts +6 -0
- package/src/handles/meta.ts +247 -0
- package/src/href-client.ts +128 -0
- package/src/href-context.ts +33 -0
- package/src/href.ts +177 -0
- package/src/index.rsc.ts +79 -0
- package/src/index.ts +87 -0
- package/src/loader.rsc.ts +204 -0
- package/src/loader.ts +47 -0
- package/src/network-error-thrower.tsx +21 -0
- package/src/outlet-context.ts +15 -0
- package/src/root-error-boundary.tsx +277 -0
- package/src/route-content-wrapper.tsx +198 -0
- package/src/route-definition.ts +1371 -0
- package/src/route-map-builder.ts +146 -0
- package/src/route-types.ts +198 -0
- package/src/route-utils.ts +89 -0
- package/src/router/__tests__/match-context.test.ts +104 -0
- package/src/router/__tests__/match-pipelines.test.ts +537 -0
- package/src/router/__tests__/match-result.test.ts +566 -0
- package/src/router/__tests__/on-error.test.ts +935 -0
- package/src/router/__tests__/pattern-matching.test.ts +577 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/handler-context.ts +158 -0
- package/src/router/loader-resolution.ts +326 -0
- package/src/router/manifest.ts +138 -0
- package/src/router/match-context.ts +264 -0
- package/src/router/match-middleware/background-revalidation.ts +236 -0
- package/src/router/match-middleware/cache-lookup.ts +261 -0
- package/src/router/match-middleware/cache-store.ts +266 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +268 -0
- package/src/router/match-middleware/segment-resolution.ts +174 -0
- package/src/router/match-pipelines.ts +214 -0
- package/src/router/match-result.ts +214 -0
- package/src/router/metrics.ts +62 -0
- package/src/router/middleware.test.ts +1355 -0
- package/src/router/middleware.ts +748 -0
- package/src/router/pattern-matching.ts +272 -0
- package/src/router/revalidation.ts +190 -0
- package/src/router/router-context.ts +299 -0
- package/src/router/types.ts +96 -0
- package/src/router.ts +3876 -0
- package/src/rsc/__tests__/helpers.test.ts +175 -0
- package/src/rsc/handler.ts +1060 -0
- package/src/rsc/helpers.ts +64 -0
- package/src/rsc/index.ts +56 -0
- package/src/rsc/nonce.ts +18 -0
- package/src/rsc/types.ts +237 -0
- package/src/segment-system.tsx +456 -0
- package/src/server/__tests__/request-context.test.ts +171 -0
- package/src/server/context.ts +417 -0
- package/src/server/handle-store.ts +230 -0
- package/src/server/loader-registry.ts +174 -0
- package/src/server/request-context.ts +554 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +146 -0
- package/src/ssr/__tests__/ssr-handler.test.tsx +188 -0
- package/src/ssr/index.tsx +234 -0
- package/src/theme/ThemeProvider.tsx +291 -0
- package/src/theme/ThemeScript.tsx +61 -0
- package/src/theme/__tests__/theme.test.ts +120 -0
- package/src/theme/constants.ts +55 -0
- package/src/theme/index.ts +58 -0
- package/src/theme/theme-context.ts +70 -0
- package/src/theme/theme-script.ts +152 -0
- package/src/theme/types.ts +182 -0
- package/src/theme/use-theme.ts +44 -0
- package/src/types.ts +1561 -0
- package/src/urls.ts +726 -0
- package/src/use-loader.tsx +346 -0
- package/src/vite/__tests__/expose-loader-id.test.ts +117 -0
- package/src/vite/expose-action-id.ts +344 -0
- package/src/vite/expose-handle-id.ts +209 -0
- package/src/vite/expose-loader-id.ts +357 -0
- package/src/vite/expose-location-state-id.ts +177 -0
- package/src/vite/index.ts +787 -0
- package/src/vite/package-resolution.ts +125 -0
- package/src/vite/version.d.ts +12 -0
- package/src/vite/virtual-entries.ts +109 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Component to render collected meta descriptors in the document head.
|
|
5
|
+
*
|
|
6
|
+
* Supports both sync and async meta descriptors. Async descriptors
|
|
7
|
+
* (Promise<MetaDescriptorBase>) are resolved using React's use() hook.
|
|
8
|
+
*
|
|
9
|
+
* When theme is enabled in the router config, MetaTags also renders
|
|
10
|
+
* the theme initialization script to prevent FOUC (flash of unstyled content).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* function RootLayout() {
|
|
15
|
+
* return (
|
|
16
|
+
* <html lang="en" suppressHydrationWarning>
|
|
17
|
+
* <head>
|
|
18
|
+
* <MetaTags />
|
|
19
|
+
* </head>
|
|
20
|
+
* <body>...</body>
|
|
21
|
+
* </html>
|
|
22
|
+
* );
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { use } from "react";
|
|
28
|
+
import { useHandle } from "../browser/react/use-handle.ts";
|
|
29
|
+
import { Meta } from "./meta.ts";
|
|
30
|
+
import type { MetaDescriptor, MetaDescriptorBase } from "../router/types.ts";
|
|
31
|
+
import { getSSRThemeConfig } from "../theme/theme-context.js";
|
|
32
|
+
import { generateThemeScript } from "../theme/theme-script.js";
|
|
33
|
+
|
|
34
|
+
// Type guards for MetaDescriptorBase variants
|
|
35
|
+
function hasCharSet(d: MetaDescriptorBase): d is { charSet: "utf-8" } {
|
|
36
|
+
return "charSet" in d && d.charSet === "utf-8";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function hasTitle(d: MetaDescriptorBase): d is { title: string } {
|
|
40
|
+
return "title" in d && typeof (d as { title?: unknown }).title === "string";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function hasNameContent(d: MetaDescriptorBase): d is { name: string; content: string } {
|
|
44
|
+
return "name" in d && "content" in d &&
|
|
45
|
+
typeof (d as { name?: unknown }).name === "string" &&
|
|
46
|
+
typeof (d as { content?: unknown }).content === "string";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function hasPropertyContent(d: MetaDescriptorBase): d is { property: string; content: string } {
|
|
50
|
+
return "property" in d && "content" in d &&
|
|
51
|
+
typeof (d as { property?: unknown }).property === "string" &&
|
|
52
|
+
typeof (d as { content?: unknown }).content === "string";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function hasHttpEquivContent(d: MetaDescriptorBase): d is { httpEquiv: string; content: string } {
|
|
56
|
+
return "httpEquiv" in d && "content" in d &&
|
|
57
|
+
typeof (d as { httpEquiv?: unknown }).httpEquiv === "string" &&
|
|
58
|
+
typeof (d as { content?: unknown }).content === "string";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function hasScriptLdJson(d: MetaDescriptorBase): d is { "script:ld+json": object } {
|
|
62
|
+
return "script:ld+json" in d;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function hasTagName(d: MetaDescriptorBase): d is { tagName: "meta" | "link"; [name: string]: string } {
|
|
66
|
+
return "tagName" in d && ((d as { tagName?: unknown }).tagName === "meta" || (d as { tagName?: unknown }).tagName === "link");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if a value is a Promise.
|
|
71
|
+
*/
|
|
72
|
+
function isPromise(value: unknown): value is Promise<unknown> {
|
|
73
|
+
return value !== null && typeof value === "object" && "then" in value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Render a single meta descriptor as a React element.
|
|
78
|
+
*/
|
|
79
|
+
function renderMetaDescriptor(
|
|
80
|
+
descriptor: MetaDescriptorBase,
|
|
81
|
+
index: number
|
|
82
|
+
): React.ReactNode {
|
|
83
|
+
// charset
|
|
84
|
+
if (hasCharSet(descriptor)) {
|
|
85
|
+
return <meta key="charSet" charSet={descriptor.charSet} />;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// title
|
|
89
|
+
if (hasTitle(descriptor)) {
|
|
90
|
+
return <title key="title">{descriptor.title}</title>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// name + content (description, viewport, etc.)
|
|
94
|
+
if (hasNameContent(descriptor)) {
|
|
95
|
+
return (
|
|
96
|
+
<meta
|
|
97
|
+
key={`name-${descriptor.name}`}
|
|
98
|
+
name={descriptor.name}
|
|
99
|
+
content={descriptor.content}
|
|
100
|
+
/>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// property + content (Open Graph, etc.)
|
|
105
|
+
if (hasPropertyContent(descriptor)) {
|
|
106
|
+
return (
|
|
107
|
+
<meta
|
|
108
|
+
key={`property-${descriptor.property}`}
|
|
109
|
+
property={descriptor.property}
|
|
110
|
+
content={descriptor.content}
|
|
111
|
+
/>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// http-equiv + content
|
|
116
|
+
if (hasHttpEquivContent(descriptor)) {
|
|
117
|
+
return (
|
|
118
|
+
<meta
|
|
119
|
+
key={`httpEquiv-${descriptor.httpEquiv}`}
|
|
120
|
+
httpEquiv={descriptor.httpEquiv}
|
|
121
|
+
content={descriptor.content}
|
|
122
|
+
/>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// JSON-LD structured data
|
|
127
|
+
if (hasScriptLdJson(descriptor)) {
|
|
128
|
+
const json = JSON.stringify(descriptor["script:ld+json"]);
|
|
129
|
+
return (
|
|
130
|
+
<script
|
|
131
|
+
key={`ld-json-${index}`}
|
|
132
|
+
type="application/ld+json"
|
|
133
|
+
dangerouslySetInnerHTML={{ __html: json }}
|
|
134
|
+
/>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Custom tagName (meta or link with arbitrary attributes)
|
|
139
|
+
if (hasTagName(descriptor)) {
|
|
140
|
+
const { tagName, ...rest } = descriptor;
|
|
141
|
+
if (tagName === "link") {
|
|
142
|
+
return <link key={`link-${index}`} {...(rest as React.LinkHTMLAttributes<HTMLLinkElement>)} />;
|
|
143
|
+
}
|
|
144
|
+
if (tagName === "meta") {
|
|
145
|
+
return <meta key={`meta-${index}`} {...(rest as React.MetaHTMLAttributes<HTMLMetaElement>)} />;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Fallback: treat as meta attributes
|
|
150
|
+
return <meta key={`meta-fallback-${index}`} {...(descriptor as React.MetaHTMLAttributes<HTMLMetaElement>)} />;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Wrapper component to resolve a Promise<MetaDescriptorBase> using use().
|
|
155
|
+
*/
|
|
156
|
+
function AsyncMetaTag({ promise, index }: { promise: Promise<MetaDescriptorBase>; index: number }): React.ReactNode {
|
|
157
|
+
const resolved = use(promise);
|
|
158
|
+
return renderMetaDescriptor(resolved, index);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Renders all collected meta descriptors from route handlers.
|
|
163
|
+
*
|
|
164
|
+
* Place this component inside the `<head>` element of your document.
|
|
165
|
+
* It will automatically update when meta descriptors change during navigation.
|
|
166
|
+
*
|
|
167
|
+
* When theme is enabled in router config, also renders the theme initialization
|
|
168
|
+
* script to prevent FOUC (flash of unstyled content).
|
|
169
|
+
*
|
|
170
|
+
* Async meta descriptors (Promise<MetaDescriptorBase>) are resolved using
|
|
171
|
+
* React's use() hook. RSC streaming handles the Promise resolution.
|
|
172
|
+
*/
|
|
173
|
+
export function MetaTags(): React.ReactNode {
|
|
174
|
+
const descriptors = useHandle(Meta) as MetaDescriptor[];
|
|
175
|
+
const themeConfig = getSSRThemeConfig();
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<>
|
|
179
|
+
{/* Theme script must be first to prevent FOUC */}
|
|
180
|
+
{themeConfig && (
|
|
181
|
+
<script
|
|
182
|
+
dangerouslySetInnerHTML={{ __html: generateThemeScript(themeConfig) }}
|
|
183
|
+
/>
|
|
184
|
+
)}
|
|
185
|
+
{descriptors.map((descriptor, index) => {
|
|
186
|
+
if (isPromise(descriptor)) {
|
|
187
|
+
return <AsyncMetaTag key={`async-${index}`} promise={descriptor} index={index} />;
|
|
188
|
+
}
|
|
189
|
+
return renderMetaDescriptor(descriptor, index);
|
|
190
|
+
})}
|
|
191
|
+
</>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in Meta handle for managing document metadata across route segments.
|
|
3
|
+
*
|
|
4
|
+
* Provides automatic deduplication: later routes override earlier ones
|
|
5
|
+
* for the same meta key (title, name, property, etc.)
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* // In route handler
|
|
10
|
+
* route("product/:id", (ctx) => {
|
|
11
|
+
* const meta = ctx.use(Meta);
|
|
12
|
+
* meta({ title: "Product Details" });
|
|
13
|
+
* meta({ name: "description", content: "..." });
|
|
14
|
+
* meta({ property: "og:title", content: "..." });
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // In layout (renders the collected meta tags)
|
|
18
|
+
* function RootLayout() {
|
|
19
|
+
* return (
|
|
20
|
+
* <html>
|
|
21
|
+
* <head>
|
|
22
|
+
* <MetaTags />
|
|
23
|
+
* </head>
|
|
24
|
+
* ...
|
|
25
|
+
* </html>
|
|
26
|
+
* );
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { createHandle, type Handle } from "../handle.ts";
|
|
32
|
+
import type {
|
|
33
|
+
MetaDescriptor,
|
|
34
|
+
TitleDescriptor,
|
|
35
|
+
UnsetDescriptor,
|
|
36
|
+
} from "../router/types.ts";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Type guard for unset descriptor
|
|
40
|
+
*/
|
|
41
|
+
function isUnsetDescriptor(
|
|
42
|
+
descriptor: MetaDescriptor
|
|
43
|
+
): descriptor is UnsetDescriptor {
|
|
44
|
+
return (
|
|
45
|
+
typeof descriptor === "object" &&
|
|
46
|
+
descriptor !== null &&
|
|
47
|
+
"unset" in descriptor &&
|
|
48
|
+
typeof (descriptor as UnsetDescriptor).unset === "string"
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Type guard for title descriptor (any form)
|
|
54
|
+
*/
|
|
55
|
+
function isTitleDescriptor(
|
|
56
|
+
descriptor: MetaDescriptor
|
|
57
|
+
): descriptor is { title: TitleDescriptor } {
|
|
58
|
+
return (
|
|
59
|
+
typeof descriptor === "object" &&
|
|
60
|
+
descriptor !== null &&
|
|
61
|
+
"title" in descriptor
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Type guard for title template descriptor
|
|
67
|
+
*/
|
|
68
|
+
function isTitleTemplate(
|
|
69
|
+
title: TitleDescriptor
|
|
70
|
+
): title is { template: string; default: string } {
|
|
71
|
+
return (
|
|
72
|
+
typeof title === "object" &&
|
|
73
|
+
title !== null &&
|
|
74
|
+
"template" in title &&
|
|
75
|
+
"default" in title
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Type guard for absolute title descriptor
|
|
81
|
+
*/
|
|
82
|
+
function isAbsoluteTitle(title: TitleDescriptor): title is { absolute: string } {
|
|
83
|
+
return typeof title === "object" && title !== null && "absolute" in title;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get a unique key for a meta descriptor for deduplication.
|
|
88
|
+
* Returns undefined for descriptors that shouldn't be deduplicated.
|
|
89
|
+
*/
|
|
90
|
+
function getMetaKey(descriptor: MetaDescriptor): string | undefined {
|
|
91
|
+
// Skip unset descriptors - they are processed separately
|
|
92
|
+
if (isUnsetDescriptor(descriptor)) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
if ("charSet" in descriptor) {
|
|
96
|
+
return "charSet";
|
|
97
|
+
}
|
|
98
|
+
if ("title" in descriptor) {
|
|
99
|
+
return "title";
|
|
100
|
+
}
|
|
101
|
+
if ("name" in descriptor && "content" in descriptor) {
|
|
102
|
+
return `name:${descriptor.name}`;
|
|
103
|
+
}
|
|
104
|
+
if ("property" in descriptor && "content" in descriptor) {
|
|
105
|
+
return `property:${descriptor.property}`;
|
|
106
|
+
}
|
|
107
|
+
if ("httpEquiv" in descriptor && "content" in descriptor) {
|
|
108
|
+
return `httpEquiv:${descriptor.httpEquiv}`;
|
|
109
|
+
}
|
|
110
|
+
if ("script:ld+json" in descriptor) {
|
|
111
|
+
// JSON-LD scripts can have multiple, don't dedupe by default
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if ("tagName" in descriptor) {
|
|
115
|
+
// For link tags, dedupe by rel if present
|
|
116
|
+
if (descriptor.tagName === "link" && "rel" in descriptor) {
|
|
117
|
+
// Some link rels should be unique (canonical), others not (stylesheet)
|
|
118
|
+
const uniqueRels = ["canonical", "icon", "apple-touch-icon"];
|
|
119
|
+
if (uniqueRels.includes(descriptor.rel as string)) {
|
|
120
|
+
return `link:${descriptor.rel}`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Default meta descriptors included automatically.
|
|
130
|
+
* These can be overridden by route handlers.
|
|
131
|
+
*/
|
|
132
|
+
const defaultMetaDescriptors: MetaDescriptor[] = [
|
|
133
|
+
{ charSet: "utf-8" },
|
|
134
|
+
{ name: "viewport", content: "width=device-width, initial-scale=1" },
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Helper to add or replace a descriptor in the result array
|
|
139
|
+
*/
|
|
140
|
+
function addOrReplace(
|
|
141
|
+
result: MetaDescriptor[],
|
|
142
|
+
keyToIndex: Map<string, number>,
|
|
143
|
+
descriptor: MetaDescriptor,
|
|
144
|
+
key: string | undefined
|
|
145
|
+
): void {
|
|
146
|
+
if (key !== undefined && keyToIndex.has(key)) {
|
|
147
|
+
result[keyToIndex.get(key)!] = descriptor;
|
|
148
|
+
} else {
|
|
149
|
+
if (key !== undefined) {
|
|
150
|
+
keyToIndex.set(key, result.length);
|
|
151
|
+
}
|
|
152
|
+
result.push(descriptor);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Helper to update indices after removing an element
|
|
158
|
+
*/
|
|
159
|
+
function updateIndicesAfterRemoval(
|
|
160
|
+
keyToIndex: Map<string, number>,
|
|
161
|
+
removedIndex: number
|
|
162
|
+
): void {
|
|
163
|
+
for (const [key, index] of keyToIndex) {
|
|
164
|
+
if (index > removedIndex) {
|
|
165
|
+
keyToIndex.set(key, index - 1);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Collect function for Meta handle.
|
|
172
|
+
* Includes default meta descriptors, then deduplicates by key with later routes overriding earlier ones.
|
|
173
|
+
* Supports title templates, absolute titles, and unset descriptors.
|
|
174
|
+
*/
|
|
175
|
+
function collectMeta(segments: MetaDescriptor[][]): MetaDescriptor[] {
|
|
176
|
+
const result: MetaDescriptor[] = [];
|
|
177
|
+
const keyToIndex = new Map<string, number>();
|
|
178
|
+
let titleTemplate: string | undefined;
|
|
179
|
+
|
|
180
|
+
// Add defaults first so they can be overridden
|
|
181
|
+
for (const descriptor of defaultMetaDescriptors) {
|
|
182
|
+
const key = getMetaKey(descriptor);
|
|
183
|
+
if (key !== undefined) {
|
|
184
|
+
keyToIndex.set(key, result.length);
|
|
185
|
+
}
|
|
186
|
+
result.push(descriptor);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
for (const descriptors of segments) {
|
|
190
|
+
for (const descriptor of descriptors) {
|
|
191
|
+
// Handle unset descriptors
|
|
192
|
+
if (isUnsetDescriptor(descriptor)) {
|
|
193
|
+
const keyToRemove = descriptor.unset;
|
|
194
|
+
if (keyToIndex.has(keyToRemove)) {
|
|
195
|
+
const idx = keyToIndex.get(keyToRemove)!;
|
|
196
|
+
result.splice(idx, 1);
|
|
197
|
+
keyToIndex.delete(keyToRemove);
|
|
198
|
+
updateIndicesAfterRemoval(keyToIndex, idx);
|
|
199
|
+
}
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Handle title descriptors with template/absolute support
|
|
204
|
+
if (isTitleDescriptor(descriptor)) {
|
|
205
|
+
const titleValue = descriptor.title;
|
|
206
|
+
|
|
207
|
+
if (isTitleTemplate(titleValue)) {
|
|
208
|
+
// Store template for subsequent title descriptors in child segments
|
|
209
|
+
titleTemplate = titleValue.template;
|
|
210
|
+
// Set the default title
|
|
211
|
+
addOrReplace(result, keyToIndex, { title: titleValue.default }, "title");
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (isAbsoluteTitle(titleValue)) {
|
|
216
|
+
// Absolute title bypasses any template
|
|
217
|
+
addOrReplace(result, keyToIndex, { title: titleValue.absolute }, "title");
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// String title - apply template if one exists
|
|
222
|
+
const finalTitle = titleTemplate
|
|
223
|
+
? titleTemplate.replace("%s", titleValue as string)
|
|
224
|
+
: titleValue;
|
|
225
|
+
addOrReplace(result, keyToIndex, { title: finalTitle as string }, "title");
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Handle all other descriptors
|
|
230
|
+
const key = getMetaKey(descriptor);
|
|
231
|
+
addOrReplace(result, keyToIndex, descriptor, key);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Built-in handle for managing document metadata.
|
|
240
|
+
*
|
|
241
|
+
* Use `ctx.use(Meta)` in route handlers to push meta descriptors.
|
|
242
|
+
* Use `<MetaTags />` component to render them in the document head.
|
|
243
|
+
*/
|
|
244
|
+
export const Meta: Handle<MetaDescriptor, MetaDescriptor[]> = createHandle<MetaDescriptor, MetaDescriptor[]>(
|
|
245
|
+
collectMeta,
|
|
246
|
+
"__rsc_router_meta__"
|
|
247
|
+
);
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-safe type-safe href function
|
|
3
|
+
*
|
|
4
|
+
* This is a compile-time only href that validates paths against registered routes.
|
|
5
|
+
* No runtime route map lookup - just an identity function with TypeScript validation.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { href } from "rsc-router/client";
|
|
10
|
+
*
|
|
11
|
+
* href("/blog/my-post"); // ✓ matches /blog/:slug
|
|
12
|
+
* href("/shop/product/widget"); // ✓ matches /shop/product/:slug
|
|
13
|
+
* href("/invalid"); // ✗ TypeScript error
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { GetRegisteredRoutes } from "./types.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parse constraint values into a union type for paths
|
|
21
|
+
* "a|b|c" → "a" | "b" | "c"
|
|
22
|
+
*/
|
|
23
|
+
type ParseConstraintPath<T extends string> =
|
|
24
|
+
T extends `${infer First}|${infer Rest}`
|
|
25
|
+
? First | ParseConstraintPath<Rest>
|
|
26
|
+
: T;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Convert a route pattern to a template literal type
|
|
30
|
+
*
|
|
31
|
+
* Supports:
|
|
32
|
+
* - Static: /about → "/about"
|
|
33
|
+
* - Dynamic: /blog/:slug → `/blog/${string}`
|
|
34
|
+
* - Optional: /:locale?/blog → "/blog" | `/${string}/blog`
|
|
35
|
+
* - Constrained: /:locale(en|gb)/blog → "/en/blog" | "/gb/blog"
|
|
36
|
+
* - Optional + Constrained: /:locale(en|gb)?/blog → "/blog" | "/en/blog" | "/gb/blog"
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* PatternToPath<"/blog/:slug"> = `/blog/${string}`
|
|
40
|
+
* PatternToPath<"/:locale?/blog"> = "/blog" | `/${string}/blog`
|
|
41
|
+
* PatternToPath<"/:locale(en|gb)/blog"> = "/en/blog" | "/gb/blog"
|
|
42
|
+
* PatternToPath<"/:locale(en|gb)?/blog"> = "/blog" | "/en/blog" | "/gb/blog"
|
|
43
|
+
*/
|
|
44
|
+
export type PatternToPath<T extends string> =
|
|
45
|
+
// Optional + constrained param in middle: /:param(a|b)?/rest
|
|
46
|
+
T extends `${infer Before}:${infer _Name}(${infer Constraint})?/${infer After}`
|
|
47
|
+
? PatternToPath<`${Before}${After}`> | `${Before}${ParseConstraintPath<Constraint>}/${PatternToPath<After>}`
|
|
48
|
+
// Optional + constrained param at end: /path/:param(a|b)?
|
|
49
|
+
: T extends `${infer Before}:${infer _Name}(${infer Constraint})?`
|
|
50
|
+
? Before | `${Before}${ParseConstraintPath<Constraint>}`
|
|
51
|
+
// Constrained param in middle: /:param(a|b)/rest
|
|
52
|
+
: T extends `${infer Before}:${infer _Name}(${infer Constraint})/${infer After}`
|
|
53
|
+
? `${Before}${ParseConstraintPath<Constraint>}/${PatternToPath<After>}`
|
|
54
|
+
// Constrained param at end: /path/:param(a|b)
|
|
55
|
+
: T extends `${infer Before}:${infer _Name}(${infer Constraint})`
|
|
56
|
+
? `${Before}${ParseConstraintPath<Constraint>}`
|
|
57
|
+
// Optional param in middle: /:param?/rest
|
|
58
|
+
: T extends `${infer Before}:${infer _Param}?/${infer After}`
|
|
59
|
+
? PatternToPath<`${Before}${After}`> | `${Before}${string}/${PatternToPath<After>}`
|
|
60
|
+
// Optional param at end: /path/:param?
|
|
61
|
+
: T extends `${infer Before}:${infer _Param}?`
|
|
62
|
+
? Before | `${Before}${string}`
|
|
63
|
+
// Required param in middle: /:param/rest
|
|
64
|
+
: T extends `${infer Before}:${infer _Param}/${infer After}`
|
|
65
|
+
? `${Before}${string}/${PatternToPath<After>}`
|
|
66
|
+
// Required param at end: /path/:param
|
|
67
|
+
: T extends `${infer Before}:${infer _Param}`
|
|
68
|
+
? `${Before}${string}`
|
|
69
|
+
// Static path
|
|
70
|
+
: T;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Allow optional query string (?...) and/or hash fragment (#...) suffix
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* WithSuffix<"/about"> = "/about" | "/about?..." | "/about#..." | "/about?...#..."
|
|
77
|
+
*/
|
|
78
|
+
type WithSuffix<T extends string> =
|
|
79
|
+
| T
|
|
80
|
+
| `${T}?${string}`
|
|
81
|
+
| `${T}#${string}`
|
|
82
|
+
| `${T}?${string}#${string}`;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Union of all valid paths from registered routes
|
|
86
|
+
*
|
|
87
|
+
* Generated from RSCRouter.RegisteredRoutes via module augmentation.
|
|
88
|
+
* Allows optional query strings and hash fragments.
|
|
89
|
+
*/
|
|
90
|
+
export type ValidPaths<TRoutes extends Record<string, string> = GetRegisteredRoutes> =
|
|
91
|
+
WithSuffix<PatternToPath<TRoutes[keyof TRoutes]>>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Type-safe href function for client-side use
|
|
95
|
+
*
|
|
96
|
+
* This is an identity function - it returns the path unchanged.
|
|
97
|
+
* The value is in TypeScript validation: invalid paths cause compile errors.
|
|
98
|
+
*
|
|
99
|
+
* Works with:
|
|
100
|
+
* - Static paths: href("/about")
|
|
101
|
+
* - Dynamic segments: href("/blog/my-post")
|
|
102
|
+
* - Multiple segments: href("/shop/product/widget/reviews/123")
|
|
103
|
+
*
|
|
104
|
+
* Does NOT validate:
|
|
105
|
+
* - Query strings (passed through as-is)
|
|
106
|
+
* - Hash fragments (passed through as-is)
|
|
107
|
+
*
|
|
108
|
+
* @param path - A valid path matching one of the registered route patterns
|
|
109
|
+
* @returns The path unchanged
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // Valid paths (compile)
|
|
114
|
+
* href("/blog/hello"); // matches /blog/:slug
|
|
115
|
+
* href("/shop/product/widget"); // matches /shop/product/:slug
|
|
116
|
+
* href("/shop/product/widget/reviews"); // matches /shop/product/:slug/reviews
|
|
117
|
+
*
|
|
118
|
+
* // Query strings and hashes pass through (not validated)
|
|
119
|
+
* href("/blog/hello?page=1");
|
|
120
|
+
* href("/about#contact");
|
|
121
|
+
*
|
|
122
|
+
* // Invalid paths (TypeScript error)
|
|
123
|
+
* href("/nonexistent"); // Error: not assignable to ValidPaths
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export function href<T extends ValidPaths>(path: T): T {
|
|
127
|
+
return path;
|
|
128
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Href Context for route name resolution
|
|
5
|
+
*
|
|
6
|
+
* This module is marked "use client" so it can be imported by both:
|
|
7
|
+
* - Server (segment-system): Gets a client reference for createElement
|
|
8
|
+
* - Client (useHref): Uses the actual context for useContext
|
|
9
|
+
*
|
|
10
|
+
* The context stores:
|
|
11
|
+
* - routeMap: Map of route names to URL patterns
|
|
12
|
+
* - routeName: Current matched route name (for local name resolution)
|
|
13
|
+
*/
|
|
14
|
+
import { createContext, type Context } from "react";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Context value for href resolution
|
|
18
|
+
*/
|
|
19
|
+
export interface HrefContextValue {
|
|
20
|
+
/** Route map: route name -> URL pattern */
|
|
21
|
+
routeMap: Record<string, string>;
|
|
22
|
+
/** Current matched route name (includes name prefix from include()) */
|
|
23
|
+
routeName?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Context for href resolution (route map and current route name)
|
|
28
|
+
*
|
|
29
|
+
* On the server: Populated by renderSegments() via HrefContext.Provider
|
|
30
|
+
* On the client: Populated by NavigationProvider from RSC metadata
|
|
31
|
+
*/
|
|
32
|
+
export const HrefContext: Context<HrefContextValue | null> =
|
|
33
|
+
createContext<HrefContextValue | null>(null);
|