@rangojs/router 0.0.0-experimental.19 → 0.0.0-experimental.1fa245e2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/{CLAUDE.md → AGENTS.md} +4 -0
  2. package/README.md +122 -30
  3. package/dist/bin/rango.js +245 -63
  4. package/dist/vite/index.js +859 -418
  5. package/package.json +3 -3
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +32 -0
  8. package/skills/caching/SKILL.md +49 -8
  9. package/skills/document-cache/SKILL.md +2 -2
  10. package/skills/hooks/SKILL.md +33 -31
  11. package/skills/host-router/SKILL.md +218 -0
  12. package/skills/links/SKILL.md +3 -1
  13. package/skills/loader/SKILL.md +72 -22
  14. package/skills/middleware/SKILL.md +2 -0
  15. package/skills/parallel/SKILL.md +126 -0
  16. package/skills/prerender/SKILL.md +112 -70
  17. package/skills/rango/SKILL.md +0 -1
  18. package/skills/route/SKILL.md +34 -4
  19. package/skills/router-setup/SKILL.md +95 -5
  20. package/skills/typesafety/SKILL.md +35 -23
  21. package/src/__internal.ts +92 -0
  22. package/src/bin/rango.ts +18 -0
  23. package/src/browser/app-version.ts +14 -0
  24. package/src/browser/event-controller.ts +5 -0
  25. package/src/browser/link-interceptor.ts +4 -0
  26. package/src/browser/navigation-bridge.ts +114 -18
  27. package/src/browser/navigation-client.ts +126 -44
  28. package/src/browser/navigation-store.ts +43 -8
  29. package/src/browser/navigation-transaction.ts +11 -9
  30. package/src/browser/partial-update.ts +80 -15
  31. package/src/browser/prefetch/cache.ts +166 -27
  32. package/src/browser/prefetch/fetch.ts +52 -39
  33. package/src/browser/prefetch/policy.ts +6 -0
  34. package/src/browser/prefetch/queue.ts +92 -20
  35. package/src/browser/prefetch/resource-ready.ts +77 -0
  36. package/src/browser/react/Link.tsx +70 -14
  37. package/src/browser/react/NavigationProvider.tsx +40 -4
  38. package/src/browser/react/context.ts +7 -2
  39. package/src/browser/react/use-handle.ts +9 -58
  40. package/src/browser/react/use-router.ts +21 -8
  41. package/src/browser/rsc-router.tsx +143 -59
  42. package/src/browser/scroll-restoration.ts +41 -42
  43. package/src/browser/segment-reconciler.ts +6 -1
  44. package/src/browser/server-action-bridge.ts +454 -436
  45. package/src/browser/types.ts +60 -5
  46. package/src/build/generate-manifest.ts +6 -6
  47. package/src/build/generate-route-types.ts +5 -0
  48. package/src/build/route-trie.ts +19 -3
  49. package/src/build/route-types/include-resolution.ts +8 -1
  50. package/src/build/route-types/router-processing.ts +346 -87
  51. package/src/build/route-types/scan-filter.ts +8 -1
  52. package/src/cache/cache-runtime.ts +15 -11
  53. package/src/cache/cache-scope.ts +48 -7
  54. package/src/cache/cf/cf-cache-store.ts +453 -11
  55. package/src/cache/cf/index.ts +5 -1
  56. package/src/cache/document-cache.ts +17 -7
  57. package/src/cache/index.ts +1 -0
  58. package/src/cache/taint.ts +55 -0
  59. package/src/client.rsc.tsx +2 -1
  60. package/src/client.tsx +3 -102
  61. package/src/context-var.ts +72 -2
  62. package/src/debug.ts +2 -2
  63. package/src/handle.ts +40 -0
  64. package/src/handles/breadcrumbs.ts +66 -0
  65. package/src/handles/index.ts +1 -0
  66. package/src/host/index.ts +0 -3
  67. package/src/index.rsc.ts +8 -37
  68. package/src/index.ts +40 -66
  69. package/src/prerender/store.ts +57 -15
  70. package/src/prerender.ts +138 -77
  71. package/src/reverse.ts +22 -1
  72. package/src/route-definition/dsl-helpers.ts +73 -25
  73. package/src/route-definition/helpers-types.ts +10 -6
  74. package/src/route-definition/index.ts +3 -3
  75. package/src/route-definition/redirect.ts +11 -3
  76. package/src/route-definition/resolve-handler-use.ts +149 -0
  77. package/src/route-map-builder.ts +7 -1
  78. package/src/route-types.ts +11 -0
  79. package/src/router/content-negotiation.ts +100 -1
  80. package/src/router/find-match.ts +4 -2
  81. package/src/router/handler-context.ts +108 -25
  82. package/src/router/intercept-resolution.ts +11 -4
  83. package/src/router/lazy-includes.ts +4 -1
  84. package/src/router/loader-resolution.ts +123 -11
  85. package/src/router/logging.ts +5 -2
  86. package/src/router/manifest.ts +9 -3
  87. package/src/router/match-api.ts +125 -190
  88. package/src/router/match-middleware/background-revalidation.ts +30 -2
  89. package/src/router/match-middleware/cache-lookup.ts +88 -16
  90. package/src/router/match-middleware/cache-store.ts +53 -10
  91. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  92. package/src/router/match-middleware/segment-resolution.ts +61 -5
  93. package/src/router/match-result.ts +22 -15
  94. package/src/router/metrics.ts +238 -13
  95. package/src/router/middleware-types.ts +53 -12
  96. package/src/router/middleware.ts +172 -85
  97. package/src/router/navigation-snapshot.ts +182 -0
  98. package/src/router/pattern-matching.ts +20 -5
  99. package/src/router/prerender-match.ts +114 -10
  100. package/src/router/preview-match.ts +30 -102
  101. package/src/router/request-classification.ts +310 -0
  102. package/src/router/revalidation.ts +27 -7
  103. package/src/router/route-snapshot.ts +245 -0
  104. package/src/router/router-context.ts +6 -1
  105. package/src/router/router-interfaces.ts +50 -5
  106. package/src/router/router-options.ts +50 -19
  107. package/src/router/segment-resolution/fresh.ts +200 -19
  108. package/src/router/segment-resolution/helpers.ts +30 -25
  109. package/src/router/segment-resolution/loader-cache.ts +1 -0
  110. package/src/router/segment-resolution/revalidation.ts +429 -301
  111. package/src/router/segment-wrappers.ts +2 -0
  112. package/src/router/trie-matching.ts +20 -2
  113. package/src/router/types.ts +1 -0
  114. package/src/router.ts +88 -15
  115. package/src/rsc/handler.ts +546 -359
  116. package/src/rsc/index.ts +0 -20
  117. package/src/rsc/manifest-init.ts +5 -1
  118. package/src/rsc/progressive-enhancement.ts +25 -8
  119. package/src/rsc/rsc-rendering.ts +35 -43
  120. package/src/rsc/server-action.ts +16 -10
  121. package/src/rsc/ssr-setup.ts +128 -0
  122. package/src/rsc/types.ts +10 -1
  123. package/src/search-params.ts +16 -13
  124. package/src/segment-system.tsx +140 -4
  125. package/src/server/context.ts +148 -16
  126. package/src/server/loader-registry.ts +9 -8
  127. package/src/server/request-context.ts +182 -34
  128. package/src/server.ts +6 -0
  129. package/src/ssr/index.tsx +4 -0
  130. package/src/static-handler.ts +18 -6
  131. package/src/theme/index.ts +4 -13
  132. package/src/types/cache-types.ts +4 -4
  133. package/src/types/handler-context.ts +149 -49
  134. package/src/types/loader-types.ts +36 -9
  135. package/src/types/route-config.ts +17 -8
  136. package/src/types/route-entry.ts +8 -1
  137. package/src/types/segments.ts +2 -5
  138. package/src/urls/path-helper-types.ts +9 -2
  139. package/src/urls/path-helper.ts +48 -13
  140. package/src/urls/pattern-types.ts +12 -0
  141. package/src/urls/response-types.ts +16 -6
  142. package/src/use-loader.tsx +73 -4
  143. package/src/vite/discovery/bundle-postprocess.ts +61 -89
  144. package/src/vite/discovery/discover-routers.ts +23 -5
  145. package/src/vite/discovery/prerender-collection.ts +48 -15
  146. package/src/vite/discovery/state.ts +17 -13
  147. package/src/vite/index.ts +8 -3
  148. package/src/vite/plugin-types.ts +51 -79
  149. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  150. package/src/vite/plugins/expose-action-id.ts +1 -3
  151. package/src/vite/plugins/performance-tracks.ts +88 -0
  152. package/src/vite/plugins/refresh-cmd.ts +127 -0
  153. package/src/vite/plugins/version-plugin.ts +13 -1
  154. package/src/vite/rango.ts +174 -211
  155. package/src/vite/router-discovery.ts +169 -42
  156. package/src/vite/utils/banner.ts +3 -3
  157. package/src/vite/utils/prerender-utils.ts +78 -0
  158. package/src/vite/utils/shared-utils.ts +3 -2
  159. package/skills/testing/SKILL.md +0 -226
  160. package/src/route-definition/route-function.ts +0 -119
