@bleedingdev/modern-js-plugin-tanstack 3.5.0-ultramodern.9 → 3.5.0-ultramodern.90

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 (270) hide show
  1. package/dist/cjs/cli/artifacts.js +157 -0
  2. package/dist/cjs/cli/index.js +8 -79
  3. package/dist/cjs/cli/tanstackTypes/canonicalRoutes.js +113 -0
  4. package/dist/cjs/cli/tanstackTypes/shared.js +123 -0
  5. package/dist/cjs/cli/tanstackTypes/source.js +255 -0
  6. package/dist/cjs/cli/tanstackTypes.js +4 -350
  7. package/dist/cjs/runtime/blockingSubscribe.js +57 -0
  8. package/dist/cjs/runtime/{pluginCore.js → clientBasepath.js} +12 -19
  9. package/dist/cjs/runtime/clientHydration.js +118 -0
  10. package/dist/cjs/runtime/dataMutation.js +11 -176
  11. package/dist/cjs/runtime/formData.js +116 -0
  12. package/dist/cjs/runtime/loaderBridge.js +7 -11
  13. package/dist/cjs/runtime/plugin.js +21 -125
  14. package/dist/cjs/runtime/plugin.node.js +45 -125
  15. package/dist/cjs/runtime/pluginShared.js +87 -0
  16. package/dist/cjs/runtime/prefetchLink.js +15 -2
  17. package/dist/cjs/runtime/register.js +3 -16
  18. package/dist/cjs/runtime/routeTree/components.js +102 -0
  19. package/dist/cjs/runtime/routeTree/construction.js +152 -0
  20. package/dist/cjs/runtime/routeTree/index.js +47 -0
  21. package/dist/cjs/runtime/routeTree/loaders.js +158 -0
  22. package/dist/cjs/runtime/routeTree/paths.js +57 -0
  23. package/dist/cjs/runtime/routeTree/staticData.js +63 -0
  24. package/dist/cjs/runtime/routeTree/tanstackRoutes.js +57 -0
  25. package/dist/cjs/runtime/routeTree/types.js +18 -0
  26. package/dist/cjs/runtime/routeTree.js +7 -320
  27. package/dist/cjs/runtime/router.js +15 -2
  28. package/dist/cjs/runtime/rsc/CompositeComponent.js +18 -13
  29. package/dist/cjs/runtime/rsc/ReplayableStream.js +0 -4
  30. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +5 -16
  31. package/dist/cjs/runtime/rsc/client.js +8 -25
  32. package/dist/cjs/runtime/rsc/flightSerialization.js +132 -0
  33. package/dist/cjs/runtime/rsc/payloadErrors.js +99 -0
  34. package/dist/cjs/runtime/rsc/payloadFetch.js +105 -0
  35. package/dist/cjs/runtime/rsc/payloadRouter.js +21 -142
  36. package/dist/cjs/runtime/rsc/payloadRoutes.js +94 -0
  37. package/dist/cjs/runtime/rsc/payloadStorage.js +58 -0
  38. package/dist/cjs/runtime/rsc/server.js +14 -18
  39. package/dist/cjs/runtime/rsc/shared.js +68 -0
  40. package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +1 -0
  41. package/dist/cjs/runtime/rsc/treeGetter.js +72 -0
  42. package/dist/cjs/runtime/shared/isAbsoluteUrl.js +47 -0
  43. package/dist/cjs/runtime/ssrManagedTags.js +103 -0
  44. package/dist/cjs/runtime/ssrPreload.js +89 -0
  45. package/dist/cjs/runtime/ssrTypes.js +18 -0
  46. package/dist/cjs/runtime/state.js +1 -1
  47. package/dist/cjs/runtime/submitAction.js +161 -0
  48. package/dist/cjs/runtime/utils.js +49 -86
  49. package/dist/esm/cli/artifacts.mjs +103 -0
  50. package/dist/esm/cli/index.mjs +8 -82
  51. package/dist/esm/cli/tanstackTypes/canonicalRoutes.mjs +75 -0
  52. package/dist/esm/cli/tanstackTypes/shared.mjs +61 -0
  53. package/dist/esm/cli/tanstackTypes/source.mjs +207 -0
  54. package/dist/esm/cli/tanstackTypes.mjs +2 -339
  55. package/dist/esm/runtime/blockingSubscribe.mjs +19 -0
  56. package/dist/esm/runtime/clientBasepath.mjs +10 -0
  57. package/dist/esm/runtime/clientHydration.mjs +71 -0
  58. package/dist/esm/runtime/dataMutation.mjs +2 -167
  59. package/dist/esm/runtime/formData.mjs +63 -0
  60. package/dist/esm/runtime/loaderBridge.mjs +5 -9
  61. package/dist/esm/runtime/plugin.mjs +14 -118
  62. package/dist/esm/runtime/plugin.node.mjs +41 -121
  63. package/dist/esm/runtime/pluginShared.mjs +40 -0
  64. package/dist/esm/runtime/prefetchLink.mjs +18 -5
  65. package/dist/esm/runtime/register.mjs +1 -14
  66. package/dist/esm/runtime/routeTree/components.mjs +55 -0
  67. package/dist/esm/runtime/routeTree/construction.mjs +111 -0
  68. package/dist/esm/runtime/routeTree/index.mjs +2 -0
  69. package/dist/esm/runtime/routeTree/loaders.mjs +117 -0
  70. package/dist/esm/runtime/routeTree/paths.mjs +16 -0
  71. package/dist/esm/runtime/routeTree/staticData.mjs +22 -0
  72. package/dist/esm/runtime/routeTree/tanstackRoutes.mjs +13 -0
  73. package/dist/esm/runtime/routeTree/types.mjs +0 -0
  74. package/dist/esm/runtime/routeTree.mjs +1 -318
  75. package/dist/esm/runtime/router.mjs +16 -2
  76. package/dist/esm/runtime/rsc/CompositeComponent.mjs +18 -13
  77. package/dist/esm/runtime/rsc/ReplayableStream.mjs +1 -1
  78. package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +4 -15
  79. package/dist/esm/runtime/rsc/client.mjs +6 -26
  80. package/dist/esm/runtime/rsc/flightSerialization.mjs +88 -0
  81. package/dist/esm/runtime/rsc/payloadErrors.mjs +49 -0
  82. package/dist/esm/runtime/rsc/payloadFetch.mjs +55 -0
  83. package/dist/esm/runtime/rsc/payloadRouter.mjs +12 -133
  84. package/dist/esm/runtime/rsc/payloadRoutes.mjs +50 -0
  85. package/dist/esm/runtime/rsc/payloadStorage.mjs +14 -0
  86. package/dist/esm/runtime/rsc/server.mjs +17 -21
  87. package/dist/esm/runtime/rsc/shared.mjs +21 -0
  88. package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +1 -0
  89. package/dist/esm/runtime/rsc/treeGetter.mjs +34 -0
  90. package/dist/esm/runtime/shared/isAbsoluteUrl.mjs +9 -0
  91. package/dist/esm/runtime/ssrManagedTags.mjs +53 -0
  92. package/dist/esm/runtime/ssrPreload.mjs +51 -0
  93. package/dist/esm/runtime/ssrTypes.mjs +0 -0
  94. package/dist/esm/runtime/state.mjs +1 -1
  95. package/dist/esm/runtime/submitAction.mjs +120 -0
  96. package/dist/esm/runtime/utils.mjs +47 -75
  97. package/dist/esm-node/cli/artifacts.mjs +104 -0
  98. package/dist/esm-node/cli/index.mjs +8 -82
  99. package/dist/esm-node/cli/tanstackTypes/canonicalRoutes.mjs +76 -0
  100. package/dist/esm-node/cli/tanstackTypes/shared.mjs +62 -0
  101. package/dist/esm-node/cli/tanstackTypes/source.mjs +208 -0
  102. package/dist/esm-node/cli/tanstackTypes.mjs +2 -339
  103. package/dist/esm-node/runtime/blockingSubscribe.mjs +20 -0
  104. package/dist/esm-node/runtime/clientBasepath.mjs +11 -0
  105. package/dist/esm-node/runtime/clientHydration.mjs +72 -0
  106. package/dist/esm-node/runtime/dataMutation.mjs +2 -167
  107. package/dist/esm-node/runtime/formData.mjs +64 -0
  108. package/dist/esm-node/runtime/loaderBridge.mjs +5 -9
  109. package/dist/esm-node/runtime/plugin.mjs +14 -118
  110. package/dist/esm-node/runtime/plugin.node.mjs +41 -121
  111. package/dist/esm-node/runtime/pluginShared.mjs +41 -0
  112. package/dist/esm-node/runtime/prefetchLink.mjs +18 -5
  113. package/dist/esm-node/runtime/register.mjs +1 -14
  114. package/dist/esm-node/runtime/routeTree/components.mjs +56 -0
  115. package/dist/esm-node/runtime/routeTree/construction.mjs +112 -0
  116. package/dist/esm-node/runtime/routeTree/index.mjs +3 -0
  117. package/dist/esm-node/runtime/routeTree/loaders.mjs +118 -0
  118. package/dist/esm-node/runtime/routeTree/paths.mjs +17 -0
  119. package/dist/esm-node/runtime/routeTree/staticData.mjs +23 -0
  120. package/dist/esm-node/runtime/routeTree/tanstackRoutes.mjs +14 -0
  121. package/dist/esm-node/runtime/routeTree/types.mjs +1 -0
  122. package/dist/esm-node/runtime/routeTree.mjs +1 -318
  123. package/dist/esm-node/runtime/router.mjs +16 -2
  124. package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +18 -13
  125. package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +1 -1
  126. package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +4 -15
  127. package/dist/esm-node/runtime/rsc/client.mjs +6 -26
  128. package/dist/esm-node/runtime/rsc/flightSerialization.mjs +89 -0
  129. package/dist/esm-node/runtime/rsc/payloadErrors.mjs +50 -0
  130. package/dist/esm-node/runtime/rsc/payloadFetch.mjs +56 -0
  131. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +12 -133
  132. package/dist/esm-node/runtime/rsc/payloadRoutes.mjs +51 -0
  133. package/dist/esm-node/runtime/rsc/payloadStorage.mjs +15 -0
  134. package/dist/esm-node/runtime/rsc/server.mjs +17 -21
  135. package/dist/esm-node/runtime/rsc/shared.mjs +22 -0
  136. package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +1 -0
  137. package/dist/esm-node/runtime/rsc/treeGetter.mjs +35 -0
  138. package/dist/esm-node/runtime/shared/isAbsoluteUrl.mjs +10 -0
  139. package/dist/esm-node/runtime/ssrManagedTags.mjs +54 -0
  140. package/dist/esm-node/runtime/ssrPreload.mjs +52 -0
  141. package/dist/esm-node/runtime/ssrTypes.mjs +1 -0
  142. package/dist/esm-node/runtime/state.mjs +1 -1
  143. package/dist/esm-node/runtime/submitAction.mjs +121 -0
  144. package/dist/esm-node/runtime/utils.mjs +47 -75
  145. package/dist/types/cli/artifacts.d.ts +33 -0
  146. package/dist/types/cli/index.d.ts +2 -22
  147. package/dist/types/cli/tanstackTypes/canonicalRoutes.d.ts +22 -0
  148. package/dist/types/cli/tanstackTypes/shared.d.ts +20 -0
  149. package/dist/types/cli/tanstackTypes/source.d.ts +12 -0
  150. package/dist/types/cli/tanstackTypes.d.ts +3 -31
  151. package/dist/types/runtime/blockingSubscribe.d.ts +1 -0
  152. package/dist/types/runtime/clientBasepath.d.ts +1 -0
  153. package/dist/types/runtime/clientHydration.d.ts +10 -0
  154. package/dist/types/runtime/dataMutation.d.ts +3 -12
  155. package/dist/types/runtime/formData.d.ts +24 -0
  156. package/dist/types/runtime/loaderBridge.d.ts +2 -1
  157. package/dist/types/runtime/plugin.d.ts +1 -1
  158. package/dist/types/runtime/plugin.node.d.ts +1 -1
  159. package/dist/types/runtime/pluginShared.d.ts +38 -0
  160. package/dist/types/runtime/register.d.ts +10 -9
  161. package/dist/types/runtime/routeTree/components.d.ts +7 -0
  162. package/dist/types/runtime/routeTree/construction.d.ts +5 -0
  163. package/dist/types/runtime/routeTree/index.d.ts +2 -0
  164. package/dist/types/runtime/routeTree/loaders.d.ts +4 -0
  165. package/dist/types/runtime/routeTree/paths.d.ts +3 -0
  166. package/dist/types/runtime/routeTree/staticData.d.ts +19 -0
  167. package/dist/types/runtime/routeTree/tanstackRoutes.d.ts +4 -0
  168. package/dist/types/runtime/routeTree/types.d.ts +76 -0
  169. package/dist/types/runtime/routeTree.d.ts +1 -9
  170. package/dist/types/runtime/router.d.ts +6 -7
  171. package/dist/types/runtime/rsc/CompositeComponent.d.ts +1 -2
  172. package/dist/types/runtime/rsc/ReplayableStream.d.ts +3 -2
  173. package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +2 -3
  174. package/dist/types/runtime/rsc/SlotContext.d.ts +2 -1
  175. package/dist/types/runtime/rsc/client.d.ts +2 -0
  176. package/dist/types/runtime/rsc/flightSerialization.d.ts +3 -0
  177. package/dist/types/runtime/rsc/payloadErrors.d.ts +6 -0
  178. package/dist/types/runtime/rsc/payloadFetch.d.ts +8 -0
  179. package/dist/types/runtime/rsc/payloadRouter.d.ts +2 -10
  180. package/dist/types/runtime/rsc/payloadRoutes.d.ts +40 -0
  181. package/dist/types/runtime/rsc/payloadStorage.d.ts +5 -0
  182. package/dist/types/runtime/rsc/shared.d.ts +18 -0
  183. package/dist/types/runtime/rsc/treeGetter.d.ts +1 -0
  184. package/dist/types/runtime/shared/isAbsoluteUrl.d.ts +1 -0
  185. package/dist/types/runtime/ssrManagedTags.d.ts +7 -0
  186. package/dist/types/runtime/ssrPreload.d.ts +2 -0
  187. package/dist/types/runtime/ssrTypes.d.ts +44 -0
  188. package/dist/types/runtime/state.d.ts +1 -1
  189. package/dist/types/runtime/submitAction.d.ts +14 -0
  190. package/dist/types/runtime/types.d.ts +4 -10
  191. package/dist/types/runtime/utils.d.ts +4 -24
  192. package/package.json +37 -17
  193. package/rstest.config.mts +23 -0
  194. package/src/cli/artifacts.ts +255 -0
  195. package/src/cli/index.ts +7 -185
  196. package/src/cli/tanstackTypes/canonicalRoutes.ts +157 -0
  197. package/src/cli/tanstackTypes/shared.ts +113 -0
  198. package/src/cli/tanstackTypes/source.ts +414 -0
  199. package/src/cli/tanstackTypes.ts +4 -647
  200. package/src/runtime/blockingSubscribe.ts +48 -0
  201. package/src/runtime/clientBasepath.ts +12 -0
  202. package/src/runtime/clientHydration.ts +149 -0
  203. package/src/runtime/dataMutation.tsx +9 -306
  204. package/src/runtime/formData.ts +153 -0
  205. package/src/runtime/loaderBridge.ts +11 -12
  206. package/src/runtime/plugin.node.tsx +76 -291
  207. package/src/runtime/plugin.tsx +21 -257
  208. package/src/runtime/pluginShared.ts +112 -0
  209. package/src/runtime/prefetchLink.tsx +78 -15
  210. package/src/runtime/register.ts +10 -42
  211. package/src/runtime/routeTree/components.ts +130 -0
  212. package/src/runtime/routeTree/construction.ts +238 -0
  213. package/src/runtime/routeTree/index.ts +7 -0
  214. package/src/runtime/routeTree/loaders.ts +232 -0
  215. package/src/runtime/routeTree/paths.ts +36 -0
  216. package/src/runtime/routeTree/staticData.ts +66 -0
  217. package/src/runtime/routeTree/tanstackRoutes.ts +36 -0
  218. package/src/runtime/routeTree/types.ts +87 -0
  219. package/src/runtime/routeTree.ts +5 -750
  220. package/src/runtime/router.ts +35 -7
  221. package/src/runtime/rsc/CompositeComponent.tsx +29 -20
  222. package/src/runtime/rsc/ReplayableStream.ts +2 -2
  223. package/src/runtime/rsc/RscNodeRenderer.tsx +9 -27
  224. package/src/runtime/rsc/SlotContext.tsx +1 -1
  225. package/src/runtime/rsc/client.tsx +12 -40
  226. package/src/runtime/rsc/flightSerialization.ts +158 -0
  227. package/src/runtime/rsc/payloadErrors.ts +73 -0
  228. package/src/runtime/rsc/payloadFetch.ts +92 -0
  229. package/src/runtime/rsc/payloadRouter.ts +29 -234
  230. package/src/runtime/rsc/payloadRoutes.ts +122 -0
  231. package/src/runtime/rsc/payloadStorage.ts +35 -0
  232. package/src/runtime/rsc/server.tsx +20 -29
  233. package/src/runtime/rsc/shared.ts +70 -0
  234. package/src/runtime/rsc/slotUsageSanitizer.ts +4 -0
  235. package/src/runtime/rsc/treeGetter.tsx +52 -0
  236. package/src/runtime/shared/isAbsoluteUrl.ts +8 -0
  237. package/src/runtime/ssrManagedTags.ts +107 -0
  238. package/src/runtime/ssrPreload.ts +125 -0
  239. package/src/runtime/ssrTypes.ts +51 -0
  240. package/src/runtime/state.ts +9 -3
  241. package/src/runtime/submitAction.ts +201 -0
  242. package/src/runtime/types.ts +4 -18
  243. package/src/runtime/utils.tsx +76 -110
  244. package/src/types/react-server-dom-rspack.d.ts +56 -0
  245. package/tests/castRatchet.test.ts +85 -0
  246. package/tests/realm-isolation.test.ts +114 -0
  247. package/tests/router/cli.test.ts +7 -0
  248. package/tests/router/clientHydration.test.tsx +61 -0
  249. package/tests/router/dataMutation.test.tsx +59 -0
  250. package/tests/router/fixtures/legacyRuntimeRouter.ts +10 -0
  251. package/tests/router/fixtures/router-gen.golden.txt +71 -0
  252. package/tests/router/fixtures/router-gen.oxlint.json +5 -0
  253. package/tests/router/flightSerialization.roundtrip.test.tsx +137 -0
  254. package/tests/router/generateRouteArtifacts.test.ts +106 -0
  255. package/tests/router/hooks.test.ts +14 -0
  256. package/tests/router/loaderBridge.test.ts +21 -0
  257. package/tests/router/prefetchLink.test.tsx +106 -59
  258. package/tests/router/prefetchLinkPreload.test.tsx +85 -0
  259. package/tests/router/register.test.ts +1 -39
  260. package/tests/router/routeTree.test.ts +51 -11
  261. package/tests/router/routerCompatibility.test.ts +245 -0
  262. package/tests/router/rsc.test.tsx +189 -0
  263. package/tests/router/rscPayloadRouterMatrix.test.ts +197 -0
  264. package/tests/router/slotUsageSanitizer.test.ts +23 -0
  265. package/tests/router/ssrPreload.test.ts +30 -0
  266. package/tests/router/tanstackTypes.test.ts +298 -1
  267. package/dist/esm/runtime/pluginCore.mjs +0 -14
  268. package/dist/esm-node/runtime/pluginCore.mjs +0 -15
  269. package/dist/types/runtime/pluginCore.d.ts +0 -21
  270. package/src/runtime/pluginCore.ts +0 -48
