@bleedingdev/modern-js-plugin-tanstack 3.5.0-ultramodern.4 → 3.5.0-ultramodern.41

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