@rangojs/router 0.0.0-experimental.92 → 0.0.0-experimental.95
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/dist/vite/index.js +335 -53
- package/package.json +1 -1
- package/src/browser/event-controller.ts +44 -4
- package/src/browser/react/NavigationProvider.tsx +20 -7
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/types.ts +6 -0
- package/src/router/match-api.ts +1 -0
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-result.ts +21 -2
- package/src/router/segment-resolution/fresh.ts +8 -0
- package/src/router/segment-resolution/revalidation.ts +65 -42
- package/src/rsc/progressive-enhancement.ts +2 -0
- package/src/rsc/rsc-rendering.ts +3 -0
- package/src/rsc/server-action.ts +2 -0
- package/src/rsc/types.ts +6 -0
- package/src/ssr/index.tsx +5 -1
- package/src/types/segments.ts +17 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/plugins/version-injector.ts +37 -11
- package/src/vite/router-discovery.ts +354 -42
|
@@ -22,6 +22,32 @@ export function markSelfGenWrite(
|
|
|
22
22
|
export function consumeSelfGenWrite(
|
|
23
23
|
state: DiscoveryState,
|
|
24
24
|
filePath: string,
|
|
25
|
+
): boolean {
|
|
26
|
+
return checkSelfGenWrite(state, filePath, true);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Non-consuming variant. Used by the `handleHotUpdate` plugin hook to
|
|
31
|
+
* suppress vite's HMR cascade for our own gen-file writes WITHOUT
|
|
32
|
+
* consuming the entry — `consumeSelfGenWrite` (called later from the
|
|
33
|
+
* chokidar `change` handler in `handleRouteFileChange`) still needs to
|
|
34
|
+
* see and consume the same entry to short-circuit our regen path.
|
|
35
|
+
*
|
|
36
|
+
* Both hooks fire for the same file change event:
|
|
37
|
+
* - `handleHotUpdate` runs first (vite's HMR pipeline).
|
|
38
|
+
* - chokidar `change` callback runs after (filesystem watcher).
|
|
39
|
+
*/
|
|
40
|
+
export function peekSelfGenWrite(
|
|
41
|
+
state: DiscoveryState,
|
|
42
|
+
filePath: string,
|
|
43
|
+
): boolean {
|
|
44
|
+
return checkSelfGenWrite(state, filePath, false);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function checkSelfGenWrite(
|
|
48
|
+
state: DiscoveryState,
|
|
49
|
+
filePath: string,
|
|
50
|
+
consume: boolean,
|
|
25
51
|
): boolean {
|
|
26
52
|
const info = state.selfWrittenGenFiles.get(filePath);
|
|
27
53
|
if (!info) return false;
|
|
@@ -33,7 +59,7 @@ export function consumeSelfGenWrite(
|
|
|
33
59
|
const current = readFileSync(filePath, "utf-8");
|
|
34
60
|
const currentHash = createHash("sha256").update(current).digest("hex");
|
|
35
61
|
if (currentHash === info.hash) {
|
|
36
|
-
state.selfWrittenGenFiles.delete(filePath);
|
|
62
|
+
if (consume) state.selfWrittenGenFiles.delete(filePath);
|
|
37
63
|
return true;
|
|
38
64
|
}
|
|
39
65
|
// Hash mismatch: file was changed externally. Keep the entry so a
|
|
@@ -47,16 +47,26 @@ export function createVersionInjectorPlugin(
|
|
|
47
47
|
return null;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
// Always prepend `import "virtual:rsc-router/routes-manifest"` as the
|
|
51
|
+
// first side-effect import. The manifest virtual module's `load()` hook
|
|
52
|
+
// awaits `s.discoveryDone` so that, by the time the rest of the entry
|
|
53
|
+
// including any module-level `router.reverse()` calls under `./router.js`
|
|
54
|
+
// evaluates, runtime discovery has rewritten `router.named-routes.gen.ts`
|
|
55
|
+
// with the full route table.
|
|
56
|
+
//
|
|
57
|
+
// ES module evaluation order matters here: while imports are *parsed*
|
|
58
|
+
// hoisted, side-effect imports are evaluated in source order in the
|
|
59
|
+
// dependency graph. A user-authored `import "virtual:rsc-router/..."`
|
|
60
|
+
// placed after `import "./router.js"` runs too late: the manifest
|
|
61
|
+
// gate fires after router.tsx has already crashed on a stale gen file.
|
|
62
|
+
// We always prepend; ESM dedups any user-written duplicate, so module
|
|
63
|
+
// initialization still runs once.
|
|
64
|
+
const prepend: string[] = [
|
|
65
|
+
`import "virtual:rsc-router/routes-manifest";`,
|
|
66
|
+
];
|
|
58
67
|
|
|
59
68
|
// Auto-inject VERSION if file uses createRSCHandler without version
|
|
69
|
+
let newCode = code;
|
|
60
70
|
const needsVersion =
|
|
61
71
|
code.includes("createRSCHandler") &&
|
|
62
72
|
!code.includes("@rangojs/router:version") &&
|
|
@@ -70,9 +80,25 @@ export function createVersionInjectorPlugin(
|
|
|
70
80
|
);
|
|
71
81
|
}
|
|
72
82
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
83
|
+
// Insert after any leading `/// <reference ... />` triple-slash
|
|
84
|
+
// directives (and surrounding blank lines). TypeScript requires those
|
|
85
|
+
// directives to precede all other code; putting our imports above
|
|
86
|
+
// them silently demotes the directives to plain comments.
|
|
87
|
+
const lines = newCode.split("\n");
|
|
88
|
+
let insertAt = 0;
|
|
89
|
+
while (insertAt < lines.length) {
|
|
90
|
+
const trimmed = lines[insertAt]!.trim();
|
|
91
|
+
if (trimmed === "" || /^\/\/\/\s*<reference\b/.test(trimmed)) {
|
|
92
|
+
insertAt++;
|
|
93
|
+
} else {
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
newCode = [
|
|
98
|
+
...lines.slice(0, insertAt),
|
|
99
|
+
...prepend,
|
|
100
|
+
...lines.slice(insertAt),
|
|
101
|
+
].join("\n");
|
|
76
102
|
|
|
77
103
|
return {
|
|
78
104
|
code: newCode,
|
|
@@ -35,7 +35,10 @@ import {
|
|
|
35
35
|
type DiscoveryState,
|
|
36
36
|
type PluginOptions,
|
|
37
37
|
} from "./discovery/state.js";
|
|
38
|
-
import {
|
|
38
|
+
import {
|
|
39
|
+
consumeSelfGenWrite,
|
|
40
|
+
peekSelfGenWrite,
|
|
41
|
+
} from "./discovery/self-gen-tracking.js";
|
|
39
42
|
import { discoverRouters } from "./discovery/discover-routers.js";
|
|
40
43
|
import {
|
|
41
44
|
writeCombinedRouteTypesWithTracking,
|
|
@@ -47,6 +50,7 @@ import {
|
|
|
47
50
|
generatePerRouterModule,
|
|
48
51
|
} from "./discovery/virtual-module-codegen.js";
|
|
49
52
|
import { postprocessBundle } from "./discovery/bundle-postprocess.js";
|
|
53
|
+
import { createDiscoveryGate } from "./discovery/gate-state.js";
|
|
50
54
|
import { resetStagedBuildAssets } from "./utils/prerender-utils.js";
|
|
51
55
|
import { createRangoDebugger, timed, timedSync, NS } from "./debug.js";
|
|
52
56
|
|
|
@@ -360,6 +364,17 @@ export function createRouterDiscoveryPlugin(
|
|
|
360
364
|
resolveDiscovery = resolve;
|
|
361
365
|
});
|
|
362
366
|
|
|
367
|
+
// Manifest-readiness gate + rediscovery scheduler.
|
|
368
|
+
// The virtual:rsc-router/routes-manifest module's `load()` hook
|
|
369
|
+
// awaits `s.discoveryDone`; the gate is reset on each discovery
|
|
370
|
+
// cycle so workerd's HMR reloads block until the new gen file is
|
|
371
|
+
// written. State machine + transitions are extracted into
|
|
372
|
+
// ./discovery/gate-state.ts and unit-tested there — see the
|
|
373
|
+
// module's JSDoc for the four-flag contract.
|
|
374
|
+
const gate = createDiscoveryGate(s, debugDiscovery);
|
|
375
|
+
const beginDiscoveryGate = gate.beginGate;
|
|
376
|
+
const resolveDiscoveryGate = gate.resolveGate;
|
|
377
|
+
|
|
363
378
|
// Compute dev server origin from resolved URLs (preferred) or config port (fallback).
|
|
364
379
|
// Called after discovery (or in the load hook) when the server may be listening.
|
|
365
380
|
const getDevServerOrigin = () =>
|
|
@@ -382,10 +397,103 @@ export function createRouterDiscoveryPlugin(
|
|
|
382
397
|
releaseBuildEnv(s).catch(() => {});
|
|
383
398
|
});
|
|
384
399
|
|
|
400
|
+
// Mirror the build-path contract (router-discovery.ts ~line 878):
|
|
401
|
+
// set __rscRouterDiscoveryActive before running user modules so any
|
|
402
|
+
// module-level router.reverse() calls return a placeholder instead
|
|
403
|
+
// of throwing. The temp Vite server's module runner has its own
|
|
404
|
+
// module context; the flag must be on globalThis to cross that
|
|
405
|
+
// boundary. Cleared in finally so the dev request handlers run with
|
|
406
|
+
// strict reverse() semantics afterwards.
|
|
407
|
+
async function importEntryAndRegistry(tempRscEnv: any): Promise<void> {
|
|
408
|
+
const flagAlreadySet = !!(globalThis as any).__rscRouterDiscoveryActive;
|
|
409
|
+
if (!flagAlreadySet) {
|
|
410
|
+
(globalThis as any).__rscRouterDiscoveryActive = true;
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
debugDiscovery?.(
|
|
414
|
+
"importEntryAndRegistry: importing entry (flag=%s)",
|
|
415
|
+
(globalThis as any).__rscRouterDiscoveryActive ?? false,
|
|
416
|
+
);
|
|
417
|
+
await tempRscEnv.runner.import(s.resolvedEntryPath!);
|
|
418
|
+
debugDiscovery?.(
|
|
419
|
+
"importEntryAndRegistry: entry import OK, fetching RouterRegistry",
|
|
420
|
+
);
|
|
421
|
+
const serverMod = await tempRscEnv.runner.import(
|
|
422
|
+
"@rangojs/router/server",
|
|
423
|
+
);
|
|
424
|
+
prerenderNodeRegistry = serverMod.RouterRegistry;
|
|
425
|
+
debugDiscovery?.(
|
|
426
|
+
"importEntryAndRegistry: registry size=%d",
|
|
427
|
+
prerenderNodeRegistry?.size ?? 0,
|
|
428
|
+
);
|
|
429
|
+
} finally {
|
|
430
|
+
if (!flagAlreadySet) {
|
|
431
|
+
delete (globalThis as any).__rscRouterDiscoveryActive;
|
|
432
|
+
debugDiscovery?.(
|
|
433
|
+
"importEntryAndRegistry: cleared __rscRouterDiscoveryActive",
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
385
439
|
async function getOrCreateTempServer(): Promise<any | null> {
|
|
386
|
-
if
|
|
387
|
-
|
|
440
|
+
// Reuse path: if a temp server is already alive, prefer reusing
|
|
441
|
+
// it over orphaning the existing instance and spinning up a new
|
|
442
|
+
// one. This handles two cases:
|
|
443
|
+
//
|
|
444
|
+
// 1. Steady-state cache hit (cold-start completed, registry
|
|
445
|
+
// cached) — return the env immediately.
|
|
446
|
+
// 2. Recovery from a failed refresh: refreshTempRscEnv() may
|
|
447
|
+
// have invalidated and nulled the registry, then thrown
|
|
448
|
+
// during importEntryAndRegistry. Without reuse, the next
|
|
449
|
+
// call would `createTempRscServer` and overwrite the
|
|
450
|
+
// handle, leaking the previous server. Try to re-import on
|
|
451
|
+
// the existing runner first; only if THAT fails do we
|
|
452
|
+
// close the orphan and create new.
|
|
453
|
+
if (prerenderTempServer) {
|
|
454
|
+
const existingEnv = (prerenderTempServer.environments as any)?.rsc;
|
|
455
|
+
if (existingEnv?.runner) {
|
|
456
|
+
if (prerenderNodeRegistry) {
|
|
457
|
+
debugDiscovery?.(
|
|
458
|
+
"getOrCreateTempServer: cached temp runner reused",
|
|
459
|
+
);
|
|
460
|
+
return existingEnv;
|
|
461
|
+
}
|
|
462
|
+
// Server alive but registry missing — likely after a prior
|
|
463
|
+
// refresh's invalidate + import threw. Try to re-import.
|
|
464
|
+
debugDiscovery?.(
|
|
465
|
+
"getOrCreateTempServer: server alive but registry missing — re-importing",
|
|
466
|
+
);
|
|
467
|
+
try {
|
|
468
|
+
await importEntryAndRegistry(existingEnv);
|
|
469
|
+
return existingEnv;
|
|
470
|
+
} catch (err: any) {
|
|
471
|
+
debugDiscovery?.(
|
|
472
|
+
"getOrCreateTempServer: reuse import failed (%s) — closing orphan and creating fresh",
|
|
473
|
+
err?.message ?? String(err),
|
|
474
|
+
);
|
|
475
|
+
await prerenderTempServer.close().catch(() => {});
|
|
476
|
+
prerenderTempServer = null;
|
|
477
|
+
prerenderNodeRegistry = null;
|
|
478
|
+
// Fall through to create-new path below.
|
|
479
|
+
}
|
|
480
|
+
} else {
|
|
481
|
+
// Server reference exists but its rsc env is unhealthy
|
|
482
|
+
// (no runner). Close and recreate.
|
|
483
|
+
debugDiscovery?.(
|
|
484
|
+
"getOrCreateTempServer: existing server has no rsc.runner — closing and recreating",
|
|
485
|
+
);
|
|
486
|
+
await prerenderTempServer.close().catch(() => {});
|
|
487
|
+
prerenderTempServer = null;
|
|
488
|
+
prerenderNodeRegistry = null;
|
|
489
|
+
}
|
|
388
490
|
}
|
|
491
|
+
|
|
492
|
+
// Create path: no existing temp server (or just nullified above).
|
|
493
|
+
debugDiscovery?.(
|
|
494
|
+
"getOrCreateTempServer: creating new temp server, entry=%s",
|
|
495
|
+
s.resolvedEntryPath ?? "(unset)",
|
|
496
|
+
);
|
|
389
497
|
try {
|
|
390
498
|
prerenderTempServer = await createTempRscServer(s, {
|
|
391
499
|
cacheDir: "node_modules/.vite_prerender",
|
|
@@ -393,14 +501,17 @@ export function createRouterDiscoveryPlugin(
|
|
|
393
501
|
|
|
394
502
|
const tempRscEnv = (prerenderTempServer.environments as any)?.rsc;
|
|
395
503
|
if (tempRscEnv?.runner) {
|
|
396
|
-
await tempRscEnv
|
|
397
|
-
const serverMod = await tempRscEnv.runner.import(
|
|
398
|
-
"@rangojs/router/server",
|
|
399
|
-
);
|
|
400
|
-
prerenderNodeRegistry = serverMod.RouterRegistry;
|
|
504
|
+
await importEntryAndRegistry(tempRscEnv);
|
|
401
505
|
return tempRscEnv;
|
|
402
506
|
}
|
|
507
|
+
debugDiscovery?.(
|
|
508
|
+
"getOrCreateTempServer: tempRscEnv.runner unavailable",
|
|
509
|
+
);
|
|
403
510
|
} catch (err: any) {
|
|
511
|
+
debugDiscovery?.(
|
|
512
|
+
"getOrCreateTempServer: FAILED message=%s",
|
|
513
|
+
err.message,
|
|
514
|
+
);
|
|
404
515
|
console.warn(
|
|
405
516
|
`[rsc-router] Failed to create temp runner: ${err.message}`,
|
|
406
517
|
);
|
|
@@ -408,6 +519,104 @@ export function createRouterDiscoveryPlugin(
|
|
|
408
519
|
return null;
|
|
409
520
|
}
|
|
410
521
|
|
|
522
|
+
// Clear the package-level singleton registries that survive a Vite
|
|
523
|
+
// moduleGraph.invalidateAll(). createRouter() / createHostRouter()
|
|
524
|
+
// call .set(id, ...) on these Maps; for "router removed" or
|
|
525
|
+
// "router id changed" edits, the OLD entry would persist after
|
|
526
|
+
// re-import without an explicit .clear(), leaving ghost routes
|
|
527
|
+
// in discoverRouters' output.
|
|
528
|
+
//
|
|
529
|
+
// We import the same module the runner imports, so the .clear()
|
|
530
|
+
// here mutates the same Map the freshly re-imported entry will
|
|
531
|
+
// populate.
|
|
532
|
+
async function clearTempRegistries(tempRscEnv: any): Promise<void> {
|
|
533
|
+
try {
|
|
534
|
+
const serverMod = await tempRscEnv.runner.import(
|
|
535
|
+
"@rangojs/router/server",
|
|
536
|
+
);
|
|
537
|
+
if (typeof serverMod?.RouterRegistry?.clear === "function") {
|
|
538
|
+
serverMod.RouterRegistry.clear();
|
|
539
|
+
}
|
|
540
|
+
if (typeof serverMod?.HostRouterRegistry?.clear === "function") {
|
|
541
|
+
serverMod.HostRouterRegistry.clear();
|
|
542
|
+
}
|
|
543
|
+
debugDiscovery?.(
|
|
544
|
+
"clearTempRegistries: cleared RouterRegistry + HostRouterRegistry",
|
|
545
|
+
);
|
|
546
|
+
} catch (err: any) {
|
|
547
|
+
// Non-fatal: if the import fails here, importEntryAndRegistry
|
|
548
|
+
// below will fail loudly with the same root cause and the
|
|
549
|
+
// caller will surface it.
|
|
550
|
+
debugDiscovery?.(
|
|
551
|
+
"clearTempRegistries: import @rangojs/router/server failed (%s)",
|
|
552
|
+
err?.message ?? String(err),
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// HMR refresh: keep the temp Vite server alive across HMR cycles and
|
|
558
|
+
// invalidate its module graph instead of close+recreate. Closing the
|
|
559
|
+
// temp server during workerd's first post-cold-start module-fetch
|
|
560
|
+
// window disrupted the main dev server's transport — the user-visible
|
|
561
|
+
// symptom was a `transport was disconnected, cannot call "fetchModule"`
|
|
562
|
+
// error on the first urls.tsx edit (workerd's cache was cold, so its
|
|
563
|
+
// eval was still in flight when our close() ran). Module-graph
|
|
564
|
+
// invalidation is the architecturally cleaner refresh: same Vite
|
|
565
|
+
// instance, same transport, fresh source.
|
|
566
|
+
//
|
|
567
|
+
// Falls back to close+recreate when neither the env-level nor
|
|
568
|
+
// server-level moduleGraph exposes invalidateAll() (defensive — Vite
|
|
569
|
+
// versions / preset configurations may differ in which graph carries
|
|
570
|
+
// the module-runner cache).
|
|
571
|
+
async function refreshTempRscEnv(): Promise<any | null> {
|
|
572
|
+
let tempRscEnv = await getOrCreateTempServer();
|
|
573
|
+
if (!tempRscEnv) return null;
|
|
574
|
+
|
|
575
|
+
// Module-runner cache is on the per-environment graph in Vite 6+;
|
|
576
|
+
// older / non-environments setups carry it on the server graph.
|
|
577
|
+
// Try env first, server second.
|
|
578
|
+
const envGraph = (tempRscEnv as any).moduleGraph;
|
|
579
|
+
const serverGraph = (prerenderTempServer as any)?.moduleGraph;
|
|
580
|
+
const target = envGraph?.invalidateAll
|
|
581
|
+
? envGraph
|
|
582
|
+
: serverGraph?.invalidateAll
|
|
583
|
+
? serverGraph
|
|
584
|
+
: null;
|
|
585
|
+
|
|
586
|
+
if (!target) {
|
|
587
|
+
// No invalidate method available — fall back to close+recreate.
|
|
588
|
+
// This preserves the previous behavior in case a Vite version
|
|
589
|
+
// doesn't expose invalidateAll on either graph.
|
|
590
|
+
debugDiscovery?.(
|
|
591
|
+
"refreshTempRscEnv: invalidateAll unavailable on env+server graphs, falling back to close+recreate",
|
|
592
|
+
);
|
|
593
|
+
if (prerenderTempServer) {
|
|
594
|
+
await prerenderTempServer.close().catch(() => {});
|
|
595
|
+
prerenderTempServer = null;
|
|
596
|
+
prerenderNodeRegistry = null;
|
|
597
|
+
}
|
|
598
|
+
return await getOrCreateTempServer();
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
debugDiscovery?.(
|
|
602
|
+
"refreshTempRscEnv: invalidating module graph (%s)",
|
|
603
|
+
envGraph?.invalidateAll ? "env" : "server",
|
|
604
|
+
);
|
|
605
|
+
target.invalidateAll();
|
|
606
|
+
// Drop the cached registry so importEntryAndRegistry re-reads it
|
|
607
|
+
// through the now-invalidated module runner.
|
|
608
|
+
prerenderNodeRegistry = null;
|
|
609
|
+
// Clear singleton Maps that Vite's moduleGraph invalidation can't
|
|
610
|
+
// reach (RouterRegistry / HostRouterRegistry). Without this, an
|
|
611
|
+
// edit that REMOVES a createRouter() call or CHANGES a router id
|
|
612
|
+
// would leave the old entry in the registry, and discoverRouters
|
|
613
|
+
// would still emit its routes alongside whatever the new source
|
|
614
|
+
// declares.
|
|
615
|
+
await clearTempRegistries(tempRscEnv);
|
|
616
|
+
await importEntryAndRegistry(tempRscEnv);
|
|
617
|
+
return tempRscEnv;
|
|
618
|
+
}
|
|
619
|
+
|
|
411
620
|
const discover = async () => {
|
|
412
621
|
const discoverStart = performance.now();
|
|
413
622
|
const rscEnv = (server.environments as any)?.rsc;
|
|
@@ -415,7 +624,10 @@ export function createRouterDiscoveryPlugin(
|
|
|
415
624
|
// Cloudflare dev: no module runner available (workerd-based RSC env).
|
|
416
625
|
// Set devServerOrigin so the virtual module can inject __PRERENDER_DEV_URL
|
|
417
626
|
// for on-demand prerender via the /__rsc_prerender endpoint.
|
|
418
|
-
debugDiscovery?.(
|
|
627
|
+
debugDiscovery?.(
|
|
628
|
+
"dev: cloudflare path start, __rscRouterDiscoveryActive=%s",
|
|
629
|
+
(globalThis as any).__rscRouterDiscoveryActive ?? false,
|
|
630
|
+
);
|
|
419
631
|
s.devServerOrigin = getDevServerOrigin();
|
|
420
632
|
|
|
421
633
|
// Create a temp Node.js server to run runtime discovery and generate
|
|
@@ -505,10 +717,14 @@ export function createRouterDiscoveryPlugin(
|
|
|
505
717
|
};
|
|
506
718
|
|
|
507
719
|
// Schedule after all plugins have finished configureServer.
|
|
508
|
-
//
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
720
|
+
// The gate (s.discoveryDone) is reset via beginDiscoveryGate() and
|
|
721
|
+
// resolved when discover() finishes, so the virtual manifest module's
|
|
722
|
+
// load() awaits the populated state.
|
|
723
|
+
beginDiscoveryGate();
|
|
724
|
+
setTimeout(
|
|
725
|
+
() => discover().then(resolveDiscoveryGate, resolveDiscoveryGate),
|
|
726
|
+
0,
|
|
727
|
+
);
|
|
512
728
|
|
|
513
729
|
// Dev-mode on-demand prerender endpoint.
|
|
514
730
|
// When workerd hits a prerender route, it fetches this endpoint instead of
|
|
@@ -720,33 +936,68 @@ export function createRouterDiscoveryPlugin(
|
|
|
720
936
|
|
|
721
937
|
// Re-run runtime discovery so factory-generated routes that the
|
|
722
938
|
// static parser cannot see are refreshed after source changes.
|
|
723
|
-
|
|
939
|
+
// The state-machine concerns (queued/pending/gatePending) are
|
|
940
|
+
// owned by the gate created above (./discovery/gate-state.ts).
|
|
941
|
+
// Here we provide just the env-specific work.
|
|
724
942
|
const refreshRuntimeDiscovery = async () => {
|
|
725
943
|
const rscEnv = (server.environments as any)?.rsc;
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
);
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
944
|
+
const hasMainRunner = !!rscEnv?.runner;
|
|
945
|
+
// Cloudflare HMR has no main RSC runner (workerd is a separate
|
|
946
|
+
// runtime). When we have a populated runtime manifest from cold
|
|
947
|
+
// start, we can re-discover via the temp Node runner — the same
|
|
948
|
+
// mechanism getOrCreateTempServer() uses at startup. Without a
|
|
949
|
+
// populated manifest there's nothing useful to do, so bail
|
|
950
|
+
// before involving the gate machine at all.
|
|
951
|
+
if (!hasMainRunner && s.perRouterManifests.length === 0) return;
|
|
952
|
+
await gate.runRefreshCycle(async () => {
|
|
953
|
+
const hmrStart = performance.now();
|
|
954
|
+
try {
|
|
955
|
+
if (hasMainRunner) {
|
|
956
|
+
await timed(debugDiscovery, "hmr discoverRouters", () =>
|
|
957
|
+
discoverRouters(s, rscEnv),
|
|
958
|
+
);
|
|
959
|
+
timedSync(debugDiscovery, "hmr writeRouteTypesFiles", () =>
|
|
960
|
+
writeRouteTypesFiles(s),
|
|
961
|
+
);
|
|
962
|
+
await timed(debugDiscovery, "hmr propagateDiscoveryState", () =>
|
|
963
|
+
propagateDiscoveryState(rscEnv),
|
|
964
|
+
);
|
|
965
|
+
} else {
|
|
966
|
+
// Cloudflare HMR: invalidate the temp server's RSC module
|
|
967
|
+
// graph (or close+recreate as a fallback) so the runner
|
|
968
|
+
// re-reads the freshly edited source. Keeping the same
|
|
969
|
+
// Vite instance alive avoids disrupting workerd's transport
|
|
970
|
+
// during the first post-cold-start module-fetch window.
|
|
971
|
+
const tempRscEnv = await timed(
|
|
972
|
+
debugDiscovery,
|
|
973
|
+
"hmr refreshTempRscEnv (cloudflare)",
|
|
974
|
+
() => refreshTempRscEnv(),
|
|
975
|
+
);
|
|
976
|
+
if (!tempRscEnv) {
|
|
977
|
+
throw new Error(
|
|
978
|
+
"temp runner unavailable for cloudflare HMR rediscovery",
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
await timed(
|
|
982
|
+
debugDiscovery,
|
|
983
|
+
"hmr discoverRouters (cloudflare)",
|
|
984
|
+
() => discoverRouters(s, tempRscEnv),
|
|
985
|
+
);
|
|
986
|
+
timedSync(debugDiscovery, "hmr writeRouteTypesFiles", () =>
|
|
987
|
+
writeRouteTypesFiles(s),
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
} catch (err: any) {
|
|
991
|
+
console.warn(
|
|
992
|
+
`[rsc-router] Runtime re-discovery failed: ${err.message}`,
|
|
993
|
+
);
|
|
994
|
+
} finally {
|
|
995
|
+
debugDiscovery?.(
|
|
996
|
+
"hmr re-discovery done (%sms)",
|
|
997
|
+
(performance.now() - hmrStart).toFixed(1),
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
750
1001
|
};
|
|
751
1002
|
|
|
752
1003
|
const scheduleRouteRegeneration = () => {
|
|
@@ -754,10 +1005,27 @@ export function createRouterDiscoveryPlugin(
|
|
|
754
1005
|
routeChangeTimer = setTimeout(() => {
|
|
755
1006
|
routeChangeTimer = undefined;
|
|
756
1007
|
const regenStart = debugDiscovery ? performance.now() : 0;
|
|
1008
|
+
const rscEnv = (server.environments as any)?.rsc;
|
|
1009
|
+
const skipStaticWrite =
|
|
1010
|
+
!rscEnv?.runner && s.perRouterManifests.length > 0;
|
|
757
1011
|
try {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
1012
|
+
// In cloudflare dev with a populated runtime manifest, the
|
|
1013
|
+
// static parser produces a strictly smaller (and actively
|
|
1014
|
+
// wrong) gen file — supplementGenFilesWithRuntimeRoutes can
|
|
1015
|
+
// only restore factory-only prefixes, and apps with mixed
|
|
1016
|
+
// static+factory routes under shared prefixes (cf-stress)
|
|
1017
|
+
// collapse to the 19-route static view. Skip the static
|
|
1018
|
+
// write entirely; runtime rediscovery below will overwrite
|
|
1019
|
+
// the gen file with the authoritative manifest.
|
|
1020
|
+
if (skipStaticWrite) {
|
|
1021
|
+
debugDiscovery?.(
|
|
1022
|
+
"watcher: skipping static write (cloudflare HMR — runtime rediscovery owns gen file)",
|
|
1023
|
+
);
|
|
1024
|
+
} else {
|
|
1025
|
+
writeCombinedRouteTypesWithTracking(s);
|
|
1026
|
+
if (s.perRouterManifests.length > 0) {
|
|
1027
|
+
supplementGenFilesWithRuntimeRoutes(s);
|
|
1028
|
+
}
|
|
761
1029
|
}
|
|
762
1030
|
} catch (err: any) {
|
|
763
1031
|
console.error(
|
|
@@ -769,12 +1037,16 @@ export function createRouterDiscoveryPlugin(
|
|
|
769
1037
|
(performance.now() - regenStart).toFixed(1),
|
|
770
1038
|
);
|
|
771
1039
|
// Async: re-run runtime discovery to refresh factory-generated
|
|
772
|
-
// routes that the static parser cannot resolve.
|
|
1040
|
+
// routes that the static parser cannot resolve. Resolves the
|
|
1041
|
+
// discovery gate when complete.
|
|
773
1042
|
if (s.perRouterManifests.length > 0) {
|
|
774
1043
|
refreshRuntimeDiscovery().catch((err: any) => {
|
|
775
1044
|
console.warn(
|
|
776
1045
|
`[rsc-router] Runtime re-discovery error: ${err.message}`,
|
|
777
1046
|
);
|
|
1047
|
+
// Even on error, unblock the gate so workerd's reload
|
|
1048
|
+
// doesn't hang indefinitely against the previous manifest.
|
|
1049
|
+
resolveDiscoveryGate();
|
|
778
1050
|
});
|
|
779
1051
|
}
|
|
780
1052
|
}, 100);
|
|
@@ -822,6 +1094,17 @@ export function createRouterDiscoveryPlugin(
|
|
|
822
1094
|
}
|
|
823
1095
|
s.cachedRouterFiles = undefined;
|
|
824
1096
|
}
|
|
1097
|
+
// Note the event in the gate machine IMMEDIATELY (before the
|
|
1098
|
+
// 100ms debounce and any downstream HMR fanout). This sets
|
|
1099
|
+
// both `pendingEvents` (so refresh's finally holds the gate
|
|
1100
|
+
// through the tail window even if no rediscovery is queued)
|
|
1101
|
+
// and resets `discoveryDone` to a fresh pending promise (so
|
|
1102
|
+
// workerd reloads triggered by the same source change can't
|
|
1103
|
+
// observe a stale resolved gate from cold-start). Resolved
|
|
1104
|
+
// by the trailing refreshRuntimeDiscovery() cycle.
|
|
1105
|
+
if (s.perRouterManifests.length > 0) {
|
|
1106
|
+
gate.noteRouteEvent();
|
|
1107
|
+
}
|
|
825
1108
|
scheduleRouteRegeneration();
|
|
826
1109
|
} catch {
|
|
827
1110
|
// Ignore read errors for deleted/moved files
|
|
@@ -946,6 +1229,35 @@ export function createRouterDiscoveryPlugin(
|
|
|
946
1229
|
}
|
|
947
1230
|
},
|
|
948
1231
|
|
|
1232
|
+
// Suppress vite's HMR cascade for our own gen-file writes.
|
|
1233
|
+
//
|
|
1234
|
+
// After every cf HMR cycle, refreshTempRscEnv → writeRouteTypesFiles
|
|
1235
|
+
// writes the configured gen files (default `router.named-routes.gen.ts`,
|
|
1236
|
+
// but the source filenames and gen suffix are user-configurable). The
|
|
1237
|
+
// chokidar watcher then fires twice independently: our
|
|
1238
|
+
// `handleRouteFileChange` (already short-circuited by
|
|
1239
|
+
// `consumeSelfGenWrite` inside `maybeHandleGeneratedRouteFileMutation`),
|
|
1240
|
+
// AND vite's own HMR pipeline (which invalidates the gen file's
|
|
1241
|
+
// importers and triggers a second workerd full reload — visible to the
|
|
1242
|
+
// user as a duplicate "[RSCRouter] HMR: version changed" on the client).
|
|
1243
|
+
//
|
|
1244
|
+
// `peekSelfGenWrite` is the authoritative filter: its map only contains
|
|
1245
|
+
// paths that `markSelfGenWrite` has registered, so it natively works
|
|
1246
|
+
// for any configured gen-file name. It is non-consuming so the chokidar
|
|
1247
|
+
// handler that fires later can still consume the same entry. Returning
|
|
1248
|
+
// [] tells vite "no modules invalidated by this change" — safe because
|
|
1249
|
+
// `s.perRouterManifests` is already up-to-date (the write that just
|
|
1250
|
+
// happened is the consequence of our just-completed rediscovery).
|
|
1251
|
+
handleHotUpdate(ctx) {
|
|
1252
|
+
if (peekSelfGenWrite(s, ctx.file)) {
|
|
1253
|
+
debugDiscovery?.(
|
|
1254
|
+
"handleHotUpdate: suppressing self-write HMR cascade for %s",
|
|
1255
|
+
ctx.file,
|
|
1256
|
+
);
|
|
1257
|
+
return [];
|
|
1258
|
+
}
|
|
1259
|
+
},
|
|
1260
|
+
|
|
949
1261
|
// Virtual module: provides the pre-generated route manifest as a JS module
|
|
950
1262
|
// that calls setCachedManifest() at import time.
|
|
951
1263
|
resolveId(id) {
|