@@ -1,38 +1,53 @@
1
+ // -- Build-time environment types -------------------------------------------
2
+
1
3
  /**
2
- * RSC plugin entry points configuration.
3
- * All entries use virtual modules by default. Specify a path to use a custom entry file.
4
+ * Context passed to a buildEnv factory function.
5
+ * Provides Vite config details for conditional env setup.
4
6
  */
5
- export interface RscEntries {
6
- /**
7
- * Path to a custom browser/client entry file.
8
- * If not specified, a default virtual entry is used.
9
- */
10
- client?: string;
7
+ export interface BuildEnvFactoryContext {
8
+ /** Vite project root directory. */
9
+ root: string;
10
+ /** Vite mode (e.g. "development", "production"). */
11
+ mode: string;
12
+ /** Vite command ("serve" for dev, "build" for production). */
13
+ command: "serve" | "build";
14
+ /** Router deployment preset. */
15
+ preset: "node" | "cloudflare";
16
+ }
11
17
 
12
- /**
13
- * Path to a custom SSR entry file.
14
- * If not specified, a default virtual entry is used.
15
- */
16
- ssr?: string;
18
+ /**
19
+ * Factory function that creates build-time environment bindings.
20
+ * Called once at plugin startup. Return `dispose` to clean up resources.
21
+ */
22
+ export type BuildEnvFactory = (
23
+ ctx: BuildEnvFactoryContext,
24
+ ) => Promise<BuildEnvResult> | BuildEnvResult;
17
25
 
18
- /**
19
- * Path to a custom RSC entry file.
20
- * If not specified, a default virtual entry is used that imports the router from the `entry` option.
21
- */
22
- rsc?: string;
26
+ /**
27
+ * Result of resolving build-time environment bindings.
28
+ */
29
+ export interface BuildEnvResult {
30
+ /** Environment bindings available to Prerender/Static handlers via ctx.env. */
31
+ env: Record<string, unknown>;
32
+ /** Called after build completes to clean up resources (e.g., miniflare). */
33
+ dispose?: () => Promise<void> | void;
23
34
  }
24
35
 
25
36
  /**
26
- * Options for @vitejs/plugin-rsc integration
37
+ * Build-time environment configuration for Prerender and Static handlers.
38
+ *
39
+ * - `false` (default): no build-time env, `ctx.env` throws.
40
+ * - `"auto"`: calls `wrangler.getPlatformProxy()` (cloudflare preset only).
41
+ * - Object: used directly as `ctx.env` during build.
42
+ * - Factory: called once at startup, must return `{ env, dispose? }`.
27
43
  */
28
- export interface RscPluginOptions {
29
- /**
30
- * Entry points for client, ssr, and rsc environments.
31
- * All entries use virtual modules by default.
32
- * Specify paths only when you need custom entry files.
33
- */
34
- entries?: RscEntries;
35
- }
44
+ export type BuildEnvOption =
45
+ | false
46
+ | "auto"
47
+ | Record<string, unknown>
48
+ | BuildEnvFactory;
49
+
50
+ // -- Plugin options ---------------------------------------------------------
36
51
 
37
52
  /**
38
53
  * Base options shared by all presets
@@ -45,27 +60,16 @@ interface RangoBaseOptions {
45
60
  banner?: boolean;
46
61
 
47
62
  /**
48
- * Generate named-routes.gen.ts by parsing url modules at startup.
49
- * Provides type-safe Handler<"name"> and href() without executing router code.
50
- * Set to `false` to disable (run `npx rango extract-names` manually instead).
51
- * @default true
52
- */
53
- staticRouteTypesGeneration?: boolean;
54
-
55
- /**
56
- * Glob patterns for files to include in route type scanning.
57
- * Only files matching at least one pattern will be scanned.
58
- * Patterns are relative to the project root.
59
- * When unset, all .ts/.tsx files are scanned.
60
- */
61
- include?: string[];
62
-
63
- /**
64
- * Glob patterns for files to exclude from route type scanning.
65
- * Takes precedence over `include`. Patterns are relative to the project root.
66
- * Defaults to common test/build directories.
63
+ * Environment bindings available to Prerender and Static handlers at build
64
+ * time via `ctx.env`. Applies to both production build and dev on-demand
65
+ * prerender (`/__rsc_prerender`).
66
+ *
67
+ * This is the build-time env supplied by the Vite plugin, not the live
68
+ * request env. It is shared across all prerender invocations for the build.
69
+ *
70
+ * @default false
67
71
  */
68
- exclude?: string[];
72
+ buildEnv?: BuildEnvOption;
69
73
  }