@@ -0,0 +1,56 @@
1
+ import "node:module";
2
+ import { notFound, redirect } from "@tanstack/react-router";
3
+ import { isAbsoluteUrl } from "../shared/isAbsoluteUrl.mjs";
4
+ import { reviveTanstackRscFlightValues } from "./flightSerialization.mjs";
5
+ const payloadFetchCache = new Map();
6
+ let payloadDecoder;
7
+ async function decodePayload(stream) {
8
+ if (payloadDecoder) return reviveTanstackRscFlightValues(await payloadDecoder(stream));
9
+ const runtime = await import("@modern-js/runtime/rsc/client");
10
+ return reviveTanstackRscFlightValues(await runtime.createFromReadableStream(stream));
11
+ }
12
+ function isServerPayload(value) {
13
+ return Boolean(value) && 'object' == typeof value && 'render' === value.type && Array.isArray(value.routes);
14
+ }
15
+ function createPayloadFetchKey(request) {
16
+ return JSON.stringify([
17
+ request.url,
18
+ request.method,
19
+ [
20
+ ...request.headers.entries()
21
+ ].sort(([left], [right])=>left.localeCompare(right))
22
+ ]);
23
+ }
24
+ async function fetchTanstackRscPayload(request) {
25
+ const headers = new Headers(request.headers);
26
+ headers.set('x-rsc-tree', 'true');
27
+ const response = await fetch(request.url, {
28
+ credentials: 'same-origin',
29
+ headers,
30
+ method: 'GET',
31
+ signal: request.signal
32
+ });
33
+ const redirectLocation = response.headers.get('X-Modernjs-Redirect');
34
+ if (redirectLocation) {
35
+ if (isAbsoluteUrl(redirectLocation)) throw redirect({
36
+ headers: response.headers,
37
+ href: redirectLocation,
38
+ statusCode: response.status
39
+ });
40
+ throw redirect({
41
+ headers: response.headers,
42
+ statusCode: response.status,
43
+ to: redirectLocation || '/'
44
+ });
45
+ }
46
+ if (404 === response.status && !response.body) throw notFound();
47
+ if (!response.body) throw new Error('TanStack RSC payload response body is null.');
48
+ const payload = await decodePayload(response.body);
49
+ if (!isServerPayload(payload)) throw new Error('Unexpected TanStack RSC payload type.');
50
+ return payload;
51
+ }
52
+ function __setTanstackRscPayloadDecoderForTests(decoder) {
53
+ payloadDecoder = decoder;
54
+ payloadFetchCache.clear();
55
+ }
56
+ export { __setTanstackRscPayloadDecoderForTests, createPayloadFetchKey, fetchTanstackRscPayload, isAbsoluteUrl, payloadFetchCache };
@@ -1,77 +1,9 @@
1
1
  import "node:module";
