@rangojs/router 0.0.0-experimental.7 → 0.0.0-experimental.71

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 (307) hide show
  1. package/AGENTS.md +9 -0
  2. package/README.md +942 -4
  3. package/dist/bin/rango.js +1689 -0
  4. package/dist/vite/index.js +4951 -930
  5. package/package.json +70 -60
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +294 -0
  8. package/skills/caching/SKILL.md +93 -23
  9. package/skills/composability/SKILL.md +172 -0
  10. package/skills/debug-manifest/SKILL.md +12 -8
  11. package/skills/document-cache/SKILL.md +18 -16
  12. package/skills/fonts/SKILL.md +167 -0
  13. package/skills/hooks/SKILL.md +334 -72
  14. package/skills/host-router/SKILL.md +218 -0
  15. package/skills/intercept/SKILL.md +131 -8
  16. package/skills/layout/SKILL.md +100 -3
  17. package/skills/links/SKILL.md +92 -31
  18. package/skills/loader/SKILL.md +404 -44
  19. package/skills/middleware/SKILL.md +173 -34
  20. package/skills/mime-routes/SKILL.md +128 -0
  21. package/skills/parallel/SKILL.md +204 -1
  22. package/skills/prerender/SKILL.md +685 -0
  23. package/skills/rango/SKILL.md +85 -16
  24. package/skills/response-routes/SKILL.md +411 -0
  25. package/skills/route/SKILL.md +257 -14
  26. package/skills/router-setup/SKILL.md +210 -32
  27. package/skills/tailwind/SKILL.md +129 -0
  28. package/skills/theme/SKILL.md +9 -8
  29. package/skills/typesafety/SKILL.md +328 -89
  30. package/skills/use-cache/SKILL.md +324 -0
  31. package/src/__internal.ts +102 -4
  32. package/src/bin/rango.ts +321 -0
  33. package/src/browser/action-coordinator.ts +97 -0
  34. package/src/browser/action-response-classifier.ts +99 -0
  35. package/src/browser/app-version.ts +14 -0
  36. package/src/browser/event-controller.ts +92 -64
  37. package/src/browser/history-state.ts +80 -0
  38. package/src/browser/intercept-utils.ts +52 -0
  39. package/src/browser/link-interceptor.ts +24 -4
  40. package/src/browser/logging.ts +55 -0
  41. package/src/browser/merge-segment-loaders.ts +20 -12
  42. package/src/browser/navigation-bridge.ts +296 -558
  43. package/src/browser/navigation-client.ts +179 -69
  44. package/src/browser/navigation-store.ts +73 -55
  45. package/src/browser/navigation-transaction.ts +297 -0
  46. package/src/browser/network-error-handler.ts +61 -0
  47. package/src/browser/partial-update.ts +328 -313
  48. package/src/browser/prefetch/cache.ts +206 -0
  49. package/src/browser/prefetch/fetch.ts +150 -0
  50. package/src/browser/prefetch/observer.ts +65 -0
  51. package/src/browser/prefetch/policy.ts +48 -0
  52. package/src/browser/prefetch/queue.ts +160 -0
  53. package/src/browser/prefetch/resource-ready.ts +77 -0
  54. package/src/browser/rango-state.ts +112 -0
  55. package/src/browser/react/Link.tsx +230 -74
  56. package/src/browser/react/NavigationProvider.tsx +87 -11
  57. package/src/browser/react/context.ts +11 -0
  58. package/src/browser/react/filter-segment-order.ts +11 -0
  59. package/src/browser/react/index.ts +12 -12
  60. package/src/browser/react/location-state-shared.ts +95 -53
  61. package/src/browser/react/location-state.ts +60 -15
  62. package/src/browser/react/mount-context.ts +6 -1
  63. package/src/browser/react/nonce-context.ts +23 -0
  64. package/src/browser/react/shallow-equal.ts +27 -0
  65. package/src/browser/react/use-action.ts +29 -51
  66. package/src/browser/react/use-client-cache.ts +5 -3
  67. package/src/browser/react/use-handle.ts +30 -126
  68. package/src/browser/react/use-href.tsx +2 -2
  69. package/src/browser/react/use-link-status.ts +6 -5
  70. package/src/browser/react/use-navigation.ts +22 -63
  71. package/src/browser/react/use-params.ts +65 -0
  72. package/src/browser/react/use-pathname.ts +47 -0
  73. package/src/browser/react/use-router.ts +76 -0
  74. package/src/browser/react/use-search-params.ts +56 -0
  75. package/src/browser/react/use-segments.ts +80 -97
  76. package/src/browser/response-adapter.ts +73 -0
  77. package/src/browser/rsc-router.tsx +214 -58
  78. package/src/browser/scroll-restoration.ts +127 -52
  79. package/src/browser/segment-reconciler.ts +221 -0
  80. package/src/browser/segment-structure-assert.ts +16 -0
  81. package/src/browser/server-action-bridge.ts +510 -603
  82. package/src/browser/shallow.ts +6 -1
  83. package/src/browser/types.ts +141 -48
  84. package/src/browser/validate-redirect-origin.ts +29 -0
  85. package/src/build/generate-manifest.ts +235 -24
  86. package/src/build/generate-route-types.ts +39 -0
  87. package/src/build/index.ts +13 -0
  88. package/src/build/route-trie.ts +265 -0
  89. package/src/build/route-types/ast-helpers.ts +25 -0
  90. package/src/build/route-types/ast-route-extraction.ts +98 -0
  91. package/src/build/route-types/codegen.ts +102 -0
  92. package/src/build/route-types/include-resolution.ts +418 -0
  93. package/src/build/route-types/param-extraction.ts +48 -0
  94. package/src/build/route-types/per-module-writer.ts +128 -0
  95. package/src/build/route-types/router-processing.ts +618 -0
  96. package/src/build/route-types/scan-filter.ts +85 -0
  97. package/src/build/runtime-discovery.ts +231 -0
  98. package/src/cache/background-task.ts +34 -0
  99. package/src/cache/cache-key-utils.ts +44 -0
  100. package/src/cache/cache-policy.ts +125 -0
  101. package/src/cache/cache-runtime.ts +342 -0
  102. package/src/cache/cache-scope.ts +167 -309
  103. package/src/cache/cf/cf-cache-store.ts +571 -17
  104. package/src/cache/cf/index.ts +13 -3
  105. package/src/cache/document-cache.ts +116 -77
  106. package/src/cache/handle-capture.ts +81 -0
  107. package/src/cache/handle-snapshot.ts +41 -0
  108. package/src/cache/index.ts +1 -15
  109. package/src/cache/memory-segment-store.ts +191 -13
  110. package/src/cache/profile-registry.ts +73 -0
  111. package/src/cache/read-through-swr.ts +134 -0
  112. package/src/cache/segment-codec.ts +256 -0
  113. package/src/cache/taint.ts +153 -0
  114. package/src/cache/types.ts +72 -122
  115. package/src/client.rsc.tsx +3 -1
  116. package/src/client.tsx +105 -179
  117. package/src/component-utils.ts +4 -4
  118. package/src/components/DefaultDocument.tsx +5 -1
  119. package/src/context-var.ts +156 -0
  120. package/src/debug.ts +19 -9
  121. package/src/errors.ts +108 -2
  122. package/src/handle.ts +55 -29
  123. package/src/handles/MetaTags.tsx +73 -20
  124. package/src/handles/breadcrumbs.ts +66 -0
  125. package/src/handles/index.ts +1 -0
  126. package/src/handles/meta.ts +30 -13
  127. package/src/host/cookie-handler.ts +21 -15
  128. package/src/host/errors.ts +8 -8
  129. package/src/host/index.ts +4 -7
  130. package/src/host/pattern-matcher.ts +27 -27
  131. package/src/host/router.ts +61 -39
  132. package/src/host/testing.ts +8 -8
  133. package/src/host/types.ts +15 -7
  134. package/src/host/utils.ts +1 -1
  135. package/src/href-client.ts +119 -29
  136. package/src/index.rsc.ts +155 -19
  137. package/src/index.ts +223 -30
  138. package/src/internal-debug.ts +11 -0
  139. package/src/loader.rsc.ts +26 -157
  140. package/src/loader.ts +27 -10
  141. package/src/network-error-thrower.tsx +3 -1
  142. package/src/outlet-provider.tsx +45 -0
  143. package/src/prerender/param-hash.ts +37 -0
  144. package/src/prerender/store.ts +186 -0
  145. package/src/prerender.ts +524 -0
  146. package/src/reverse.ts +351 -0
  147. package/src/root-error-boundary.tsx +41 -29
  148. package/src/route-content-wrapper.tsx +7 -4
  149. package/src/route-definition/dsl-helpers.ts +982 -0
  150. package/src/route-definition/helper-factories.ts +200 -0
  151. package/src/route-definition/helpers-types.ts +434 -0
  152. package/src/route-definition/index.ts +55 -0
  153. package/src/route-definition/redirect.ts +101 -0
  154. package/src/route-definition/resolve-handler-use.ts +149 -0
  155. package/src/route-definition.ts +1 -1428
  156. package/src/route-map-builder.ts +217 -123
  157. package/src/route-name.ts +53 -0
  158. package/src/route-types.ts +70 -8
  159. package/src/router/content-negotiation.ts +215 -0
  160. package/src/router/debug-manifest.ts +72 -0
  161. package/src/router/error-handling.ts +9 -9
  162. package/src/router/find-match.ts +160 -0
  163. package/src/router/handler-context.ts +435 -86
  164. package/src/router/intercept-resolution.ts +402 -0
  165. package/src/router/lazy-includes.ts +237 -0
  166. package/src/router/loader-resolution.ts +356 -128
  167. package/src/router/logging.ts +251 -0
  168. package/src/router/manifest.ts +154 -35
  169. package/src/router/match-api.ts +555 -0
  170. package/src/router/match-context.ts +5 -3
  171. package/src/router/match-handlers.ts +440 -0
  172. package/src/router/match-middleware/background-revalidation.ts +108 -93
  173. package/src/router/match-middleware/cache-lookup.ts +459 -10
  174. package/src/router/match-middleware/cache-store.ts +98 -26
  175. package/src/router/match-middleware/intercept-resolution.ts +57 -17
  176. package/src/router/match-middleware/segment-resolution.ts +80 -6
  177. package/src/router/match-pipelines.ts +10 -45
  178. package/src/router/match-result.ts +135 -35
  179. package/src/router/metrics.ts +240 -15
  180. package/src/router/middleware-cookies.ts +55 -0
  181. package/src/router/middleware-types.ts +220 -0
  182. package/src/router/middleware.ts +324 -369
  183. package/src/router/navigation-snapshot.ts +182 -0
  184. package/src/router/pattern-matching.ts +211 -43
  185. package/src/router/prerender-match.ts +502 -0
  186. package/src/router/preview-match.ts +98 -0
  187. package/src/router/request-classification.ts +310 -0
  188. package/src/router/revalidation.ts +137 -38
  189. package/src/router/route-snapshot.ts +245 -0
  190. package/src/router/router-context.ts +41 -21
  191. package/src/router/router-interfaces.ts +484 -0
  192. package/src/router/router-options.ts +618 -0
  193. package/src/router/router-registry.ts +24 -0
  194. package/src/router/segment-resolution/fresh.ts +748 -0
  195. package/src/router/segment-resolution/helpers.ts +268 -0
  196. package/src/router/segment-resolution/loader-cache.ts +199 -0
  197. package/src/router/segment-resolution/revalidation.ts +1379 -0
  198. package/src/router/segment-resolution/static-store.ts +67 -0
  199. package/src/router/segment-resolution.ts +21 -0
  200. package/src/router/segment-wrappers.ts +291 -0
  201. package/src/router/telemetry-otel.ts +299 -0
  202. package/src/router/telemetry.ts +300 -0
  203. package/src/router/timeout.ts +148 -0
  204. package/src/router/trie-matching.ts +239 -0
  205. package/src/router/types.ts +78 -3
  206. package/src/router.ts +740 -4252
  207. package/src/rsc/handler-context.ts +45 -0
  208. package/src/rsc/handler.ts +907 -797
  209. package/src/rsc/helpers.ts +140 -6
  210. package/src/rsc/index.ts +0 -20
  211. package/src/rsc/loader-fetch.ts +229 -0
  212. package/src/rsc/manifest-init.ts +90 -0
  213. package/src/rsc/nonce.ts +14 -0
  214. package/src/rsc/origin-guard.ts +141 -0
  215. package/src/rsc/progressive-enhancement.ts +391 -0
  216. package/src/rsc/response-error.ts +37 -0
  217. package/src/rsc/response-route-handler.ts +347 -0
  218. package/src/rsc/rsc-rendering.ts +246 -0
  219. package/src/rsc/runtime-warnings.ts +42 -0
  220. package/src/rsc/server-action.ts +356 -0
  221. package/src/rsc/ssr-setup.ts +128 -0
  222. package/src/rsc/types.ts +46 -11
  223. package/src/search-params.ts +230 -0
  224. package/src/segment-system.tsx +165 -17
  225. package/src/server/context.ts +315 -58
  226. package/src/server/cookie-store.ts +190 -0
  227. package/src/server/fetchable-loader-store.ts +37 -0
  228. package/src/server/handle-store.ts +113 -15
  229. package/src/server/loader-registry.ts +24 -64
  230. package/src/server/request-context.ts +607 -81
  231. package/src/server.ts +35 -130
  232. package/src/ssr/index.tsx +103 -30
  233. package/src/static-handler.ts +126 -0
  234. package/src/theme/ThemeProvider.tsx +21 -15
  235. package/src/theme/ThemeScript.tsx +5 -5
  236. package/src/theme/constants.ts +5 -2
  237. package/src/theme/index.ts +4 -14
  238. package/src/theme/theme-context.ts +4 -30
  239. package/src/theme/theme-script.ts +21 -18
  240. package/src/types/boundaries.ts +158 -0
  241. package/src/types/cache-types.ts +198 -0
  242. package/src/types/error-types.ts +192 -0
  243. package/src/types/global-namespace.ts +100 -0
  244. package/src/types/handler-context.ts +791 -0
  245. package/src/types/index.ts +88 -0
  246. package/src/types/loader-types.ts +210 -0
  247. package/src/types/route-config.ts +170 -0
  248. package/src/types/route-entry.ts +109 -0
  249. package/src/types/segments.ts +151 -0
  250. package/src/types.ts +1 -1623
  251. package/src/urls/include-helper.ts +197 -0
  252. package/src/urls/index.ts +53 -0
  253. package/src/urls/path-helper-types.ts +346 -0
  254. package/src/urls/path-helper.ts +364 -0
  255. package/src/urls/pattern-types.ts +107 -0
  256. package/src/urls/response-types.ts +116 -0
  257. package/src/urls/type-extraction.ts +372 -0
  258. package/src/urls/urls-function.ts +98 -0
  259. package/src/urls.ts +1 -802
  260. package/src/use-loader.tsx +161 -81
  261. package/src/vite/discovery/bundle-postprocess.ts +181 -0
  262. package/src/vite/discovery/discover-routers.ts +348 -0
  263. package/src/vite/discovery/prerender-collection.ts +439 -0
  264. package/src/vite/discovery/route-types-writer.ts +258 -0
  265. package/src/vite/discovery/self-gen-tracking.ts +47 -0
  266. package/src/vite/discovery/state.ts +117 -0
  267. package/src/vite/discovery/virtual-module-codegen.ts +203 -0
  268. package/src/vite/index.ts +15 -1129
  269. package/src/vite/plugin-types.ts +103 -0
  270. package/src/vite/plugins/cjs-to-esm.ts +93 -0
  271. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  272. package/src/vite/plugins/client-ref-hashing.ts +105 -0
  273. package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +72 -53
  274. package/src/vite/plugins/expose-id-utils.ts +299 -0
  275. package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
  276. package/src/vite/plugins/expose-ids/handler-transform.ts +209 -0
  277. package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
  278. package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
  279. package/src/vite/plugins/expose-ids/types.ts +45 -0
  280. package/src/vite/plugins/expose-internal-ids.ts +786 -0
  281. package/src/vite/plugins/performance-tracks.ts +88 -0
  282. package/src/vite/plugins/refresh-cmd.ts +127 -0
  283. package/src/vite/plugins/use-cache-transform.ts +323 -0
  284. package/src/vite/plugins/version-injector.ts +83 -0
  285. package/src/vite/plugins/version-plugin.ts +266 -0
  286. package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +23 -14
  287. package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
  288. package/src/vite/rango.ts +462 -0
  289. package/src/vite/router-discovery.ts +918 -0
  290. package/src/vite/utils/ast-handler-extract.ts +517 -0
  291. package/src/vite/utils/banner.ts +36 -0
  292. package/src/vite/utils/bundle-analysis.ts +137 -0
  293. package/src/vite/utils/manifest-utils.ts +70 -0
  294. package/src/vite/{package-resolution.ts → utils/package-resolution.ts} +25 -29
  295. package/src/vite/utils/prerender-utils.ts +207 -0
  296. package/src/vite/utils/shared-utils.ts +170 -0
  297. package/CLAUDE.md +0 -43
  298. package/src/browser/lru-cache.ts +0 -69
  299. package/src/browser/request-controller.ts +0 -164
  300. package/src/cache/memory-store.ts +0 -253
  301. package/src/href-context.ts +0 -33
  302. package/src/href.ts +0 -255
  303. package/src/server/route-manifest-cache.ts +0 -173
  304. package/src/vite/expose-handle-id.ts +0 -209
  305. package/src/vite/expose-loader-id.ts +0 -426
  306. package/src/vite/expose-location-state-id.ts +0 -177
  307. /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Prefetch Cache
