@rangojs/router 0.0.0-experimental.b02a2fec → 0.0.0-experimental.bf1b128c

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 (103) hide show
  1. package/README.md +50 -20
  2. package/dist/vite/index.js +1338 -462
  3. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  4. package/package.json +7 -5
  5. package/skills/breadcrumbs/SKILL.md +3 -1
  6. package/skills/handler-use/SKILL.md +362 -0
  7. package/skills/hooks/SKILL.md +28 -20
  8. package/skills/intercept/SKILL.md +20 -0
  9. package/skills/layout/SKILL.md +22 -0
  10. package/skills/links/SKILL.md +88 -16
  11. package/skills/loader/SKILL.md +66 -2
  12. package/skills/middleware/SKILL.md +32 -3
  13. package/skills/migrate-nextjs/SKILL.md +560 -0
  14. package/skills/migrate-react-router/SKILL.md +765 -0
  15. package/skills/parallel/SKILL.md +66 -0
  16. package/skills/rango/SKILL.md +24 -22
  17. package/skills/response-routes/SKILL.md +8 -0
  18. package/skills/route/SKILL.md +24 -0
  19. package/skills/streams-and-websockets/SKILL.md +283 -0
  20. package/skills/typesafety/SKILL.md +3 -1
  21. package/src/browser/app-shell.ts +52 -0
  22. package/src/browser/navigation-bridge.ts +71 -5
  23. package/src/browser/navigation-client.ts +64 -13
  24. package/src/browser/navigation-store.ts +25 -1
  25. package/src/browser/partial-update.ts +34 -3
  26. package/src/browser/prefetch/cache.ts +129 -21
  27. package/src/browser/prefetch/fetch.ts +148 -16
  28. package/src/browser/prefetch/queue.ts +36 -5
  29. package/src/browser/rango-state.ts +53 -13
  30. package/src/browser/react/Link.tsx +30 -2
  31. package/src/browser/react/NavigationProvider.tsx +50 -11
  32. package/src/browser/react/use-navigation.ts +22 -2
  33. package/src/browser/react/use-params.ts +11 -1
  34. package/src/browser/react/use-router.ts +8 -1
  35. package/src/browser/rsc-router.tsx +34 -6
  36. package/src/browser/segment-reconciler.ts +36 -14
  37. package/src/browser/types.ts +13 -0
  38. package/src/build/route-trie.ts +50 -24
  39. package/src/cache/cf/cf-cache-store.ts +5 -7
  40. package/src/client.tsx +82 -174
  41. package/src/index.rsc.ts +3 -0
  42. package/src/index.ts +40 -9
  43. package/src/outlet-context.ts +1 -1
  44. package/src/response-utils.ts +28 -0
  45. package/src/reverse.ts +7 -3
  46. package/src/route-definition/dsl-helpers.ts +175 -23
  47. package/src/route-definition/helpers-types.ts +63 -14
  48. package/src/route-definition/resolve-handler-use.ts +6 -0
  49. package/src/route-types.ts +7 -0
  50. package/src/router/handler-context.ts +24 -4
  51. package/src/router/lazy-includes.ts +6 -6
  52. package/src/router/loader-resolution.ts +3 -0
  53. package/src/router/manifest.ts +22 -13
  54. package/src/router/match-api.ts +3 -3
  55. package/src/router/middleware-types.ts +2 -22
  56. package/src/router/middleware.ts +54 -7
  57. package/src/router/pattern-matching.ts +60 -9
  58. package/src/router/revalidation.ts +15 -1
  59. package/src/router/segment-resolution/revalidation.ts +63 -58
  60. package/src/router/trie-matching.ts +10 -4
  61. package/src/router/url-params.ts +49 -0
  62. package/src/router.ts +1 -2
  63. package/src/rsc/handler.ts +8 -4
  64. package/src/rsc/helpers.ts +69 -41
  65. package/src/rsc/progressive-enhancement.ts +2 -0
  66. package/src/rsc/response-route-handler.ts +14 -1
  67. package/src/rsc/rsc-rendering.ts +7 -0
  68. package/src/rsc/server-action.ts +2 -0
  69. package/src/segment-content-promise.ts +67 -0
  70. package/src/segment-loader-promise.ts +122 -0
  71. package/src/segment-system.tsx +11 -61
  72. package/src/server/context.ts +26 -3
  73. package/src/server/request-context.ts +10 -42
  74. package/src/types/handler-context.ts +12 -39
  75. package/src/types/loader-types.ts +5 -6
  76. package/src/types/request-scope.ts +126 -0
  77. package/src/types/route-entry.ts +11 -0
  78. package/src/types/segments.ts +0 -1
  79. package/src/urls/include-helper.ts +24 -14
  80. package/src/urls/path-helper-types.ts +30 -4
  81. package/src/urls/response-types.ts +2 -10
  82. package/src/vite/debug.ts +184 -0
  83. package/src/vite/discovery/discover-routers.ts +31 -3
  84. package/src/vite/discovery/gate-state.ts +171 -0
  85. package/src/vite/discovery/prerender-collection.ts +48 -1
  86. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  87. package/src/vite/plugins/cjs-to-esm.ts +5 -0
  88. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  89. package/src/vite/plugins/client-ref-hashing.ts +16 -4
  90. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  91. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  92. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  93. package/src/vite/plugins/expose-action-id.ts +52 -28
  94. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  95. package/src/vite/plugins/expose-internal-ids.ts +516 -486
  96. package/src/vite/plugins/performance-tracks.ts +17 -9
  97. package/src/vite/plugins/use-cache-transform.ts +56 -43
  98. package/src/vite/plugins/version-injector.ts +37 -11
  99. package/src/vite/rango.ts +49 -14
  100. package/src/vite/router-discovery.ts +558 -53
  101. package/src/vite/utils/banner.ts +1 -1
  102. package/src/vite/utils/package-resolution.ts +41 -1
  103. package/src/vite/utils/prerender-utils.ts +20 -6
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  import type { TrieNode, TrieLeaf } from "../build/route-trie.js";
9
+ import { safeDecodeURIComponent } from "./url-params.js";
9
10
 