2
- import { isRouteErrorResponse } from "@modern-js/runtime-utils/router";
3
- import { notFound, redirect } from "@tanstack/react-router";
4
- const payloadFetchCache = new Map();
5
- let payloadDecoder;
6
- function getRouteId(match) {
7
- const routeId = match.routeId ?? match.route?.id ?? match.id;
8
- return 'string' == typeof routeId ? routeId : void 0;
9
- }
10
- function getRouteStaticData(match) {
11
- return match.route?.options?.staticData || {};
12
- }
13
- function getRouteParentId(match) {
14
- const parentId = match.route?.parentRoute?.id;
15
- return 'string' == typeof parentId ? parentId : void 0;
16
- }
17
- function toRoutePath(match) {
18
- const path = match.route?.options?.path;
19
- return 'string' == typeof path ? path : void 0;
20
- }
21
- function toPayloadRoute(match) {
22
- const routeId = getRouteId(match);
23
- if (!routeId) return;
24
- const staticData = getRouteStaticData(match);
25
- const params = match.params && 'object' == typeof match.params ? match.params : {};
26
- const pathname = 'string' == typeof match.pathname ? match.pathname : '';
27
- return {
28
- handle: staticData.modernRouteHandle,
29
- hasAction: Boolean(staticData.modernRouteHasAction || staticData.modernRouteAction),
30
- hasErrorBoundary: false,
31
- hasLoader: Boolean(staticData.modernRouteHasLoader),
32
- hasClientLoader: Boolean(staticData.modernRouteHasClientLoader),
33
- id: routeId,
34
- index: Boolean(match.route?.options?.index) || void 0,
35
- params,
36
- parentId: getRouteParentId(match),
37
- path: toRoutePath(match),
38
- pathname,
39
- pathnameBase: 'string' == typeof match.pathnameBase ? match.pathnameBase : pathname
40
- };
41
- }
42
- function shouldRedactServerError(status = 500) {
43
- return status >= 500 && 'development' !== process.env.NODE_ENV && 'test' !== process.env.NODE_ENV;
44
- }
45
- function serializePayloadError(error) {
46
- if (isRouteErrorResponse(error)) {
47
- if (shouldRedactServerError(error.status)) return {
48
- status: error.status,
49
- statusText: 'Internal Server Error',
50
- data: 'Unexpected Server Error',
51
- __type: 'RouteErrorResponse'
52
- };
53
- return {
54
- ...error,
55
- __type: 'RouteErrorResponse'
56
- };
57
- }
58
- if (error instanceof Error) {
59
- if (shouldRedactServerError()) return {
60
- message: 'Unexpected Server Error',
61
- stack: void 0,
62
- __type: 'Error'
63
- };
64
- return {
65
- message: error.message,
66
- stack: error.stack,
67
- __type: 'Error',
68
- ...'Error' !== error.name ? {
69
- __subType: error.name
70
- } : {}
71
- };
72
- }
73
- return error;
74
- }
2
+ import { notFound } from "@tanstack/react-router";
3
+ import { serializeTanstackRscFlightValues } from "./flightSerialization.mjs";
4
+ import { isSerializedNotFound, serializePayloadError, toRouteErrors, toRouteLoaderData } from "./payloadErrors.mjs";
5
+ import { __setTanstackRscPayloadDecoderForTests, createPayloadFetchKey, fetchTanstackRscPayload, payloadFetchCache } from "./payloadFetch.mjs";
6
+ import { toPayloadRoute, toPlainLocation } from "./payloadRoutes.mjs";
75
7
  function createTanstackRscServerPayload(router, options = {}) {
76
8
  const matches = Array.isArray(router.state?.matches) ? router.state.matches : [];
77
9
  const routes = [];
@@ -81,7 +13,7 @@ function createTanstackRscServerPayload(router, options = {}) {
81
13
  const payloadRoute = toPayloadRoute(match);
82
14
  if (payloadRoute) {
83
15
  routes.push(payloadRoute);
84
- if ('loaderData' in match && void 0 !== match.loaderData && !(options.omitClientLoaderData && payloadRoute.hasClientLoader)) loaderData[payloadRoute.id] = match.loaderData;
16
+ if ('loaderData' in match && void 0 !== match.loaderData && !(options.omitClientLoaderData && payloadRoute.hasClientLoader)) loaderData[payloadRoute.id] = serializeTanstackRscFlightValues(match.loaderData);
85
17
  if (void 0 !== match.error) errors[payloadRoute.id] = serializePayloadError(match.error);
86
18
  }
87
19
  }
@@ -90,14 +22,17 @@ function createTanstackRscServerPayload(router, options = {}) {
90
22
  actionData: null,
91
23
  errors: Object.keys(errors).length > 0 ? errors : null,
92
24
  loaderData,
93
- location: router.state?.location,
25
+ location: toPlainLocation(router.state?.location),
94
26
  routes
95
27
  };
96
28
  }