70
74
 
71
75
  /**
@@ -76,38 +80,6 @@ export interface RangoNodeOptions extends RangoBaseOptions {
76
80
  * Deployment preset. Defaults to 'node' when not specified.
77
81
  */
78
82
  preset?: "node";
79
-
80
- /**
81
- * Path to your router configuration file that exports the route tree.
82
- * This file must export a `router` object created with `createRouter()`.
83
- *
84
- * When omitted, auto-discovers the router by scanning for files containing
85
- * `createRouter`. If exactly one is found, it is used automatically.
86
- * If multiple are found, an error is thrown with the list of candidates.
87
- *
88
- * @example
89
- * ```ts
90
- * rango({ router: './src/router.tsx' })
91
- * // or simply:
92
- * rango()
93
- * ```
94
- */
95
- router?: string;
96
-
97
- /**
98
- * RSC plugin configuration. By default, rsc-router includes @vitejs/plugin-rsc
99
- * with sensible defaults.
100
- *
101
- * Entry files (browser, ssr, rsc) are optional - if they don't exist,
102
- * virtual defaults are used.
103
- *
104
- * - Omit or pass `true`/`{}` to use defaults (recommended)
105
- * - Pass `{ entries: {...} }` to customize entry paths
106
- * - Pass `false` to disable (for manual @vitejs/plugin-rsc configuration)
107
- *
108
- * @default true
109
- */
110
- rsc?: boolean | RscPluginOptions;
111
83
  }