10
11
  export interface TrieMatchResult {
11
12
  /** Route name */
@@ -173,20 +174,25 @@ function validateAndBuild(
173
174
  originalPathname: string,
174
175
  pathnameHasTrailingSlash: boolean,
175
176
  ): TrieMatchResult | null {
176
- // Build named params by zipping leaf.pa with positional paramValues
177
+ // Build named params by zipping leaf.pa with positional paramValues.
178
+ // Params are URL-decoded at this boundary so ctx.params holds the values
179
+ // apps expect (matching Express/React Router) and round-trip cleanly
180
+ // through ctx.reverse.
177
181
  const params: Record<string, string> = {};
178
182
  if (leaf.pa) {
179
183
  for (let i = 0; i < leaf.pa.length && i < paramValues.length; i++) {
180
- params[leaf.pa[i]] = paramValues[i];
184
+ params[leaf.pa[i]] = safeDecodeURIComponent(paramValues[i]);
181
185
  }
182
186
  }
183
187
 
184
188
  // Add wildcard param (wildcard leaves have pn from TrieNode.w type)
185
189
  if (wildcardValue !== undefined && "pn" in leaf) {
186
- params[(leaf as TrieLeaf & { pn: string }).pn] = wildcardValue;
190
+ params[(leaf as TrieLeaf & { pn: string }).pn] =
191
+ safeDecodeURIComponent(wildcardValue);
187
192
  }
188
193
 
189
- // Validate constraints
194
+ // Validate constraints against decoded values so constraint lists can be
195
+ // written in decoded form (e.g. ["en-GB", "en US"]).
190
196
  if (leaf.cv) {
191
197
  for (const paramName in leaf.cv) {
192
198
  const allowed = leaf.cv[paramName]!;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * URL param encode/decode at the route boundary.
3
+ *
4
+ * Extraction (decode): regex/trie matchers keep param values URL-encoded;
5
+ * `safeDecodeURIComponent` turns them back into raw strings so `ctx.params`
6
+ * matches the contract apps expect (Express/React Router/Fastify/Koa) and
7
+ * round-trips through reverse stay stable. Malformed %-encoding is
8
+ * preserved as-is so a broken URL doesn't crash matching.
9
+ *
10
+ * Reversal (encode): `encodePathSegment` escapes only what RFC 3986
11
+ * requires for a path segment — `/`, `?`, `#`, space, control chars,
12
+ * non-ASCII — and leaves pchar sub-delims (`@ : $ & + , ; =` and friends)
13
+ * readable. `encodeURIComponent` over-encodes for path segments, which
14
+ * makes generated URLs harder for humans to read in the address bar
15
+ * (e.g. mailbox IDs like `ivo@example.com` would become
16
+ * `ivo%40example.com` even though `@` is path-legal).
17
+ */
18
+
19
+ export function safeDecodeURIComponent(raw: string): string {
20
+ if (raw === "" || raw.indexOf("%") === -1) return raw;
21
+ try {
22
+ return decodeURIComponent(raw);
23
+ } catch {
24
+ return raw;
25
+ }
26
+ }
27
+
28
+ // encodeURIComponent over-encodes for path segments. After running it,
29
+ // un-encode the pchar sub-delims + (`:` / `@`) so the resulting URL
30
+ // keeps human-readable characters that are legal in a path segment.
31
+ // Everything dangerous — `/ ? # %` and space/control/non-ASCII — stays
32
+ // encoded.
33
+ const PATH_SAFE_ESCAPES: Record<string, string> = {
34
+ "%3A": ":",
35
+ "%40": "@",
36
+ "%24": "$",
37
+ "%26": "&",
38
+ "%2B": "+",
39
+ "%2C": ",",
40
+ "%3B": ";",
41
+ "%3D": "=",
42
+ };
43
+
44
+ export function encodePathSegment(value: string): string {
45
+ return encodeURIComponent(value).replace(
46
+ /%(?:3A|40|24|26|2B|2C|3B|3D)/gi,
47
+ (match) => PATH_SAFE_ESCAPES[match.toUpperCase()] ?? match,
48
+ );
49
+ }
package/src/router.ts CHANGED
@@ -22,8 +22,7 @@ import type { UrlPatterns } from "./urls.js";
22
22
  import type { UrlBuilder } from "./urls/pattern-types.js";
23
23
  import { urls } from "./urls.js";
24
24
  import {
25
- EntryData,
26
- InterceptSelectorContext,
25
+ type EntryData,
27
26
  getContext,
28
27
  RSCRouterContext,
29
28
  type MetricsStore,
@@ -31,6 +31,7 @@ import {
31
31
  interceptRedirectForPartial,
32
32
  buildRouteMiddlewareEntries,
33
33
  } from "./helpers.js";
34
+ import { isWebSocketUpgradeResponse } from "../response-utils.js";
34
35
  import {
35
36
  handleResponseRoute,
36
37
  type ResponseRouteMatch,
@@ -56,6 +57,7 @@ import {
56
57
  getRouterTrie,
57
58
  } from "../route-map-builder.js";
58
59
  import type { HandlerContext } from "./handler-context.js";
60
+ import type { SegmentCacheStore } from "../cache/types.js";
59
61
  import { buildRouterTrieFromUrlpatterns } from "./manifest-init.js";
60
62
  import { handleProgressiveEnhancement } from "./progressive-enhancement.js";
61
63
  import {
@@ -352,7 +354,7 @@ export function createRSCHandler<
352
354
  // Resolve cache store configuration
353
355
  // Priority: options.cache (handler override) > router.cache (router default)
354
356
  // Store is enabled only if: config provided, enabled, and no ?__no_cache query param
355
- let cacheStore = undefined;
357
+ let cacheStore: SegmentCacheStore | undefined;
356
358
  const cacheOption = options.cache ?? router.cache;
357
359
  if (cacheOption && !url.searchParams.has("__no_cache")) {
358
360
  const cacheConfig =
@@ -533,7 +535,9 @@ export function createRSCHandler<
533
535
  }
534
536
 
535
537
  const fullTiming = timingParts.join(", ");
536
- if (fullTiming) response.headers.set("Server-Timing", fullTiming);
538
+ if (fullTiming && !isWebSocketUpgradeResponse(response)) {
539
+ response.headers.set("Server-Timing", fullTiming);
540
+ }
537
541
 
538
542
  return response;
539
543
  });
@@ -804,7 +808,7 @@ export function createRSCHandler<
804
808
  );
805
809
  }
806
810
  const response = responseOutcome.result;
807
- if (plan.negotiated) {
811
+ if (plan.negotiated && !isWebSocketUpgradeResponse(response)) {
808
812
  response.headers.append("Vary", "Accept");
809
813
  }
810
814
  return response;
@@ -1014,7 +1018,7 @@ export function createRSCHandler<
1014
1018
  nonce,
1015
1019
  );
1016
1020
  }
1017
- if (negotiated) {
1021
+ if (negotiated && !isWebSocketUpgradeResponse(response)) {
1018
1022
  response.headers.append("Vary", "Accept");
1019
1023
  }
1020
1024
  return response;
@@ -8,9 +8,49 @@ import {
8
8
  _getRequestContext,
9
9
  getLocationState,
10
10
  } from "../server/request-context.js";
11
+ import type { RequestContext } from "../server/request-context.js";
11
12
  import { resolveLocationStateEntries } from "../browser/react/location-state-shared.js";
12
13
  import type { MiddlewareEntry, MiddlewareFn } from "../router/middleware.js";
13
14
 
15
+ /**
16
+ * Copy stub headers from the request context onto a target Headers instance:
17
+ * append Set-Cookie entries, set everything else only if absent. Header
18
+ * mutation failures are swallowed so the same logic works against Response
19
+ * headers that may be immutable (e.g. Cloudflare protocol-switch responses).
20
+ */
21
+ function applyStubHeaders(target: Headers, stub: Headers): void {
22
+ stub.forEach((value, name) => {
23
+ try {
24
+ if (name.toLowerCase() === "set-cookie") {
25
+ target.append(name, value);
26
+ } else if (!target.has(name)) {
27
+ target.set(name, value);
28
+ }
29
+ } catch {
30
+ // Headers immutable — skip.
31
+ }
32
+ });
33
+ }
34
+
35
+ /**
36
+ * Drain ctx._onResponseCallbacks onto a response. Swapping the array before
37
+ * iteration prevents re-entrant registrations from double-firing and matches
38
+ * the contract that each callback runs at most once per request.
39
+ */
40
+ function drainOnResponseCallbacks(
41
+ ctx: RequestContext,
42
+ response: Response,
43
+ ): Response {
44
+ const callbacks = ctx._onResponseCallbacks;
45
+ if (callbacks.length === 0) return response;
46
+ ctx._onResponseCallbacks = [];
47
+ let result = response;
48
+ for (const callback of callbacks) {
49
+ result = callback(result) ?? result;
50
+ }
51
+ return result;
52
+ }
53
+
14
54
  /**
15
55
  * Check if a request body has content to decode
16
56
  */
@@ -39,40 +79,23 @@ export function createResponseWithMergedHeaders(
39
79
  return new Response(body, init);
40
80
  }
41
81
 
42
- // Merge headers from stub response into the new response.
43
- // Delete Set-Cookie from the stub after consuming so that downstream
44
- // merge points (e.g. executeMiddleware) do not duplicate them.
82
+ // Delete Set-Cookie from the stub after consuming so downstream merge
83
+ // points (e.g. executeMiddleware) don't duplicate them.
45
84
  const mergedHeaders = new Headers(init.headers);
46
- ctx.res.headers.forEach((value, name) => {
47
- if (name.toLowerCase() === "set-cookie") {
48
- mergedHeaders.append(name, value);
49
- } else if (!mergedHeaders.has(name)) {
50
- // Only set if not already present in init.headers
51
- mergedHeaders.set(name, value);
52
- }
53
- });
85
+ applyStubHeaders(mergedHeaders, ctx.res.headers);
54
86
  ctx.res.headers.delete("set-cookie");
55
87
 
56
- // Use ctx.res.status if it was set (e.g., 404 for notFound, 500 for error)
57
- // Otherwise use the status from init
88
+ // ctx.res.status overrides init.status when explicitly set (e.g. 404 for
89
+ // notFound, 500 for error). Default ctx.res.status is 200.
58
90
  const status = ctx.res.status !== 200 ? ctx.res.status : init.status;
59
91
 
60
- let response = new Response(body, {
92
+ const response = new Response(body, {
61
93
  ...init,
62
94
  status,
63
95
  headers: mergedHeaders,
64
96
  });
65
97
 
66
- // Run onResponse callbacks - each can inspect/modify the response.
67
- // Drain the array so that downstream callers (e.g. finalizeResponse)
68
- // do not re-execute the same callbacks on this response.
69
- const callbacks = ctx._onResponseCallbacks;
70
- ctx._onResponseCallbacks = [];
71
- for (const callback of callbacks) {
72
- response = callback(response) ?? response;
73
- }
74
-
75
- return response;
98
+ return drainOnResponseCallbacks(ctx, response);
76
99
  }
77
100
 
78
101
  /**
@@ -175,24 +198,29 @@ export function buildRouteMiddlewareEntries<TEnv>(
175
198
  }
176
199
 
177
200
  /**
178
- * Run onResponse callbacks on an existing Response.
179
- *
180
- * Used for code paths that bypass createResponseWithMergedHeaders(), such as
181
- * middleware short-circuits where the Response is already constructed but
182
- * ctx.onResponse() callbacks still need to fire.
201
+ * Merge stub headers from the request context onto an existing Response in
202
+ * place, then drain onResponse callbacks. Used when a Response cannot flow
203
+ * through `new Response()` status 101 is outside the constructor's
204
+ * 200-599 range, and the Cloudflare-specific `webSocket` property would be
205
+ * lost on reconstruction.
183
206
  */
184
- export function finalizeResponse(response: Response): Response {
207
+ export function mergeStubHeadersAndFinalize(response: Response): Response {
185
208
  const ctx = _getRequestContext();
186
- if (!ctx || ctx._onResponseCallbacks.length === 0) {
187
- return response;
188
- }
209
+ if (!ctx) return response;
189
210
 
190
- // Drain the array so callbacks run at most once per request.
191
- const callbacks = ctx._onResponseCallbacks;
192
- ctx._onResponseCallbacks = [];
193
- let result = response;
194
- for (const callback of callbacks) {
195
- result = callback(result) ?? result;
196
- }
197
- return result;
211
+ applyStubHeaders(response.headers, ctx.res.headers);
212
+ ctx.res.headers.delete("set-cookie");
213
+
214
+ return drainOnResponseCallbacks(ctx, response);
215
+ }
216
+
217
+ /**
218
+ * Run onResponse callbacks on an existing Response. Used by code paths that
219
+ * bypass createResponseWithMergedHeaders (e.g. middleware short-circuits)
220
+ * but still need ctx.onResponse() callbacks to fire.
221
+ */
222
+ export function finalizeResponse(response: Response): Response {
223
+ const ctx = _getRequestContext();
224
+ if (!ctx) return response;
225
+ return drainOnResponseCallbacks(ctx, response);
198
226
  }
@@ -248,6 +248,7 @@ export async function handleProgressiveEnhancement<TEnv>(
248
248
  segments: match.segments,
249
249
  matched: match.matched,
250
250
  diff: match.diff,
251
+ params: match.params,
251
252
  isPartial: false,
252
253
  rootLayout: ctx.router.rootLayout,
253
254
  handles: handleStore.stream(),
@@ -353,6 +354,7 @@ async function renderPeErrorBoundary<TEnv>(
353
354
  segments: errorResult.segments,
354
355
  matched: errorResult.matched,
355
356
  diff: errorResult.diff,
357
+ params: errorResult.params,
356
358
  isPartial: false,
357
359
  isError: true,
358
360
  rootLayout: ctx.router.rootLayout,
@@ -26,7 +26,9 @@ import {
26
26
  finalizeResponse,
27
27
  isCacheableStatus,
28
28
  buildRouteMiddlewareEntries,
29
+ mergeStubHeadersAndFinalize,
29
30
  } from "./helpers.js";
31
+ import { isWebSocketUpgradeResponse } from "../response-utils.js";
30
32
 
31
33
  export interface ResponseRouteMatch {
32
34
  responseType: string;
@@ -78,10 +80,13 @@ export async function handleResponseRoute<TEnv>(
78
80
  env,
79
81
  searchParams: cleanUrl.searchParams,
80
82
  url: cleanUrl,
83
+ originalUrl: reqCtx.originalUrl,
81
84
  pathname: url.pathname,
82
85
  reverse: createReverseFunction(handlerCtx.getRequiredRouteMap()),
83
86
  get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
84
87
  header: (name: string, value: string) => reqCtx.header(name, value),
88
+ waitUntil: reqCtx.waitUntil.bind(reqCtx),
89
+ executionContext: reqCtx.executionContext,
85
90
  _responseType: preview.responseType,
86
91
  };
87
92
  // Brand with taint symbol so "use cache" detects it as request-scoped
@@ -96,6 +101,12 @@ export async function handleResponseRoute<TEnv>(
96
101
  // so that stub headers (cookies, custom headers set via ctx.header()) are included.
97
102
  // Use Headers (not Record<string, string>) to preserve duplicate entries like Set-Cookie.
98
103
  const rewrapResponse = (result: Response) => {
104
+ // 204/205/304 are NOT short-circuited — they're valid for the Response
105
+ // constructor and must honor ctx.setStatus() overrides. Only upgrade
106
+ // responses (status 101 / `webSocket` property) bypass reconstruction.
107
+ if (isWebSocketUpgradeResponse(result)) {
108
+ return mergeStubHeadersAndFinalize(result);
109
+ }
99
110
  const headers = new Headers();
100
111
  result.headers.forEach((value, key) => {
101
112
  if (key.toLowerCase() === "set-cookie") {
@@ -196,7 +207,9 @@ export async function handleResponseRoute<TEnv>(
196
207
  // Wrap callHandler to append Vary: Accept on content-negotiated responses
197
208
  const callHandlerWithVary = async () => {
198
209
  const response = await callHandler();
199
- if (preview.negotiated) {
210
+ if (preview.negotiated && !isWebSocketUpgradeResponse(response)) {
211
+ // Skip Vary on upgrade responses: headers are semantically immutable
212
+ // on some runtimes, and Vary is meaningless for a 101 response.
200
213
  response.headers.append("Vary", "Accept");
201
214
  }
202
215
  return response;
@@ -204,6 +204,13 @@ export async function handleRscRendering<TEnv>(
204
204
  "content-type": "text/x-component;charset=utf-8",
205
205
  vary: "accept, X-Rango-State, X-RSC-Router-Client-Path",
206
206
  };
207
+ // Tell the client's prefetch cache to scope this response to its source
208
+ // URL (instead of the default source-agnostic wildcard). Intercept
209
+ // responses depend on the source page matching an intercept rule, so
210
+ // they must not be reused for navigations from other sources.
211
+ if (hasInterceptSlots) {
212
+ rscHeaders["x-rsc-prefetch-scope"] = "source";
213
+ }
207
214
  // Enable browser HTTP caching for prefetch responses only.
208
215
  // Requires X-Rango-Prefetch header (sent by Link prefetch fetch),
209
216
  // non-intercept context (intercept responses depend on source page),
@@ -213,6 +213,7 @@ export async function executeServerAction<TEnv>(
213
213
  isPartial: true,
214
214
  matched: errorResult.matched,
215
215
  diff: errorResult.diff,
216
+ params: errorResult.params,
216
217
  isError: true,
217
218
  handles: handleStore.stream(),
218
219
  version: ctx.version,
@@ -323,6 +324,7 @@ export async function revalidateAfterAction<TEnv>(
323
324
  isPartial: true,
324
325
  matched: matchResult.matched,
325
326
  diff: matchResult.diff,
327
+ params: matchResult.params,
326
328
  slots: matchResult.slots,
327
329
  handles: handleStore.stream(),
328
330
  version: ctx.version,
@@ -0,0 +1,67 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ /**
4
+ * Stable Promise wrappers keyed on the component itself. Objects (React
5
+ * elements, functions, lazy payloads) land in a WeakMap so entries GC when
6
+ * the underlying component is released; primitives (string, number, boolean,
7
+ * null) land in a Map so memoization still applies to text-/null-backed
8
+ * segments like those in partial-update flows. Keeping this cache outside
9
+ * the segment eliminates preservation fields on ResolvedSegment — it survives
10
+ * reconciliation naturally because the component ref is what's stable.
11
+ *
12
+ * Browser-only. On the server each SSR render needs a fresh pending promise
13
+ * so Suspense can emit the loading fallback HTML before content streams. A
14
+ * shared already-resolved promise has `.status === "fulfilled"` attached by
15
+ * React on its first observation — subsequent `use()` calls return
16
+ * synchronously without suspending, so the Suspense fallback never makes it
17
+ * into the initial HTML. Route-definition components share refs across
18
+ * requests, so a global cache would leak tracked state between renders.
19
+ */
20
+ const IS_BROWSER = typeof window !== "undefined";
21
+ const objectContentCache = IS_BROWSER
22
+ ? new WeakMap<object, Promise<ReactNode>>()
23
+ : null;
24
+ const primitiveContentCache = IS_BROWSER
25
+ ? new Map<unknown, Promise<ReactNode>>()
26
+ : null;
27
+
28
+ /**
29
+ * Return a stable Promise wrapping `component`, memoized on the component ref.
30
+ *
31
+ * A fresh `Promise.resolve(component)` each render would suspend for one
32
+ * microtask and briefly commit the loading fallback inside Suspender — the
33
+ * intercept / parallel-slot flicker this indirection prevents. Reusing the
34
+ * same Promise ref keeps React's `use()` in "known fulfilled" state after
35
+ * the first observation.
36
+ *
37
+ * @internal
38
+ */
39
+ export function getMemoizedContentPromise(
40
+ component: ReactNode,
41
+ ): Promise<ReactNode> {
42
+ if (component instanceof Promise) {
43
+ return component as Promise<ReactNode>;
44
+ }
45
+
46
+ if (!objectContentCache || !primitiveContentCache) {
47
+ return Promise.resolve(component);
48
+ }
49
+
50
+ if (component !== null && typeof component === "object") {
51
+ const cached = objectContentCache.get(component);
52
+ if (cached) {
53
+ return cached;
54
+ }
55
+ const promise = Promise.resolve(component);
56
+ objectContentCache.set(component, promise);
57
+ return promise;
58
+ }
59
+
60
+ const cached = primitiveContentCache.get(component);
61
+ if (cached) {
62
+ return cached;
63
+ }
64
+ const promise = Promise.resolve(component);
65
+ primitiveContentCache.set(component, promise);
66
+ return promise;
67
+ }
@@ -0,0 +1,122 @@
1
+ import type { ResolvedSegment } from "./types.js";
2
+
3
+ /**
4
+ * Cache of aggregate Promise.all results keyed on the first loader's
5
+ * `loaderData` reference. Each entry holds the source refs it was built from
6
+ * plus the resulting Promise/array; lookup scans entries for the matching
7
+ * source array (typically a single entry, since distinct loader groups rarely
8
+ * share a first source). Object first-refs live in a WeakMap (auto-GC);
9
+ * primitive first-refs (strings/numbers/booleans/null) live in a Map so
10
+ * loaders that resolve to primitive data are memoized too — bounded in
11
+ * practice by the application's loader set.
12
+ *
13
+ * Keying externally means reconciliation's fresh segment objects no longer
14
+ * drop memoization — the cache survives as long as the underlying loader
15
+ * segments do, and GC collects entries when those loaders are released
16
+ * (object keys only).
17
+ *
18
+ * Browser-only. On the server each SSR render needs a fresh Promise so
19
+ * Suspense can actually suspend and emit the loading fallback HTML before
20
+ * content streams. A shared already-resolved promise has `.status` attached
21
+ * by React on first `use()`; subsequent observations return synchronously
22
+ * and skip the fallback. The zero-loader case is especially prone because
23
+ * every empty-loader site would otherwise share one promise across requests.
24
+ */
25
+ const IS_BROWSER = typeof window !== "undefined";
26
+
27
+ interface LoaderCacheEntry {
28
+ sources: any[];
29
+ promise: Promise<any[]> | any[];
30
+ }
31
+
32
+ const objectLoaderCache = IS_BROWSER
33
+ ? new WeakMap<object, LoaderCacheEntry[]>()
34
+ : null;
35
+ const primitiveLoaderCache = IS_BROWSER
36
+ ? new Map<unknown, LoaderCacheEntry[]>()
37
+ : null;
38
+
39
+ // In the browser, a single shared empty aggregate is safe (and desirable) —
40
+ // reusing the same resolved promise keeps React's `use()` in a known-fulfilled
41
+ // state across renders. On the server it would leak `.status = "fulfilled"`
42
+ // across requests and skip the Suspense fallback, so we rebuild on each call.
43
+ const SHARED_EMPTY_LOADER_PROMISE: Promise<any[]> | null = IS_BROWSER
44
+ ? Promise.resolve([])
45
+ : null;
46
+
47
+ function hasSameReferences(a: any[], b: any[]): boolean {
48
+ if (a.length !== b.length) {
49
+ return false;
50
+ }
51
+ for (let i = 0; i < a.length; i++) {
52
+ if (a[i] !== b[i]) {
53
+ return false;
54
+ }
55
+ }
56
+ return true;
57
+ }
58
+
59
+ function buildLoaderPromise(loaders: ResolvedSegment[]): Promise<any[]> {
60
+ if (loaders.length === 0) {
61
+ return Promise.resolve([]);
62
+ }
63
+ return Promise.all(
64
+ loaders.map((loader) =>
65
+ loader.loaderData instanceof Promise
66
+ ? loader.loaderData
67
+ : Promise.resolve(loader.loaderData),
68
+ ),
69
+ );
70
+ }
71
+
72
+ function isObjectLike(value: unknown): value is object {
73
+ return (
74
+ value !== null && (typeof value === "object" || typeof value === "function")
75
+ );
76
+ }
77
+
78
+ /**
79
+ * Memoize an aggregate Promise.all for a set of loader segments. Reusing the
80
+ * same aggregate across renders — invalidated only when any underlying
81
+ * loader.loaderData ref changes — keeps React's `use()` in "known fulfilled"
82
+ * state and prevents a fresh Promise.all from suspending (and briefly
83
+ * committing the Suspense fallback) on every partial update that doesn't
84
+ * actually change loader data.
85
+ *
86
+ * @internal
87
+ */
88
+ export function getMemoizedLoaderPromise(
89
+ loaders: ResolvedSegment[],
90
+ ): Promise<any[]> | any[] {
91
+ if (loaders.length === 0) {
92
+ return SHARED_EMPTY_LOADER_PROMISE ?? buildLoaderPromise(loaders);
93
+ }
94
+ if (!objectLoaderCache || !primitiveLoaderCache) {
95
+ return buildLoaderPromise(loaders);
96
+ }
97
+
98
+ const sources = loaders.map((loader) => loader.loaderData);
99
+ const first = sources[0];
100
+ const entries = isObjectLike(first)
101
+ ? objectLoaderCache.get(first)
102
+ : primitiveLoaderCache.get(first);
103
+
104
+ if (entries) {
105
+ for (const entry of entries) {
106
+ if (hasSameReferences(entry.sources, sources)) {
107
+ return entry.promise;
108
+ }
109
+ }
110
+ }
111
+
112
+ const promise = buildLoaderPromise(loaders);
113
+ const newEntry: LoaderCacheEntry = { sources, promise };
114
+ if (entries) {
115
+ entries.push(newEntry);
116
+ } else if (isObjectLike(first)) {
117
+ objectLoaderCache.set(first, [newEntry]);
118
+ } else {
119
+ primitiveLoaderCache.set(first, [newEntry]);
120
+ }
121
+ return promise;
122
+ }