97
29
  function handleTanstackRscRedirect(headers, basename, status) {
98
30
  const newHeaders = new Headers(headers);
99
31
  let redirectUrl = headers.get('Location') || '/';
100
- if ('/' !== basename) redirectUrl = redirectUrl.replace(basename, '') || '/';
32
+ if ('/' !== basename) {
33
+ if (redirectUrl === basename) redirectUrl = '/';
34
+ else if (redirectUrl.startsWith(`${basename}/`)) redirectUrl = redirectUrl.slice(basename.length);
35
+ }
101
36
  newHeaders.set('X-Modernjs-Redirect', redirectUrl);
102
37
  newHeaders.set('X-Modernjs-BaseUrl', basename);
103
38
  newHeaders.delete('Location');
@@ -109,69 +44,17 @@ function handleTanstackRscRedirect(headers, basename, status) {
109
44
  function isTanstackRscPayloadNavigationEnabled() {
110
45
  return "u" > typeof window;
111
46
  }
112
- async function decodePayload(stream) {
113
- if (payloadDecoder) return payloadDecoder(stream);
114
- const runtime = await import("@modern-js/runtime/rsc/client");
115
- return runtime.createFromReadableStream(stream);
116
- }
117
- function isServerPayload(value) {
118
- return Boolean(value) && 'object' == typeof value && 'render' === value.type && Array.isArray(value.routes);
119
- }
120
- function createPayloadFetchKey(request) {
121
- return request.url;
122
- }
123
- function isAbsoluteUrl(value) {
124
- try {
125
- new URL(value);
126
- return true;
127
- } catch {
128
- return false;
129
- }
130
- }
131
- async function fetchTanstackRscPayload(request) {
132
- const headers = new Headers(request.headers);
133
- headers.set('x-rsc-tree', 'true');
134
- const response = await fetch(request.url, {
135
- credentials: 'same-origin',
136
- headers,
137
- method: 'GET',
138
- signal: request.signal
139
- });
140
- const redirectLocation = response.headers.get('X-Modernjs-Redirect');
141
- if (redirectLocation) {
142
- if (isAbsoluteUrl(redirectLocation)) throw redirect({
143
- href: redirectLocation
144
- });
145
- throw redirect({
146
- to: redirectLocation || '/'
147
- });
148
- }
149
- if (404 === response.status && !response.body) throw notFound();
150
- if (!response.body) throw new Error('TanStack RSC payload response body is null.');
151
- const payload = await decodePayload(response.body);
152
- if (!isServerPayload(payload)) throw new Error('Unexpected TanStack RSC payload type.');
153
- return payload;
154
- }
155
47
  function loadTanstackRscPayload(request) {
156
48
  const key = createPayloadFetchKey(request);
157
49
  let payloadPromise = payloadFetchCache.get(key);
158
50
  if (!payloadPromise) {
159
51
  payloadPromise = fetchTanstackRscPayload(request).finally(()=>{
160
- payloadFetchCache.delete(key);
52
+ payloadFetchCache["delete"](key);
161
53
  });
162
54
  payloadFetchCache.set(key, payloadPromise);
163
55
  }
164
56
  return payloadPromise;
165
57
  }
166
- function isSerializedNotFound(value) {
167
- return Boolean(value) && 'object' == typeof value && true === value.isNotFound;
168
- }
169
- function toRouteErrors(payload) {
170
- return payload.errors && 'object' == typeof payload.errors ? payload.errors : {};
171
- }
172
- function toRouteLoaderData(payload) {
173
- return payload.loaderData && 'object' == typeof payload.loaderData ? payload.loaderData : {};
174
- }
175
58
  async function loadTanstackRscRouteData({ hasClientLoader, loadClientData, request, routeId }) {
176
59
  if (hasClientLoader) return loadClientData();
177
60
  if (!routeId) return loadClientData();
@@ -190,8 +73,4 @@ async function loadTanstackRscRouteData({ hasClientLoader, loadClientData, reque
190
73
  const payloadRoute = payload.routes.find((route)=>route.id === routeId);
191
74
  if (payloadRoute && payloadRoute.hasClientLoader) return loadClientData();
192
75
  }
193
- function __setTanstackRscPayloadDecoderForTests(decoder) {
194
- payloadDecoder = decoder;
195
- payloadFetchCache.clear();
196
- }
197
76
  export { __setTanstackRscPayloadDecoderForTests, createTanstackRscServerPayload, handleTanstackRscRedirect, isTanstackRscPayloadNavigationEnabled, loadTanstackRscPayload, loadTanstackRscRouteData };
@@ -0,0 +1,51 @@
1
+ import "node:module";
2
+ function toPlainRecord(value) {
3
+ if (!value || 'object' != typeof value) return {};
4
+ return Object.fromEntries(Object.entries(value));
5
+ }
6
+ function toPlainLocation(location) {
7
+ if (!location || 'object' != typeof location) return location;
8
+ const plainLocation = toPlainRecord(location);
9
+ for (const key of [
10
+ 'search',
11
+ 'state'
12
+ ])if (plainLocation[key] && 'object' == typeof plainLocation[key] && !Array.isArray(plainLocation[key])) plainLocation[key] = toPlainRecord(plainLocation[key]);
13
+ return plainLocation;
14
+ }
15
+ function getRouteId(match) {
16
+ const routeId = match.routeId ?? match.route?.id ?? match.id;
17
+ return 'string' == typeof routeId ? routeId : void 0;
18
+ }
19
+ function getRouteStaticData(match) {
20
+ return match.route?.options?.staticData || {};
21
+ }
22
+ function getRouteParentId(match) {
23
+ const parentId = match.route?.parentRoute?.id;
24
+ return 'string' == typeof parentId ? parentId : void 0;
25
+ }
26
+ function toRoutePath(match) {
27
+ const path = match.route?.options?.path;
28
+ return 'string' == typeof path ? path : void 0;
29
+ }
30
+ function toPayloadRoute(match) {
31
+ const routeId = getRouteId(match);
32
+ if (!routeId) return;
33
+ const staticData = getRouteStaticData(match);
34
+ const params = match.params && 'object' == typeof match.params ? toPlainRecord(match.params) : {};
35
+ const pathname = 'string' == typeof match.pathname ? match.pathname : '';
36
+ return {
37
+ handle: staticData.modernRouteHandle,
38
+ hasAction: Boolean(staticData.modernRouteHasAction || staticData.modernRouteAction),
39
+ hasErrorBoundary: false,
40
+ hasLoader: Boolean(staticData.modernRouteHasLoader),
41
+ hasClientLoader: Boolean(staticData.modernRouteHasClientLoader),
42
+ id: routeId,
43
+ index: Boolean(match.route?.options?.index) || void 0,
44
+ params,
45
+ parentId: getRouteParentId(match),
46
+ path: toRoutePath(match),
47
+ pathname,
48
+ pathnameBase: 'string' == typeof match.pathnameBase ? match.pathnameBase : pathname
49
+ };
50
+ }
51
+ export { getRouteId, toPayloadRoute, toPlainLocation };
@@ -0,0 +1,15 @@
1
+ import "node:module";
2
+ import { storage } from "@modern-js/runtime-utils/node";
3
+ const setTanstackRscServerPayload = (payload)=>{
4
+ const storageContext = storage.useContext?.();
5
+ if (storageContext) storageContext.serverPayload = payload;
6
+ };
7
+ const setTanstackRscRouter = (router)=>{
8
+ const storageContext = storage.useContext?.();
9
+ if (storageContext) storageContext.tanstackRscRouter = router;
10
+ };
11
+ const getTanstackRscRouter = ()=>{
12
+ const storageContext = storage.useContext?.();
13
+ return storageContext?.tanstackRscRouter;
14
+ };
15
+ export { getTanstackRscRouter, setTanstackRscRouter, setTanstackRscServerPayload };
@@ -1,27 +1,12 @@
1
1
  import "node:module";
2
2
  import { Fragment, jsx } from "react/jsx-runtime";
3
- import { createFromReadableStream, renderRsc } from "@modern-js/runtime/rsc/server";
4
- import { createSerializationAdapter } from "@tanstack/react-router";
5
- import { RawStream } from "@tanstack/router-core";
6
- import { createElement, use } from "react";
3
+ import { renderRsc } from "@modern-js/runtime/rsc/server";
4
+ import { RawStream, createSerializationAdapter } from "@tanstack/router-core";
5
+ import { createElement } from "react";
7
6
  import { ClientSlot } from "./ClientSlot.mjs";
8
- import { createRscProxy } from "./createRscProxy.mjs";
9
7
  import { ReplayableStream } from "./ReplayableStream.mjs";
10
8
  import { sanitizeSlotArgs } from "./slotUsageSanitizer.mjs";
11
- import { RSC_SLOT_USAGES_STREAM, SERVER_COMPONENT_STREAM, isRenderableServerComponent, isServerComponent } from "./symbols.mjs";
12
- function createTreeGetter(stream) {
13
- let ready = false;
14
- let tree;
15
- const treePromise = Promise.resolve(createFromReadableStream(stream.createReplayStream())).then((value)=>{
16
- tree = value;
17
- ready = true;
18
- return value;
19
- });
20
- return ()=>{
21
- if (ready) return tree;
22
- return use(treePromise);
23
- };
24
- }
9
+ import { RENDERABLE_RSC, RSC_SLOT_USAGES_STREAM, SERVER_COMPONENT_STREAM, isRenderableServerComponent, isServerComponent } from "./symbols.mjs";
25
10
  function createSlotProxy(options) {
26
11
  const cache = new Map();
27
12
  return new Proxy({}, {
@@ -45,6 +30,17 @@ function createSlotProxy(options) {
45
30
  }
46
31
  });
47
32
  }
33
+ function createServerRscValue(options) {
34
+ return {
35
+ [SERVER_COMPONENT_STREAM]: options.stream,
36
+ ...options.renderable ? {
37
+ [RENDERABLE_RSC]: true
38
+ } : {},
39
+ ...options.slotUsagesStream ? {
40
+ [RSC_SLOT_USAGES_STREAM]: options.slotUsagesStream
41
+ } : {}
42
+ };
43
+ }
48
44
  function toReplayableFlightStream(node, handlers) {
49
45
  const flightStream = renderRsc({
50
46
  element: node
@@ -58,7 +54,7 @@ async function renderServerComponent(node) {
58
54
  const streamWrapper = {
59
55
  createReplayStream: ()=>stream.createReplayStream()
60
56
  };
61
- return createRscProxy(createTreeGetter(streamWrapper), {
57
+ return createServerRscValue({
62
58
  renderable: true,
63
59
  stream: streamWrapper
64
60
  });
@@ -87,7 +83,7 @@ async function createCompositeComponent(component) {
87
83
  const streamWrapper = {
88
84
  createReplayStream: ()=>flightStream.createReplayStream()
89
85
  };
90
- return createRscProxy(createTreeGetter(streamWrapper), {
86
+ return createServerRscValue({
91
87
  renderable: false,
92
88
  slotUsagesStream: slotUsagesEmitter?.stream,
93
89
  stream: streamWrapper
@@ -0,0 +1,22 @@
1
+ import "node:module";
2
+ import { RSC_PROXY_GET_TREE, RSC_PROXY_PATH } from "./symbols.mjs";
3
+ const TANSTACK_RSC_FLIGHT_VALUE = '__modernTanstackRsc';
4
+ function isSerializedTanstackRscFlightValue(value) {
5
+ return Boolean(value) && 'object' == typeof value && true === value[TANSTACK_RSC_FLIGHT_VALUE] && ('renderable' === value.kind || 'composite' === value.kind) && value.stream instanceof ReadableStream;
6
+ }
7
+ function EmptyFallback() {
8
+ return null;
9
+ }
10
+ function selectTreePath(tree, path) {
11
+ let current = tree;
12
+ for (const key of path){
13
+ if (null === current || 'object' != typeof current) return null;
14
+ current = current[key];
15
+ }
16
+ return current;
17
+ }
18
+ function renderSelectedTree(src, { getTree = src[RSC_PROXY_GET_TREE], missingTreeError }) {
19
+ if (!getTree) throw new Error(missingTreeError);
20
+ return selectTreePath(getTree(), src[RSC_PROXY_PATH] || []);
21
+ }
22
+ export { EmptyFallback, TANSTACK_RSC_FLIGHT_VALUE, isSerializedTanstackRscFlightValue, renderSelectedTree };
@@ -14,6 +14,7 @@ function sanitizeSlotArg(value, seen) {
14
14
  if (null == value) return value;
15
15
  if ('string' == typeof value || 'number' == typeof value || 'boolean' == typeof value || 'bigint' == typeof value) return value;
16
16
  if ('object' != typeof value && 'function' != typeof value) return String(value);
17
+ if ('function' == typeof value) return '[Function]';
17
18
  if (seen.has(value)) return '[Circular]';
18
19
  seen.add(value);
19
20
  if (Array.isArray(value)) return value.map((item)=>sanitizeSlotArg(item, seen));
@@ -0,0 +1,35 @@
1
+ import "node:module";
2
+ "use client";
3
+ import { use } from "react";
4
+ let modernRscClientRuntimePromise;
5
+ function loadModernRscClientRuntime() {
6
+ modernRscClientRuntimePromise ??= import("@modern-js/runtime/rsc/client").then((runtime)=>{
7
+ runtime.setServerCallback(runtime.callServer);
8
+ return runtime;
9
+ });
10
+ return modernRscClientRuntimePromise;
11
+ }
12
+ async function createFromFlightStream(stream) {
13
+ if ("u" < typeof window) {
14
+ const runtime = await import("react-server-dom-rspack/client.edge");
15
+ return runtime.createFromReadableStream(stream);
16
+ }
17
+ const runtime = await loadModernRscClientRuntime();
18
+ return runtime.createFromReadableStream(stream, {
19
+ callServer: runtime.callServer
20
+ });
21
+ }
22
+ function createTreeGetterFromFlightStream(stream) {
23
+ let ready = false;
24
+ let tree;
25
+ const treePromise = createFromFlightStream(stream).then((value)=>{
26
+ tree = value;
27
+ ready = true;
28
+ return value;
29
+ });
30
+ return ()=>{
31
+ if (ready) return tree;
32
+ return use(treePromise);
33
+ };
34
+ }
35
+ export { createTreeGetterFromFlightStream };
@@ -0,0 +1,10 @@
1
+ import "node:module";
2
+ function isAbsoluteUrl(value) {
3
+ try {
4
+ new URL(value);
5
+ return true;
6
+ } catch {
7
+ return false;
8
+ }
9
+ }
10
+ export { isAbsoluteUrl };
@@ -0,0 +1,54 @@
1
+ import "node:module";
2
+ async function waitForRouterSerialization(tanstackRouter) {
3
+ const serverSsr = tanstackRouter.serverSsr;
4
+ if (!serverSsr || 'function' != typeof serverSsr.onSerializationFinished || serverSsr.isSerializationFinished?.()) return;
5
+ await new Promise((resolve)=>{
6
+ serverSsr.onSerializationFinished?.(resolve);
7
+ });
8
+ }
9
+ function htmlEscapeAttr(value) {
10
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;');
11
+ }
12
+ function routerManagedTagToHtml(tag) {
13
+ if (!tag || 'object' != typeof tag) return '';
14
+ const managedTag = tag;
15
+ if (!managedTag || "script" !== managedTag.tag) return '';
16
+ const attrs = managedTag.attrs || {};
17
+ const attrsStr = Object.entries(attrs).filter(([, v])=>null != v && false !== v).map(([k, v])=>{
18
+ const name = 'className' === k ? 'class' : k;
19
+ if (true === v) return name;
20
+ return `${name}="${htmlEscapeAttr(String(v))}"`;
21
+ }).join(' ');
22
+ const open = attrsStr.length ? `<script ${attrsStr}>` : "<script>";
23
+ const children = 'string' == typeof managedTag.children ? managedTag.children : '';
24
+ return `${open}${children}</script>`;
25
+ }
26
+ function routerManagedTagsToHtml(tags) {
27
+ const normalizedTags = Array.isArray(tags) ? tags : [
28
+ tags
29
+ ];
30
+ return normalizedTags.map(routerManagedTagToHtml).filter(Boolean);
31
+ }
32
+ function createGetSsrHref(request) {
33
+ const url = new URL(request.url);
34
+ return `${url.pathname}${url.search}${url.hash}`;
35
+ }
36
+ function collectRouterErrors(tanstackRouter) {
37
+ const state = tanstackRouter.state;
38
+ const matches = Array.isArray(state.matches) ? state.matches : [];
39
+ const errors = matches.reduce((acc, match)=>{
40
+ if (!match.error) return acc;
41
+ const routeId = 'string' == typeof match.routeId ? match.routeId : 'string' == typeof match.route?.id ? match.route.id : `match-${Object.keys(acc).length}`;
42
+ acc[routeId] = match.error;
43
+ return acc;
44
+ }, {});
45
+ return Object.keys(errors).length > 0 ? errors : void 0;
46
+ }
47
+ async function attachServerSsrUtils(router) {
48
+ const { attachRouterServerSsrUtils } = await import("@tanstack/router-core/ssr/server");
49
+ attachRouterServerSsrUtils({
50
+ router,
51
+ manifest: void 0
52
+ });
53
+ }
54
+ export { attachServerSsrUtils, collectRouterErrors, createGetSsrHref, routerManagedTagsToHtml, waitForRouterSerialization };
@@ -0,0 +1,52 @@
1
+ import "node:module";
2
+ function isPromiseLike(value) {
3
+ return Boolean(value && 'function' == typeof value.then);
4
+ }
5
+ function isPreloadableRouteComponent(component) {
6
+ if (!component || 'function' != typeof component) return false;
7
+ const preloadable = component;
8
+ return 'function' == typeof preloadable.load || 'function' == typeof preloadable.preload;
9
+ }
10
+ function isReactLazyRouteComponent(component) {
11
+ return null != component && 'object' == typeof component && 'function' == typeof component._init && '_payload' in component;
12
+ }
13
+ async function preloadReactLazyRouteComponent(component) {
14
+ try {
15
+ component._init?.(component._payload);
16
+ } catch (thrown) {
17
+ if (!isPromiseLike(thrown)) throw thrown;
18
+ await thrown;
19
+ component._init?.(component._payload);
20
+ }
21
+ }
22
+ async function preloadRouteComponent(component, preloadedComponents) {
23
+ if (isReactLazyRouteComponent(component)) {
24
+ if (preloadedComponents.has(component)) return;
25
+ preloadedComponents.add(component);
26
+ await preloadReactLazyRouteComponent(component);
27
+ return;
28
+ }
29
+ if (!isPreloadableRouteComponent(component)) return;
30
+ if (preloadedComponents.has(component)) return;
31
+ preloadedComponents.add(component);
32
+ if ('function' == typeof component.load) return void await component.load({});
33
+ await component.preload?.({});
34
+ }
35
+ async function preloadMatchedRouteComponents(tanstackRouter) {
36
+ const matches = Array.isArray(tanstackRouter.state.matches) ? tanstackRouter.state.matches : [];
37
+ const routesById = tanstackRouter.routesById || {};
38
+ const preloadedComponents = new Set();
39
+ await Promise.all(matches.map(async (match)=>{
40
+ const routeId = 'string' == typeof match.routeId ? match.routeId : 'string' == typeof match.route?.id ? match.route.id : void 0;
41
+ const route = routeId ? routesById[routeId] : match.route;
42
+ const options = route?.options;
43
+ if (!options) return;
44
+ await Promise.all([
45
+ preloadRouteComponent(options.component, preloadedComponents),
46
+ preloadRouteComponent(options.pendingComponent, preloadedComponents),
47
+ preloadRouteComponent(options.errorComponent, preloadedComponents),
48
+ preloadRouteComponent(options.notFoundComponent, preloadedComponents)
49
+ ]);
50
+ }));
51
+ }
52
+ export { preloadMatchedRouteComponents };
@@ -0,0 +1 @@
1
+ import "node:module";
@@ -2,7 +2,7 @@ import "node:module";
2
2
  import { getRouterRuntimeState } from "@modern-js/runtime/context";
3
3
  function getTanstackRouterState(context) {
4
4
  const state = getRouterRuntimeState(context);
5
- if (void 0 === state || 'tanstack' !== state.framework) return;
5
+ if (void 0 === state || 'tanstack' !== state.framework || void 0 === state.instance) return;
6
6
  return state;
7
7
  }
8
8
  export { getTanstackRouterState };