112
84
 
113
85
  /**
@@ -0,0 +1,115 @@
1
+ import type { Plugin, ResolvedConfig } from "vite";
2
+
3
+ const CLIENT_IN_SERVER_PROXY_PREFIX =
4
+ "virtual:vite-rsc/client-in-server-package-proxy/";
5
+
6
+ /**
7
+ * Extract the bare package name from an absolute node_modules path.
8
+ * Handles scoped packages (@org/name) and nested node_modules.
9
+ * Returns null if the path doesn't contain a valid package reference.
10
+ *
11
+ * NOTE: This is a lossy transformation. It maps a specific submodule path
12
+ * (e.g., pkg/internal/context.js) to the package root (pkg). The load()
13
+ * hook then re-exports via the bare specifier, which resolves to the
14
+ * package entry point. This works for packages that barrel-export their
15
+ * "use client" symbols from the root, which covers the common case
16
+ * (component libraries like @mantine/core, @chakra-ui/react, etc.).
17
+ * Packages whose client symbols are only available from deep subpaths
18
+ * (not re-exported from the root) would lose those symbols after the
19
+ * rewrite. A more precise approach would resolve through the package's
20
+ * exports map to find the correct entry point, but that adds significant
21
+ * complexity for a rare edge case.
22
+ * See: https://github.com/cloudflare/vinext/pull/413
23
+ */
24
+ export function extractPackageName(absolutePath: string): string | null {
25
+ // Find the last /node_modules/ segment (handles nested node_modules)
26
+ const marker = "/node_modules/";
27
+ const idx = absolutePath.lastIndexOf(marker);
28
+ if (idx === -1) return null;
29
+
30
+ const afterModules = absolutePath.slice(idx + marker.length);
31
+
32
+ if (afterModules.startsWith("@")) {
33
+ // Scoped package: @org/name
34
+ const parts = afterModules.split("/");
35
+ if (parts.length < 2 || !parts[1]) return null;
36
+ return `${parts[0]}/${parts[1]}`;
37
+ }
38
+
39
+ // Unscoped package: name
40
+ const name = afterModules.split("/")[0];
41
+ return name || null;
42
+ }
43
+
44
+ /**
45
+ * Vite plugin that deduplicates client references from third-party packages
46
+ * in dev mode.
47
+ *
48
+ * When @vitejs/plugin-rsc encounters a "use client" submodule inside a
49
+ * package imported from a server component, it creates a
50
+ * client-in-server-package-proxy virtual module that re-exports from the
51
+ * absolute file path. In the client environment, this absolute path bypasses
52
+ * Vite's pre-bundling, while direct client imports of the same package go
53
+ * through .vite/deps/. Two separate module instances are created, breaking
54
+ * React contexts (createContext runs twice, provider/consumer mismatch).
55
+ *
56
+ * This plugin intercepts absolute node_modules imports from proxy modules
57
+ * in the client environment and rewrites them to bare specifier imports
58
+ * that go through pre-bundling, ensuring a single module instance.
59
+ *
60
+ * Dev-only: production builds use the SSR manifest which handles module
61
+ * identity correctly.
62
+ */
63
+ export function clientRefDedup(): Plugin {
64
+ let clientExclude: string[] = [];
65
+
66
+ return {
67
+ name: "@rangojs/router:client-ref-dedup",
68
+ enforce: "pre",
69
+ apply: "serve",
70
+
71
+ configResolved(config: ResolvedConfig) {
72
+ // Respect user's optimizeDeps.exclude — if a package is explicitly
73
+ // excluded from pre-bundling, we shouldn't redirect it there.
74
+ const clientEnv = config.environments?.["client"];
75
+ clientExclude =
76
+ clientEnv?.optimizeDeps?.exclude ?? config.optimizeDeps?.exclude ?? [];
77
+ },
78
+
79
+ resolveId(source, importer, options) {
80
+ // Only intercept in the client environment
81
+ if (this.environment?.name !== "client") return;
82
+
83
+ // Only handle imports from client-in-server-package-proxy virtual modules
84
+ if (!importer?.includes(CLIENT_IN_SERVER_PROXY_PREFIX)) return;
85
+
86
+ // Only handle absolute node_modules paths
87
+ if (!source.includes("/node_modules/")) return;
88
+
89
+ // Must have an importer
90
+ if (!importer) return;
91
+
92
+ const packageName = extractPackageName(source);
93
+ if (!packageName) return;
94
+
95
+ // Don't redirect packages that are excluded from optimization
96
+ if (clientExclude.includes(packageName)) return;
97
+
98
+ // Return a virtual module that re-exports via bare specifier
99
+ return `\0rango:dedup/${packageName}`;
100
+ },
101
+
102
+ load(id) {
103
+ if (!id.startsWith("\0rango:dedup/")) return;
104
+
105
+ const packageName = id.slice("\0rango:dedup/".length);
106
+
107
+ // Re-export via bare specifier so Vite routes through pre-bundling
108
+ return [
109
+ `export * from ${JSON.stringify(packageName)};`,
110
+ `import * as __all__ from ${JSON.stringify(packageName)};`,
111
+ `export default __all__.default;`,
112
+ ].join("\n");
113
+ },
114
+ };
115
+ }
@@ -278,9 +278,7 @@ export function exposeActionId(): Plugin {
278
278
  if (!rscPluginApi) {
279
279
  throw new Error(
280
280
  "[rsc-router] Could not find @vitejs/plugin-rsc. " +
281
- "@rangojs/router requires the Vite RSC plugin.\n" +
282
- "The RSC plugin should be included automatically. If you disabled it with\n" +
283
- "rango({ rsc: false }), add rsc() before rango() in your config.",
281
+ "@rangojs/router requires the Vite RSC plugin, which is included automatically by rango().",
284
282
  );
285
283
  }
286
284
 
@@ -0,0 +1,88 @@
1
+ /**
2
+ * React Performance Tracks — RSDW client patch
3
+ *
4
+ * Patches the RSDW client so _debugInfo recovery works for plain-object
5
+ * payloads (our RscPayload shape). Without this, the Server Components
6
+ * track in Chrome DevTools stays empty.
7
+ *
8
+ * React's flushComponentPerformance uses splice(0) to empty _debugInfo
9
+ * after resolution, then recovers it from the resolved value — but only
10
+ * for arrays, async iterables, React elements, and lazy types. Since our
11
+ * RscPayload is a plain object, _debugInfo is lost. This patch relaxes
12
+ * the check so _debugInfo is recovered from any object.
13
+ */
14
+
15
+ import type { Plugin } from "vite";
16
+ import { readFile } from "node:fs/promises";
17
+
18
+ const RSDW_PATCH_RE =
19
+ /((?:var|let|const)\s+\w+\s*=\s*root\._children\s*,\s*(\w+)\s*=\s*root\._debugInfo\s*[;,])/;
20
+
21
+ function buildPatchReplacement(match: string, debugInfoVar: string): string {
22
+ return `${match}
23
+ if (${debugInfoVar} && 0 === ${debugInfoVar}.length && "fulfilled" === root.status) {
24
+ var _resolved = "function" === typeof resolveLazy ? resolveLazy(root.value) : root.value;
25
+ if ("object" === typeof _resolved && null !== _resolved && isArrayImpl(_resolved._debugInfo)) {
26
+ ${debugInfoVar} = _resolved._debugInfo;
27
+ }
28
+ }`;
29
+ }
30
+
31
+ export function patchRsdwClientDebugInfoRecovery(code: string): {
32
+ code: string;
33
+ debugInfoVar: string | null;
34
+ } {
35
+ const match = code.match(RSDW_PATCH_RE);
36
+ if (!match) {
37
+ return { code, debugInfoVar: null };
38
+ }
39
+
40
+ return {
41
+ code: code.replace(match[1]!, buildPatchReplacement(match[1]!, match[2]!)),
42
+ debugInfoVar: match[2]!,
43
+ };
44
+ }
45
+
46
+ export function performanceTracksOptimizeDepsPlugin(): {
47
+ name: string;
48
+ setup(build: any): void;
49
+ } {
50
+ return {
51
+ name: "@rangojs/router:performance-tracks-optimize-deps",
52
+ setup(build: any): void {
53
+ build.onLoad(
54
+ {
55
+ filter:
56
+ /react-server-dom-webpack-client\.browser\.(development|production)\.js$/,
57
+ },
58
+ async (args: { path: string }) => {
59
+ const code = await readFile(args.path, "utf8");
60
+ const patched = patchRsdwClientDebugInfoRecovery(code);
61
+ return {
62
+ contents: patched.code,
63
+ loader: "js",
64
+ };
65
+ },
66
+ );
67
+ },
68
+ };
69
+ }
70
+
71
+ export function performanceTracksPlugin(): Plugin {
72
+ return {
73
+ name: "@rangojs/router:performance-tracks",
74
+
75
+ transform(code, id) {
76
+ if (!id.includes("react-server-dom") || !id.includes("client")) return;
77
+ const patched = patchRsdwClientDebugInfoRecovery(code);
78
+ if (!patched.debugInfoVar) return;
79
+ if (process.env.INTERNAL_RANGO_DEBUG)
80
+ console.log(
81
+ "[perf-tracks] patched RSDW client (var:",
82
+ patched.debugInfoVar,
83
+ ")",
84
+ );
85
+ return patched.code;
86
+ },
87
+ };
88
+ }
@@ -0,0 +1,127 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ /**
4
+ * Vite plugin that triggers a full browser reload from terminal input.
5
+ *
6
+ * This plugin is intentionally passive:
7
+ * - it never enables raw mode on stdin
8
+ * - it never restores terminal state
9
+ * - it reacts to Ctrl+R when that raw byte reaches the process
10
+ * - it also supports safe line-based fallbacks like "e" + Enter
11
+ *
12
+ * Usage:
13
+ * ```ts
14
+ * import { poke } from "@rangojs/router/vite";
15
+ *
16
+ * export default defineConfig({
17
+ * plugins: [rango(), poke()],
18
+ * });
19
+ * ```
20
+ */
21
+ export function poke(): Plugin {
22
+ return {
23
+ name: "vite-plugin-poke",
24
+ apply: "serve",
25
+
26
+ configureServer(server) {
27
+ const stdin = process.stdin;
28
+ const debug = process.env.RANGO_POKE_DEBUG === "1";
29
+
30
+ const triggerReload = (source: string) => {
31
+ server.hot.send({ type: "full-reload", path: "*" });
32
+ server.config.logger.info(` browser reload (${source})`, {
33
+ timestamp: true,
34
+ });
35
+ };
36
+
37
+ const toBuffer = (chunk: string | Buffer): Buffer => {
38
+ return typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk;
39
+ };
40
+
41
+ const formatChunk = (chunk: string | Buffer): string => {
42
+ const data = toBuffer(chunk);
43
+ const hex = Array.from(data)
44
+ .map((byte) => `0x${byte.toString(16).padStart(2, "0")}`)
45
+ .join(" ");
46
+ const ascii = Array.from(data)
47
+ .map((byte) => {
48
+ if (byte >= 0x20 && byte <= 0x7e) return String.fromCharCode(byte);
49
+ if (byte === 0x0a) return "\\n";
50
+ if (byte === 0x0d) return "\\r";
51
+ if (byte === 0x09) return "\\t";
52
+ return ".";
53
+ })
54
+ .join("");
55
+ return `len=${data.length} hex=[${hex}] ascii="${ascii}"`;
56
+ };
57
+
58
+ const readCtrlR = (chunk: string | Buffer): boolean => {
59
+ const data =
60
+ typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk;
61
+ return data.length === 1 && data[0] === 0x12;
62
+ };
63
+
64
+ const readSubmittedCommands = (chunk: string | Buffer): string[] => {
65
+ const text = toBuffer(chunk)
66
+ .toString("utf8")
67
+ .replace(/\r\n/g, "\n")
68
+ .replace(/\r/g, "\n");
69
+
70
+ if (!text.includes("\n")) return [];
71
+
72
+ const lines = text.split("\n");
73
+ lines.pop();
74
+ return lines;
75
+ };
76
+
77
+ if (debug) {
78
+ server.config.logger.info(
79
+ ` poke debug enabled (isTTY=${stdin.isTTY ? "yes" : "no"}, isRaw=${stdin.isTTY ? (stdin.isRaw ? "yes" : "no") : "n/a"})`,
80
+ { timestamp: true },
81
+ );
82
+ }
83
+
84
+ if (stdin.isTTY) {
85
+ server.config.logger.info(
86
+ " poke ready: press e + enter to reload browser (ctrl+r also works when available)",
87
+ { timestamp: true },
88
+ );
89
+ }
90
+
91
+ const onData = (data: string | Buffer) => {
92
+ if (debug) {
93
+ server.config.logger.info(` poke stdin ${formatChunk(data)}`, {
94
+ timestamp: true,
95
+ });
96
+ }
97
+
98
+ // Only react to the exact Ctrl+R byte when some host terminal or
99
+ // wrapper already delivers it to this process. We intentionally do
100
+ // not enable raw mode here because that can steal Vite shortcuts
101
+ // like "r" / "q" and interfere with terminal-level controls.
102
+ if (readCtrlR(data)) {
103
+ triggerReload("ctrl+r");
104
+ return;
105
+ }
106
+
107
+ for (const command of readSubmittedCommands(data)) {
108
+ if (command === "e") {
109
+ triggerReload("e+enter");
110
+ return;
111
+ }
112
+
113
+ if (command === "\u001br") {
114
+ triggerReload("option+r+enter");
115
+ return;
116
+ }
117
+ }
118
+ };
119
+
120
+ stdin.on("data", onData);
121
+
122
+ server.httpServer?.on("close", () => {
123
+ stdin.off("data", onData);
124
+ });
125
+ },
126
+ };
127
+ }
@@ -135,8 +135,11 @@ export function createVersionPlugin(): Plugin {
135
135
  let server: any = null;
136
136
  const clientModuleSignatures = new Map<string, ClientModuleSignature>();
137
137
 
138
+ let versionCounter = 0;
138
139
  const bumpVersion = (reason: string) => {
139
- currentVersion = Date.now().toString(16);
140
+ // Use timestamp + counter to guarantee uniqueness even when multiple
141
+ // bumps happen within the same millisecond (e.g. cascading HMR events).
142
+ currentVersion = Date.now().toString(16) + String(++versionCounter);
140
143
  console.log(`[rsc-router] ${reason}, version updated: ${currentVersion}`);
141
144
 
142
145
  const rscEnv = server?.environments?.rsc;
@@ -211,6 +214,15 @@ export function createVersionPlugin(): Plugin {
211
214
 
212
215
  if (!isRscModule) return;
213
216
 
217
+ // Skip re-bumping when the version virtual module itself is invalidated
218
+ // (our own bumpVersion() invalidates it, which re-triggers hotUpdate).
219
+ if (
220
+ ctx.modules.length === 1 &&
221
+ ctx.modules[0].id === "\0" + VIRTUAL_IDS.version
222
+ ) {
223
+ return;
224
+ }
225
+
214
226
  if (isCodeModule(ctx.file)) {
215
227
  const filePath = normalizeModuleId(ctx.file);
216
228
  const previousSignature = clientModuleSignatures.get(filePath);