3
+ *
4
+ * In-memory cache storing prefetched Response objects for instant cache hits
5
+ * on subsequent navigation. Cache key is source-dependent (includes the
6
+ * current page URL) because the server's diff-based response depends on
7
+ * where the user navigates from.
8
+ *
9
+ * Also tracks in-flight prefetch promises. Each promise resolves to the
10
+ * navigation branch of a tee'd Response, allowing navigation to adopt a
11
+ * still-downloading prefetch without reparsing or buffering the body.
12
+ *
13
+ * Replaces the previous browser HTTP cache approach which was unreliable
14
+ * due to response draining race conditions and browser inconsistencies.
15
+ */
16
+
17
+ import { abortAllPrefetches } from "./queue.js";
18
+ import { invalidateRangoState } from "../rango-state.js";
19
+
20
+ // Default TTL: 5 minutes. Overridden by initPrefetchCache() with
21
+ // the server-configured prefetchCacheTTL from router options.
22
+ // 0 disables the in-memory cache entirely.
23
+ let cacheTTL = 300_000;
24
+
25
+ /**
26
+ * Initialize the prefetch cache with the configured TTL.
27
+ * Called once at app startup with the value from server metadata.
28
+ * A TTL of 0 disables the in-memory cache and all prefetching.
29
+ */
30
+ export function initPrefetchCache(ttlMs: number): void {
31
+ cacheTTL = ttlMs;
32
+ }
33
+
34
+ /**
35
+ * Check if the prefetch cache is disabled (TTL <= 0).
36
+ * When disabled, no prefetch requests should be issued.
37
+ */
38
+ export function isPrefetchCacheDisabled(): boolean {
39
+ return cacheTTL <= 0;
40
+ }
41
+ const MAX_PREFETCH_CACHE_SIZE = 50;
42
+
43
+ interface PrefetchCacheEntry {
44
+ response: Response;
45
+ timestamp: number;
46
+ }
47
+
48
+ const cache = new Map<string, PrefetchCacheEntry>();
49
+ const inflight = new Set<string>();
50
+
51
+ /**
52
+ * In-flight promise map. When a prefetch fetch is in progress, its
53
+ * Promise<Response | null> is stored here so navigation can await
54
+ * it instead of starting a duplicate request.
55
+ */
56
+ const inflightPromises = new Map<string, Promise<Response | null>>();
57
+
58
+ // Generation counter incremented on each clearPrefetchCache(). Fetches that
59
+ // started before a clear carry a stale generation and must not store their
60
+ // response (the data may be stale due to a server action invalidation).
61
+ let generation = 0;
62
+
63
+ /**
64
+ * Build a source-dependent cache key.
65
+ * Includes the source page href so the same target prefetched from
66
+ * different pages gets separate entries — the server response varies
67
+ * based on the source page context (diff-based rendering).
68
+ */
69
+ export function buildPrefetchKey(sourceHref: string, targetUrl: URL): string {
70
+ return sourceHref + "\0" + targetUrl.pathname + targetUrl.search;
71
+ }
72
+
73
+ /**
74
+ * Check if a prefetch is already cached, in-flight, or queued for the given key.
75
+ */
76
+ export function hasPrefetch(key: string): boolean {
77
+ if (inflight.has(key)) return true;
78
+ if (cacheTTL <= 0) return false;
79
+ const entry = cache.get(key);
80
+ if (!entry) return false;
81
+ if (Date.now() - entry.timestamp > cacheTTL) {
82
+ cache.delete(key);
83
+ return false;
84
+ }
85
+ return true;
86
+ }
87
+
88
+ /**
89
+ * Consume a cached prefetch response. Returns null if not found or expired.
90
+ * One-time consumption: the entry is deleted after retrieval.
91
+ * Returns null when caching is disabled (TTL <= 0).
92
+ *
93
+ * Does NOT check in-flight prefetches — use consumeInflightPrefetch()
94
+ * for that (returns a Promise instead of a Response).
95
+ */
96
+ export function consumePrefetch(key: string): Response | null {
97
+ if (cacheTTL <= 0) return null;
98
+ const entry = cache.get(key);
99
+ if (!entry) return null;
100
+ if (Date.now() - entry.timestamp > cacheTTL) {
101
+ cache.delete(key);
102
+ return null;
103
+ }
104
+ cache.delete(key);
105
+ return entry.response;
106
+ }
107
+
108
+ /**
109
+ * Consume an in-flight prefetch promise. Returns null if no prefetch is
110
+ * in-flight for this key. The returned Promise resolves to the buffered
111
+ * Response (or null if the fetch failed/was aborted).
112
+ *
113
+ * One-time consumption: the promise entry is removed so a second call
114
+ * returns null. The `inflight` set entry is intentionally kept so that
115
+ * hasPrefetch() continues to return true while the underlying fetch is
116
+ * still downloading — this prevents prefetchDirect() or other callers
117
+ * from starting a duplicate request during the handoff window. The
118
+ * inflight flag is cleaned up naturally by clearPrefetchInflight() in
119
+ * the fetch's .finally().
120
+ */
121
+ export function consumeInflightPrefetch(
122
+ key: string,
123
+ ): Promise<Response | null> | null {
124
+ const promise = inflightPromises.get(key);
125
+ if (!promise) return null;
126
+ // Remove the promise (one-time consumption) but keep the inflight flag.
127
+ inflightPromises.delete(key);
128
+ return promise;
129
+ }
130
+
131
+ /**
132
+ * Store a prefetch response in the in-memory cache.
133
+ * The response should be a clone() of the original so the caller can
134
+ * still consume the body. The clone's body streams independently.
135
+ *
136
+ * Skips storage if the generation has changed since the fetch started
137
+ * (a server action invalidated the cache mid-flight).
138
+ */
139
+ export function storePrefetch(
140
+ key: string,
141
+ response: Response,
142
+ fetchGeneration: number,
143
+ ): void {
144
+ if (cacheTTL <= 0) return;
145
+ if (fetchGeneration !== generation) return;
146
+
147
+ // Evict expired entries
148
+ const now = Date.now();
149
+ for (const [k, entry] of cache) {
150
+ if (now - entry.timestamp > cacheTTL) {
151
+ cache.delete(k);
152
+ }
153
+ }
154
+
155
+ // FIFO eviction if at capacity
156
+ if (cache.size >= MAX_PREFETCH_CACHE_SIZE) {
157
+ const oldest = cache.keys().next().value;
158
+ if (oldest) cache.delete(oldest);
159
+ }
160
+
161
+ cache.set(key, { response, timestamp: now });
162
+ }
163
+
164
+ /**
165
+ * Capture the current generation. The returned value is passed to
166
+ * storePrefetch so it can detect stale completions.
167
+ */
168
+ export function currentGeneration(): number {
169
+ return generation;
170
+ }
171
+
172
+ export function markPrefetchInflight(key: string): void {
173
+ inflight.add(key);
174
+ }
175
+
176
+ /**
177
+ * Store the in-flight Promise for a prefetch so navigation can reuse it.
178
+ */
179
+ export function setInflightPromise(
180
+ key: string,
181
+ promise: Promise<Response | null>,
182
+ ): void {
183
+ inflightPromises.set(key, promise);
184
+ }
185
+
186
+ export function clearPrefetchInflight(key: string): void {
187
+ inflight.delete(key);
188
+ inflightPromises.delete(key);
189
+ }
190
+
191
+ /**
192
+ * Invalidate all prefetch state. Called when server actions mutate data.
193
+ * Clears the in-memory cache, cancels in-flight prefetches, and rotates
194
+ * the Rango state key so CDN-cached responses are also invalidated.
195
+ *
196
+ * Uses abortAllPrefetches (hard cancel) because in-flight responses
197
+ * may contain stale data after a mutation.
198
+ */
199
+ export function clearPrefetchCache(): void {
200
+ generation++;
201
+ inflight.clear();
202
+ inflightPromises.clear();
203
+ cache.clear();
204
+ abortAllPrefetches();
205
+ invalidateRangoState();
206
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Prefetch Fetch
3
+ *
4
+ * Fetch-based prefetch logic used by Link (hover/viewport/render strategies)
5
+ * and useRouter().prefetch(). Sends the same headers and segment IDs as a
6
+ * real navigation so the server returns a proper diff. The Response is fully
7
+ * buffered and stored in an in-memory cache for instant consumption on
8
+ * subsequent navigation.
9
+ *
10
+ * In-flight promises are tracked in the cache so that navigation can reuse
11
+ * a prefetch that is still downloading instead of starting a duplicate request.
12
+ */
13
+
14
+ import {
15
+ buildPrefetchKey,
16
+ hasPrefetch,
17
+ markPrefetchInflight,
18
+ setInflightPromise,
19
+ storePrefetch,
20
+ clearPrefetchInflight,
21
+ currentGeneration,
22
+ } from "./cache.js";
23
+ import { getRangoState } from "../rango-state.js";
24
+ import { enqueuePrefetch } from "./queue.js";
25
+ import { shouldPrefetch } from "./policy.js";
26
+
27
+ /**
28
+ * Build an RSC partial URL for prefetching.
29
+ * Includes _rsc_segments so the server can diff against currently mounted
30
+ * segments, and _rsc_v for version mismatch detection.
31
+ * Returns null for malformed or cross-origin URLs.
32
+ */
33
+ function buildPrefetchUrl(
34
+ url: string,
35
+ segmentIds: string[],
36
+ version?: string,
37
+ routerId?: string,
38
+ ): URL | null {
39
+ let targetUrl: URL;
40
+ try {
41
+ targetUrl = new URL(url, window.location.origin);
42
+ } catch {
43
+ return null;
44
+ }
45
+ if (targetUrl.origin !== window.location.origin) {
46
+ return null;
47
+ }
48
+ targetUrl.searchParams.set("_rsc_partial", "true");
49
+ if (segmentIds.length > 0) {
50
+ targetUrl.searchParams.set("_rsc_segments", segmentIds.join(","));
51
+ }
52
+ if (version) {
53
+ targetUrl.searchParams.set("_rsc_v", version);
54
+ }
55
+ if (routerId) {
56
+ targetUrl.searchParams.set("_rsc_rid", routerId);
57
+ }
58
+ return targetUrl;
59
+ }
60
+
61
+ /**
62
+ * Core prefetch fetch logic. Fetches the response, tees the body, and stores
63
+ * one branch in the in-memory cache. The returned Promise resolves to the
64
+ * sibling navigation branch (or null on failure) so navigation can safely
65
+ * reuse an in-flight prefetch via consumeInflightPrefetch().
66
+ */
67
+ function executePrefetchFetch(
68
+ key: string,
69
+ fetchUrl: string,
70
+ signal?: AbortSignal,
71
+ ): Promise<Response | null> {
72
+ const gen = currentGeneration();
73
+ markPrefetchInflight(key);
74
+
75
+ const promise: Promise<Response | null> = fetch(fetchUrl, {
76
+ priority: "low" as RequestPriority,
77
+ signal,
78
+ headers: {
79
+ "X-Rango-State": getRangoState(),
80
+ "X-RSC-Router-Client-Path": window.location.href,
81
+ "X-Rango-Prefetch": "1",
82
+ },
83
+ })
84
+ .then((response) => {
85
+ if (!response.ok) return null;
86
+ // Don't buffer with arrayBuffer() — that blocks until the entire
87
+ // body downloads, defeating streaming for slow loaders.
88
+ // Tee the body: one branch for navigation, one for cache storage.
89
+ const [navStream, cacheStream] = response.body!.tee();
90
+ const responseInit = {
91
+ headers: response.headers,
92
+ status: response.status,
93
+ statusText: response.statusText,
94
+ };
95
+ storePrefetch(key, new Response(cacheStream, responseInit), gen);
96
+ return new Response(navStream, responseInit);
97
+ })
98
+ .catch(() => null)
99
+ .finally(() => {
100
+ clearPrefetchInflight(key);
101
+ });
102
+
103
+ setInflightPromise(key, promise);
104
+ return promise;
105
+ }
106
+
107
+ /**
108
+ * Prefetch (direct): fetch with low priority and store in in-memory cache.
109
+ * Used by hover strategy -- fires immediately without queueing.
110
+ */
111
+ export function prefetchDirect(
112
+ url: string,
113
+ segmentIds: string[],
114
+ version?: string,
115
+ routerId?: string,
116
+ ): void {
117
+ if (!shouldPrefetch()) return;
118
+
119
+ const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
120
+ if (!targetUrl) return;
121
+ const key = buildPrefetchKey(window.location.href, targetUrl);
122
+ if (hasPrefetch(key)) return;
123
+ executePrefetchFetch(key, targetUrl.toString());
124
+ }
125
+
126
+ /**
127
+ * Prefetch (queued): goes through the concurrency-limited queue.
128
+ * Used by viewport/render strategies to avoid flooding the server.
129
+ * Returns the cache key for use in cleanup.
130
+ */
131
+ export function prefetchQueued(
132
+ url: string,
133
+ segmentIds: string[],
134
+ version?: string,
135
+ routerId?: string,
136
+ ): string {
137
+ if (!shouldPrefetch()) return "";
138
+ const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
139
+ if (!targetUrl) return "";
140
+ const key = buildPrefetchKey(window.location.href, targetUrl);
141
+ if (hasPrefetch(key)) return key;
142
+ const fetchUrlStr = targetUrl.toString();
143
+ enqueuePrefetch(key, (signal) => {
144
+ // Re-check at execution time: a hover-triggered prefetchDirect may
145
+ // have started or completed this key while the item sat in the queue.
146
+ if (hasPrefetch(key)) return Promise.resolve();
147
+ return executePrefetchFetch(key, fetchUrlStr, signal).then(() => {});
148
+ });
149
+ return key;
150
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Prefetch Observer
3
+ *
4
+ * Shared singleton IntersectionObserver for viewport-based prefetching.
5
+ * One observer handles all Link components with prefetch="viewport".
6
+ *
7
+ * Lazy-created on first call to avoid issues in SSR or test environments
8
+ * where IntersectionObserver may not exist.
9
+ *
10
+ * Observation is one-shot: once a link enters the viewport and the callback
11
+ * fires, the element is unobserved. This prevents re-prefetching when a link
12
+ * scrolls in and out repeatedly.
13
+ */
14
+
15
+ type PrefetchCallback = () => void;
16
+
17
+ const callbacks = new Map<Element, PrefetchCallback>();
18
+ let observer: IntersectionObserver | null = null;
19
+
20
+ function getObserver(): IntersectionObserver {
21
+ if (!observer) {
22
+ observer = new IntersectionObserver(
23
+ (entries) => {
24
+ for (const entry of entries) {
25
+ if (entry.isIntersecting) {
26
+ const callback = callbacks.get(entry.target);
27
+ if (callback) {
28
+ observer!.unobserve(entry.target);
29
+ callbacks.delete(entry.target);
30
+ callback();
31
+ }
32
+ }
33
+ }
34
+ },
35
+ { rootMargin: "200px" },
36
+ );
37
+ }
38
+ return observer;
39
+ }
40
+
41
+ /**
42
+ * Observe an element for viewport intersection.
43
+ * When the element becomes visible (within 200px margin), the callback fires
44
+ * and the element is automatically unobserved.
45
+ * No-op in environments without IntersectionObserver (SSR, some test runners).
46
+ */
47
+ export function observeForPrefetch(
48
+ element: Element,
49
+ onVisible: PrefetchCallback,
50
+ ): void {
51
+ if (typeof IntersectionObserver === "undefined") return;
52
+ callbacks.set(element, onVisible);
53
+ getObserver().observe(element);
54
+ }
55
+
56
+ /**
57
+ * Stop observing an element. Used for cleanup when a Link unmounts
58
+ * before entering the viewport.
59
+ */
60
+ export function unobserveForPrefetch(element: Element): void {
61
+ callbacks.delete(element);
62
+ if (observer) {
63
+ observer.unobserve(element);
64
+ }
65
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Prefetch Policy
3
+ *
4
+ * Determines whether speculative prefetching should run for the current user.
5
+ * Honors browser reduced-data preferences when available.
6
+ */
7
+
8
+ import { isPrefetchCacheDisabled } from "./cache.js";
9
+
10
+ type NavigatorWithConnection = Navigator & {
11
+ connection?: {
12
+ saveData?: boolean;
13
+ };
14
+ };
15
+
16
+ /**
17
+ * Evaluate on every call so runtime changes to Save-Data or
18
+ * prefers-reduced-data are respected immediately.
19
+ */
20
+ export function shouldPrefetch(): boolean {
21
+ if (typeof window === "undefined") return false;
22
+
23
+ // When prefetchCacheTTL is false/0, prefetching is fully disabled —
24
+ // no point issuing requests whose responses will be discarded.
25
+ if (isPrefetchCacheDisabled()) return false;
26
+
27
+ const nav =
28
+ typeof navigator !== "undefined"
29
+ ? (navigator as NavigatorWithConnection)
30
+ : undefined;
31
+
32
+ if (nav?.connection?.saveData) return false;
33
+
34
+ if (typeof window.matchMedia === "function") {
35
+ try {
36
+ if (window.matchMedia("(prefers-reduced-data: reduce)").matches) {
37
+ return false;
38
+ }
39
+ } catch {
40
+ // Ignore unsupported query errors and allow prefetch.
41
+ }
42
+ }
43
+
44
+ return true;
45
+ }
46
+
47
+ /** No-op, kept for test compatibility. */
48
+ export function resetPrefetchPolicy(): void {}
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Prefetch Queue
3
+ *
4
+ * Concurrency-limited FIFO queue for speculative prefetches (viewport/render).
5
+ * Hover prefetches bypass this queue — they fire directly for immediate response
6
+ * to user intent.
7
+ *
8
+ * Draining waits for an idle main-thread moment and for viewport images to
9
+ * finish loading, so prefetch fetch() calls never compete with critical
10
+ * resources for the browser's connection pool.
11
+ *
12
+ * When a navigation starts, queued prefetches are cancelled but executing ones
13
+ * are left running. Navigation can reuse their in-flight responses via the
14
+ * prefetch cache's inflight promise map, avoiding duplicate requests.
15
+ */
16
+
17
+ import { wait, waitForIdle, waitForViewportImages } from "./resource-ready.js";
18
+
19
+ const MAX_CONCURRENT = 2;
20
+ const IMAGE_WAIT_TIMEOUT = 2000;
21
+
22
+ let active = 0;
23
+ const queue: Array<{
24
+ key: string;
25
+ execute: (signal: AbortSignal) => Promise<void>;
26
+ }> = [];
27
+ const queued = new Set<string>();
28
+ const executing = new Set<string>();
29
+ const abortControllers = new Map<string, AbortController>();
30
+ let drainScheduled = false;
31
+ let drainGeneration = 0;
32
+
33
+ function startExecution(
34
+ key: string,
35
+ execute: (signal: AbortSignal) => Promise<void>,
36
+ ): void {
37
+ active++;
38
+ executing.add(key);
39
+ const ac = new AbortController();
40
+ abortControllers.set(key, ac);
41
+ execute(ac.signal).finally(() => {
42
+ abortControllers.delete(key);
43
+ // Only decrement if this key wasn't already cleared by cancelAllPrefetches.
44
+ // Without this guard, cancelled tasks' .finally() would underflow active
45
+ // below zero, breaking the MAX_CONCURRENT guarantee.
46
+ if (executing.delete(key)) {
47
+ active--;
48
+ }
49
+ scheduleDrain();
50
+ });
51
+ }
52
+
53
+ /**
54
+ * Schedule a drain after the browser is idle and viewport images are loaded.
55
+ * Coalesces multiple drain requests into a single deferred callback so
56
+ * batch completion doesn't schedule redundant waits.
57
+ *
58
+ * The two-step wait ensures prefetch fetch() calls don't compete with
59
+ * images for the browser's connection pool:
60
+ * 1. waitForIdle — yield until the main thread has a quiet moment
61
+ * 2. waitForViewportImages OR 2s timeout — yield until visible images
62
+ * finish loading, but don't let slow/broken images block indefinitely
63
+ */
64
+ function scheduleDrain(): void {
65
+ if (drainScheduled) return;
66
+ if (active >= MAX_CONCURRENT || queue.length === 0) return;
67
+ drainScheduled = true;
68
+ const gen = drainGeneration;
69
+ waitForIdle()
70
+ .then(() =>
71
+ Promise.race([waitForViewportImages(), wait(IMAGE_WAIT_TIMEOUT)]),
72
+ )
73
+ .then(() => {
74
+ drainScheduled = false;
75
+ // Stale drain: a cancel/abort happened while we were waiting.
76
+ // A fresh scheduleDrain will be called by whatever enqueues next.
77
+ if (gen !== drainGeneration) return;
78
+ if (queue.length > 0) drain();
79
+ });
80
+ }
81
+
82
+ function drain(): void {
83
+ while (active < MAX_CONCURRENT && queue.length > 0) {
84
+ const item = queue.shift()!;
85
+ queued.delete(item.key);
86
+ startExecution(item.key, item.execute);
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Enqueue a prefetch for concurrency-limited execution.
92
+ * Execution is deferred until the browser is idle and viewport images
93
+ * have finished loading, so prefetches never compete with critical
94
+ * resources. Deduplicates by key — items already queued or executing
95
+ * are skipped.
96
+ *
97
+ * The executor receives an AbortSignal that is aborted when
98
+ * cancelAllPrefetches() is called (e.g. on navigation start).
99
+ */
100
+ export function enqueuePrefetch(
101
+ key: string,
102
+ execute: (signal: AbortSignal) => Promise<void>,
103
+ ): void {
104
+ if (queued.has(key) || executing.has(key)) return;
105
+
106
+ queued.add(key);
107
+ queue.push({ key, execute });
108
+ scheduleDrain();
109
+ }
110
+
111
+ /**
112
+ * Cancel queued prefetches and abort in-flight ones that don't match
113
+ * the current navigation target. If `keepUrl` is provided, the
114
+ * executing prefetch whose key contains that URL is kept alive so
115
+ * navigation can reuse its response via consumeInflightPrefetch.
116
+ *
117
+ * Called when a navigation starts via the NavigationProvider's
118
+ * event controller subscription.
119
+ */
120
+ export function cancelAllPrefetches(keepUrl?: string | null): void {
121
+ queue.length = 0;
122
+ queued.clear();
123
+ drainScheduled = false;
124
+ drainGeneration++;
125
+
126
+ // Abort in-flight prefetches that aren't for the navigation target.
127
+ // Keys use format "sourceHref\0targetPathname+search" — match the
128
+ // target portion (after \0) against keepUrl.
129
+ for (const [key, ac] of abortControllers) {
130
+ const target = key.split("\0")[1];
131
+ if (keepUrl && target && keepUrl.startsWith(target)) continue;
132
+ ac.abort();
133
+ abortControllers.delete(key);
134
+ if (executing.delete(key)) {
135
+ active--;
136
+ }
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Hard-cancel everything including in-flight prefetches.
142
+ * Used by clearPrefetchCache (server action invalidation) where
143
+ * in-flight responses would be stale.
144
+ */
145
+ export function abortAllPrefetches(): void {
146
+ for (const ac of abortControllers.values()) {
147
+ ac.abort();
148
+ }
149
+ abortControllers.clear();
150
+
151
+ queue.length = 0;
152
+ queued.clear();
153
+ // Clear executing before resetting active. In-flight .finally() callbacks
154
+ // check executing.delete(key) — if the key is gone, they skip decrementing,
155
+ // so active settles at 0 without underflow.
156
+ executing.clear();
157
+ active = 0;
158
+ drainScheduled = false;
159
+ drainGeneration++;
160
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Resource Readiness
3
+ *
4
+ * Utilities to defer speculative prefetches until critical resources
5
+ * (viewport images) have finished loading. Prevents prefetch fetch()
6
+ * calls from competing with images for the browser's connection pool.
7
+ */
8
+
9
+ /**
10
+ * Resolve when all in-viewport images have finished loading.
11
+ * Returns immediately if no images are pending.
12
+ *
13
+ * Only checks images that exist at call time — does not observe
14
+ * dynamically added images. For SPA navigations where new images
15
+ * appear after render, call this after the navigation settles.
16
+ */
17
+ export function waitForViewportImages(): Promise<void> {
18
+ if (typeof document === "undefined") return Promise.resolve();
19
+
20
+ const pending = Array.from(document.querySelectorAll("img")).filter((img) => {
21
+ if (img.complete) return false;
22
+ const rect = img.getBoundingClientRect();
23
+ return (
24
+ rect.bottom > 0 &&
25
+ rect.right > 0 &&
26
+ rect.top < window.innerHeight &&
27
+ rect.left < window.innerWidth
28
+ );
29
+ });
30
+
31
+ if (pending.length === 0) return Promise.resolve();
32
+
33
+ return new Promise((resolve) => {
34
+ const settled = new Set<HTMLImageElement>();
35
+
36
+ const settle = (img: HTMLImageElement) => {
37
+ if (settled.has(img)) return;
38
+ settled.add(img);
39
+ if (settled.size >= pending.length) resolve();
40
+ };
41
+
42
+ for (const img of pending) {
43
+ img.addEventListener("load", () => settle(img), { once: true });
44
+ img.addEventListener("error", () => settle(img), { once: true });
45
+ // Re-check: image may have completed between the initial filter
46
+ // and listener attachment. settle() is idempotent per image, so
47
+ // a queued load event firing afterward is harmless.
48
+ if (img.complete) settle(img);
49
+ }
50
+ });
51
+ }
52
+
53
+ /**
54
+ * Resolve after the given number of milliseconds.
55
+ */
56
+ export function wait(ms: number): Promise<void> {
57
+ return new Promise((resolve) => setTimeout(resolve, ms));
58
+ }
59
+
60
+ /**
61
+ * Resolve when the browser has an idle main-thread moment.
62
+ * Uses requestIdleCallback where available, falls back to setTimeout.
63
+ *
64
+ * This is a scheduling hint, not an asset-loaded detector — combine
65
+ * with waitForViewportImages() for full resource readiness.
66
+ */
67
+ export function waitForIdle(timeout = 200): Promise<void> {
68
+ if (typeof window !== "undefined" && "requestIdleCallback" in window) {
69
+ return new Promise((resolve) => {
70
+ window.requestIdleCallback(() => resolve(), { timeout });
71
+ });
72
+ }
73
+
74
+ return new Promise((resolve) => {
75
+ setTimeout(resolve, 0);
76
+ });
77
+ }