@rangojs/router 0.0.0-experimental.92 → 0.0.0-experimental.93

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.
@@ -2040,7 +2040,7 @@ import { resolve } from "node:path";
2040
2040
  // package.json
2041
2041
  var package_default = {
2042
2042
  name: "@rangojs/router",
2043
- version: "0.0.0-experimental.92",
2043
+ version: "0.0.0-experimental.93",
2044
2044
  description: "Django-inspired RSC router with composable URL patterns",
2045
2045
  keywords: [
2046
2046
  "react",
@@ -3376,11 +3376,10 @@ function createVersionInjectorPlugin(rscEntryPath) {
3376
3376
  if (normalizedId !== normalizedEntry) {
3377
3377
  return null;
3378
3378
  }
3379
- const prepend = [];
3379
+ const prepend = [
3380
+ `import "virtual:rsc-router/routes-manifest";`
3381
+ ];
3380
3382
  let newCode = code;
3381
- if (!code.includes("virtual:rsc-router/routes-manifest")) {
3382
- prepend.push(`import "virtual:rsc-router/routes-manifest";`);
3383
- }
3384
3383
  const needsVersion = code.includes("createRSCHandler") && !code.includes("@rangojs/router:version") && /createRSCHandler\s*\(\s*\{/.test(code);
3385
3384
  if (needsVersion) {
3386
3385
  prepend.push(`import { VERSION } from "@rangojs/router:version";`);
@@ -3389,8 +3388,21 @@ function createVersionInjectorPlugin(rscEntryPath) {
3389
3388
  "createRSCHandler({\n version: VERSION,"
3390
3389
  );
3391
3390
  }
3392
- if (prepend.length === 0 && newCode === code) return null;
3393
- newCode = prepend.join("\n") + (prepend.length > 0 ? "\n" : "") + newCode;
3391
+ const lines = newCode.split("\n");
3392
+ let insertAt = 0;
3393
+ while (insertAt < lines.length) {
3394
+ const trimmed = lines[insertAt].trim();
3395
+ if (trimmed === "" || /^\/\/\/\s*<reference\b/.test(trimmed)) {
3396
+ insertAt++;
3397
+ } else {
3398
+ break;
3399
+ }
3400
+ }
3401
+ newCode = [
3402
+ ...lines.slice(0, insertAt),
3403
+ ...prepend,
3404
+ ...lines.slice(insertAt)
3405
+ ].join("\n");
3394
3406
  return {
3395
3407
  code: newCode,
3396
3408
  map: null
@@ -3784,6 +3796,12 @@ function markSelfGenWrite(state, filePath, content) {
3784
3796
  state.selfWrittenGenFiles.set(filePath, { at: Date.now(), hash });
3785
3797
  }
3786
3798
  function consumeSelfGenWrite(state, filePath) {
3799
+ return checkSelfGenWrite(state, filePath, true);
3800
+ }
3801
+ function peekSelfGenWrite(state, filePath) {
3802
+ return checkSelfGenWrite(state, filePath, false);
3803
+ }
3804
+ function checkSelfGenWrite(state, filePath, consume) {
3787
3805
  const info = state.selfWrittenGenFiles.get(filePath);
3788
3806
  if (!info) return false;
3789
3807
  if (Date.now() - info.at > state.SELF_WRITE_WINDOW_MS) {
@@ -3794,7 +3812,7 @@ function consumeSelfGenWrite(state, filePath) {
3794
3812
  const current = readFileSync3(filePath, "utf-8");
3795
3813
  const currentHash = createHash3("sha256").update(current).digest("hex");
3796
3814
  if (currentHash === info.hash) {
3797
- state.selfWrittenGenFiles.delete(filePath);
3815
+ if (consume) state.selfWrittenGenFiles.delete(filePath);
3798
3816
  return true;
3799
3817
  }
3800
3818
  return false;
@@ -5067,6 +5085,80 @@ function postprocessBundle(state) {
5067
5085
  }
5068
5086
  }
5069
5087
 
5088
+ // src/vite/discovery/gate-state.ts
5089
+ function createDiscoveryGate(s, debug11) {
5090
+ let gatePending = false;
5091
+ let gateResolver = () => {
5092
+ };
5093
+ let inProgress = false;
5094
+ let queued = false;
5095
+ let pendingEvents = false;
5096
+ const beginGate = () => {
5097
+ if (gatePending) return;
5098
+ s.discoveryDone = new Promise((resolve10) => {
5099
+ gateResolver = resolve10;
5100
+ });
5101
+ gatePending = true;
5102
+ };
5103
+ const resolveGate = () => {
5104
+ if (!gatePending) return;
5105
+ if (inProgress || queued || pendingEvents) {
5106
+ debug11?.(
5107
+ "hmr: resolveGate deferred \u2014 work in flight (inProgress=%s queued=%s pendingEvents=%s)",
5108
+ inProgress,
5109
+ queued,
5110
+ pendingEvents
5111
+ );
5112
+ return;
5113
+ }
5114
+ gatePending = false;
5115
+ debug11?.("hmr: discoveryDone resolved");
5116
+ gateResolver();
5117
+ };
5118
+ const noteRouteEvent = () => {
5119
+ pendingEvents = true;
5120
+ beginGate();
5121
+ };
5122
+ const runRefreshCycle = async (work) => {
5123
+ if (inProgress) {
5124
+ queued = true;
5125
+ debug11?.("hmr: rediscovery in flight \u2014 queued for a follow-up cycle");
5126
+ return;
5127
+ }
5128
+ pendingEvents = false;
5129
+ inProgress = true;
5130
+ try {
5131
+ await work();
5132
+ } finally {
5133
+ inProgress = false;
5134
+ if (queued) {
5135
+ queued = false;
5136
+ debug11?.("hmr: consuming queued rediscovery");
5137
+ runRefreshCycle(work).catch((err) => {
5138
+ debug11?.(
5139
+ "hmr: queued cycle rejected \u2014 releasing gate (%s)",
5140
+ err instanceof Error ? err.message : String(err)
5141
+ );
5142
+ resolveGate();
5143
+ });
5144
+ } else if (pendingEvents) {
5145
+ debug11?.(
5146
+ "hmr: holding gate for pending events (debounce not yet fired)"
5147
+ );
5148
+ } else {
5149
+ resolveGate();
5150
+ }
5151
+ }
5152
+ };
5153
+ return {
5154
+ beginGate,
5155
+ resolveGate,
5156
+ noteRouteEvent,
5157
+ runRefreshCycle,
5158
+ state: () => ({ gatePending, inProgress, queued, pendingEvents })
5159
+ };
5160
+ }
5161
+
5070
5162
  // src/vite/router-discovery.ts
5071
5163
  var debugDiscovery = createRangoDebugger(NS.discovery);
5072
5164
  var debugRoutes = createRangoDebugger(NS.routes);
@@ -5231,6 +5323,9 @@ function createRouterDiscoveryPlugin(entryPath, opts) {
5231
5323
  const discoveryPromise = new Promise((resolve10) => {
5232
5324
  resolveDiscovery = resolve10;
5233
5325
  });
5326
+ const gate = createDiscoveryGate(s, debugDiscovery);
5327
+ const beginDiscoveryGate = gate.beginGate;
5328
+ const resolveDiscoveryGate = gate.resolveGate;
5234
5329
  const getDevServerOrigin = () => server.resolvedUrls?.local?.[0]?.replace(/\/$/, "") || `http://localhost:${server.config.server.port || 5173}`;
5235
5330
  let prerenderTempServer = null;
5236
5331
  let prerenderNodeRegistry = null;
@@ -5243,35 +5338,157 @@ function createRouterDiscoveryPlugin(entryPath, opts) {
5243
5338
  releaseBuildEnv(s).catch(() => {
5244
5339
  });
5245
5340
  });
5341
+ async function importEntryAndRegistry(tempRscEnv) {
5342
+ const flagAlreadySet = !!globalThis.__rscRouterDiscoveryActive;
5343
+ if (!flagAlreadySet) {
5344
+ globalThis.__rscRouterDiscoveryActive = true;
5345
+ }
5346
+ try {
5347
+ debugDiscovery?.(
5348
+ "importEntryAndRegistry: importing entry (flag=%s)",
5349
+ globalThis.__rscRouterDiscoveryActive ?? false
5350
+ );
5351
+ await tempRscEnv.runner.import(s.resolvedEntryPath);
5352
+ debugDiscovery?.(
5353
+ "importEntryAndRegistry: entry import OK, fetching RouterRegistry"
5354
+ );
5355
+ const serverMod = await tempRscEnv.runner.import(
5356
+ "@rangojs/router/server"
5357
+ );
5358
+ prerenderNodeRegistry = serverMod.RouterRegistry;
5359
+ debugDiscovery?.(
5360
+ "importEntryAndRegistry: registry size=%d",
5361
+ prerenderNodeRegistry?.size ?? 0
5362
+ );
5363
+ } finally {
5364
+ if (!flagAlreadySet) {
5365
+ delete globalThis.__rscRouterDiscoveryActive;
5366
+ debugDiscovery?.(
5367
+ "importEntryAndRegistry: cleared __rscRouterDiscoveryActive"
5368
+ );
5369
+ }
5370
+ }
5371
+ }
5246
5372
  async function getOrCreateTempServer() {
5247
- if (prerenderNodeRegistry) {
5248
- return prerenderTempServer.environments?.rsc ?? null;
5373
+ if (prerenderTempServer) {
5374
+ const existingEnv = prerenderTempServer.environments?.rsc;
5375
+ if (existingEnv?.runner) {
5376
+ if (prerenderNodeRegistry) {
5377
+ debugDiscovery?.(
5378
+ "getOrCreateTempServer: cached temp runner reused"
5379
+ );
5380
+ return existingEnv;
5381
+ }
5382
+ debugDiscovery?.(
5383
+ "getOrCreateTempServer: server alive but registry missing \u2014 re-importing"
5384
+ );
5385
+ try {
5386
+ await importEntryAndRegistry(existingEnv);
5387
+ return existingEnv;
5388
+ } catch (err) {
5389
+ debugDiscovery?.(
5390
+ "getOrCreateTempServer: reuse import failed (%s) \u2014 closing orphan and creating fresh",
5391
+ err?.message ?? String(err)
5392
+ );
5393
+ await prerenderTempServer.close().catch(() => {
5394
+ });
5395
+ prerenderTempServer = null;
5396
+ prerenderNodeRegistry = null;
5397
+ }
5398
+ } else {
5399
+ debugDiscovery?.(
5400
+ "getOrCreateTempServer: existing server has no rsc.runner \u2014 closing and recreating"
5401
+ );
5402
+ await prerenderTempServer.close().catch(() => {
5403
+ });
5404
+ prerenderTempServer = null;
5405
+ prerenderNodeRegistry = null;
5406
+ }
5249
5407
  }
5408
+ debugDiscovery?.(
5409
+ "getOrCreateTempServer: creating new temp server, entry=%s",
5410
+ s.resolvedEntryPath ?? "(unset)"
5411
+ );
5250
5412
  try {
5251
5413
  prerenderTempServer = await createTempRscServer(s, {
5252
5414
  cacheDir: "node_modules/.vite_prerender"
5253
5415
  });
5254
5416
  const tempRscEnv = prerenderTempServer.environments?.rsc;
5255
5417
  if (tempRscEnv?.runner) {
5256
- await tempRscEnv.runner.import(s.resolvedEntryPath);
5257
- const serverMod = await tempRscEnv.runner.import(
5258
- "@rangojs/router/server"
5259
- );
5260
- prerenderNodeRegistry = serverMod.RouterRegistry;
5418
+ await importEntryAndRegistry(tempRscEnv);
5261
5419
  return tempRscEnv;
5262
5420
  }
5421
+ debugDiscovery?.(
5422
+ "getOrCreateTempServer: tempRscEnv.runner unavailable"
5423
+ );
5263
5424
  } catch (err) {
5425
+ debugDiscovery?.(
5426
+ "getOrCreateTempServer: FAILED message=%s",
5427
+ err.message
5428
+ );
5264
5429
  console.warn(
5265
5430
  `[rsc-router] Failed to create temp runner: ${err.message}`
5266
5431
  );
5267
5432
  }
5268
5433
  return null;
5269
5434
  }
5435
+ async function clearTempRegistries(tempRscEnv) {
5436
+ try {
5437
+ const serverMod = await tempRscEnv.runner.import(
5438
+ "@rangojs/router/server"
5439
+ );
5440
+ if (typeof serverMod?.RouterRegistry?.clear === "function") {
5441
+ serverMod.RouterRegistry.clear();
5442
+ }
5443
+ if (typeof serverMod?.HostRouterRegistry?.clear === "function") {
5444
+ serverMod.HostRouterRegistry.clear();
5445
+ }
5446
+ debugDiscovery?.(
5447
+ "clearTempRegistries: cleared RouterRegistry + HostRouterRegistry"
5448
+ );
5449
+ } catch (err) {
5450
+ debugDiscovery?.(
5451
+ "clearTempRegistries: import @rangojs/router/server failed (%s)",
5452
+ err?.message ?? String(err)
5453
+ );
5454
+ }
5455
+ }
5456
+ async function refreshTempRscEnv() {
5457
+ let tempRscEnv = await getOrCreateTempServer();
5458
+ if (!tempRscEnv) return null;
5459
+ const envGraph = tempRscEnv.moduleGraph;
5460
+ const serverGraph = prerenderTempServer?.moduleGraph;
5461
+ const target = envGraph?.invalidateAll ? envGraph : serverGraph?.invalidateAll ? serverGraph : null;
5462
+ if (!target) {
5463
+ debugDiscovery?.(
5464
+ "refreshTempRscEnv: invalidateAll unavailable on env+server graphs, falling back to close+recreate"
5465
+ );
5466
+ if (prerenderTempServer) {
5467
+ await prerenderTempServer.close().catch(() => {
5468
+ });
5469
+ prerenderTempServer = null;
5470
+ prerenderNodeRegistry = null;
5471
+ }
5472
+ return await getOrCreateTempServer();
5473
+ }
5474
+ debugDiscovery?.(
5475
+ "refreshTempRscEnv: invalidating module graph (%s)",
5476
+ envGraph?.invalidateAll ? "env" : "server"
5477
+ );
5478
+ target.invalidateAll();
5479
+ prerenderNodeRegistry = null;
5480
+ await clearTempRegistries(tempRscEnv);
5481
+ await importEntryAndRegistry(tempRscEnv);
5482
+ return tempRscEnv;
5483
+ }
5270
5484
  const discover = async () => {
5271
5485
  const discoverStart = performance.now();
5272
5486
  const rscEnv = server.environments?.rsc;
5273
5487
  if (!rscEnv?.runner) {
5274
- debugDiscovery?.("dev: no rsc runner (cloudflare path)");
5488
+ debugDiscovery?.(
5489
+ "dev: cloudflare path start, __rscRouterDiscoveryActive=%s",
5490
+ globalThis.__rscRouterDiscoveryActive ?? false
5491
+ );
5275
5492
  s.devServerOrigin = getDevServerOrigin();
5276
5493
  try {
5277
5494
  await timed(
@@ -5353,9 +5570,11 @@ ${err.stack}`
5353
5570
  resolveDiscovery();
5354
5571
  }
5355
5572
  };
5356
- s.discoveryDone = new Promise((resolve10) => {
5357
- setTimeout(() => discover().then(resolve10, resolve10), 0);
5358
- });
5573
+ beginDiscoveryGate();
5574
+ setTimeout(
5575
+ () => discover().then(resolveDiscoveryGate, resolveDiscoveryGate),
5576
+ 0
5577
+ );
5359
5578
  let mainRegistry = null;
5360
5579
  const propagateDiscoveryState = async (rscEnv) => {
5361
5580
  const serverMod = await rscEnv.runner.import("@rangojs/router/server");
@@ -5504,49 +5723,80 @@ ${err.stack}`
5504
5723
  return true;
5505
5724
  };
5506
5725
  let routeChangeTimer;
5507
- let runtimeRediscoveryInProgress = false;
5508
5726
  const refreshRuntimeDiscovery = async () => {
5509
5727
  const rscEnv = server.environments?.rsc;
5510
- if (!rscEnv?.runner || runtimeRediscoveryInProgress) return;
5511
- runtimeRediscoveryInProgress = true;
5512
- const hmrStart = performance.now();
5513
- try {
5514
- await timed(
5515
- debugDiscovery,
5516
- "hmr discoverRouters",
5517
- () => discoverRouters(s, rscEnv)
5518
- );
5519
- timedSync(
5520
- debugDiscovery,
5521
- "hmr writeRouteTypesFiles",
5522
- () => writeRouteTypesFiles(s)
5523
- );
5524
- await timed(
5525
- debugDiscovery,
5526
- "hmr propagateDiscoveryState",
5527
- () => propagateDiscoveryState(rscEnv)
5528
- );
5529
- } catch (err) {
5530
- console.warn(
5531
- `[rsc-router] Runtime re-discovery failed: ${err.message}`
5532
- );
5533
- } finally {
5534
- debugDiscovery?.(
5535
- "hmr re-discovery done (%sms)",
5536
- (performance.now() - hmrStart).toFixed(1)
5537
- );
5538
- runtimeRediscoveryInProgress = false;
5539
- }
5728
+ const hasMainRunner = !!rscEnv?.runner;
5729
+ if (!hasMainRunner && s.perRouterManifests.length === 0) return;
5730
+ await gate.runRefreshCycle(async () => {
5731
+ const hmrStart = performance.now();
5732
+ try {
5733
+ if (hasMainRunner) {
5734
+ await timed(
5735
+ debugDiscovery,
5736
+ "hmr discoverRouters",
5737
+ () => discoverRouters(s, rscEnv)
5738
+ );
5739
+ timedSync(
5740
+ debugDiscovery,
5741
+ "hmr writeRouteTypesFiles",
5742
+ () => writeRouteTypesFiles(s)
5743
+ );
5744
+ await timed(
5745
+ debugDiscovery,
5746
+ "hmr propagateDiscoveryState",
5747
+ () => propagateDiscoveryState(rscEnv)
5748
+ );
5749
+ } else {
5750
+ const tempRscEnv = await timed(
5751
+ debugDiscovery,
5752
+ "hmr refreshTempRscEnv (cloudflare)",
5753
+ () => refreshTempRscEnv()
5754
+ );
5755
+ if (!tempRscEnv) {
5756
+ throw new Error(
5757
+ "temp runner unavailable for cloudflare HMR rediscovery"
5758
+ );
5759
+ }
5760
+ await timed(
5761
+ debugDiscovery,
5762
+ "hmr discoverRouters (cloudflare)",
5763
+ () => discoverRouters(s, tempRscEnv)
5764
+ );
5765
+ timedSync(
5766
+ debugDiscovery,
5767
+ "hmr writeRouteTypesFiles",
5768
+ () => writeRouteTypesFiles(s)
5769
+ );
5770
+ }
5771
+ } catch (err) {
5772
+ console.warn(
5773
+ `[rsc-router] Runtime re-discovery failed: ${err.message}`
5774
+ );
5775
+ } finally {
5776
+ debugDiscovery?.(
5777
+ "hmr re-discovery done (%sms)",
5778
+ (performance.now() - hmrStart).toFixed(1)
5779
+ );
5780
+ }
5781
+ });
5540
5782
  };
5541
5783
  const scheduleRouteRegeneration = () => {
5542
5784
  clearTimeout(routeChangeTimer);
5543
5785
  routeChangeTimer = setTimeout(() => {
5544
5786
  routeChangeTimer = void 0;
5545
5787
  const regenStart = debugDiscovery ? performance.now() : 0;
5788
+ const rscEnv = server.environments?.rsc;
5789
+ const skipStaticWrite = !rscEnv?.runner && s.perRouterManifests.length > 0;
5546
5790
  try {
5547
- writeCombinedRouteTypesWithTracking(s);
5548
- if (s.perRouterManifests.length > 0) {
5549
- supplementGenFilesWithRuntimeRoutes(s);
5791
+ if (skipStaticWrite) {
5792
+ debugDiscovery?.(
5793
+ "watcher: skipping static write (cloudflare HMR \u2014 runtime rediscovery owns gen file)"
5794
+ );
5795
+ } else {
5796
+ writeCombinedRouteTypesWithTracking(s);
5797
+ if (s.perRouterManifests.length > 0) {
5798
+ supplementGenFilesWithRuntimeRoutes(s);
5799
+ }
5550
5800
  }
5551
5801
  } catch (err) {
5552
5802
  console.error(
@@ -5562,6 +5812,7 @@ ${err.stack}`
5562
5812
  console.warn(
5563
5813
  `[rsc-router] Runtime re-discovery error: ${err.message}`
5564
5814
  );
5815
+ resolveDiscoveryGate();
5565
5816
  });
5566
5817
  }
5567
5818
  }, 100);
@@ -5598,6 +5849,9 @@ ${err.stack}`
5598
5849
  }
5599
5850
  s.cachedRouterFiles = void 0;
5600
5851
  }
5852
+ if (s.perRouterManifests.length > 0) {
5853
+ gate.noteRouteEvent();
5854
+ }
5601
5855
  scheduleRouteRegeneration();
5602
5856
  } catch {
5603
5857
  }
@@ -5696,6 +5950,34 @@ ${details}`
5696
5950
  );
5697
5951
  }
5698
5952
  },
5953
+ // Suppress vite's HMR cascade for our own gen-file writes.
5954
+ //
5955
+ // After every cf HMR cycle, refreshTempRscEnv → writeRouteTypesFiles
5956
+ // writes the configured gen files (default `router.named-routes.gen.ts`,
5957
+ // but the source filenames and gen suffix are user-configurable). The
5958
+ // chokidar watcher then fires twice independently: our
5959
+ // `handleRouteFileChange` (already short-circuited by
5960
+ // `consumeSelfGenWrite` inside `maybeHandleGeneratedRouteFileMutation`),
5961
+ // AND vite's own HMR pipeline (which invalidates the gen file's
5962
+ // importers and triggers a second workerd full reload — visible to the
5963
+ // user as a duplicate "[RSCRouter] HMR: version changed" on the client).
5964
+ //
5965
+ // `peekSelfGenWrite` is the authoritative filter: its map only contains
5966
+ // paths that `markSelfGenWrite` has registered, so it natively works
5967
+ // for any configured gen-file name. It is non-consuming so the chokidar
5968
+ // handler that fires later can still consume the same entry. Returning
5969
+ // [] tells vite "no modules invalidated by this change" — safe because
5970
+ // `s.perRouterManifests` is already up-to-date (the write that just
5971
+ // happened is the consequence of our just-completed rediscovery).
5972
+ handleHotUpdate(ctx) {
5973
+ if (peekSelfGenWrite(s, ctx.file)) {
5974
+ debugDiscovery?.(
5975
+ "handleHotUpdate: suppressing self-write HMR cascade for %s",
5976
+ ctx.file
5977
+ );
5978
+ return [];
5979
+ }
5980
+ },
5699
5981
  // Virtual module: provides the pre-generated route manifest as a JS module
5700
5982
  // that calls setCachedManifest() at import time.
5701
5983
  resolveId(id) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rangojs/router",
3
- "version": "0.0.0-experimental.92",
3
+ "version": "0.0.0-experimental.93",
4
4
  "description": "Django-inspired RSC router with composable URL patterns",
5
5
  "keywords": [
6
6
  "react",
@@ -0,0 +1,171 @@
1
+ import type { Debugger } from "../debug.js";
2
+
3
+ /**
4
+ * Manifest-readiness gate + rediscovery scheduler.
5
+ *
6
+ * Owns the four pieces of state that cooperate to keep
7
+ * `s.discoveryDone` (the promise the manifest virtual module's `load()`
8
+ * hook awaits) consistent across HMR fan-out:
9
+ *
10
+ * - **gatePending**: a Promise has been issued and not yet resolved.
11
+ * Workerd's manifest virtual module load() is blocked on it.
12
+ * - **inProgress**: a refresh's work callback is currently executing.
13
+ * - **queued**: a refresh was attempted while one was already in
14
+ * flight; the active run consumes this in its `finally` and
15
+ * recurses.
16
+ * - **pendingEvents**: a route-file event has been received (gate
17
+ * already reset) but the corresponding refresh's work hasn't started
18
+ * yet — i.e. the debounce hasn't fired. Set in `noteRouteEvent`,
19
+ * cleared at the start of each refresh cycle. Refresh's finally MUST
20
+ * hold the gate if this is true even when `queued` is false,
21
+ * otherwise an event whose debounce fires AFTER the active refresh
22
+ * completes (the "tail-race" window) would observe a resolved gate.
23
+ *
24
+ * The HMR-event flow (cloudflare-stress repro):
25
+ *
26
+ * t=0 Touch 1 → noteRouteEvent → pendingEvents=true, beginGate
27
+ * (gate1 pending)
28
+ * → debounce 100ms
29
+ * t=100 runRefreshCycle(work) → clear pendingEvents, work starts
30
+ * t=750 Touch 2 → noteRouteEvent → pendingEvents=true (no-op gate)
31
+ * → debounce fires at t=850
32
+ * t=800 refresh A's finally → queued=false, pendingEvents=true
33
+ * → HOLD gate (don't resolve)
34
+ * t=850 runRefreshCycle (debounce) → clear pendingEvents, work starts
35
+ * t=1500 refresh B's finally → queued=false, pendingEvents=false
36
+ * → resolveGate (gate1 resolves)
37
+ *
38
+ * @internal Exported only for unit tests.
39
+ */
40
+ export interface DiscoveryGate {
41
+ /**
42
+ * Reset the gate to a fresh pending Promise via `s.discoveryDone`.
43
+ * No-op when a gate is already pending — file watchers can fire
44
+ * multiple events for one save, and replacing the resolver would
45
+ * orphan the original promise (workerd's manifest load() would hang).
46
+ */
47
+ beginGate(): void;
48
+ /**
49
+ * Resolve the current pending gate. No-op when no gate is pending.
50
+ * Called at the tail of the last refresh cycle in a burst.
51
+ */
52
+ resolveGate(): void;
53
+ /**
54
+ * Record that a route-file event has arrived. Sets `pendingEvents`
55
+ * and begins the gate. Idempotent for both flags.
56
+ */
57
+ noteRouteEvent(): void;
58
+ /**
59
+ * Run one refresh cycle, managing queue + pending state around it.
60
+ * If a cycle is already in flight, sets `queued=true` and returns.
61
+ * Otherwise clears `pendingEvents`, runs `work`, and in `finally`:
62
+ *
63
+ * - queued → recurse, gate stays pending
64
+ * - pendingEvents → hold gate (next debounced cycle resolves)
65
+ * - neither → resolveGate
66
+ */
67
+ runRefreshCycle(work: () => Promise<void>): Promise<void>;
68
+ /** Snapshot of internal state. Test-only. */
69
+ readonly state: () => Readonly<{
70
+ gatePending: boolean;
71
+ inProgress: boolean;
72
+ queued: boolean;
73
+ pendingEvents: boolean;
74
+ }>;
75
+ }
76
+
77
+ /** State container the gate writes `discoveryDone` into. */
78
+ export interface GateOwner {
79
+ discoveryDone: Promise<void> | null | undefined;
80
+ }
81
+
82
+ export function createDiscoveryGate(
83
+ s: GateOwner,
84
+ debug?: Debugger,
85
+ ): DiscoveryGate {
86
+ let gatePending = false;
87
+ let gateResolver: () => void = () => {};
88
+ let inProgress = false;
89
+ let queued = false;
90
+ let pendingEvents = false;
91
+
92
+ const beginGate = (): void => {
93
+ if (gatePending) return;
94
+ s.discoveryDone = new Promise<void>((resolve) => {
95
+ gateResolver = resolve;
96
+ });
97
+ gatePending = true;
98
+ };
99
+
100
+ const resolveGate = (): void => {
101
+ if (!gatePending) return;
102
+ // Defer resolution while a refresh cycle is in flight or queued, or
103
+ // while an unprocessed route-file event is pending its debounce.
104
+ // Without this guard, cold-start's `discover().then(resolveGate)`
105
+ // could fire while an HMR-triggered runRefreshCycle is mid-flight,
106
+ // prematurely unblocking workerd's manifest load() against the
107
+ // stale cold-start gen. The active cycle's `finally` calls
108
+ // resolveGate again at the tail and finishes the resolution then.
109
+ if (inProgress || queued || pendingEvents) {
110
+ debug?.(
111
+ "hmr: resolveGate deferred — work in flight (inProgress=%s queued=%s pendingEvents=%s)",
112
+ inProgress,
113
+ queued,
114
+ pendingEvents,
115
+ );
116
+ return;
117
+ }
118
+ gatePending = false;
119
+ debug?.("hmr: discoveryDone resolved");
120
+ gateResolver();
121
+ };
122
+
123
+ const noteRouteEvent = (): void => {
124
+ pendingEvents = true;
125
+ beginGate();
126
+ };
127
+
128
+ const runRefreshCycle = async (work: () => Promise<void>): Promise<void> => {
129
+ if (inProgress) {
130
+ queued = true;
131
+ debug?.("hmr: rediscovery in flight — queued for a follow-up cycle");
132
+ return;
133
+ }
134
+ // Snapshot the current pendingEvents into "we're about to process";
135
+ // events arriving from now on re-set it.
136
+ pendingEvents = false;
137
+ inProgress = true;
138
+ try {
139
+ await work();
140
+ } finally {
141
+ inProgress = false;
142
+ if (queued) {
143
+ queued = false;
144
+ debug?.("hmr: consuming queued rediscovery");
145
+ runRefreshCycle(work).catch((err: unknown) => {
146
+ debug?.(
147
+ "hmr: queued cycle rejected — releasing gate (%s)",
148
+ err instanceof Error ? err.message : String(err),
149
+ );
150
+ // Belt-and-suspenders: even if the queued cycle's own try/catch
151
+ // missed something, ensure workerd doesn't hang.
152
+ resolveGate();
153
+ });
154
+ } else if (pendingEvents) {
155
+ debug?.(
156
+ "hmr: holding gate for pending events (debounce not yet fired)",
157
+ );
158
+ } else {
159
+ resolveGate();
160
+ }
161
+ }
162
+ };
163
+
164
+ return {
165
+ beginGate,
166
+ resolveGate,
167
+ noteRouteEvent,
168
+ runRefreshCycle,
169
+ state: () => ({ gatePending, inProgress, queued, pendingEvents }),
170
+ };
171
+ }
@@ -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
- // Prepend imports at the top of the file. ES imports are hoisted
51
- // by the module system, so source position is irrelevant.
52
- const prepend: string[] = [];
53
- let newCode = code;
54
-
55
- if (!code.includes("virtual:rsc-router/routes-manifest")) {
56
- prepend.push(`import "virtual:rsc-router/routes-manifest";`);
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
- if (prepend.length === 0 && newCode === code) return null;
74
-
75
- newCode = prepend.join("\n") + (prepend.length > 0 ? "\n" : "") + newCode;
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 { consumeSelfGenWrite } from "./discovery/self-gen-tracking.js";
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 (prerenderNodeRegistry) {
387
- return (prerenderTempServer.environments as any)?.rsc ?? null;
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.runner.import(s.resolvedEntryPath!);
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?.("dev: no rsc runner (cloudflare path)");
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
- // Store the promise so the virtual module's load hook can await it.
509
- s.discoveryDone = new Promise<void>((resolve) => {
510
- setTimeout(() => discover().then(resolve, resolve), 0);
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
- let runtimeRediscoveryInProgress = false;
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
- if (!rscEnv?.runner || runtimeRediscoveryInProgress) return;
727
- runtimeRediscoveryInProgress = true;
728
- const hmrStart = performance.now();
729
- try {
730
- await timed(debugDiscovery, "hmr discoverRouters", () =>
731
- discoverRouters(s, rscEnv),
732
- );
733
- timedSync(debugDiscovery, "hmr writeRouteTypesFiles", () =>
734
- writeRouteTypesFiles(s),
735
- );
736
- await timed(debugDiscovery, "hmr propagateDiscoveryState", () =>
737
- propagateDiscoveryState(rscEnv),
738
- );
739
- } catch (err: any) {
740
- console.warn(
741
- `[rsc-router] Runtime re-discovery failed: ${err.message}`,
742
- );
743
- } finally {
744
- debugDiscovery?.(
745
- "hmr re-discovery done (%sms)",
746
- (performance.now() - hmrStart).toFixed(1),
747
- );
748
- runtimeRediscoveryInProgress = false;
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
- writeCombinedRouteTypesWithTracking(s);
759
- if (s.perRouterManifests.length > 0) {
760
- supplementGenFilesWithRuntimeRoutes(s);
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) {