@rangojs/router 0.0.0-experimental.19 → 0.0.0-experimental.1fa245e2
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 → AGENTS.md} +4 -0
- package/README.md +122 -30
- package/dist/bin/rango.js +245 -63
- package/dist/vite/index.js +859 -418
- package/package.json +3 -3
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +49 -8
- package/skills/document-cache/SKILL.md +2 -2
- package/skills/hooks/SKILL.md +33 -31
- package/skills/host-router/SKILL.md +218 -0
- package/skills/links/SKILL.md +3 -1
- package/skills/loader/SKILL.md +72 -22
- package/skills/middleware/SKILL.md +2 -0
- package/skills/parallel/SKILL.md +126 -0
- package/skills/prerender/SKILL.md +112 -70
- package/skills/rango/SKILL.md +0 -1
- package/skills/route/SKILL.md +34 -4
- package/skills/router-setup/SKILL.md +95 -5
- package/skills/typesafety/SKILL.md +35 -23
- package/src/__internal.ts +92 -0
- package/src/bin/rango.ts +18 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +5 -0
- package/src/browser/link-interceptor.ts +4 -0
- package/src/browser/navigation-bridge.ts +114 -18
- package/src/browser/navigation-client.ts +126 -44
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/navigation-transaction.ts +11 -9
- package/src/browser/partial-update.ts +80 -15
- package/src/browser/prefetch/cache.ts +166 -27
- package/src/browser/prefetch/fetch.ts +52 -39
- package/src/browser/prefetch/policy.ts +6 -0
- package/src/browser/prefetch/queue.ts +92 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/react/Link.tsx +70 -14
- package/src/browser/react/NavigationProvider.tsx +40 -4
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +143 -59
- package/src/browser/scroll-restoration.ts +41 -42
- package/src/browser/segment-reconciler.ts +6 -1
- package/src/browser/server-action-bridge.ts +454 -436
- package/src/browser/types.ts +60 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +5 -0
- package/src/build/route-trie.ts +19 -3
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +346 -87
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +48 -7
- package/src/cache/cf/cf-cache-store.ts +453 -11
- package/src/cache/cf/index.ts +5 -1
- package/src/cache/document-cache.ts +17 -7
- package/src/cache/index.ts +1 -0
- package/src/cache/taint.ts +55 -0
- package/src/client.rsc.tsx +2 -1
- package/src/client.tsx +3 -102
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/handle.ts +40 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/host/index.ts +0 -3
- package/src/index.rsc.ts +8 -37
- package/src/index.ts +40 -66
- package/src/prerender/store.ts +57 -15
- package/src/prerender.ts +138 -77
- package/src/reverse.ts +22 -1
- package/src/route-definition/dsl-helpers.ts +73 -25
- package/src/route-definition/helpers-types.ts +10 -6
- package/src/route-definition/index.ts +3 -3
- package/src/route-definition/redirect.ts +11 -3
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +108 -25
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +4 -1
- package/src/router/loader-resolution.ts +123 -11
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +9 -3
- package/src/router/match-api.ts +125 -190
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +88 -16
- package/src/router/match-middleware/cache-store.ts +53 -10
- package/src/router/match-middleware/intercept-resolution.ts +9 -7
- package/src/router/match-middleware/segment-resolution.ts +61 -5
- package/src/router/match-result.ts +22 -15
- package/src/router/metrics.ts +238 -13
- package/src/router/middleware-types.ts +53 -12
- package/src/router/middleware.ts +172 -85
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +20 -5
- package/src/router/prerender-match.ts +114 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/revalidation.ts +27 -7
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +50 -5
- package/src/router/router-options.ts +50 -19
- package/src/router/segment-resolution/fresh.ts +200 -19
- package/src/router/segment-resolution/helpers.ts +30 -25
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +429 -301
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/trie-matching.ts +20 -2
- package/src/router/types.ts +1 -0
- package/src/router.ts +88 -15
- package/src/rsc/handler.ts +546 -359
- package/src/rsc/index.ts +0 -20
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +25 -8
- package/src/rsc/rsc-rendering.ts +35 -43
- package/src/rsc/server-action.ts +16 -10
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +10 -1
- package/src/search-params.ts +16 -13
- package/src/segment-system.tsx +140 -4
- package/src/server/context.ts +148 -16
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +182 -34
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/theme/index.ts +4 -13
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +149 -49
- package/src/types/loader-types.ts +36 -9
- package/src/types/route-config.ts +17 -8
- package/src/types/route-entry.ts +8 -1
- package/src/types/segments.ts +2 -5
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +48 -13
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +73 -4
- package/src/vite/discovery/bundle-postprocess.ts +61 -89
- package/src/vite/discovery/discover-routers.ts +23 -5
- package/src/vite/discovery/prerender-collection.ts +48 -15
- package/src/vite/discovery/state.ts +17 -13
- package/src/vite/index.ts +8 -3
- package/src/vite/plugin-types.ts +51 -79
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/expose-action-id.ts +1 -3
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +174 -211
- package/src/vite/router-discovery.ts +169 -42
- package/src/vite/utils/banner.ts +3 -3
- package/src/vite/utils/prerender-utils.ts +78 -0
- package/src/vite/utils/shared-utils.ts +3 -2
- package/skills/testing/SKILL.md +0 -226
- package/src/route-definition/route-function.ts +0 -119
package/src/use-loader.tsx
CHANGED
|
@@ -1,9 +1,70 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
isValidElement,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
type ReactNode,
|
|
12
|
+
} from "react";
|
|
4
13
|
import { OutletContext, type OutletContextValue } from "./outlet-context.js";
|
|
5
14
|
import type { LoaderDefinition, LoadOptions } from "./types.js";
|
|
6
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Extract a specific loader's data from a content ReactNode.
|
|
18
|
+
*
|
|
19
|
+
* When a route registers loaders via loader(), the resolved data lives in
|
|
20
|
+
* the route's OutletProvider (rendered as <Outlet /> content). Parallel
|
|
21
|
+
* slots are siblings of <Outlet />, so they can't find it by walking
|
|
22
|
+
* the parent context chain. This helper traverses wrapper elements
|
|
23
|
+
* (MountContextProvider, ViewTransition, etc.) to reach the OutletProvider
|
|
24
|
+
* and extract the loader data directly.
|
|
25
|
+
*/
|
|
26
|
+
const NOT_FOUND = Symbol("not-found");
|
|
27
|
+
|
|
28
|
+
function extractContentLoaderData(
|
|
29
|
+
node: ReactNode,
|
|
30
|
+
loaderId: string,
|
|
31
|
+
): unknown | typeof NOT_FOUND {
|
|
32
|
+
if (!isValidElement(node)) return NOT_FOUND;
|
|
33
|
+
const props = node.props as Record<string, any> | undefined;
|
|
34
|
+
if (!props) return NOT_FOUND;
|
|
35
|
+
|
|
36
|
+
// Direct OutletProvider with loaderData
|
|
37
|
+
if (props.loaderData && loaderId in props.loaderData) {
|
|
38
|
+
return props.loaderData[loaderId];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// LoaderBoundary: loaderIds + loaderDataPromise (already resolved array).
|
|
42
|
+
// When the segment has loading(), loaderData is resolved inside
|
|
43
|
+
// LoaderBoundary via use(). If the promise was pre-awaited (forceAwait
|
|
44
|
+
// or isAction), the prop is a raw array we can index into.
|
|
45
|
+
if (
|
|
46
|
+
props.loaderIds &&
|
|
47
|
+
Array.isArray(props.loaderIds) &&
|
|
48
|
+
props.loaderDataPromise &&
|
|
49
|
+
!(props.loaderDataPromise instanceof Promise)
|
|
50
|
+
) {
|
|
51
|
+
const idx = (props.loaderIds as string[]).indexOf(loaderId);
|
|
52
|
+
if (idx !== -1) {
|
|
53
|
+
const data = (props.loaderDataPromise as any[])[idx];
|
|
54
|
+
// loaderDataPromise entries may be { ok, data } result objects
|
|
55
|
+
if (data && typeof data === "object" && "ok" in data) {
|
|
56
|
+
return data.ok ? data.data : NOT_FOUND;
|
|
57
|
+
}
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Traverse into wrapper elements (MountContextProvider, ViewTransition,
|
|
63
|
+
// Suspense wrappers, etc.)
|
|
64
|
+
if (props.children) return extractContentLoaderData(props.children, loaderId);
|
|
65
|
+
return NOT_FOUND;
|
|
66
|
+
}
|
|
67
|
+
|
|
7
68
|
/**
|
|
8
69
|
* Payload returned by loader RSC requests
|
|
9
70
|
*/
|
|
@@ -71,19 +132,27 @@ function useLoaderInternal<T>(
|
|
|
71
132
|
const context = useContext(OutletContext);
|
|
72
133
|
|
|
73
134
|
// Get data from context (SSR/navigation)
|
|
74
|
-
const
|
|
135
|
+
const contextData = useMemo((): T | undefined => {
|
|
75
136
|
let current: OutletContextValue | null | undefined = context;
|
|
76
137
|
while (current) {
|
|
77
138
|
if (current.loaderData && loader.$$id in current.loaderData) {
|
|
78
139
|
return current.loaderData[loader.$$id] as T;
|
|
79
140
|
}
|
|
141
|
+
// Check content element — the route's OutletProvider is rendered as
|
|
142
|
+
// <Outlet /> content (a child), so its loaderData isn't in the parent
|
|
143
|
+
// chain. Parallel slots need to reach into it to find route-level loaders.
|
|
144
|
+
const contentData = extractContentLoaderData(
|
|
145
|
+
current.content,
|
|
146
|
+
loader.$$id,
|
|
147
|
+
);
|
|
148
|
+
if (contentData !== NOT_FOUND) {
|
|
149
|
+
return contentData as T;
|
|
150
|
+
}
|
|
80
151
|
current = current.parent;
|
|
81
152
|
}
|
|
82
153
|
return undefined;
|
|
83
154
|
}, [context, loader.$$id]);
|
|
84
155
|
|
|
85
|
-
const contextData = getContextData();
|
|
86
|
-
|
|
87
156
|
// Local state for fetched data (from load() calls)
|
|
88
157
|
const [fetchedData, setFetchedData] = useState<T | undefined>(undefined);
|
|
89
158
|
const [isLoading, setIsLoading] = useState(false);
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { resolve } from "node:path";
|
|
9
|
-
import {
|
|
10
|
-
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
|
|
9
|
+
import { readFileSync, writeFileSync, existsSync } from "node:fs";
|
|
11
10
|
import { evictHandlerCode } from "../utils/bundle-analysis.js";
|
|
11
|
+
import { copyStagedBuildAssets } from "../utils/prerender-utils.js";
|
|
12
12
|
import type { DiscoveryState } from "./state.js";
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -17,11 +17,11 @@ import type { DiscoveryState } from "./state.js";
|
|
|
17
17
|
*/
|
|
18
18
|
export function postprocessBundle(state: DiscoveryState): void {
|
|
19
19
|
const hasPrerenderData =
|
|
20
|
-
state.
|
|
21
|
-
Object.keys(state.
|
|
20
|
+
state.prerenderManifestEntries &&
|
|
21
|
+
Object.keys(state.prerenderManifestEntries).length > 0;
|
|
22
22
|
const hasStaticData =
|
|
23
|
-
state.
|
|
24
|
-
Object.keys(state.
|
|
23
|
+
state.staticManifestEntries &&
|
|
24
|
+
Object.keys(state.staticManifestEntries).length > 0;
|
|
25
25
|
if (!hasPrerenderData && !hasStaticData) return;
|
|
26
26
|
|
|
27
27
|
// Find RSC entry (recorded in generateBundle, fallback to dist/rsc/index.js)
|
|
@@ -31,25 +31,25 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
31
31
|
state.rscEntryFileName ?? "index.js",
|
|
32
32
|
);
|
|
33
33
|
|
|
34
|
-
// 1. Evict handler code from
|
|
35
|
-
//
|
|
34
|
+
// 1. Evict handler code from whichever chunks contain handler exports.
|
|
35
|
+
// handlerChunkInfoMap/staticHandlerChunkInfoMap are populated by generateBundle
|
|
36
36
|
// after the production RSC build. In Vite 6 multi-environment builds, the
|
|
37
|
-
// RSC build runs twice (analysis + production).
|
|
38
|
-
//
|
|
37
|
+
// RSC build runs twice (analysis + production). The maps are cleared at the
|
|
38
|
+
// start of each generateBundle pass so only production data is used here.
|
|
39
39
|
const evictionTargets: Array<{
|
|
40
|
-
|
|
40
|
+
infos: Iterable<import("./state.js").ChunkInfo>;
|
|
41
41
|
fnName: string;
|
|
42
42
|
brand: string;
|
|
43
43
|
label: string;
|
|
44
44
|
}> = [
|
|
45
45
|
{
|
|
46
|
-
|
|
46
|
+
infos: state.handlerChunkInfoMap.values(),
|
|
47
47
|
fnName: "Prerender",
|
|
48
48
|
brand: "prerenderHandler",
|
|
49
49
|
label: "handler code from RSC bundle",
|
|
50
50
|
},
|
|
51
51
|
{
|
|
52
|
-
|
|
52
|
+
infos: state.staticHandlerChunkInfoMap.values(),
|
|
53
53
|
fnName: "Static",
|
|
54
54
|
brand: "staticHandler",
|
|
55
55
|
label: "static handler code",
|
|
@@ -57,70 +57,58 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
57
57
|
];
|
|
58
58
|
|
|
59
59
|
for (const target of evictionTargets) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
60
|
+
for (const info of target.infos) {
|
|
61
|
+
const chunkPath = resolve(state.projectRoot, "dist/rsc", info.fileName);
|
|
62
|
+
try {
|
|
63
|
+
const code = readFileSync(chunkPath, "utf-8");
|
|
64
|
+
const result = evictHandlerCode(
|
|
65
|
+
code,
|
|
66
|
+
info.exports,
|
|
67
|
+
target.fnName,
|
|
68
|
+
target.brand,
|
|
69
|
+
);
|
|
70
|
+
if (result) {
|
|
71
|
+
writeFileSync(chunkPath, result.code);
|
|
72
|
+
const savedKB = (result.savedBytes / 1024).toFixed(1);
|
|
73
|
+
console.log(
|
|
74
|
+
`[rsc-router] Evicted ${target.label} (${savedKB} KB saved): ${info.fileName}`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
} catch (replaceErr: any) {
|
|
78
|
+
console.warn(
|
|
79
|
+
`[rsc-router] Failed to evict ${target.label}: ${replaceErr.message}`,
|
|
79
80
|
);
|
|
80
81
|
}
|
|
81
|
-
} catch (replaceErr: any) {
|
|
82
|
-
console.warn(
|
|
83
|
-
`[rsc-router] Failed to evict ${target.label}: ${replaceErr.message}`,
|
|
84
|
-
);
|
|
85
82
|
}
|
|
86
83
|
}
|
|
87
|
-
state.
|
|
88
|
-
state.
|
|
84
|
+
state.handlerChunkInfoMap.clear();
|
|
85
|
+
state.staticHandlerChunkInfoMap.clear();
|
|
89
86
|
|
|
90
87
|
// 2. Write prerender data as separate importable asset modules
|
|
91
|
-
// and inject a manifest
|
|
88
|
+
// and inject a lazy manifest loader into the RSC entry.
|
|
92
89
|
if (hasPrerenderData && existsSync(rscEntryPath)) {
|
|
93
90
|
const rscCode = readFileSync(rscEntryPath, "utf-8");
|
|
94
|
-
// Check for the specific injection marker
|
|
95
|
-
// The runtime code (prerender store) also references __PRERENDER_MANIFEST,
|
|
96
|
-
// so a broad string check would false-positive and skip injection.
|
|
91
|
+
// Check for the specific injection marker to avoid double-injection.
|
|
97
92
|
if (!rscCode.includes("__prerender-manifest.js")) {
|
|
98
93
|
try {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
let totalBytes = 0;
|
|
94
|
+
let totalBytes = copyStagedBuildAssets(
|
|
95
|
+
state.projectRoot,
|
|
96
|
+
Object.values(state.prerenderManifestEntries!),
|
|
97
|
+
);
|
|
104
98
|
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
const manifestMap: Record<string, string> = {};
|
|
100
|
+
for (const [key, assetFileName] of Object.entries(
|
|
101
|
+
state.prerenderManifestEntries!,
|
|
107
102
|
)) {
|
|
108
|
-
|
|
109
|
-
const contentHash = createHash("sha256")
|
|
110
|
-
.update(entryJson)
|
|
111
|
-
.digest("hex")
|
|
112
|
-
.slice(0, 8);
|
|
113
|
-
const assetFileName = `__pr-${contentHash}.js`;
|
|
114
|
-
const assetPath = resolve(assetsDir, assetFileName);
|
|
115
|
-
const assetCode = `export default ${entryJson};\n`;
|
|
116
|
-
writeFileSync(assetPath, assetCode);
|
|
117
|
-
totalBytes += Buffer.byteLength(assetCode);
|
|
118
|
-
manifestEntries.push(
|
|
119
|
-
`${JSON.stringify(key)}:()=>import("./assets/${assetFileName}")`,
|
|
120
|
-
);
|
|
103
|
+
manifestMap[key] = `./assets/${assetFileName}`;
|
|
121
104
|
}
|
|
122
105
|
|
|
123
|
-
const manifestCode =
|
|
106
|
+
const manifestCode = [
|
|
107
|
+
`const m=JSON.parse('${JSON.stringify(manifestMap).replace(/'/g, "\\'")}');`,
|
|
108
|
+
`export function loadPrerenderAsset(s){return import(s)}`,
|
|
109
|
+
`export default m;`,
|
|
110
|
+
"",
|
|
111
|
+
].join("\n");
|
|
124
112
|
const manifestPath = resolve(
|
|
125
113
|
state.projectRoot,
|
|
126
114
|
"dist/rsc/__prerender-manifest.js",
|
|
@@ -128,12 +116,12 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
128
116
|
writeFileSync(manifestPath, manifestCode);
|
|
129
117
|
totalBytes += Buffer.byteLength(manifestCode);
|
|
130
118
|
|
|
131
|
-
const injection = `
|
|
119
|
+
const injection = `globalThis.__loadPrerenderManifestModule = () => import("./__prerender-manifest.js");\n`;
|
|
132
120
|
writeFileSync(rscEntryPath, injection + rscCode);
|
|
133
121
|
|
|
134
122
|
const totalKB = (totalBytes / 1024).toFixed(1);
|
|
135
123
|
console.log(
|
|
136
|
-
`[rsc-router] Wrote prerender assets (${totalKB} KB total, ${Object.keys(state.
|
|
124
|
+
`[rsc-router] Wrote prerender assets (${totalKB} KB total, ${Object.keys(state.prerenderManifestEntries!).length} entries)`,
|
|
137
125
|
);
|
|
138
126
|
} catch (err: any) {
|
|
139
127
|
throw new Error(
|
|
@@ -147,33 +135,17 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
147
135
|
// and inject a __STATIC_MANIFEST import into the RSC entry.
|
|
148
136
|
if (hasStaticData && existsSync(rscEntryPath)) {
|
|
149
137
|
const rscCode = readFileSync(rscEntryPath, "utf-8");
|
|
150
|
-
if (!rscCode.includes("
|
|
138
|
+
if (!rscCode.includes("__static-manifest.js")) {
|
|
151
139
|
try {
|
|
152
|
-
const assetsDir = resolve(state.projectRoot, "dist/rsc/assets");
|
|
153
|
-
mkdirSync(assetsDir, { recursive: true });
|
|
154
|
-
|
|
155
140
|
const manifestEntries: string[] = [];
|
|
156
|
-
let totalBytes =
|
|
141
|
+
let totalBytes = copyStagedBuildAssets(
|
|
142
|
+
state.projectRoot,
|
|
143
|
+
Object.values(state.staticManifestEntries!),
|
|
144
|
+
);
|
|
157
145
|
|
|
158
|
-
for (const [handlerId,
|
|
159
|
-
state.
|
|
146
|
+
for (const [handlerId, assetFileName] of Object.entries(
|
|
147
|
+
state.staticManifestEntries!,
|
|
160
148
|
)) {
|
|
161
|
-
// Store both the Flight payload and handle data
|
|
162
|
-
const hasHandles = Object.keys(handles).length > 0;
|
|
163
|
-
const exportValue = hasHandles
|
|
164
|
-
? JSON.stringify({ encoded, handles })
|
|
165
|
-
: JSON.stringify(encoded);
|
|
166
|
-
// Hash the full payload that is written so distinct handle
|
|
167
|
-
// snapshots produce distinct asset filenames.
|
|
168
|
-
const contentHash = createHash("sha256")
|
|
169
|
-
.update(exportValue)
|
|
170
|
-
.digest("hex")
|
|
171
|
-
.slice(0, 8);
|
|
172
|
-
const assetFileName = `__st-${contentHash}.js`;
|
|
173
|
-
const assetPath = resolve(assetsDir, assetFileName);
|
|
174
|
-
const assetCode = `export default ${exportValue};\n`;
|
|
175
|
-
writeFileSync(assetPath, assetCode);
|
|
176
|
-
totalBytes += Buffer.byteLength(assetCode);
|
|
177
149
|
manifestEntries.push(
|
|
178
150
|
`${JSON.stringify(handlerId)}:()=>import("./assets/${assetFileName}")`,
|
|
179
151
|
);
|
|
@@ -197,7 +169,7 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
197
169
|
|
|
198
170
|
const totalKB = (totalBytes / 1024).toFixed(1);
|
|
199
171
|
console.log(
|
|
200
|
-
`[rsc-router] Wrote static assets (${totalKB} KB total, ${Object.keys(state.
|
|
172
|
+
`[rsc-router] Wrote static assets (${totalKB} KB total, ${Object.keys(state.staticManifestEntries!).length} entries)`,
|
|
201
173
|
);
|
|
202
174
|
} catch (err: any) {
|
|
203
175
|
throw new Error(
|
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
* router, and builds route tries for O(path_length) matching.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
buildCombinedRouteMapForRouterFile,
|
|
11
|
+
formatNestedRouterConflictError,
|
|
12
|
+
findNestedRouterConflict,
|
|
13
|
+
} from "../../build/generate-route-types.js";
|
|
10
14
|
import {
|
|
11
15
|
flattenLeafEntries,
|
|
12
16
|
buildRouteToStaticPrefix,
|
|
@@ -44,9 +48,8 @@ export async function discoverRouters(
|
|
|
44
48
|
// No RSC routers found directly. Check for host routers with lazy handlers
|
|
45
49
|
// that need to be resolved to trigger sub-app createRouter() calls.
|
|
46
50
|
try {
|
|
47
|
-
const hostMod = await rscEnv.runner.import("@rangojs/router/host");
|
|
48
51
|
const hostRegistry: Map<string, any> | undefined =
|
|
49
|
-
|
|
52
|
+
serverMod.HostRouterRegistry;
|
|
50
53
|
|
|
51
54
|
if (hostRegistry && hostRegistry.size > 0) {
|
|
52
55
|
console.log(
|
|
@@ -85,7 +88,7 @@ export async function discoverRouters(
|
|
|
85
88
|
}
|
|
86
89
|
}
|
|
87
90
|
} catch {
|
|
88
|
-
//
|
|
91
|
+
// Host-router discovery is best-effort; skip if unavailable
|
|
89
92
|
}
|
|
90
93
|
|
|
91
94
|
// If still no routers after host router resolution, fail
|
|
@@ -100,6 +103,17 @@ export async function discoverRouters(
|
|
|
100
103
|
const buildMod = await rscEnv.runner.import("@rangojs/router/build");
|
|
101
104
|
const generateManifestFull = buildMod.generateManifestFull;
|
|
102
105
|
|
|
106
|
+
const nestedRouterConflict = findNestedRouterConflict(
|
|
107
|
+
[...registry.values()]
|
|
108
|
+
.map((router) => router.__sourceFile)
|
|
109
|
+
.filter(
|
|
110
|
+
(sourceFile): sourceFile is string => typeof sourceFile === "string",
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
if (nestedRouterConflict) {
|
|
114
|
+
throw new Error(formatNestedRouterConflictError(nestedRouterConflict));
|
|
115
|
+
}
|
|
116
|
+
|
|
103
117
|
// Build into local variables first. Only commit to state after the
|
|
104
118
|
// full pass succeeds, so a failed re-discovery preserves the last
|
|
105
119
|
// known-good state instead of leaving it partially wiped.
|
|
@@ -121,7 +135,11 @@ export async function discoverRouters(
|
|
|
121
135
|
continue;
|
|
122
136
|
}
|
|
123
137
|
|
|
124
|
-
const manifest = generateManifestFull(
|
|
138
|
+
const manifest = generateManifestFull(
|
|
139
|
+
router.urlpatterns,
|
|
140
|
+
routerMountIndex,
|
|
141
|
+
router.__basename ? { urlPrefix: router.__basename } : undefined,
|
|
142
|
+
);
|
|
125
143
|
routerMountIndex++;
|
|
126
144
|
allManifests.push({ id, manifest });
|
|
127
145
|
const routeCount = Object.keys(manifest.routeManifest).length;
|
|
@@ -13,12 +13,14 @@ import {
|
|
|
13
13
|
runWithConcurrency,
|
|
14
14
|
groupByConcurrency,
|
|
15
15
|
notifyOnError,
|
|
16
|
+
stageBuildAssetModule,
|
|
16
17
|
} from "../utils/prerender-utils.js";
|
|
17
18
|
import type { DiscoveryState } from "./state.js";
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* Expand prerender routes into concrete URLs and render them via the
|
|
21
|
-
* RSC runner.
|
|
22
|
+
* RSC runner. Stages asset modules and stores key-to-file entries in
|
|
23
|
+
* state.prerenderManifestEntries.
|
|
22
24
|
*/
|
|
23
25
|
export async function expandPrerenderRoutes(
|
|
24
26
|
state: DiscoveryState,
|
|
@@ -52,11 +54,12 @@ export async function expandPrerenderRoutes(
|
|
|
52
54
|
for (const { manifest } of allManifests) {
|
|
53
55
|
if (!manifest.prerenderRoutes) continue;
|
|
54
56
|
const defs = manifest._prerenderDefs || {};
|
|
57
|
+
const passthroughSet = new Set(manifest.passthroughRoutes || []);
|
|
55
58
|
for (const routeName of manifest.prerenderRoutes) {
|
|
56
59
|
const pattern = manifest.routeManifest[routeName];
|
|
57
60
|
if (!pattern) continue;
|
|
58
61
|
const def = defs[routeName];
|
|
59
|
-
const isPassthroughRoute =
|
|
62
|
+
const isPassthroughRoute = passthroughSet.has(routeName);
|
|
60
63
|
const hasDynamic = pattern.includes(":") || pattern.includes("*");
|
|
61
64
|
if (!hasDynamic) {
|
|
62
65
|
// Static route: use pattern directly (strip trailing slash for URL)
|
|
@@ -71,12 +74,21 @@ export async function expandPrerenderRoutes(
|
|
|
71
74
|
if (def?.getParams) {
|
|
72
75
|
try {
|
|
73
76
|
const buildVars: Record<string, any> = {};
|
|
77
|
+
const buildEnv = state.resolvedBuildEnv;
|
|
74
78
|
const getParamsCtx = {
|
|
75
79
|
build: true as const,
|
|
80
|
+
dev: !state.isBuildMode,
|
|
76
81
|
set: ((keyOrVar: any, value: any) => {
|
|
77
82
|
contextSet(buildVars, keyOrVar, value);
|
|
78
83
|
}) as any,
|
|
79
84
|
reverse: getParamsReverse,
|
|
85
|
+
get env() {
|
|
86
|
+
if (buildEnv !== undefined) return buildEnv;
|
|
87
|
+
throw new Error(
|
|
88
|
+
"[rsc-router] ctx.env is not available during build-time getParams(). " +
|
|
89
|
+
"Configure buildEnv in your rango() plugin options to enable build-time env access.",
|
|
90
|
+
);
|
|
91
|
+
},
|
|
80
92
|
};
|
|
81
93
|
const paramsList = await def.getParams(getParamsCtx);
|
|
82
94
|
const concurrency = def.options?.concurrency ?? 1;
|
|
@@ -150,7 +162,7 @@ export async function expandPrerenderRoutes(
|
|
|
150
162
|
|
|
151
163
|
const { hashParams } = await rscEnv.runner.import("@rangojs/router/build");
|
|
152
164
|
|
|
153
|
-
const
|
|
165
|
+
const manifestEntries: Record<string, string> = {};
|
|
154
166
|
let doneCount = 0;
|
|
155
167
|
let skipCount = 0;
|
|
156
168
|
const startTotal = performance.now();
|
|
@@ -173,6 +185,7 @@ export async function expandPrerenderRoutes(
|
|
|
173
185
|
{},
|
|
174
186
|
entry.buildVars,
|
|
175
187
|
entry.isPassthroughRoute,
|
|
188
|
+
state.resolvedBuildEnv,
|
|
176
189
|
);
|
|
177
190
|
if (!result) continue;
|
|
178
191
|
|
|
@@ -187,18 +200,30 @@ export async function expandPrerenderRoutes(
|
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
const paramHash = hashParams(result.params || {});
|
|
190
|
-
|
|
203
|
+
const mainKey = `${result.routeName}/${paramHash}`;
|
|
204
|
+
const mainValue = JSON.stringify({
|
|
191
205
|
segments: result.segments,
|
|
192
206
|
handles: result.handles,
|
|
193
|
-
};
|
|
207
|
+
});
|
|
208
|
+
manifestEntries[mainKey] = stageBuildAssetModule(
|
|
209
|
+
state.projectRoot,
|
|
210
|
+
"__pr",
|
|
211
|
+
mainValue,
|
|
212
|
+
);
|
|
194
213
|
if (result.interceptSegments?.length) {
|
|
195
|
-
|
|
214
|
+
const interceptKey = `${result.routeName}/${paramHash}/i`;
|
|
215
|
+
const interceptValue = JSON.stringify({
|
|
196
216
|
segments: [...result.segments, ...result.interceptSegments],
|
|
197
217
|
handles: {
|
|
198
218
|
...result.handles,
|
|
199
219
|
...(result.interceptHandles || {}),
|
|
200
220
|
},
|
|
201
|
-
};
|
|
221
|
+
});
|
|
222
|
+
manifestEntries[interceptKey] = stageBuildAssetModule(
|
|
223
|
+
state.projectRoot,
|
|
224
|
+
"__pr",
|
|
225
|
+
interceptValue,
|
|
226
|
+
);
|
|
202
227
|
}
|
|
203
228
|
const elapsed = (performance.now() - startUrl).toFixed(0);
|
|
204
229
|
console.log(
|
|
@@ -244,7 +269,7 @@ export async function expandPrerenderRoutes(
|
|
|
244
269
|
|
|
245
270
|
const totalElapsed = (performance.now() - startTotal).toFixed(0);
|
|
246
271
|
if (doneCount > 0) {
|
|
247
|
-
state.
|
|
272
|
+
state.prerenderManifestEntries = manifestEntries;
|
|
248
273
|
}
|
|
249
274
|
const parts = [`${doneCount} done`];
|
|
250
275
|
if (skipCount > 0) parts.push(`${skipCount} skipped`);
|
|
@@ -256,7 +281,8 @@ export async function expandPrerenderRoutes(
|
|
|
256
281
|
/**
|
|
257
282
|
* Render Static handlers at build time. Each Static handler is called
|
|
258
283
|
* with a synthetic BuildContext and its output is RSC-serialized.
|
|
259
|
-
*
|
|
284
|
+
* Stages asset modules and stores handlerId-to-file entries in
|
|
285
|
+
* state.staticManifestEntries.
|
|
260
286
|
*/
|
|
261
287
|
export async function renderStaticHandlers(
|
|
262
288
|
state: DiscoveryState,
|
|
@@ -270,10 +296,7 @@ export async function renderStaticHandlers(
|
|
|
270
296
|
)
|
|
271
297
|
return;
|
|
272
298
|
|
|
273
|
-
const
|
|
274
|
-
string,
|
|
275
|
-
{ encoded: string; handles: Record<string, unknown[]> }
|
|
276
|
-
> = {};
|
|
299
|
+
const manifestEntries: Record<string, string> = {};
|
|
277
300
|
let staticDone = 0;
|
|
278
301
|
let staticSkip = 0;
|
|
279
302
|
let totalStaticCount = 0;
|
|
@@ -314,9 +337,19 @@ export async function renderStaticHandlers(
|
|
|
314
337
|
def.handler,
|
|
315
338
|
def.$$id,
|
|
316
339
|
(def as any).$$routePrefix,
|
|
340
|
+
state.resolvedBuildEnv,
|
|
341
|
+
!state.isBuildMode,
|
|
317
342
|
);
|
|
318
343
|
if (result) {
|
|
319
|
-
|
|
344
|
+
const hasHandles = Object.keys(result.handles).length > 0;
|
|
345
|
+
const exportValue = hasHandles
|
|
346
|
+
? JSON.stringify(result)
|
|
347
|
+
: JSON.stringify(result.encoded);
|
|
348
|
+
manifestEntries[def.$$id] = stageBuildAssetModule(
|
|
349
|
+
state.projectRoot,
|
|
350
|
+
"__st",
|
|
351
|
+
exportValue,
|
|
352
|
+
);
|
|
320
353
|
const elapsed = (performance.now() - startHandler).toFixed(0);
|
|
321
354
|
console.log(
|
|
322
355
|
`[rsc-router] OK ${name.padEnd(40)} (${elapsed}ms)`,
|
|
@@ -355,7 +388,7 @@ export async function renderStaticHandlers(
|
|
|
355
388
|
|
|
356
389
|
const totalStaticElapsed = (performance.now() - startStatic).toFixed(0);
|
|
357
390
|
if (staticDone > 0) {
|
|
358
|
-
state.
|
|
391
|
+
state.staticManifestEntries = manifestEntries;
|
|
359
392
|
}
|
|
360
393
|
const staticParts = [`${staticDone} done`];
|
|
361
394
|
if (staticSkip > 0) staticParts.push(`${staticSkip} skipped`);
|
|
@@ -13,11 +13,13 @@ export const VIRTUAL_ROUTES_MANIFEST_ID = "virtual:rsc-router/routes-manifest";
|
|
|
13
13
|
export interface PluginOptions {
|
|
14
14
|
enableBuildPrerender?: boolean;
|
|
15
15
|
staticRouteTypesGeneration?: boolean;
|
|
16
|
-
include?: string[];
|
|
17
|
-
exclude?: string[];
|
|
18
16
|
// Mutable ref for deferred auto-discovery (node preset).
|
|
19
17
|
// The auto-discover config() hook populates this before configResolved.
|
|
20
18
|
routerPathRef?: { path?: string };
|
|
19
|
+
/** Build-time env option from rango() config. */
|
|
20
|
+
buildEnv?: import("../plugin-types.js").BuildEnvOption;
|
|
21
|
+
/** Deployment preset (needed for buildEnv "auto" resolution). */
|
|
22
|
+
preset?: "node" | "cloudflare";
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export interface PrecomputedEntry {
|
|
@@ -56,13 +58,10 @@ export interface DiscoveryState {
|
|
|
56
58
|
perRouterPrecomputedMap: Map<string, PrecomputedEntry[]>;
|
|
57
59
|
perRouterManifestDataMap: Map<string, Record<string, string>>;
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
> | null;
|
|
64
|
-
handlerChunkInfo: ChunkInfo | null;
|
|
65
|
-
staticHandlerChunkInfo: ChunkInfo | null;
|
|
61
|
+
prerenderManifestEntries: Record<string, string> | null;
|
|
62
|
+
staticManifestEntries: Record<string, string> | null;
|
|
63
|
+
handlerChunkInfoMap: Map<string, ChunkInfo>;
|
|
64
|
+
staticHandlerChunkInfoMap: Map<string, ChunkInfo>;
|
|
66
65
|
rscEntryFileName: string | null;
|
|
67
66
|
resolvedPrerenderModules: Map<string, string[]> | undefined;
|
|
68
67
|
resolvedStaticModules: Map<string, string[]> | undefined;
|
|
@@ -72,6 +71,11 @@ export interface DiscoveryState {
|
|
|
72
71
|
devServer: any;
|
|
73
72
|
selfWrittenGenFiles: Map<string, { at: number; hash: string }>;
|
|
74
73
|
SELF_WRITE_WINDOW_MS: number;
|
|
74
|
+
|
|
75
|
+
/** Resolved build-time env bindings (set during buildStart/configureServer). */
|
|
76
|
+
resolvedBuildEnv?: Record<string, unknown>;
|
|
77
|
+
/** Cleanup function for build-time env resources (e.g., miniflare). */
|
|
78
|
+
buildEnvDispose?: (() => Promise<void> | void) | null;
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
export function createDiscoveryState(
|
|
@@ -96,10 +100,10 @@ export function createDiscoveryState(
|
|
|
96
100
|
perRouterPrecomputedMap: new Map(),
|
|
97
101
|
perRouterManifestDataMap: new Map(),
|
|
98
102
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
prerenderManifestEntries: null,
|
|
104
|
+
staticManifestEntries: null,
|
|
105
|
+
handlerChunkInfoMap: new Map(),
|
|
106
|
+
staticHandlerChunkInfoMap: new Map(),
|
|
103
107
|
rscEntryFileName: null,
|
|
104
108
|
resolvedPrerenderModules: undefined,
|
|
105
109
|
resolvedStaticModules: undefined,
|
package/src/vite/index.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Public API for @rangojs/router/vite
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* consumed via direct imports within the package.
|
|
4
|
+
* Exports: rango() plugin factory, poke() dev utility plugin,
|
|
5
|
+
* and related option types. All other utilities are internal implementation
|
|
6
|
+
* details consumed via direct imports within the package.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
export { rango } from "./rango.js";
|
|
10
|
+
export { poke } from "./plugins/refresh-cmd.js";
|
|
10
11
|
|
|
11
12
|
export type {
|
|
12
13
|
RangoNodeOptions,
|
|
13
14
|
RangoCloudflareOptions,
|
|
14
15
|
RangoOptions,
|
|
16
|
+
BuildEnvOption,
|
|
17
|
+
BuildEnvFactory,
|
|
18
|
+
BuildEnvFactoryContext,
|
|
19
|
+
BuildEnvResult,
|
|
15
20
|
} from "./plugin-types.js";
|