@rangojs/router 0.0.0-experimental.77 → 0.0.0-experimental.77ed8945
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 +120 -25
- package/dist/bin/rango.js +147 -57
- package/dist/vite/index.js +2103 -861
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +13 -8
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +220 -30
- package/skills/caching/SKILL.md +116 -8
- package/skills/composability/SKILL.md +27 -2
- package/skills/css/SKILL.md +76 -0
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +3 -1
- package/skills/hooks/SKILL.md +229 -20
- package/skills/host-router/SKILL.md +66 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +26 -4
- package/skills/layout/SKILL.md +6 -7
- package/skills/links/SKILL.md +247 -17
- package/skills/loader/SKILL.md +219 -9
- package/skills/middleware/SKILL.md +47 -12
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +12 -6
- package/skills/prerender/SKILL.md +14 -33
- package/skills/rango/SKILL.md +238 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +122 -47
- package/skills/route/SKILL.md +33 -4
- package/skills/router-setup/SKILL.md +3 -3
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/tailwind/SKILL.md +27 -3
- package/skills/typesafety/SKILL.md +319 -27
- package/skills/use-cache/SKILL.md +34 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +39 -0
- package/src/browser/event-controller.ts +86 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/navigation-bridge.ts +29 -9
- package/src/browser/navigation-client.ts +99 -77
- package/src/browser/navigation-store.ts +7 -8
- package/src/browser/navigation-transaction.ts +10 -28
- package/src/browser/partial-update.ts +60 -40
- package/src/browser/prefetch/cache.ts +196 -49
- package/src/browser/prefetch/fetch.ts +203 -59
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +37 -13
- package/src/browser/react/Link.tsx +18 -13
- package/src/browser/react/NavigationProvider.tsx +75 -31
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +17 -9
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +23 -2
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +52 -1
- package/src/browser/rsc-router.tsx +71 -22
- package/src/browser/scroll-restoration.ts +22 -14
- package/src/browser/segment-reconciler.ts +10 -14
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +44 -30
- package/src/browser/types.ts +12 -2
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +60 -35
- package/src/build/generate-route-types.ts +2 -0
- package/src/build/index.ts +8 -1
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +45 -1
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +1 -1
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +55 -14
- package/src/build/route-types/scan-filter.ts +1 -1
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-runtime.ts +17 -5
- package/src/cache/cache-scope.ts +51 -49
- package/src/cache/cf/cf-cache-store.ts +502 -32
- package/src/cache/cf/index.ts +3 -0
- package/src/cache/handle-snapshot.ts +103 -0
- package/src/cache/index.ts +3 -0
- package/src/cache/memory-segment-store.ts +3 -2
- package/src/cache/types.ts +10 -6
- package/src/client.rsc.tsx +3 -0
- package/src/client.tsx +96 -205
- package/src/context-var.ts +5 -5
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -4
- package/src/handle.ts +4 -6
- package/src/host/index.ts +2 -2
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -21
- package/src/index.rsc.ts +10 -6
- package/src/index.ts +17 -8
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +2 -5
- package/src/loader.ts +3 -10
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +9 -7
- package/src/prerender.ts +4 -4
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -39
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +253 -265
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +43 -15
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-types.ts +26 -41
- package/src/router/content-negotiation.ts +15 -2
- package/src/router/error-handling.ts +1 -1
- package/src/router/find-match.ts +54 -6
- package/src/router/handler-context.ts +21 -41
- package/src/router/intercept-resolution.ts +4 -18
- package/src/router/lazy-includes.ts +41 -22
- package/src/router/loader-resolution.ts +82 -36
- package/src/router/manifest.ts +41 -19
- package/src/router/match-api.ts +4 -3
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-middleware/cache-lookup.ts +57 -95
- package/src/router/match-middleware/cache-store.ts +3 -2
- package/src/router/match-result.ts +53 -32
- package/src/router/metrics.ts +1 -1
- package/src/router/middleware-types.ts +15 -26
- package/src/router/middleware.ts +99 -84
- package/src/router/pattern-matching.ts +116 -19
- package/src/router/prerender-match.ts +40 -15
- package/src/router/preview-match.ts +3 -1
- package/src/router/request-classification.ts +40 -37
- package/src/router/revalidation.ts +58 -2
- package/src/router/router-interfaces.ts +51 -35
- package/src/router/router-options.ts +25 -1
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +27 -6
- package/src/router/segment-resolution/revalidation.ts +147 -106
- package/src/router/segment-resolution/static-store.ts +19 -5
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/trie-matching.ts +40 -16
- package/src/router/types.ts +8 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +37 -25
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +58 -77
- package/src/rsc/helpers.ts +72 -43
- package/src/rsc/index.ts +1 -1
- package/src/rsc/manifest-init.ts +28 -41
- package/src/rsc/origin-guard.ts +30 -10
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +76 -61
- package/src/rsc/rsc-rendering.ts +45 -51
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +33 -39
- package/src/rsc/ssr-setup.ts +16 -0
- package/src/rsc/types.ts +8 -2
- package/src/search-params.ts +4 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +132 -116
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +175 -53
- package/src/server/cookie-store.ts +28 -4
- package/src/server/request-context.ts +57 -51
- package/src/ssr/index.tsx +5 -1
- package/src/static-handler.ts +1 -1
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +68 -50
- package/src/types/index.ts +1 -0
- package/src/types/loader-types.ts +11 -9
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +35 -2
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +1 -5
- package/src/urls/path-helper-types.ts +17 -3
- package/src/urls/path-helper.ts +17 -52
- package/src/urls/pattern-types.ts +36 -19
- package/src/urls/response-types.ts +22 -29
- package/src/urls/type-extraction.ts +58 -139
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +413 -42
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +6 -6
- package/src/vite/discovery/discover-routers.ts +106 -75
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +72 -31
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +33 -0
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +2 -0
- package/src/vite/plugin-types.ts +67 -0
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- 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 +54 -30
- package/src/vite/plugins/expose-id-utils.ts +12 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +496 -486
- package/src/vite/plugins/performance-tracks.ts +29 -25
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +59 -2
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +116 -29
- package/src/vite/router-discovery.ts +753 -104
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +8 -59
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +5 -4
- package/src/vite/utils/shared-utils.ts +107 -26
- package/src/browser/action-response-classifier.ts +0 -99
|
@@ -10,15 +10,20 @@ import type { Plugin } from "vite";
|
|
|
10
10
|
import { createServer as createViteServer } from "vite";
|
|
11
11
|
import { resolve } from "node:path";
|
|
12
12
|
import { readFileSync } from "node:fs";
|
|
13
|
-
import { createRequire } from "node:module";
|
|
13
|
+
import { createRequire, register } from "node:module";
|
|
14
14
|
import { pathToFileURL } from "node:url";
|
|
15
15
|
import {
|
|
16
16
|
formatNestedRouterConflictError,
|
|
17
17
|
findNestedRouterConflict,
|
|
18
18
|
findRouterFiles,
|
|
19
19
|
} from "../build/generate-route-types.js";
|
|
20
|
+
import { firstCodeMatchIndex } from "../build/route-types/source-scan.js";
|
|
20
21
|
import { createVersionPlugin } from "./plugins/version-plugin.js";
|
|
21
22
|
import { createVirtualStubPlugin } from "./plugins/virtual-stub-plugin.js";
|
|
23
|
+
import {
|
|
24
|
+
BUILD_ENV_GLOBAL_KEY,
|
|
25
|
+
createCloudflareProtocolStubPlugin,
|
|
26
|
+
} from "./plugins/cloudflare-protocol-stub.js";
|
|
22
27
|
import {
|
|
23
28
|
exposeInternalIds,
|
|
24
29
|
exposeRouterId,
|
|
@@ -31,7 +36,10 @@ import {
|
|
|
31
36
|
type DiscoveryState,
|
|
32
37
|
type PluginOptions,
|
|
33
38
|
} from "./discovery/state.js";
|
|
34
|
-
import {
|
|
39
|
+
import {
|
|
40
|
+
consumeSelfGenWrite,
|
|
41
|
+
peekSelfGenWrite,
|
|
42
|
+
} from "./discovery/self-gen-tracking.js";
|
|
35
43
|
import { discoverRouters } from "./discovery/discover-routers.js";
|
|
36
44
|
import {
|
|
37
45
|
writeCombinedRouteTypesWithTracking,
|
|
@@ -43,10 +51,65 @@ import {
|
|
|
43
51
|
generatePerRouterModule,
|
|
44
52
|
} from "./discovery/virtual-module-codegen.js";
|
|
45
53
|
import { postprocessBundle } from "./discovery/bundle-postprocess.js";
|
|
54
|
+
import { createDiscoveryGate } from "./discovery/gate-state.js";
|
|
46
55
|
import { resetStagedBuildAssets } from "./utils/prerender-utils.js";
|
|
56
|
+
import { resolveRscEntryFromConfig } from "./utils/shared-utils.js";
|
|
57
|
+
import {
|
|
58
|
+
pickForwardedRunnerConfig,
|
|
59
|
+
selectForwardableResolvePlugins,
|
|
60
|
+
} from "./utils/forward-user-plugins.js";
|
|
61
|
+
import { createRangoDebugger, timed, timedSync, NS } from "./debug.js";
|
|
62
|
+
|
|
63
|
+
const debugDiscovery = createRangoDebugger(NS.discovery);
|
|
64
|
+
const debugRoutes = createRangoDebugger(NS.routes);
|
|
65
|
+
const debugBuild = createRangoDebugger(NS.build);
|
|
66
|
+
const debugDev = createRangoDebugger(NS.dev);
|
|
47
67
|
|
|
48
68
|
export { VIRTUAL_ROUTES_MANIFEST_ID };
|
|
49
69
|
|
|
70
|
+
// ============================================================================
|
|
71
|
+
// Node ESM Loader Hook Registration
|
|
72
|
+
// ============================================================================
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Registers a Node ESM loader hook that resolves `cloudflare:*` specifiers
|
|
76
|
+
* to a data: URL stub. Defense-in-depth alongside the Vite transform in
|
|
77
|
+
* `cloudflare-protocol-stub.ts`:
|
|
78
|
+
*
|
|
79
|
+
* - The Vite transform catches `cloudflare:*` imports in modules that flow
|
|
80
|
+
* through Vite's plugin pipeline. That's the vast majority of cases.
|
|
81
|
+
* - The Node loader catches imports in modules that Vite/Rollup externalize
|
|
82
|
+
* (e.g. the `partyserver` package, which has a top-level
|
|
83
|
+
* `import { DurableObject, env } from "cloudflare:workers"` and ships
|
|
84
|
+
* shapes plugin-rsc marks as external). Externalized modules are loaded
|
|
85
|
+
* via Node's native ESM loader, which rejects URL schemes.
|
|
86
|
+
*
|
|
87
|
+
* Registration is process-global and one-shot. The hook only intercepts
|
|
88
|
+
* `cloudflare:*` specifiers; everything else passes through via
|
|
89
|
+
* `nextResolve()`. It runs in a separate worker thread (Node ESM loader
|
|
90
|
+
* architecture), so it can't read the `globalThis[BUILD_ENV_GLOBAL_KEY]`
|
|
91
|
+
* bridge that the Vite transform uses — the stubs served here always
|
|
92
|
+
* return `env = {}`. That's fine because externalized libraries don't
|
|
93
|
+
* typically access `env` at module top level; user source (where real
|
|
94
|
+
* `env` matters at build time) flows through the Vite transform.
|
|
95
|
+
*/
|
|
96
|
+
let loaderHookRegistered = false;
|
|
97
|
+
function ensureCloudflareProtocolLoaderRegistered(): void {
|
|
98
|
+
if (loaderHookRegistered) return;
|
|
99
|
+
loaderHookRegistered = true;
|
|
100
|
+
try {
|
|
101
|
+
register(
|
|
102
|
+
new URL("./plugins/cloudflare-protocol-loader-hook.mjs", import.meta.url),
|
|
103
|
+
);
|
|
104
|
+
} catch (err: any) {
|
|
105
|
+
// register() requires Node 18.19+ / 20.6+. Older Node still has the
|
|
106
|
+
// Vite transform as primary defense.
|
|
107
|
+
console.warn(
|
|
108
|
+
`[rango] Could not register Node ESM loader hook for cloudflare:* imports (${err?.message ?? err}). Falling back to Vite transform only.`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
50
113
|
// ============================================================================
|
|
51
114
|
// Temp Server Factory
|
|
52
115
|
// ============================================================================
|
|
@@ -66,15 +129,32 @@ async function createTempRscServer(
|
|
|
66
129
|
state: DiscoveryState,
|
|
67
130
|
options: { forceBuild?: boolean; cacheDir?: string } = {},
|
|
68
131
|
) {
|
|
132
|
+
// Install the Node ESM loader hook before any module evaluation so
|
|
133
|
+
// `cloudflare:*` specifiers in externalized/loader-delegated modules
|
|
134
|
+
// (e.g. packages plugin-rsc marks as external) resolve to stubs
|
|
135
|
+
// instead of crashing Node's native loader.
|
|
136
|
+
ensureCloudflareProtocolLoaderRegistered();
|
|
69
137
|
const { default: rsc } = await import("@vitejs/plugin-rsc");
|
|
138
|
+
// Mirror the user's resolution config + plugins so discovery (and the
|
|
139
|
+
// prerender/static rendering that shares this runner) resolves modules the
|
|
140
|
+
// same way the real environment does. Falls back to the legacy alias-only
|
|
141
|
+
// behavior if configResolved hasn't populated the parity slice yet.
|
|
142
|
+
const runnerConfig = state.userRunnerConfig;
|
|
143
|
+
const resolveConfig = runnerConfig?.resolve ?? {
|
|
144
|
+
alias: state.userResolveAlias,
|
|
145
|
+
};
|
|
146
|
+
const oxcConfig = runnerConfig?.oxc ?? {
|
|
147
|
+
jsx: { runtime: "automatic", importSource: "react" },
|
|
148
|
+
};
|
|
70
149
|
return createViteServer({
|
|
71
150
|
root: state.projectRoot,
|
|
72
151
|
configFile: false,
|
|
73
152
|
server: { middlewareMode: true },
|
|
74
153
|
appType: "custom",
|
|
75
154
|
logLevel: "silent",
|
|
76
|
-
resolve:
|
|
77
|
-
|
|
155
|
+
resolve: resolveConfig,
|
|
156
|
+
...(runnerConfig?.define ? { define: runnerConfig.define } : {}),
|
|
157
|
+
oxc: oxcConfig as any,
|
|
78
158
|
...(options.cacheDir && { cacheDir: options.cacheDir }),
|
|
79
159
|
plugins: [
|
|
80
160
|
rsc({
|
|
@@ -88,10 +168,15 @@ async function createTempRscServer(
|
|
|
88
168
|
...(options.forceBuild ? [hashClientRefs(state.projectRoot)] : []),
|
|
89
169
|
createVersionPlugin(),
|
|
90
170
|
createVirtualStubPlugin(),
|
|
171
|
+
createCloudflareProtocolStubPlugin(),
|
|
91
172
|
// Dev prerender must use dev-mode IDs (path-based) to match the workerd
|
|
92
173
|
// runtime. forceBuild produces hashed IDs for production bundle consistency.
|
|
93
174
|
exposeInternalIds(options.forceBuild ? { forceBuild: true } : undefined),
|
|
94
175
|
exposeRouterId(),
|
|
176
|
+
// Forwarded user resolution plugins (e.g. vite-tsconfig-paths). Stripped
|
|
177
|
+
// to resolveId/load and placed last so framework resolution runs first;
|
|
178
|
+
// Vite re-sorts by `enforce`, so `enforce: "pre"` resolvers still lead.
|
|
179
|
+
...state.userResolvePlugins,
|
|
95
180
|
],
|
|
96
181
|
});
|
|
97
182
|
}
|
|
@@ -119,7 +204,7 @@ async function resolveBuildEnv(
|
|
|
119
204
|
if (option === "auto") {
|
|
120
205
|
if (factoryCtx.preset !== "cloudflare") {
|
|
121
206
|
throw new Error(
|
|
122
|
-
'[
|
|
207
|
+
'[rango] buildEnv: "auto" is only supported with preset: "cloudflare". ' +
|
|
123
208
|
"Use a factory function or plain object for other presets.",
|
|
124
209
|
);
|
|
125
210
|
}
|
|
@@ -141,7 +226,7 @@ async function resolveBuildEnv(
|
|
|
141
226
|
};
|
|
142
227
|
} catch (err: any) {
|
|
143
228
|
throw new Error(
|
|
144
|
-
'[
|
|
229
|
+
'[rango] buildEnv: "auto" requires wrangler to be installed.\n' +
|
|
145
230
|
`Install it with: pnpm add -D wrangler\n${err.message}`,
|
|
146
231
|
);
|
|
147
232
|
}
|
|
@@ -177,6 +262,11 @@ async function acquireBuildEnv(
|
|
|
177
262
|
|
|
178
263
|
s.resolvedBuildEnv = result.env;
|
|
179
264
|
s.buildEnvDispose = result.dispose ?? null;
|
|
265
|
+
// Bridge the resolved env into `cloudflare:workers`'s stubbed `env`
|
|
266
|
+
// export so user code that does `import { env } from "cloudflare:workers"`
|
|
267
|
+
// sees the real bindings proxy during discovery + prerender instead of
|
|
268
|
+
// an empty object. The stub reads this global at module-evaluation time.
|
|
269
|
+
(globalThis as Record<string, unknown>)[BUILD_ENV_GLOBAL_KEY] = result.env;
|
|
180
270
|
return true;
|
|
181
271
|
}
|
|
182
272
|
|
|
@@ -188,11 +278,12 @@ async function releaseBuildEnv(s: DiscoveryState): Promise<void> {
|
|
|
188
278
|
try {
|
|
189
279
|
await s.buildEnvDispose();
|
|
190
280
|
} catch (err: any) {
|
|
191
|
-
console.warn(`[
|
|
281
|
+
console.warn(`[rango] buildEnv dispose failed: ${err.message}`);
|
|
192
282
|
}
|
|
193
283
|
s.buildEnvDispose = null;
|
|
194
284
|
}
|
|
195
285
|
s.resolvedBuildEnv = undefined;
|
|
286
|
+
delete (globalThis as Record<string, unknown>)[BUILD_ENV_GLOBAL_KEY];
|
|
196
287
|
}
|
|
197
288
|
|
|
198
289
|
/**
|
|
@@ -240,23 +331,28 @@ export function createRouterDiscoveryPlugin(
|
|
|
240
331
|
viteMode = config.mode;
|
|
241
332
|
// Capture user's resolve aliases for the temp server
|
|
242
333
|
s.userResolveAlias = config.resolve.alias;
|
|
334
|
+
// Capture the data-only resolution config (resolve.*, define, oxc) and
|
|
335
|
+
// the user's resolution plugins (resolveId/load) so the discovery temp
|
|
336
|
+
// server resolves modules the same way the real environment does.
|
|
337
|
+
// Without this, both flavors of user resolution are absent during
|
|
338
|
+
// discovery/prerender/static rendering even though they apply at request
|
|
339
|
+
// time: third-party resolvers (e.g. vite-tsconfig-paths, forwarded as
|
|
340
|
+
// plugins) and Vite 8's native resolve.tsconfigPaths (forwarded in the
|
|
341
|
+
// data slice). See utils/forward-user-plugins.ts.
|
|
342
|
+
s.userRunnerConfig = pickForwardedRunnerConfig(config);
|
|
343
|
+
s.userResolvePlugins = selectForwardableResolvePlugins(
|
|
344
|
+
config.plugins as any,
|
|
345
|
+
);
|
|
243
346
|
// Node preset: pick up auto-discovered router path from the config() hook.
|
|
244
347
|
// The auto-discover plugin runs in config() using Vite's resolved root,
|
|
245
348
|
// populating the mutable ref before configResolved fires.
|
|
246
349
|
if (!s.resolvedEntryPath && opts?.routerPathRef?.path) {
|
|
247
350
|
s.resolvedEntryPath = opts.routerPathRef.path;
|
|
248
351
|
}
|
|
249
|
-
// Cloudflare preset:
|
|
250
|
-
// The @cloudflare/vite-plugin reads wrangler config (toml/json/jsonc)
|
|
251
|
-
// and sets optimizeDeps.entries on the RSC environment.
|
|
352
|
+
// Cloudflare preset: entry comes from the resolved RSC env config.
|
|
252
353
|
if (!s.resolvedEntryPath) {
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
if (typeof entries === "string") {
|
|
256
|
-
s.resolvedEntryPath = entries;
|
|
257
|
-
} else if (Array.isArray(entries) && entries.length > 0) {
|
|
258
|
-
s.resolvedEntryPath = entries[0];
|
|
259
|
-
}
|
|
354
|
+
const entry = resolveRscEntryFromConfig(config);
|
|
355
|
+
if (entry) s.resolvedEntryPath = entry;
|
|
260
356
|
}
|
|
261
357
|
// Generate combined named-routes.gen.ts from static source parsing.
|
|
262
358
|
// Runs before the dev server starts so the gen file exists immediately for IDE.
|
|
@@ -295,6 +391,17 @@ export function createRouterDiscoveryPlugin(
|
|
|
295
391
|
resolveDiscovery = resolve;
|
|
296
392
|
});
|
|
297
393
|
|
|
394
|
+
// Manifest-readiness gate + rediscovery scheduler.
|
|
395
|
+
// The virtual:rsc-router/routes-manifest module's `load()` hook
|
|
396
|
+
// awaits `s.discoveryDone`; the gate is reset on each discovery
|
|
397
|
+
// cycle so workerd's HMR reloads block until the new gen file is
|
|
398
|
+
// written. State machine + transitions are extracted into
|
|
399
|
+
// ./discovery/gate-state.ts and unit-tested there — see the
|
|
400
|
+
// module's JSDoc for the four-flag contract.
|
|
401
|
+
const gate = createDiscoveryGate(s, debugDiscovery);
|
|
402
|
+
const beginDiscoveryGate = gate.beginGate;
|
|
403
|
+
const resolveDiscoveryGate = gate.resolveGate;
|
|
404
|
+
|
|
298
405
|
// Compute dev server origin from resolved URLs (preferred) or config port (fallback).
|
|
299
406
|
// Called after discovery (or in the load hook) when the server may be listening.
|
|
300
407
|
const getDevServerOrigin = () =>
|
|
@@ -317,10 +424,103 @@ export function createRouterDiscoveryPlugin(
|
|
|
317
424
|
releaseBuildEnv(s).catch(() => {});
|
|
318
425
|
});
|
|
319
426
|
|
|
427
|
+
// Mirror the build-path contract (router-discovery.ts ~line 878):
|
|
428
|
+
// set __rscRouterDiscoveryActive before running user modules so any
|
|
429
|
+
// module-level router.reverse() calls return a placeholder instead
|
|
430
|
+
// of throwing. The temp Vite server's module runner has its own
|
|
431
|
+
// module context; the flag must be on globalThis to cross that
|
|
432
|
+
// boundary. Cleared in finally so the dev request handlers run with
|
|
433
|
+
// strict reverse() semantics afterwards.
|
|
434
|
+
async function importEntryAndRegistry(tempRscEnv: any): Promise<void> {
|
|
435
|
+
const flagAlreadySet = !!(globalThis as any).__rscRouterDiscoveryActive;
|
|
436
|
+
if (!flagAlreadySet) {
|
|
437
|
+
(globalThis as any).__rscRouterDiscoveryActive = true;
|
|
438
|
+
}
|
|
439
|
+
try {
|
|
440
|
+
debugDiscovery?.(
|
|
441
|
+
"importEntryAndRegistry: importing entry (flag=%s)",
|
|
442
|
+
(globalThis as any).__rscRouterDiscoveryActive ?? false,
|
|
443
|
+
);
|
|
444
|
+
await tempRscEnv.runner.import(s.resolvedEntryPath!);
|
|
445
|
+
debugDiscovery?.(
|
|
446
|
+
"importEntryAndRegistry: entry import OK, fetching RouterRegistry",
|
|
447
|
+
);
|
|
448
|
+
const serverMod = await tempRscEnv.runner.import(
|
|
449
|
+
"@rangojs/router/server",
|
|
450
|
+
);
|
|
451
|
+
prerenderNodeRegistry = serverMod.RouterRegistry;
|
|
452
|
+
debugDiscovery?.(
|
|
453
|
+
"importEntryAndRegistry: registry size=%d",
|
|
454
|
+
prerenderNodeRegistry?.size ?? 0,
|
|
455
|
+
);
|
|
456
|
+
} finally {
|
|
457
|
+
if (!flagAlreadySet) {
|
|
458
|
+
delete (globalThis as any).__rscRouterDiscoveryActive;
|
|
459
|
+
debugDiscovery?.(
|
|
460
|
+
"importEntryAndRegistry: cleared __rscRouterDiscoveryActive",
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
320
466
|
async function getOrCreateTempServer(): Promise<any | null> {
|
|
321
|
-
if
|
|
322
|
-
|
|
467
|
+
// Reuse path: if a temp server is already alive, prefer reusing
|
|
468
|
+
// it over orphaning the existing instance and spinning up a new
|
|
469
|
+
// one. This handles two cases:
|
|
470
|
+
//
|
|
471
|
+
// 1. Steady-state cache hit (cold-start completed, registry
|
|
472
|
+
// cached) — return the env immediately.
|
|
473
|
+
// 2. Recovery from a failed refresh: refreshTempRscEnv() may
|
|
474
|
+
// have invalidated and nulled the registry, then thrown
|
|
475
|
+
// during importEntryAndRegistry. Without reuse, the next
|
|
476
|
+
// call would `createTempRscServer` and overwrite the
|
|
477
|
+
// handle, leaking the previous server. Try to re-import on
|
|
478
|
+
// the existing runner first; only if THAT fails do we
|
|
479
|
+
// close the orphan and create new.
|
|
480
|
+
if (prerenderTempServer) {
|
|
481
|
+
const existingEnv = (prerenderTempServer.environments as any)?.rsc;
|
|
482
|
+
if (existingEnv?.runner) {
|
|
483
|
+
if (prerenderNodeRegistry) {
|
|
484
|
+
debugDiscovery?.(
|
|
485
|
+
"getOrCreateTempServer: cached temp runner reused",
|
|
486
|
+
);
|
|
487
|
+
return existingEnv;
|
|
488
|
+
}
|
|
489
|
+
// Server alive but registry missing — likely after a prior
|
|
490
|
+
// refresh's invalidate + import threw. Try to re-import.
|
|
491
|
+
debugDiscovery?.(
|
|
492
|
+
"getOrCreateTempServer: server alive but registry missing — re-importing",
|
|
493
|
+
);
|
|
494
|
+
try {
|
|
495
|
+
await importEntryAndRegistry(existingEnv);
|
|
496
|
+
return existingEnv;
|
|
497
|
+
} catch (err: any) {
|
|
498
|
+
debugDiscovery?.(
|
|
499
|
+
"getOrCreateTempServer: reuse import failed (%s) — closing orphan and creating fresh",
|
|
500
|
+
err?.message ?? String(err),
|
|
501
|
+
);
|
|
502
|
+
await prerenderTempServer.close().catch(() => {});
|
|
503
|
+
prerenderTempServer = null;
|
|
504
|
+
prerenderNodeRegistry = null;
|
|
505
|
+
// Fall through to create-new path below.
|
|
506
|
+
}
|
|
507
|
+
} else {
|
|
508
|
+
// Server reference exists but its rsc env is unhealthy
|
|
509
|
+
// (no runner). Close and recreate.
|
|
510
|
+
debugDiscovery?.(
|
|
511
|
+
"getOrCreateTempServer: existing server has no rsc.runner — closing and recreating",
|
|
512
|
+
);
|
|
513
|
+
await prerenderTempServer.close().catch(() => {});
|
|
514
|
+
prerenderTempServer = null;
|
|
515
|
+
prerenderNodeRegistry = null;
|
|
516
|
+
}
|
|
323
517
|
}
|
|
518
|
+
|
|
519
|
+
// Create path: no existing temp server (or just nullified above).
|
|
520
|
+
debugDiscovery?.(
|
|
521
|
+
"getOrCreateTempServer: creating new temp server, entry=%s",
|
|
522
|
+
s.resolvedEntryPath ?? "(unset)",
|
|
523
|
+
);
|
|
324
524
|
try {
|
|
325
525
|
prerenderTempServer = await createTempRscServer(s, {
|
|
326
526
|
cacheDir: "node_modules/.vite_prerender",
|
|
@@ -328,64 +528,189 @@ export function createRouterDiscoveryPlugin(
|
|
|
328
528
|
|
|
329
529
|
const tempRscEnv = (prerenderTempServer.environments as any)?.rsc;
|
|
330
530
|
if (tempRscEnv?.runner) {
|
|
331
|
-
await tempRscEnv
|
|
332
|
-
const serverMod = await tempRscEnv.runner.import(
|
|
333
|
-
"@rangojs/router/server",
|
|
334
|
-
);
|
|
335
|
-
prerenderNodeRegistry = serverMod.RouterRegistry;
|
|
531
|
+
await importEntryAndRegistry(tempRscEnv);
|
|
336
532
|
return tempRscEnv;
|
|
337
533
|
}
|
|
534
|
+
debugDiscovery?.(
|
|
535
|
+
"getOrCreateTempServer: tempRscEnv.runner unavailable",
|
|
536
|
+
);
|
|
338
537
|
} catch (err: any) {
|
|
339
|
-
|
|
340
|
-
|
|
538
|
+
debugDiscovery?.(
|
|
539
|
+
"getOrCreateTempServer: FAILED message=%s",
|
|
540
|
+
err.message,
|
|
341
541
|
);
|
|
542
|
+
console.warn(`[rango] Failed to create temp runner: ${err.message}`);
|
|
342
543
|
}
|
|
343
544
|
return null;
|
|
344
545
|
}
|
|
345
546
|
|
|
547
|
+
// Clear the package-level singleton registries that survive a Vite
|
|
548
|
+
// moduleGraph.invalidateAll(). createRouter() / createHostRouter()
|
|
549
|
+
// call .set(id, ...) on these Maps; for "router removed" or
|
|
550
|
+
// "router id changed" edits, the OLD entry would persist after
|
|
551
|
+
// re-import without an explicit .clear(), leaving ghost routes
|
|
552
|
+
// in discoverRouters' output.
|
|
553
|
+
//
|
|
554
|
+
// We import the same module the runner imports, so the .clear()
|
|
555
|
+
// here mutates the same Map the freshly re-imported entry will
|
|
556
|
+
// populate.
|
|
557
|
+
async function clearTempRegistries(tempRscEnv: any): Promise<void> {
|
|
558
|
+
try {
|
|
559
|
+
const serverMod = await tempRscEnv.runner.import(
|
|
560
|
+
"@rangojs/router/server",
|
|
561
|
+
);
|
|
562
|
+
if (typeof serverMod?.RouterRegistry?.clear === "function") {
|
|
563
|
+
serverMod.RouterRegistry.clear();
|
|
564
|
+
}
|
|
565
|
+
if (typeof serverMod?.HostRouterRegistry?.clear === "function") {
|
|
566
|
+
serverMod.HostRouterRegistry.clear();
|
|
567
|
+
}
|
|
568
|
+
debugDiscovery?.(
|
|
569
|
+
"clearTempRegistries: cleared RouterRegistry + HostRouterRegistry",
|
|
570
|
+
);
|
|
571
|
+
} catch (err: any) {
|
|
572
|
+
// Non-fatal: if the import fails here, importEntryAndRegistry
|
|
573
|
+
// below will fail loudly with the same root cause and the
|
|
574
|
+
// caller will surface it.
|
|
575
|
+
debugDiscovery?.(
|
|
576
|
+
"clearTempRegistries: import @rangojs/router/server failed (%s)",
|
|
577
|
+
err?.message ?? String(err),
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// HMR refresh: keep the temp Vite server alive across HMR cycles and
|
|
583
|
+
// invalidate its module graph instead of close+recreate. Closing the
|
|
584
|
+
// temp server during workerd's first post-cold-start module-fetch
|
|
585
|
+
// window disrupted the main dev server's transport — the user-visible
|
|
586
|
+
// symptom was a `transport was disconnected, cannot call "fetchModule"`
|
|
587
|
+
// error on the first urls.tsx edit (workerd's cache was cold, so its
|
|
588
|
+
// eval was still in flight when our close() ran). Module-graph
|
|
589
|
+
// invalidation is the architecturally cleaner refresh: same Vite
|
|
590
|
+
// instance, same transport, fresh source.
|
|
591
|
+
//
|
|
592
|
+
// Falls back to close+recreate when neither the env-level nor
|
|
593
|
+
// server-level moduleGraph exposes invalidateAll() (defensive — Vite
|
|
594
|
+
// versions / preset configurations may differ in which graph carries
|
|
595
|
+
// the module-runner cache).
|
|
596
|
+
async function refreshTempRscEnv(): Promise<any | null> {
|
|
597
|
+
let tempRscEnv = await getOrCreateTempServer();
|
|
598
|
+
if (!tempRscEnv) return null;
|
|
599
|
+
|
|
600
|
+
// Module-runner cache is on the per-environment graph in Vite 6+;
|
|
601
|
+
// older / non-environments setups carry it on the server graph.
|
|
602
|
+
// Try env first, server second.
|
|
603
|
+
const envGraph = (tempRscEnv as any).moduleGraph;
|
|
604
|
+
const serverGraph = (prerenderTempServer as any)?.moduleGraph;
|
|
605
|
+
const target = envGraph?.invalidateAll
|
|
606
|
+
? envGraph
|
|
607
|
+
: serverGraph?.invalidateAll
|
|
608
|
+
? serverGraph
|
|
609
|
+
: null;
|
|
610
|
+
|
|
611
|
+
if (!target) {
|
|
612
|
+
// No invalidate method available — fall back to close+recreate.
|
|
613
|
+
// This preserves the previous behavior in case a Vite version
|
|
614
|
+
// doesn't expose invalidateAll on either graph.
|
|
615
|
+
debugDiscovery?.(
|
|
616
|
+
"refreshTempRscEnv: invalidateAll unavailable on env+server graphs, falling back to close+recreate",
|
|
617
|
+
);
|
|
618
|
+
if (prerenderTempServer) {
|
|
619
|
+
await prerenderTempServer.close().catch(() => {});
|
|
620
|
+
prerenderTempServer = null;
|
|
621
|
+
prerenderNodeRegistry = null;
|
|
622
|
+
}
|
|
623
|
+
return await getOrCreateTempServer();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
debugDiscovery?.(
|
|
627
|
+
"refreshTempRscEnv: invalidating module graph (%s)",
|
|
628
|
+
envGraph?.invalidateAll ? "env" : "server",
|
|
629
|
+
);
|
|
630
|
+
target.invalidateAll();
|
|
631
|
+
// Drop the cached registry so importEntryAndRegistry re-reads it
|
|
632
|
+
// through the now-invalidated module runner.
|
|
633
|
+
prerenderNodeRegistry = null;
|
|
634
|
+
// Clear singleton Maps that Vite's moduleGraph invalidation can't
|
|
635
|
+
// reach (RouterRegistry / HostRouterRegistry). Without this, an
|
|
636
|
+
// edit that REMOVES a createRouter() call or CHANGES a router id
|
|
637
|
+
// would leave the old entry in the registry, and discoverRouters
|
|
638
|
+
// would still emit its routes alongside whatever the new source
|
|
639
|
+
// declares.
|
|
640
|
+
await clearTempRegistries(tempRscEnv);
|
|
641
|
+
await importEntryAndRegistry(tempRscEnv);
|
|
642
|
+
return tempRscEnv;
|
|
643
|
+
}
|
|
644
|
+
|
|
346
645
|
const discover = async () => {
|
|
646
|
+
const discoverStart = performance.now();
|
|
347
647
|
const rscEnv = (server.environments as any)?.rsc;
|
|
348
648
|
if (!rscEnv?.runner) {
|
|
349
649
|
// Cloudflare dev: no module runner available (workerd-based RSC env).
|
|
350
650
|
// Set devServerOrigin so the virtual module can inject __PRERENDER_DEV_URL
|
|
351
651
|
// for on-demand prerender via the /__rsc_prerender endpoint.
|
|
652
|
+
debugDiscovery?.(
|
|
653
|
+
"dev: cloudflare path start, __rscRouterDiscoveryActive=%s",
|
|
654
|
+
(globalThis as any).__rscRouterDiscoveryActive ?? false,
|
|
655
|
+
);
|
|
352
656
|
s.devServerOrigin = getDevServerOrigin();
|
|
353
657
|
|
|
354
658
|
// Create a temp Node.js server to run runtime discovery and generate
|
|
355
659
|
// named route types (static parser can't resolve factory calls).
|
|
356
660
|
try {
|
|
357
661
|
// Acquire build-time env bindings for dev prerender
|
|
358
|
-
await
|
|
662
|
+
await timed(debugDiscovery, "acquireBuildEnv", () =>
|
|
663
|
+
acquireBuildEnv(s, viteCommand, viteMode),
|
|
664
|
+
);
|
|
359
665
|
|
|
360
|
-
const tempRscEnv = await
|
|
666
|
+
const tempRscEnv = await timed(
|
|
667
|
+
debugDiscovery,
|
|
668
|
+
"getOrCreateTempServer",
|
|
669
|
+
() => getOrCreateTempServer(),
|
|
670
|
+
);
|
|
361
671
|
if (tempRscEnv) {
|
|
362
|
-
await discoverRouters(
|
|
363
|
-
|
|
672
|
+
await timed(debugDiscovery, "discoverRouters (cloudflare)", () =>
|
|
673
|
+
discoverRouters(s, tempRscEnv),
|
|
674
|
+
);
|
|
675
|
+
timedSync(debugDiscovery, "writeRouteTypesFiles", () =>
|
|
676
|
+
writeRouteTypesFiles(s),
|
|
677
|
+
);
|
|
364
678
|
}
|
|
365
679
|
} catch (err: any) {
|
|
366
680
|
console.warn(
|
|
367
|
-
`[
|
|
681
|
+
`[rango] Cloudflare dev discovery failed: ${err.message}\n${err.stack}`,
|
|
368
682
|
);
|
|
369
683
|
}
|
|
370
684
|
|
|
685
|
+
debugDiscovery?.(
|
|
686
|
+
"dev discovery done (%sms)",
|
|
687
|
+
(performance.now() - discoverStart).toFixed(1),
|
|
688
|
+
);
|
|
371
689
|
resolveDiscovery!();
|
|
372
690
|
return;
|
|
373
691
|
}
|
|
374
692
|
|
|
375
693
|
try {
|
|
376
694
|
// Acquire build-time env bindings for dev prerender (Node.js path)
|
|
377
|
-
|
|
695
|
+
debugDiscovery?.("dev: node path start");
|
|
696
|
+
await timed(debugDiscovery, "acquireBuildEnv", () =>
|
|
697
|
+
acquireBuildEnv(s, viteCommand, viteMode),
|
|
698
|
+
);
|
|
378
699
|
|
|
379
700
|
// Set the readiness gate BEFORE discovery so early requests
|
|
380
701
|
// block until manifest is populated
|
|
381
|
-
const serverMod = await
|
|
382
|
-
|
|
702
|
+
const serverMod = await timed(
|
|
703
|
+
debugDiscovery,
|
|
704
|
+
"import @rangojs/router/server",
|
|
705
|
+
() => rscEnv.runner.import("@rangojs/router/server"),
|
|
383
706
|
);
|
|
384
707
|
if (serverMod?.setManifestReadyPromise) {
|
|
385
708
|
serverMod.setManifestReadyPromise(discoveryPromise);
|
|
386
709
|
}
|
|
387
710
|
|
|
388
|
-
await
|
|
711
|
+
await timed(debugDiscovery, "discoverRouters", () =>
|
|
712
|
+
discoverRouters(s, rscEnv),
|
|
713
|
+
);
|
|
389
714
|
|
|
390
715
|
// Store server origin for dev prerender endpoint (virtual module injection)
|
|
391
716
|
s.devServerOrigin = getDevServerOrigin();
|
|
@@ -395,24 +720,36 @@ export function createRouterDiscoveryPlugin(
|
|
|
395
720
|
// routes (e.g. Array.from loops) that the static parser cannot see.
|
|
396
721
|
// writeRouteTypesFiles() only writes when content changes, so this
|
|
397
722
|
// won't cause unnecessary HMR triggers.
|
|
398
|
-
writeRouteTypesFiles(
|
|
723
|
+
timedSync(debugDiscovery, "writeRouteTypesFiles", () =>
|
|
724
|
+
writeRouteTypesFiles(s),
|
|
725
|
+
);
|
|
399
726
|
|
|
400
727
|
// Populate the route map and per-router data in the RSC env
|
|
401
|
-
await propagateDiscoveryState(
|
|
728
|
+
await timed(debugDiscovery, "propagateDiscoveryState", () =>
|
|
729
|
+
propagateDiscoveryState(rscEnv),
|
|
730
|
+
);
|
|
402
731
|
} catch (err: any) {
|
|
403
732
|
console.warn(
|
|
404
|
-
`[
|
|
733
|
+
`[rango] Router discovery failed: ${err.message}\n${err.stack}`,
|
|
405
734
|
);
|
|
406
735
|
} finally {
|
|
736
|
+
debugDiscovery?.(
|
|
737
|
+
"dev discovery done (%sms)",
|
|
738
|
+
(performance.now() - discoverStart).toFixed(1),
|
|
739
|
+
);
|
|
407
740
|
resolveDiscovery!();
|
|
408
741
|
}
|
|
409
742
|
};
|
|
410
743
|
|
|
411
744
|
// Schedule after all plugins have finished configureServer.
|
|
412
|
-
//
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
745
|
+
// The gate (s.discoveryDone) is reset via beginDiscoveryGate() and
|
|
746
|
+
// resolved when discover() finishes, so the virtual manifest module's
|
|
747
|
+
// load() awaits the populated state.
|
|
748
|
+
beginDiscoveryGate();
|
|
749
|
+
setTimeout(
|
|
750
|
+
() => discover().then(resolveDiscoveryGate, resolveDiscoveryGate),
|
|
751
|
+
0,
|
|
752
|
+
);
|
|
416
753
|
|
|
417
754
|
// Dev-mode on-demand prerender endpoint.
|
|
418
755
|
// When workerd hits a prerender route, it fetches this endpoint instead of
|
|
@@ -452,24 +789,30 @@ export function createRouterDiscoveryPlugin(
|
|
|
452
789
|
if (s.mergedRouteTrie && serverMod.setRouteTrie) {
|
|
453
790
|
serverMod.setRouteTrie(s.mergedRouteTrie);
|
|
454
791
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
}
|
|
465
|
-
if (serverMod.setRouterPrecomputedEntries) {
|
|
466
|
-
for (const [routerId, entries] of s.perRouterPrecomputedMap) {
|
|
467
|
-
serverMod.setRouterPrecomputedEntries(routerId, entries);
|
|
468
|
-
}
|
|
792
|
+
const perRouterSetters: Array<[Map<string, any>, string]> = [
|
|
793
|
+
[s.perRouterManifestDataMap, "setRouterManifest"],
|
|
794
|
+
[s.perRouterTrieMap, "setRouterTrie"],
|
|
795
|
+
[s.perRouterPrecomputedMap, "setRouterPrecomputedEntries"],
|
|
796
|
+
];
|
|
797
|
+
for (const [map, fn] of perRouterSetters) {
|
|
798
|
+
const setter = serverMod[fn];
|
|
799
|
+
if (typeof setter !== "function") continue;
|
|
800
|
+
for (const [routerId, value] of map) setter(routerId, value);
|
|
469
801
|
}
|
|
470
802
|
};
|
|
471
803
|
|
|
472
804
|
server.middlewares.use("/__rsc_prerender", async (req: any, res: any) => {
|
|
805
|
+
const reqStart = debugDev ? performance.now() : 0;
|
|
806
|
+
const logResult = (status: number, note: string) => {
|
|
807
|
+
debugDev?.(
|
|
808
|
+
"/__rsc_prerender %s -> %d %s (%sms)",
|
|
809
|
+
req.url,
|
|
810
|
+
status,
|
|
811
|
+
note,
|
|
812
|
+
(performance.now() - reqStart).toFixed(1),
|
|
813
|
+
);
|
|
814
|
+
};
|
|
815
|
+
|
|
473
816
|
if (s.discoveryDone) await s.discoveryDone;
|
|
474
817
|
|
|
475
818
|
const url = new URL(req.url || "/", "http://localhost");
|
|
@@ -477,6 +820,7 @@ export function createRouterDiscoveryPlugin(
|
|
|
477
820
|
if (!pathname) {
|
|
478
821
|
res.statusCode = 400;
|
|
479
822
|
res.end("Missing pathname");
|
|
823
|
+
logResult(400, "missing pathname");
|
|
480
824
|
return;
|
|
481
825
|
}
|
|
482
826
|
|
|
@@ -496,10 +840,11 @@ export function createRouterDiscoveryPlugin(
|
|
|
496
840
|
registry = serverMod.RouterRegistry ?? null;
|
|
497
841
|
} catch (err: any) {
|
|
498
842
|
console.warn(
|
|
499
|
-
`[
|
|
843
|
+
`[rango] Dev prerender module refresh failed: ${err.message}`,
|
|
500
844
|
);
|
|
501
845
|
res.statusCode = 500;
|
|
502
846
|
res.end(`Prerender handler error: ${err.message}`);
|
|
847
|
+
logResult(500, "module refresh failed");
|
|
503
848
|
return;
|
|
504
849
|
}
|
|
505
850
|
} else {
|
|
@@ -518,6 +863,7 @@ export function createRouterDiscoveryPlugin(
|
|
|
518
863
|
if (!registry || registry.size === 0) {
|
|
519
864
|
res.statusCode = 503;
|
|
520
865
|
res.end("Prerender runner not available");
|
|
866
|
+
logResult(503, "no registry");
|
|
521
867
|
return;
|
|
522
868
|
}
|
|
523
869
|
|
|
@@ -547,25 +893,26 @@ export function createRouterDiscoveryPlugin(
|
|
|
547
893
|
if (wantIntercept && result.interceptSegments?.length) {
|
|
548
894
|
payload = {
|
|
549
895
|
segments: [...result.segments, ...result.interceptSegments],
|
|
550
|
-
handles
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
},
|
|
896
|
+
// Pre-encoded MERGED handle string from the producer (handles are
|
|
897
|
+
// Flight-encoded so Promise/ReactNode values survive the wire).
|
|
898
|
+
handles: result.interceptHandles ?? "",
|
|
554
899
|
};
|
|
555
900
|
} else {
|
|
556
901
|
payload = { segments: result.segments, handles: result.handles };
|
|
557
902
|
}
|
|
558
903
|
res.end(JSON.stringify(payload));
|
|
904
|
+
logResult(200, `match ${result.routeName}`);
|
|
559
905
|
return;
|
|
560
906
|
} catch (err: any) {
|
|
561
907
|
console.warn(
|
|
562
|
-
`[
|
|
908
|
+
`[rango] Dev prerender failed for ${pathname}: ${err.message}`,
|
|
563
909
|
);
|
|
564
910
|
}
|
|
565
911
|
}
|
|
566
912
|
|
|
567
913
|
res.statusCode = 404;
|
|
568
914
|
res.end("No prerender match");
|
|
915
|
+
logResult(404, "no match");
|
|
569
916
|
});
|
|
570
917
|
|
|
571
918
|
// Watch url module and router files for changes and regenerate named-routes.gen.ts.
|
|
@@ -608,21 +955,135 @@ export function createRouterDiscoveryPlugin(
|
|
|
608
955
|
|
|
609
956
|
// Re-run runtime discovery so factory-generated routes that the
|
|
610
957
|
// static parser cannot see are refreshed after source changes.
|
|
611
|
-
|
|
958
|
+
// The state-machine concerns (queued/pending/gatePending) are
|
|
959
|
+
// owned by the gate created above (./discovery/gate-state.ts).
|
|
960
|
+
// Here we provide just the env-specific work.
|
|
612
961
|
const refreshRuntimeDiscovery = async () => {
|
|
613
962
|
const rscEnv = (server.environments as any)?.rsc;
|
|
614
|
-
|
|
615
|
-
|
|
963
|
+
const hasMainRunner = !!rscEnv?.runner;
|
|
964
|
+
// Cloudflare HMR has no main RSC runner (workerd is a separate
|
|
965
|
+
// runtime). When we have a populated runtime manifest from cold
|
|
966
|
+
// start, we can re-discover via the temp Node runner — the same
|
|
967
|
+
// mechanism getOrCreateTempServer() uses at startup. Without a
|
|
968
|
+
// populated manifest there's nothing useful to do, so bail
|
|
969
|
+
// before involving the gate machine at all.
|
|
970
|
+
if (!hasMainRunner && s.perRouterManifests.length === 0) return;
|
|
971
|
+
await gate.runRefreshCycle(async () => {
|
|
972
|
+
const hmrStart = performance.now();
|
|
973
|
+
try {
|
|
974
|
+
if (hasMainRunner) {
|
|
975
|
+
await timed(debugDiscovery, "hmr discoverRouters", () =>
|
|
976
|
+
discoverRouters(s, rscEnv),
|
|
977
|
+
);
|
|
978
|
+
timedSync(debugDiscovery, "hmr writeRouteTypesFiles", () =>
|
|
979
|
+
writeRouteTypesFiles(s),
|
|
980
|
+
);
|
|
981
|
+
await timed(debugDiscovery, "hmr propagateDiscoveryState", () =>
|
|
982
|
+
propagateDiscoveryState(rscEnv),
|
|
983
|
+
);
|
|
984
|
+
} else {
|
|
985
|
+
// Cloudflare HMR: invalidate the temp server's RSC module
|
|
986
|
+
// graph (or close+recreate as a fallback) so the runner
|
|
987
|
+
// re-reads the freshly edited source. Keeping the same
|
|
988
|
+
// Vite instance alive avoids disrupting workerd's transport
|
|
989
|
+
// during the first post-cold-start module-fetch window.
|
|
990
|
+
const tempRscEnv = await timed(
|
|
991
|
+
debugDiscovery,
|
|
992
|
+
"hmr refreshTempRscEnv (cloudflare)",
|
|
993
|
+
() => refreshTempRscEnv(),
|
|
994
|
+
);
|
|
995
|
+
if (!tempRscEnv) {
|
|
996
|
+
throw new Error(
|
|
997
|
+
"temp runner unavailable for cloudflare HMR rediscovery",
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
await timed(
|
|
1001
|
+
debugDiscovery,
|
|
1002
|
+
"hmr discoverRouters (cloudflare)",
|
|
1003
|
+
() => discoverRouters(s, tempRscEnv),
|
|
1004
|
+
);
|
|
1005
|
+
timedSync(debugDiscovery, "hmr writeRouteTypesFiles", () =>
|
|
1006
|
+
writeRouteTypesFiles(s),
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
if (s.lastDiscoveryError) {
|
|
1010
|
+
debugDiscovery?.(
|
|
1011
|
+
"hmr: cleared lastDiscoveryError (%s) after successful rediscovery",
|
|
1012
|
+
s.lastDiscoveryError.message,
|
|
1013
|
+
);
|
|
1014
|
+
s.lastDiscoveryError = null;
|
|
1015
|
+
}
|
|
1016
|
+
// Cloudflare dev: on a successful cycle drop the workerd runner's
|
|
1017
|
+
// cached worker-entry chain so the next request re-evaluates
|
|
1018
|
+
// createRouter() with the new routes. Fired here in the work path
|
|
1019
|
+
// (not the caller's .then()) so a queued follow-up cycle that
|
|
1020
|
+
// succeeds after an earlier failed cycle still reloads:
|
|
1021
|
+
// runRefreshCycle recurses queued work without awaiting it, so the
|
|
1022
|
+
// original call already resolved on the failed cycle. A failed
|
|
1023
|
+
// cycle throws above and never reaches here, so a broken edit
|
|
1024
|
+
// never reloads the worker onto bad source.
|
|
1025
|
+
if (rscEnv && !rscEnv.runner) forceCloudflareWorkerReload(rscEnv);
|
|
1026
|
+
} catch (err: any) {
|
|
1027
|
+
s.lastDiscoveryError = {
|
|
1028
|
+
message: err?.message ?? String(err),
|
|
1029
|
+
at: Date.now(),
|
|
1030
|
+
};
|
|
1031
|
+
console.warn(
|
|
1032
|
+
`[rango] Runtime re-discovery failed: ${err.message}`,
|
|
1033
|
+
);
|
|
1034
|
+
debugDiscovery?.(
|
|
1035
|
+
"hmr: lastDiscoveryError set (%s) — manifest preserved at last-good; recovery mode active (any in-scan source change will trigger rediscovery)",
|
|
1036
|
+
err?.message,
|
|
1037
|
+
);
|
|
1038
|
+
} finally {
|
|
1039
|
+
debugDiscovery?.(
|
|
1040
|
+
"hmr re-discovery done (%sms)",
|
|
1041
|
+
(performance.now() - hmrStart).toFixed(1),
|
|
1042
|
+
);
|
|
1043
|
+
}
|
|
1044
|
+
});
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
// Cloudflare dev only. workerd serves every request through the
|
|
1048
|
+
// runner-worker singleton, which re-resolves the worker entry per
|
|
1049
|
+
// request via runner.import("virtual:cloudflare/worker-entry"). The
|
|
1050
|
+
// route table lives in the user's createRouter() instance, captured
|
|
1051
|
+
// when that entry chain (entry -> router -> urls) was last evaluated
|
|
1052
|
+
// and then cached in the runner's evaluatedModules. The route-file
|
|
1053
|
+
// watcher refreshes discovery + types on the Node side, but the worker
|
|
1054
|
+
// keeps serving the cached (stale) router: route-definition modules
|
|
1055
|
+
// have no import.meta.hot boundary, so Vite never sends the worker an
|
|
1056
|
+
// HMR update for them and the entry chain is never evicted.
|
|
1057
|
+
//
|
|
1058
|
+
// Fix: after discovery completes, (1) invalidate the worker env's
|
|
1059
|
+
// Node-side module graph, then (2) send a full-reload to the worker.
|
|
1060
|
+
// Step (2) alone is insufficient: the full-reload handler clears the
|
|
1061
|
+
// runner's evaluatedModules and re-imports entrypoints, but each
|
|
1062
|
+
// re-import fetches the module back through this Node-side graph, which
|
|
1063
|
+
// still holds the pre-edit transform of urls.tsx — so createRouter()
|
|
1064
|
+
// rebuilds the stale route table and the new route 404s/hits the
|
|
1065
|
+
// catch-all. Invalidating the graph forces a fresh transform on
|
|
1066
|
+
// re-fetch (the same mechanism refreshTempRscEnv uses for discovery),
|
|
1067
|
+
// so the re-import re-runs createRouter() with the new routes. This is
|
|
1068
|
+
// the programmatic equivalent of the dev-server "r + enter" restart,
|
|
1069
|
+
// scoped to the worker environment instead of tearing down the server.
|
|
1070
|
+
const forceCloudflareWorkerReload = (rscEnv: any) => {
|
|
1071
|
+
if (!rscEnv?.hot) return;
|
|
616
1072
|
try {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
1073
|
+
const graph = rscEnv.moduleGraph;
|
|
1074
|
+
if (graph?.invalidateAll) {
|
|
1075
|
+
graph.invalidateAll();
|
|
1076
|
+
debugDiscovery?.("hmr: invalidated workerd rsc module graph");
|
|
1077
|
+
}
|
|
1078
|
+
rscEnv.hot.send({ type: "full-reload" });
|
|
1079
|
+
debugDiscovery?.(
|
|
1080
|
+
"hmr: forced workerd rsc env reload (full-reload)",
|
|
1081
|
+
);
|
|
620
1082
|
} catch (err: any) {
|
|
621
|
-
|
|
622
|
-
|
|
1083
|
+
debugDiscovery?.(
|
|
1084
|
+
"hmr: workerd reload failed: %s",
|
|
1085
|
+
err?.message ?? err,
|
|
623
1086
|
);
|
|
624
|
-
} finally {
|
|
625
|
-
runtimeRediscoveryInProgress = false;
|
|
626
1087
|
}
|
|
627
1088
|
};
|
|
628
1089
|
|
|
@@ -630,23 +1091,50 @@ export function createRouterDiscoveryPlugin(
|
|
|
630
1091
|
clearTimeout(routeChangeTimer);
|
|
631
1092
|
routeChangeTimer = setTimeout(() => {
|
|
632
1093
|
routeChangeTimer = undefined;
|
|
1094
|
+
const regenStart = debugDiscovery ? performance.now() : 0;
|
|
1095
|
+
const rscEnv = (server.environments as any)?.rsc;
|
|
1096
|
+
const skipStaticWrite =
|
|
1097
|
+
!rscEnv?.runner && s.perRouterManifests.length > 0;
|
|
633
1098
|
try {
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
1099
|
+
// In cloudflare dev with a populated runtime manifest, the
|
|
1100
|
+
// static parser produces a strictly smaller (and actively
|
|
1101
|
+
// wrong) gen file — supplementGenFilesWithRuntimeRoutes can
|
|
1102
|
+
// only restore factory-only prefixes, and apps with mixed
|
|
1103
|
+
// static+factory routes under shared prefixes (cf-stress)
|
|
1104
|
+
// collapse to the 19-route static view. Skip the static
|
|
1105
|
+
// write entirely; runtime rediscovery below will overwrite
|
|
1106
|
+
// the gen file with the authoritative manifest.
|
|
1107
|
+
if (skipStaticWrite) {
|
|
1108
|
+
debugDiscovery?.(
|
|
1109
|
+
"watcher: skipping static write (cloudflare HMR — runtime rediscovery owns gen file)",
|
|
1110
|
+
);
|
|
1111
|
+
} else {
|
|
1112
|
+
writeCombinedRouteTypesWithTracking(s);
|
|
1113
|
+
if (s.perRouterManifests.length > 0) {
|
|
1114
|
+
supplementGenFilesWithRuntimeRoutes(s);
|
|
1115
|
+
}
|
|
637
1116
|
}
|
|
638
1117
|
} catch (err: any) {
|
|
639
|
-
console.error(
|
|
640
|
-
`[rsc-router] Route regeneration error: ${err.message}`,
|
|
641
|
-
);
|
|
1118
|
+
console.error(`[rango] Route regeneration error: ${err.message}`);
|
|
642
1119
|
}
|
|
1120
|
+
debugDiscovery?.(
|
|
1121
|
+
"watcher: regenerated gen files (%sms)",
|
|
1122
|
+
(performance.now() - regenStart).toFixed(1),
|
|
1123
|
+
);
|
|
643
1124
|
// Async: re-run runtime discovery to refresh factory-generated
|
|
644
|
-
// routes that the static parser cannot resolve.
|
|
1125
|
+
// routes that the static parser cannot resolve. Resolves the
|
|
1126
|
+
// discovery gate when complete.
|
|
645
1127
|
if (s.perRouterManifests.length > 0) {
|
|
1128
|
+
// The cloudflare workerd reload fires inside refreshRuntimeDiscovery
|
|
1129
|
+
// on the successful cycle (see forceCloudflareWorkerReload call
|
|
1130
|
+
// there) so queued follow-up cycles also trigger it.
|
|
646
1131
|
refreshRuntimeDiscovery().catch((err: any) => {
|
|
647
1132
|
console.warn(
|
|
648
|
-
`[
|
|
1133
|
+
`[rango] Runtime re-discovery error: ${err.message}`,
|
|
649
1134
|
);
|
|
1135
|
+
// Even on error, unblock the gate so workerd's reload doesn't
|
|
1136
|
+
// hang indefinitely against the previous manifest.
|
|
1137
|
+
resolveDiscoveryGate();
|
|
650
1138
|
});
|
|
651
1139
|
}
|
|
652
1140
|
}, 100);
|
|
@@ -659,21 +1147,74 @@ export function createRouterDiscoveryPlugin(
|
|
|
659
1147
|
!filePath.endsWith(".tsx") &&
|
|
660
1148
|
!filePath.endsWith(".js") &&
|
|
661
1149
|
!filePath.endsWith(".jsx")
|
|
662
|
-
)
|
|
1150
|
+
) {
|
|
1151
|
+
if (s.lastDiscoveryError) {
|
|
1152
|
+
debugDiscovery?.(
|
|
1153
|
+
"watcher: skip non-source %s [LASTERR %s]",
|
|
1154
|
+
filePath,
|
|
1155
|
+
s.lastDiscoveryError.message,
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
663
1158
|
return;
|
|
1159
|
+
}
|
|
664
1160
|
// Apply scan filter as early-exit before reading file
|
|
665
|
-
if (s.scanFilter && !s.scanFilter(filePath))
|
|
1161
|
+
if (s.scanFilter && !s.scanFilter(filePath)) {
|
|
1162
|
+
if (s.lastDiscoveryError) {
|
|
1163
|
+
debugDiscovery?.(
|
|
1164
|
+
"watcher: skip scan-filter %s [LASTERR %s]",
|
|
1165
|
+
filePath,
|
|
1166
|
+
s.lastDiscoveryError.message,
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
// Recovery mode: when the previous HMR re-discovery failed, the
|
|
1172
|
+
// import graph is incomplete and the manifest is stuck at the
|
|
1173
|
+
// last-good state. The fix may land in a non-route file (e.g. a
|
|
1174
|
+
// helper imported by the router, a missing module being created,
|
|
1175
|
+
// or a "use client" component) that the narrow content sniff
|
|
1176
|
+
// would otherwise filter out. While in recovery, treat any
|
|
1177
|
+
// in-scan source change as a candidate for rediscovery; the
|
|
1178
|
+
// tighter filter resumes once discovery succeeds again.
|
|
1179
|
+
const inRecoveryMode = !!s.lastDiscoveryError;
|
|
666
1180
|
try {
|
|
667
1181
|
const source = readFileSync(filePath, "utf-8");
|
|
668
1182
|
const trimmed = source.trimStart();
|
|
669
|
-
|
|
1183
|
+
const isUseClient =
|
|
670
1184
|
trimmed.startsWith('"use client"') ||
|
|
671
|
-
trimmed.startsWith("'use client'")
|
|
672
|
-
)
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
1185
|
+
trimmed.startsWith("'use client'");
|
|
1186
|
+
if (!inRecoveryMode && isUseClient) return;
|
|
1187
|
+
// Cheap raw pre-check first; only when a candidate token is present
|
|
1188
|
+
// do we confirm it occurs in real code (not a comment/string) via a
|
|
1189
|
+
// single allocation-free code-region scan. Most saved files contain
|
|
1190
|
+
// neither token and skip the scan entirely. This avoids a comment or
|
|
1191
|
+
// string mention spuriously marking a file relevant and triggering an
|
|
1192
|
+
// unnecessary re-discovery on save.
|
|
1193
|
+
let hasUrls = source.includes("urls(");
|
|
1194
|
+
let hasCreateRouter = /\bcreateRouter\s*[<(]/.test(source);
|
|
1195
|
+
if (hasUrls) hasUrls = firstCodeMatchIndex(source, /urls\(/g) >= 0;
|
|
1196
|
+
if (hasCreateRouter) {
|
|
1197
|
+
hasCreateRouter =
|
|
1198
|
+
firstCodeMatchIndex(source, /\bcreateRouter\s*[<(]/g) >= 0;
|
|
1199
|
+
}
|
|
1200
|
+
if (!inRecoveryMode && !hasUrls && !hasCreateRouter) return;
|
|
1201
|
+
if (inRecoveryMode) {
|
|
1202
|
+
debugDiscovery?.(
|
|
1203
|
+
"watcher: recovery rediscovery for %s (urls=%s, router=%s, useClient=%s) [LASTERR %s]",
|
|
1204
|
+
filePath,
|
|
1205
|
+
hasUrls,
|
|
1206
|
+
hasCreateRouter,
|
|
1207
|
+
isUseClient,
|
|
1208
|
+
s.lastDiscoveryError!.message,
|
|
1209
|
+
);
|
|
1210
|
+
} else {
|
|
1211
|
+
debugDiscovery?.(
|
|
1212
|
+
"watcher: %s matches (urls=%s, router=%s)",
|
|
1213
|
+
filePath,
|
|
1214
|
+
hasUrls,
|
|
1215
|
+
hasCreateRouter,
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
677
1218
|
// Invalidate cache when a router file changes (new router added/removed)
|
|
678
1219
|
if (hasCreateRouter) {
|
|
679
1220
|
const nestedRouterConflict = findNestedRouterConflict([
|
|
@@ -688,8 +1229,27 @@ export function createRouterDiscoveryPlugin(
|
|
|
688
1229
|
}
|
|
689
1230
|
s.cachedRouterFiles = undefined;
|
|
690
1231
|
}
|
|
1232
|
+
// Note the event in the gate machine IMMEDIATELY (before the
|
|
1233
|
+
// 100ms debounce and any downstream HMR fanout). This sets
|
|
1234
|
+
// both `pendingEvents` (so refresh's finally holds the gate
|
|
1235
|
+
// through the tail window even if no rediscovery is queued)
|
|
1236
|
+
// and resets `discoveryDone` to a fresh pending promise (so
|
|
1237
|
+
// workerd reloads triggered by the same source change can't
|
|
1238
|
+
// observe a stale resolved gate from cold-start). Resolved
|
|
1239
|
+
// by the trailing refreshRuntimeDiscovery() cycle.
|
|
1240
|
+
if (s.perRouterManifests.length > 0) {
|
|
1241
|
+
gate.noteRouteEvent();
|
|
1242
|
+
}
|
|
691
1243
|
scheduleRouteRegeneration();
|
|
692
|
-
} catch {
|
|
1244
|
+
} catch (readErr: any) {
|
|
1245
|
+
if (s.lastDiscoveryError) {
|
|
1246
|
+
debugDiscovery?.(
|
|
1247
|
+
"watcher: read error %s: %s [LASTERR %s]",
|
|
1248
|
+
filePath,
|
|
1249
|
+
readErr?.message,
|
|
1250
|
+
s.lastDiscoveryError.message,
|
|
1251
|
+
);
|
|
1252
|
+
}
|
|
693
1253
|
// Ignore read errors for deleted/moved files
|
|
694
1254
|
}
|
|
695
1255
|
};
|
|
@@ -718,13 +1278,23 @@ export function createRouterDiscoveryPlugin(
|
|
|
718
1278
|
async buildStart() {
|
|
719
1279
|
if (!s.isBuildMode) return;
|
|
720
1280
|
// Only run once across environment builds
|
|
721
|
-
if (s.mergedRouteManifest !== null)
|
|
1281
|
+
if (s.mergedRouteManifest !== null) {
|
|
1282
|
+
debugDiscovery?.(
|
|
1283
|
+
"build: skip (already discovered, env=%s)",
|
|
1284
|
+
this.environment?.name ?? "?",
|
|
1285
|
+
);
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
const buildStartTime = performance.now();
|
|
1289
|
+
debugDiscovery?.("build: start (env=%s)", this.environment?.name ?? "?");
|
|
722
1290
|
resetStagedBuildAssets(s.projectRoot);
|
|
723
1291
|
s.prerenderManifestEntries = null;
|
|
724
1292
|
s.staticManifestEntries = null;
|
|
725
1293
|
|
|
726
1294
|
// Acquire build-time env bindings if configured
|
|
727
|
-
await
|
|
1295
|
+
await timed(debugDiscovery, "build acquireBuildEnv", () =>
|
|
1296
|
+
acquireBuildEnv(s, viteCommand, viteMode),
|
|
1297
|
+
);
|
|
728
1298
|
|
|
729
1299
|
let tempServer: any = null;
|
|
730
1300
|
// Signal to user-space code (e.g. reverse.ts) that build-time discovery
|
|
@@ -733,12 +1303,16 @@ export function createRouterDiscoveryPlugin(
|
|
|
733
1303
|
// between the vite plugin and user code loaded via runner.import().
|
|
734
1304
|
(globalThis as any).__rscRouterDiscoveryActive = true;
|
|
735
1305
|
try {
|
|
736
|
-
tempServer = await
|
|
1306
|
+
tempServer = await timed(
|
|
1307
|
+
debugDiscovery,
|
|
1308
|
+
"build createTempRscServer",
|
|
1309
|
+
() => createTempRscServer(s, { forceBuild: true }),
|
|
1310
|
+
);
|
|
737
1311
|
|
|
738
1312
|
const rscEnv = (tempServer.environments as any)?.rsc;
|
|
739
1313
|
if (!rscEnv?.runner) {
|
|
740
1314
|
console.warn(
|
|
741
|
-
"[
|
|
1315
|
+
"[rango] RSC environment runner not available during build, skipping manifest generation",
|
|
742
1316
|
);
|
|
743
1317
|
return;
|
|
744
1318
|
}
|
|
@@ -753,11 +1327,15 @@ export function createRouterDiscoveryPlugin(
|
|
|
753
1327
|
s.resolvedStaticModules = tempIdsPlugin.api.staticHandlerModules;
|
|
754
1328
|
}
|
|
755
1329
|
|
|
756
|
-
await
|
|
1330
|
+
await timed(debugDiscovery, "build discoverRouters", () =>
|
|
1331
|
+
discoverRouters(s, rscEnv),
|
|
1332
|
+
);
|
|
757
1333
|
// Update named-routes.gen.ts from runtime discovery.
|
|
758
1334
|
// The runtime manifest includes dynamically generated routes
|
|
759
1335
|
// that the static parser cannot extract from source code.
|
|
760
|
-
writeRouteTypesFiles(
|
|
1336
|
+
timedSync(debugDiscovery, "build writeRouteTypesFiles", () =>
|
|
1337
|
+
writeRouteTypesFiles(s),
|
|
1338
|
+
);
|
|
761
1339
|
} catch (err: any) {
|
|
762
1340
|
// Extract the user source file from the stack trace (skip internal frames)
|
|
763
1341
|
const sourceFile = err.stack
|
|
@@ -777,14 +1355,50 @@ export function createRouterDiscoveryPlugin(
|
|
|
777
1355
|
.filter(Boolean)
|
|
778
1356
|
.join("\n");
|
|
779
1357
|
throw new Error(
|
|
780
|
-
`[
|
|
1358
|
+
`[rango] Build-time router discovery failed:\n${details}`,
|
|
1359
|
+
{ cause: err },
|
|
781
1360
|
);
|
|
782
1361
|
} finally {
|
|
783
1362
|
delete (globalThis as any).__rscRouterDiscoveryActive;
|
|
784
1363
|
if (tempServer) {
|
|
785
|
-
await tempServer.close()
|
|
1364
|
+
await timed(debugDiscovery, "build tempServer.close", () =>
|
|
1365
|
+
tempServer.close(),
|
|
1366
|
+
);
|
|
786
1367
|
}
|
|
787
1368
|
await releaseBuildEnv(s);
|
|
1369
|
+
debugDiscovery?.(
|
|
1370
|
+
"build discovery done (%sms)",
|
|
1371
|
+
(performance.now() - buildStartTime).toFixed(1),
|
|
1372
|
+
);
|
|
1373
|
+
}
|
|
1374
|
+
},
|
|
1375
|
+
|
|
1376
|
+
// Suppress vite's HMR cascade for our own gen-file writes.
|
|
1377
|
+
//
|
|
1378
|
+
// After every cf HMR cycle, refreshTempRscEnv → writeRouteTypesFiles
|
|
1379
|
+
// writes the configured gen files (default `router.named-routes.gen.ts`,
|
|
1380
|
+
// but the source filenames and gen suffix are user-configurable). The
|
|
1381
|
+
// chokidar watcher then fires twice independently: our
|
|
1382
|
+
// `handleRouteFileChange` (already short-circuited by
|
|
1383
|
+
// `consumeSelfGenWrite` inside `maybeHandleGeneratedRouteFileMutation`),
|
|
1384
|
+
// AND vite's own HMR pipeline (which invalidates the gen file's
|
|
1385
|
+
// importers and triggers a second workerd full reload — visible to the
|
|
1386
|
+
// user as a duplicate "[Rango] HMR: version changed" on the client).
|
|
1387
|
+
//
|
|
1388
|
+
// `peekSelfGenWrite` is the authoritative filter: its map only contains
|
|
1389
|
+
// paths that `markSelfGenWrite` has registered, so it natively works
|
|
1390
|
+
// for any configured gen-file name. It is non-consuming so the chokidar
|
|
1391
|
+
// handler that fires later can still consume the same entry. Returning
|
|
1392
|
+
// [] tells vite "no modules invalidated by this change" — safe because
|
|
1393
|
+
// `s.perRouterManifests` is already up-to-date (the write that just
|
|
1394
|
+
// happened is the consequence of our just-completed rediscovery).
|
|
1395
|
+
handleHotUpdate(ctx) {
|
|
1396
|
+
if (peekSelfGenWrite(s, ctx.file)) {
|
|
1397
|
+
debugDiscovery?.(
|
|
1398
|
+
"handleHotUpdate: suppressing self-write HMR cascade for %s",
|
|
1399
|
+
ctx.file,
|
|
1400
|
+
);
|
|
1401
|
+
return [];
|
|
788
1402
|
}
|
|
789
1403
|
},
|
|
790
1404
|
|
|
@@ -808,19 +1422,38 @@ export function createRouterDiscoveryPlugin(
|
|
|
808
1422
|
// This is critical for Cloudflare dev where the worker runs in a separate
|
|
809
1423
|
// Miniflare process and can only receive manifest data via the virtual module.
|
|
810
1424
|
if (s.discoveryDone) {
|
|
811
|
-
await
|
|
1425
|
+
await timed(
|
|
1426
|
+
debugRoutes,
|
|
1427
|
+
"await discoveryDone (manifest)",
|
|
1428
|
+
() => s.discoveryDone,
|
|
1429
|
+
);
|
|
812
1430
|
}
|
|
813
|
-
|
|
1431
|
+
const code = await timed(
|
|
1432
|
+
debugRoutes,
|
|
1433
|
+
"generateRoutesManifestModule",
|
|
1434
|
+
() => generateRoutesManifestModule(s),
|
|
1435
|
+
);
|
|
1436
|
+
debugRoutes?.("manifest module emitted (%d bytes)", code?.length ?? 0);
|
|
1437
|
+
return code;
|
|
814
1438
|
}
|
|
815
1439
|
// Per-router virtual modules: pure data exports (no side effects).
|
|
816
1440
|
// ensureRouterManifest() imports the module and stores the data.
|
|
817
1441
|
const perRouterPrefix = "\0" + VIRTUAL_ROUTES_MANIFEST_ID + "/";
|
|
818
1442
|
if (id.startsWith(perRouterPrefix)) {
|
|
819
1443
|
if (s.discoveryDone) {
|
|
820
|
-
await
|
|
1444
|
+
await timed(
|
|
1445
|
+
debugRoutes,
|
|
1446
|
+
"await discoveryDone (per-router)",
|
|
1447
|
+
() => s.discoveryDone,
|
|
1448
|
+
);
|
|
821
1449
|
}
|
|
822
1450
|
const routerId = id.slice(perRouterPrefix.length);
|
|
823
|
-
|
|
1451
|
+
const code = await timed(
|
|
1452
|
+
debugRoutes,
|
|
1453
|
+
`generatePerRouterModule ${routerId}`,
|
|
1454
|
+
() => generatePerRouterModule(s, routerId),
|
|
1455
|
+
);
|
|
1456
|
+
return code;
|
|
824
1457
|
}
|
|
825
1458
|
// virtual:rsc-router/prerender-paths load handler removed
|
|
826
1459
|
return null;
|
|
@@ -830,6 +1463,7 @@ export function createRouterDiscoveryPlugin(
|
|
|
830
1463
|
// Used by closeBundle for handler code eviction and prerender data injection.
|
|
831
1464
|
generateBundle(_options: any, bundle: any) {
|
|
832
1465
|
if (this.environment?.name !== "rsc") return;
|
|
1466
|
+
const genStart = debugBuild ? performance.now() : 0;
|
|
833
1467
|
|
|
834
1468
|
// Record RSC entry chunk filename for closeBundle injection
|
|
835
1469
|
for (const [fileName, chunk] of Object.entries(bundle) as [
|
|
@@ -842,8 +1476,13 @@ export function createRouterDiscoveryPlugin(
|
|
|
842
1476
|
}
|
|
843
1477
|
}
|
|
844
1478
|
|
|
845
|
-
if (!s.resolvedPrerenderModules?.size && !s.resolvedStaticModules?.size)
|
|
1479
|
+
if (!s.resolvedPrerenderModules?.size && !s.resolvedStaticModules?.size) {
|
|
1480
|
+
debugBuild?.(
|
|
1481
|
+
"generateBundle (rsc): no handlers to scan (%sms)",
|
|
1482
|
+
(performance.now() - genStart).toFixed(1),
|
|
1483
|
+
);
|
|
846
1484
|
return;
|
|
1485
|
+
}
|
|
847
1486
|
|
|
848
1487
|
// Clear maps at the start of each RSC generateBundle pass.
|
|
849
1488
|
// Vite 6 multi-environment builds run RSC twice (analysis + production);
|
|
@@ -898,6 +1537,14 @@ export function createRouterDiscoveryPlugin(
|
|
|
898
1537
|
}
|
|
899
1538
|
}
|
|
900
1539
|
}
|
|
1540
|
+
|
|
1541
|
+
debugBuild?.(
|
|
1542
|
+
"generateBundle (rsc): scanned %d chunks, %d prerender chunk(s), %d static chunk(s) (%sms)",
|
|
1543
|
+
Object.keys(bundle).length,
|
|
1544
|
+
s.handlerChunkInfoMap.size,
|
|
1545
|
+
s.staticHandlerChunkInfoMap.size,
|
|
1546
|
+
(performance.now() - genStart).toFixed(1),
|
|
1547
|
+
);
|
|
901
1548
|
},
|
|
902
1549
|
|
|
903
1550
|
// Build-time pre-rendering: evict handler code and inject collected prerender data.
|
|
@@ -911,7 +1558,9 @@ export function createRouterDiscoveryPlugin(
|
|
|
911
1558
|
// Only run for the RSC environment — other environments (client, ssr) have
|
|
912
1559
|
// no prerender/static data to process and would just do redundant file I/O.
|
|
913
1560
|
if (this.environment && this.environment.name !== "rsc") return;
|
|
914
|
-
postprocessBundle(
|
|
1561
|
+
timedSync(debugBuild, "closeBundle postprocessBundle", () =>
|
|
1562
|
+
postprocessBundle(s),
|
|
1563
|
+
);
|
|
915
1564
|
},
|
|
916
1565
|
},
|
|
917
1566
|
};
|