@modern-js/runtime 2.67.11 → 2.68.1
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.
- package/dist/cjs/cli/alias.js +1 -1
- package/dist/cjs/cli/code.js +6 -3
- package/dist/cjs/cli/index.js +4 -2
- package/dist/cjs/cli/ssr/index.js +1 -1
- package/dist/cjs/cli/template.js +72 -21
- package/dist/cjs/cli/template.server.js +62 -13
- package/dist/cjs/core/context/index.js +19 -2
- package/dist/cjs/core/context/serverPayload.server.js +40 -0
- package/dist/cjs/core/server/requestHandler.js +47 -11
- package/dist/cjs/router/cli/code/index.js +13 -6
- package/dist/cjs/router/cli/code/templates.js +44 -20
- package/dist/cjs/router/cli/handler.js +17 -2
- package/dist/cjs/router/cli/index.js +6 -5
- package/dist/cjs/router/index.js +0 -21
- package/dist/cjs/router/internal.js +30 -0
- package/dist/cjs/router/runtime/PrefetchLink.js +3 -4
- package/dist/cjs/router/runtime/constants.js +2 -2
- package/dist/cjs/router/runtime/index.js +1 -12
- package/dist/cjs/router/runtime/internal.js +36 -0
- package/dist/cjs/router/runtime/plugin.js +135 -76
- package/dist/cjs/router/runtime/plugin.node.js +73 -32
- package/dist/cjs/router/runtime/rsc-router.js +322 -0
- package/dist/cjs/router/runtime/utils.js +76 -1
- package/dist/cjs/rsc/client.js +11 -0
- package/dist/cjs/rsc/server.js +40 -0
- package/dist/esm/cli/alias.js +1 -1
- package/dist/esm/cli/code.js +6 -3
- package/dist/esm/cli/index.js +4 -2
- package/dist/esm/cli/ssr/index.js +1 -1
- package/dist/esm/cli/template.js +25 -6
- package/dist/esm/cli/template.server.js +3 -3
- package/dist/esm/core/context/index.js +14 -1
- package/dist/esm/core/context/serverPayload.server.js +15 -0
- package/dist/esm/core/server/requestHandler.js +102 -9
- package/dist/esm/router/cli/code/index.js +12 -6
- package/dist/esm/router/cli/code/templates.js +29 -13
- package/dist/esm/router/cli/handler.js +33 -6
- package/dist/esm/router/cli/index.js +6 -5
- package/dist/esm/router/index.js +0 -6
- package/dist/esm/router/internal.js +5 -0
- package/dist/esm/router/runtime/PrefetchLink.js +2 -2
- package/dist/esm/router/runtime/constants.js +2 -2
- package/dist/esm/router/runtime/index.js +1 -9
- package/dist/esm/router/runtime/internal.js +10 -0
- package/dist/esm/router/runtime/plugin.js +135 -85
- package/dist/esm/router/runtime/plugin.node.js +110 -40
- package/dist/esm/router/runtime/rsc-router.js +437 -0
- package/dist/esm/router/runtime/utils.js +95 -1
- package/dist/esm/rsc/client.js +6 -0
- package/dist/esm/rsc/server.js +86 -0
- package/dist/esm-node/cli/alias.js +1 -1
- package/dist/esm-node/cli/code.js +6 -3
- package/dist/esm-node/cli/index.js +4 -2
- package/dist/esm-node/cli/ssr/index.js +1 -1
- package/dist/esm-node/cli/template.js +72 -21
- package/dist/esm-node/cli/template.server.js +62 -13
- package/dist/esm-node/core/context/index.js +14 -1
- package/dist/esm-node/core/context/serverPayload.server.js +15 -0
- package/dist/esm-node/core/server/requestHandler.js +45 -9
- package/dist/esm-node/router/cli/code/index.js +12 -6
- package/dist/esm-node/router/cli/code/templates.js +44 -20
- package/dist/esm-node/router/cli/handler.js +17 -2
- package/dist/esm-node/router/cli/index.js +6 -5
- package/dist/esm-node/router/index.js +0 -6
- package/dist/esm-node/router/internal.js +5 -0
- package/dist/esm-node/router/runtime/PrefetchLink.js +2 -2
- package/dist/esm-node/router/runtime/constants.js +2 -2
- package/dist/esm-node/router/runtime/index.js +1 -9
- package/dist/esm-node/router/runtime/internal.js +10 -0
- package/dist/esm-node/router/runtime/plugin.js +127 -78
- package/dist/esm-node/router/runtime/plugin.node.js +75 -34
- package/dist/esm-node/router/runtime/rsc-router.js +284 -0
- package/dist/esm-node/router/runtime/utils.js +74 -1
- package/dist/esm-node/rsc/client.js +6 -0
- package/dist/esm-node/rsc/server.js +35 -0
- package/dist/types/cli/template.d.ts +4 -2
- package/dist/types/cli/template.server.d.ts +2 -1
- package/dist/types/common.d.ts +1 -1
- package/dist/types/config.d.ts +1 -1
- package/dist/types/core/context/index.d.ts +38 -3
- package/dist/types/core/context/serverPayload.server.d.ts +3 -0
- package/dist/types/core/server/requestHandler.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/router/cli/code/index.d.ts +1 -0
- package/dist/types/router/cli/code/templates.d.ts +4 -2
- package/dist/types/router/index.d.ts +0 -2
- package/dist/types/router/internal.d.ts +2 -0
- package/dist/types/router/runtime/PrefetchLink.d.ts +0 -1
- package/dist/types/router/runtime/constants.d.ts +3 -3
- package/dist/types/router/runtime/index.d.ts +1 -9
- package/dist/types/router/runtime/internal.d.ts +8 -0
- package/dist/types/router/runtime/rsc-router.d.ts +14 -0
- package/dist/types/router/runtime/utils.d.ts +26 -5
- package/dist/types/rsc/client.d.ts +1 -0
- package/dist/types/rsc/server.d.ts +1 -0
- package/package.json +22 -13
- package/static/modern-inline.js +1 -1
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
5
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
6
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
7
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
8
|
+
import { ElementsContext, createFromReadableStream } from "@modern-js/render/client";
|
|
9
|
+
import { StaticRouterProvider, createStaticRouter } from "@modern-js/runtime-utils/node/router";
|
|
10
|
+
import { createBrowserRouter, redirect } from "@modern-js/runtime-utils/router";
|
|
11
|
+
import React from "react";
|
|
12
|
+
var safeUse = function(promise) {
|
|
13
|
+
if (typeof React.use === "function") {
|
|
14
|
+
return React.use(promise);
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
};
|
|
18
|
+
var createServerPayload = function(routerContext, routes) {
|
|
19
|
+
return {
|
|
20
|
+
type: "render",
|
|
21
|
+
actionData: routerContext.actionData,
|
|
22
|
+
errors: routerContext.errors,
|
|
23
|
+
loaderData: routerContext.loaderData,
|
|
24
|
+
location: routerContext.location,
|
|
25
|
+
routes: routerContext.matches.map(function(match, index, matches) {
|
|
26
|
+
var element = match.route.element;
|
|
27
|
+
var parentMatch = index > 0 ? matches[index - 1] : void 0;
|
|
28
|
+
var processedElement;
|
|
29
|
+
if (element) {
|
|
30
|
+
var _routerContext_loaderData, _routerContext_actionData;
|
|
31
|
+
var ElementComponent = element.type;
|
|
32
|
+
processedElement = /* @__PURE__ */ React.createElement(ElementComponent, {
|
|
33
|
+
loaderData: routerContext === null || routerContext === void 0 ? void 0 : (_routerContext_loaderData = routerContext.loaderData) === null || _routerContext_loaderData === void 0 ? void 0 : _routerContext_loaderData[match.route.id],
|
|
34
|
+
actionData: routerContext === null || routerContext === void 0 ? void 0 : (_routerContext_actionData = routerContext.actionData) === null || _routerContext_actionData === void 0 ? void 0 : _routerContext_actionData[match.route.id],
|
|
35
|
+
params: match.params,
|
|
36
|
+
matches: routerContext.matches.map(function(m) {
|
|
37
|
+
var _routerContext_loaderData2;
|
|
38
|
+
var route = m.route, pathname = m.pathname, params = m.params;
|
|
39
|
+
return {
|
|
40
|
+
id: route.id,
|
|
41
|
+
pathname,
|
|
42
|
+
params,
|
|
43
|
+
data: routerContext === null || routerContext === void 0 ? void 0 : (_routerContext_loaderData2 = routerContext.loaderData) === null || _routerContext_loaderData2 === void 0 ? void 0 : _routerContext_loaderData2[route.id],
|
|
44
|
+
handle: route.handle
|
|
45
|
+
};
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
element: processedElement,
|
|
51
|
+
errorElement: match.route.errorElement,
|
|
52
|
+
handle: match.route.handle,
|
|
53
|
+
hasAction: !!match.route.action,
|
|
54
|
+
hasErrorBoundary: !!match.route.hasErrorBoundary,
|
|
55
|
+
hasLoader: !!match.route.loader,
|
|
56
|
+
hasClientLoader: !!match.route.hasClientLoader,
|
|
57
|
+
id: match.route.id,
|
|
58
|
+
index: match.route.index,
|
|
59
|
+
params: match.params,
|
|
60
|
+
parentId: (parentMatch === null || parentMatch === void 0 ? void 0 : parentMatch.route.id) || match.route.parentId,
|
|
61
|
+
path: match.route.path,
|
|
62
|
+
pathname: match.pathname,
|
|
63
|
+
pathnameBase: match.pathnameBase
|
|
64
|
+
};
|
|
65
|
+
})
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
var handleRSCRedirect = function(headers, basename, status) {
|
|
69
|
+
var newHeaders = new Headers(headers);
|
|
70
|
+
var redirectUrl = headers.get("Location");
|
|
71
|
+
if (basename !== "/") {
|
|
72
|
+
redirectUrl = redirectUrl.replace(basename, "");
|
|
73
|
+
}
|
|
74
|
+
newHeaders.set("X-Modernjs-Redirect", redirectUrl);
|
|
75
|
+
newHeaders.set("X-Modernjs-BaseUrl", basename);
|
|
76
|
+
newHeaders.delete("Location");
|
|
77
|
+
return new Response(null, {
|
|
78
|
+
status,
|
|
79
|
+
headers: newHeaders
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
var prepareRSCRoutes = function() {
|
|
83
|
+
var _ref = _async_to_generator(function(routes) {
|
|
84
|
+
var isLazyComponent, processRoutes;
|
|
85
|
+
return _ts_generator(this, function(_state) {
|
|
86
|
+
switch (_state.label) {
|
|
87
|
+
case 0:
|
|
88
|
+
isLazyComponent = function(component) {
|
|
89
|
+
return component && (typeof component === "undefined" ? "undefined" : _type_of(component)) === "object" && component._init !== void 0 && component._payload !== void 0;
|
|
90
|
+
};
|
|
91
|
+
processRoutes = function() {
|
|
92
|
+
var _ref2 = _async_to_generator(function(routesList) {
|
|
93
|
+
return _ts_generator(this, function(_state2) {
|
|
94
|
+
switch (_state2.label) {
|
|
95
|
+
case 0:
|
|
96
|
+
return [
|
|
97
|
+
4,
|
|
98
|
+
Promise.all(routesList.map(function() {
|
|
99
|
+
var _ref3 = _async_to_generator(function(route) {
|
|
100
|
+
return _ts_generator(this, function(_state3) {
|
|
101
|
+
switch (_state3.label) {
|
|
102
|
+
case 0:
|
|
103
|
+
if (!("lazyImport" in route && isLazyComponent(route.component)))
|
|
104
|
+
return [
|
|
105
|
+
3,
|
|
106
|
+
2
|
|
107
|
+
];
|
|
108
|
+
return [
|
|
109
|
+
4,
|
|
110
|
+
route.lazyImport()
|
|
111
|
+
];
|
|
112
|
+
case 1:
|
|
113
|
+
route.component = _state3.sent().default;
|
|
114
|
+
_state3.label = 2;
|
|
115
|
+
case 2:
|
|
116
|
+
if (!(route.children && Array.isArray(route.children)))
|
|
117
|
+
return [
|
|
118
|
+
3,
|
|
119
|
+
4
|
|
120
|
+
];
|
|
121
|
+
return [
|
|
122
|
+
4,
|
|
123
|
+
processRoutes(route.children)
|
|
124
|
+
];
|
|
125
|
+
case 3:
|
|
126
|
+
_state3.sent();
|
|
127
|
+
_state3.label = 4;
|
|
128
|
+
case 4:
|
|
129
|
+
return [
|
|
130
|
+
2
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
return function(route) {
|
|
136
|
+
return _ref3.apply(this, arguments);
|
|
137
|
+
};
|
|
138
|
+
}()))
|
|
139
|
+
];
|
|
140
|
+
case 1:
|
|
141
|
+
_state2.sent();
|
|
142
|
+
return [
|
|
143
|
+
2
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
return function processRoutes2(routesList) {
|
|
149
|
+
return _ref2.apply(this, arguments);
|
|
150
|
+
};
|
|
151
|
+
}();
|
|
152
|
+
return [
|
|
153
|
+
4,
|
|
154
|
+
processRoutes(routes)
|
|
155
|
+
];
|
|
156
|
+
case 1:
|
|
157
|
+
_state.sent();
|
|
158
|
+
return [
|
|
159
|
+
2
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
return function prepareRSCRoutes2(routes) {
|
|
165
|
+
return _ref.apply(this, arguments);
|
|
166
|
+
};
|
|
167
|
+
}();
|
|
168
|
+
var mergeRoutes = function(routes, originalRoutes) {
|
|
169
|
+
if (!originalRoutes || !Array.isArray(originalRoutes)) {
|
|
170
|
+
return routes;
|
|
171
|
+
}
|
|
172
|
+
var routesMap = /* @__PURE__ */ new Map();
|
|
173
|
+
var buildRoutesMap = function(routesList) {
|
|
174
|
+
routesList.forEach(function(route) {
|
|
175
|
+
if (route.id) {
|
|
176
|
+
routesMap.set(route.id, route);
|
|
177
|
+
}
|
|
178
|
+
if ("children" in route && route.children && Array.isArray(route.children)) {
|
|
179
|
+
buildRoutesMap(route.children);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
buildRoutesMap(routes);
|
|
184
|
+
var mergeRoutesRecursive = function(origRoutes) {
|
|
185
|
+
return origRoutes.map(function(origRoute) {
|
|
186
|
+
if (origRoute.id && routesMap.has(origRoute.id)) {
|
|
187
|
+
var matchedRoute = routesMap.get(origRoute.id);
|
|
188
|
+
var result = _object_spread({
|
|
189
|
+
loader: origRoute.hasClientLoader ? origRoute.loader : void 0
|
|
190
|
+
}, matchedRoute);
|
|
191
|
+
if (origRoute.children && Array.isArray(origRoute.children)) {
|
|
192
|
+
result.children = mergeRoutesRecursive(origRoute.children);
|
|
193
|
+
}
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
return origRoute;
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
return mergeRoutesRecursive(originalRoutes);
|
|
200
|
+
};
|
|
201
|
+
var findRouteInTree = function(routes, routeId) {
|
|
202
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
203
|
+
try {
|
|
204
|
+
for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
205
|
+
var route = _step.value;
|
|
206
|
+
if (route.id === routeId) {
|
|
207
|
+
return route;
|
|
208
|
+
}
|
|
209
|
+
if (route.children && Array.isArray(route.children)) {
|
|
210
|
+
var found = findRouteInTree(route.children, routeId);
|
|
211
|
+
if (found) {
|
|
212
|
+
return found;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
} catch (err) {
|
|
217
|
+
_didIteratorError = true;
|
|
218
|
+
_iteratorError = err;
|
|
219
|
+
} finally {
|
|
220
|
+
try {
|
|
221
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
222
|
+
_iterator.return();
|
|
223
|
+
}
|
|
224
|
+
} finally {
|
|
225
|
+
if (_didIteratorError) {
|
|
226
|
+
throw _iteratorError;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
};
|
|
232
|
+
var createClientRouterFromPayload = function(payload, originalRoutes) {
|
|
233
|
+
var basename = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "";
|
|
234
|
+
var processedRoutes = payload.routes.reduceRight(function(previous, route) {
|
|
235
|
+
if (previous.length > 0) {
|
|
236
|
+
return [
|
|
237
|
+
_object_spread_props(_object_spread({}, route), {
|
|
238
|
+
children: previous
|
|
239
|
+
})
|
|
240
|
+
];
|
|
241
|
+
}
|
|
242
|
+
return [
|
|
243
|
+
route
|
|
244
|
+
];
|
|
245
|
+
}, []);
|
|
246
|
+
var mergedRoutes = mergeRoutes(processedRoutes, originalRoutes);
|
|
247
|
+
var router = createBrowserRouter(mergedRoutes, {
|
|
248
|
+
//@ts-ignore
|
|
249
|
+
hydrationData: payload,
|
|
250
|
+
basename,
|
|
251
|
+
dataStrategy: function() {
|
|
252
|
+
var _ref = _async_to_generator(function(context) {
|
|
253
|
+
var request, matches, results, clientMatches, fetchPromise, clientLoadersPromise, res, redirectLocation, _ref2, clientLoaderResults, _$payload;
|
|
254
|
+
return _ts_generator(this, function(_state) {
|
|
255
|
+
switch (_state.label) {
|
|
256
|
+
case 0:
|
|
257
|
+
request = context.request, matches = context.matches;
|
|
258
|
+
results = {};
|
|
259
|
+
clientMatches = matches.filter(function(match) {
|
|
260
|
+
return match.route.hasClientLoader;
|
|
261
|
+
});
|
|
262
|
+
fetchPromise = fetch(request.url, {
|
|
263
|
+
headers: {
|
|
264
|
+
"x-rsc-tree": "true"
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
clientLoadersPromise = clientMatches.length > 0 ? Promise.all(clientMatches.map(function() {
|
|
268
|
+
var _ref3 = _async_to_generator(function(clientMatch) {
|
|
269
|
+
var foundRoute, res2;
|
|
270
|
+
return _ts_generator(this, function(_state2) {
|
|
271
|
+
switch (_state2.label) {
|
|
272
|
+
case 0:
|
|
273
|
+
foundRoute = findRouteInTree(originalRoutes, clientMatch.route.id);
|
|
274
|
+
clientMatch.route.loader = foundRoute === null || foundRoute === void 0 ? void 0 : foundRoute.loader;
|
|
275
|
+
return [
|
|
276
|
+
4,
|
|
277
|
+
clientMatch.resolve()
|
|
278
|
+
];
|
|
279
|
+
case 1:
|
|
280
|
+
res2 = _state2.sent();
|
|
281
|
+
return [
|
|
282
|
+
2,
|
|
283
|
+
{
|
|
284
|
+
routeId: clientMatch.route.id,
|
|
285
|
+
result: res2
|
|
286
|
+
}
|
|
287
|
+
];
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
return function(clientMatch) {
|
|
292
|
+
return _ref3.apply(this, arguments);
|
|
293
|
+
};
|
|
294
|
+
}())) : Promise.resolve([]);
|
|
295
|
+
return [
|
|
296
|
+
4,
|
|
297
|
+
fetchPromise
|
|
298
|
+
];
|
|
299
|
+
case 1:
|
|
300
|
+
res = _state.sent();
|
|
301
|
+
redirectLocation = res.headers.get("X-Modernjs-Redirect");
|
|
302
|
+
if (redirectLocation) {
|
|
303
|
+
matches.forEach(function(match) {
|
|
304
|
+
var routeId = match.route.id;
|
|
305
|
+
if (routeId) {
|
|
306
|
+
results[routeId] = {
|
|
307
|
+
type: "redirect",
|
|
308
|
+
result: redirect(redirectLocation)
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
return [
|
|
313
|
+
2,
|
|
314
|
+
results
|
|
315
|
+
];
|
|
316
|
+
}
|
|
317
|
+
return [
|
|
318
|
+
4,
|
|
319
|
+
Promise.all([
|
|
320
|
+
clientLoadersPromise
|
|
321
|
+
])
|
|
322
|
+
];
|
|
323
|
+
case 2:
|
|
324
|
+
_ref2 = _sliced_to_array.apply(void 0, [
|
|
325
|
+
_state.sent(),
|
|
326
|
+
1
|
|
327
|
+
]), clientLoaderResults = _ref2[0];
|
|
328
|
+
clientLoaderResults.forEach(function(param) {
|
|
329
|
+
var routeId = param.routeId, result = param.result;
|
|
330
|
+
results[routeId] = result;
|
|
331
|
+
});
|
|
332
|
+
return [
|
|
333
|
+
4,
|
|
334
|
+
createFromReadableStream(res.body)
|
|
335
|
+
];
|
|
336
|
+
case 3:
|
|
337
|
+
_$payload = _state.sent();
|
|
338
|
+
if (typeof _$payload.type === "undefined" || _$payload.type !== "render") {
|
|
339
|
+
throw new Error("Unexpected payload type");
|
|
340
|
+
}
|
|
341
|
+
matches.forEach(function(match) {
|
|
342
|
+
var _payload_loaderData;
|
|
343
|
+
var routeId = match.route.id;
|
|
344
|
+
var matchedRoute = _$payload.routes.find(function(route) {
|
|
345
|
+
return route.id === routeId;
|
|
346
|
+
});
|
|
347
|
+
if (matchedRoute) {
|
|
348
|
+
router.patchRoutes(matchedRoute.parentId, [
|
|
349
|
+
matchedRoute
|
|
350
|
+
], true);
|
|
351
|
+
}
|
|
352
|
+
if ((_payload_loaderData = _$payload.loaderData) === null || _payload_loaderData === void 0 ? void 0 : _payload_loaderData[routeId]) {
|
|
353
|
+
results[routeId] = {
|
|
354
|
+
type: "data",
|
|
355
|
+
result: _$payload.loaderData[routeId]
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
return [
|
|
360
|
+
2,
|
|
361
|
+
results
|
|
362
|
+
];
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
return function(context) {
|
|
367
|
+
return _ref.apply(this, arguments);
|
|
368
|
+
};
|
|
369
|
+
}()
|
|
370
|
+
});
|
|
371
|
+
return router;
|
|
372
|
+
};
|
|
373
|
+
var createRSCStaticRouterComponent = function(payload, basename) {
|
|
374
|
+
var routerContext = {
|
|
375
|
+
actionData: payload.actionData,
|
|
376
|
+
actionHeaders: {},
|
|
377
|
+
activeDeferreds: {},
|
|
378
|
+
basename: basename || "",
|
|
379
|
+
errors: payload.errors,
|
|
380
|
+
loaderData: payload.loaderData,
|
|
381
|
+
loaderHeaders: {},
|
|
382
|
+
location: payload.location,
|
|
383
|
+
statusCode: 200,
|
|
384
|
+
matches: payload.routes.map(function(match) {
|
|
385
|
+
return {
|
|
386
|
+
params: match.params,
|
|
387
|
+
pathname: match.pathname,
|
|
388
|
+
pathnameBase: match.pathnameBase,
|
|
389
|
+
route: {
|
|
390
|
+
id: match.id,
|
|
391
|
+
action: match.hasAction || !!match.clientAction,
|
|
392
|
+
handle: match.handle,
|
|
393
|
+
hasErrorBoundary: match.hasErrorBoundary,
|
|
394
|
+
loader: match.hasLoader || !!match.clientLoader,
|
|
395
|
+
index: match.index,
|
|
396
|
+
path: match.path,
|
|
397
|
+
shouldRevalidate: match.shouldRevalidate
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
})
|
|
401
|
+
};
|
|
402
|
+
var processedRoutes = payload.routes.reduceRight(function(previous, route) {
|
|
403
|
+
if (previous.length > 0) {
|
|
404
|
+
return [
|
|
405
|
+
_object_spread_props(_object_spread({}, route), {
|
|
406
|
+
children: previous
|
|
407
|
+
})
|
|
408
|
+
];
|
|
409
|
+
}
|
|
410
|
+
return [
|
|
411
|
+
route
|
|
412
|
+
];
|
|
413
|
+
}, []);
|
|
414
|
+
var router = createStaticRouter(processedRoutes, routerContext);
|
|
415
|
+
return /* @__PURE__ */ _jsx(StaticRouterProvider, {
|
|
416
|
+
context: routerContext,
|
|
417
|
+
router,
|
|
418
|
+
hydrate: false
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
var RSCStaticRouter = function(param) {
|
|
422
|
+
var basename = param.basename;
|
|
423
|
+
var payload = safeUse(safeUse(ElementsContext));
|
|
424
|
+
if (!payload || payload.type !== "render") {
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
428
|
+
children: createRSCStaticRouterComponent(payload, basename)
|
|
429
|
+
});
|
|
430
|
+
};
|
|
431
|
+
export {
|
|
432
|
+
RSCStaticRouter,
|
|
433
|
+
createClientRouterFromPayload,
|
|
434
|
+
createServerPayload,
|
|
435
|
+
handleRSCRedirect,
|
|
436
|
+
prepareRSCRoutes
|
|
437
|
+
};
|
|
@@ -3,10 +3,12 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
|
3
3
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
4
|
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
|
|
5
5
|
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
6
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
6
7
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
7
8
|
import { renderNestedRoute } from "@modern-js/runtime-utils/browser";
|
|
8
9
|
import { UNSAFE_ErrorResponseImpl as ErrorResponseImpl } from "@modern-js/runtime-utils/remix-router";
|
|
9
10
|
import { Route, isRouteErrorResponse } from "@modern-js/runtime-utils/router";
|
|
11
|
+
import React from "react";
|
|
10
12
|
import { DefaultNotFound } from "./DefaultNotFound";
|
|
11
13
|
import DeferredDataScripts from "./DeferredDataScripts";
|
|
12
14
|
function getRouteComponents(routes, param) {
|
|
@@ -64,6 +66,96 @@ function getRouteComponents(routes, param) {
|
|
|
64
66
|
}, "*"));
|
|
65
67
|
return routeElements;
|
|
66
68
|
}
|
|
69
|
+
function getRouteObjects(routes, param) {
|
|
70
|
+
var globalApp = param.globalApp, ssrMode = param.ssrMode, props = param.props;
|
|
71
|
+
var createLayoutElement = function(Component) {
|
|
72
|
+
var GlobalLayout = globalApp;
|
|
73
|
+
if (!GlobalLayout) {
|
|
74
|
+
return Component;
|
|
75
|
+
}
|
|
76
|
+
var LayoutWrapper = function(props2) {
|
|
77
|
+
var LayoutComponent2 = GlobalLayout;
|
|
78
|
+
return /* @__PURE__ */ _jsx(LayoutComponent2, _object_spread({
|
|
79
|
+
Component
|
|
80
|
+
}, props2));
|
|
81
|
+
};
|
|
82
|
+
return LayoutWrapper;
|
|
83
|
+
};
|
|
84
|
+
var routeObjects = [];
|
|
85
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
86
|
+
try {
|
|
87
|
+
for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
88
|
+
var route = _step.value;
|
|
89
|
+
if (route.type === "nested") {
|
|
90
|
+
var nestedRouteObject = {
|
|
91
|
+
path: route.path,
|
|
92
|
+
id: route.id,
|
|
93
|
+
loader: route.loader,
|
|
94
|
+
action: route.action,
|
|
95
|
+
hasErrorBoundary: route.hasErrorBoundary,
|
|
96
|
+
shouldRevalidate: route.shouldRevalidate,
|
|
97
|
+
handle: route.handle,
|
|
98
|
+
index: route.index,
|
|
99
|
+
hasClientLoader: !!route.clientData,
|
|
100
|
+
Component: route.component ? route.component : void 0,
|
|
101
|
+
errorElement: route.error ? /* @__PURE__ */ _jsx(route.error, {}) : void 0,
|
|
102
|
+
children: route.children ? route.children.map(function(child) {
|
|
103
|
+
return getRouteObjects([
|
|
104
|
+
child
|
|
105
|
+
], {
|
|
106
|
+
globalApp,
|
|
107
|
+
ssrMode,
|
|
108
|
+
props
|
|
109
|
+
})[0];
|
|
110
|
+
}) : void 0
|
|
111
|
+
};
|
|
112
|
+
routeObjects.push(nestedRouteObject);
|
|
113
|
+
} else {
|
|
114
|
+
if (typeof route.component === "function" || _type_of(route.component) === "object") {
|
|
115
|
+
var LayoutComponent = createLayoutElement(route.component);
|
|
116
|
+
var routeObject = {
|
|
117
|
+
path: route.path,
|
|
118
|
+
element: /* @__PURE__ */ React.createElement(LayoutComponent)
|
|
119
|
+
};
|
|
120
|
+
routeObjects.push(routeObject);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} catch (err) {
|
|
125
|
+
_didIteratorError = true;
|
|
126
|
+
_iteratorError = err;
|
|
127
|
+
} finally {
|
|
128
|
+
try {
|
|
129
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
130
|
+
_iterator.return();
|
|
131
|
+
}
|
|
132
|
+
} finally {
|
|
133
|
+
if (_didIteratorError) {
|
|
134
|
+
throw _iteratorError;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
routeObjects.push({
|
|
139
|
+
path: "*",
|
|
140
|
+
element: /* @__PURE__ */ _jsx(DefaultNotFound, {})
|
|
141
|
+
});
|
|
142
|
+
return routeObjects;
|
|
143
|
+
}
|
|
144
|
+
function createRouteObjectsFromConfig(param) {
|
|
145
|
+
var routesConfig = param.routesConfig, props = param.props, ssrMode = param.ssrMode;
|
|
146
|
+
if (!routesConfig) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
var routes = routesConfig.routes, globalApp = routesConfig.globalApp;
|
|
150
|
+
if (!routes) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
return getRouteObjects(routes, {
|
|
154
|
+
globalApp,
|
|
155
|
+
ssrMode,
|
|
156
|
+
props
|
|
157
|
+
});
|
|
158
|
+
}
|
|
67
159
|
function renderRoutes(param) {
|
|
68
160
|
var routesConfig = param.routesConfig, props = param.props, ssrMode = param.ssrMode;
|
|
69
161
|
if (!routesConfig) {
|
|
@@ -82,7 +174,7 @@ function renderRoutes(param) {
|
|
|
82
174
|
}
|
|
83
175
|
function getLocation(serverContext) {
|
|
84
176
|
var _url_replace;
|
|
85
|
-
var _ref = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {},
|
|
177
|
+
var _ref = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {}, _ref_pathname = _ref.pathname, pathname = _ref_pathname === void 0 ? "" : _ref_pathname, _ref_url = _ref.url, url = _ref_url === void 0 ? "" : _ref_url;
|
|
86
178
|
var cleanUrl = url === null || url === void 0 ? void 0 : (_url_replace = url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
|
|
87
179
|
var index = (cleanUrl || "").indexOf(pathname);
|
|
88
180
|
if (index === -1) {
|
|
@@ -191,9 +283,11 @@ function deserializeErrors(errors) {
|
|
|
191
283
|
return serialized;
|
|
192
284
|
}
|
|
193
285
|
export {
|
|
286
|
+
createRouteObjectsFromConfig,
|
|
194
287
|
deserializeErrors,
|
|
195
288
|
getLocation,
|
|
196
289
|
getRouteComponents,
|
|
290
|
+
getRouteObjects,
|
|
197
291
|
renderRoutes,
|
|
198
292
|
serializeErrors,
|
|
199
293
|
standardSlash,
|
package/dist/esm/rsc/client.js
CHANGED
package/dist/esm/rsc/server.js
CHANGED
|
@@ -1 +1,87 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
1
4
|
export * from "@modern-js/render/rsc";
|
|
5
|
+
function processRSCStream(rscStream, controller, encoder) {
|
|
6
|
+
return _processRSCStream.apply(this, arguments);
|
|
7
|
+
}
|
|
8
|
+
function _processRSCStream() {
|
|
9
|
+
_processRSCStream = _async_to_generator(function(rscStream, controller, encoder) {
|
|
10
|
+
var reader, decoder, _ref, done, value, chunk, scriptTag, _String, base64, scriptTag1, remaining, scriptTag2, error;
|
|
11
|
+
return _ts_generator(this, function(_state) {
|
|
12
|
+
switch (_state.label) {
|
|
13
|
+
case 0:
|
|
14
|
+
_state.trys.push([
|
|
15
|
+
0,
|
|
16
|
+
4,
|
|
17
|
+
,
|
|
18
|
+
5
|
|
19
|
+
]);
|
|
20
|
+
reader = rscStream.getReader();
|
|
21
|
+
decoder = new TextDecoder("utf-8", {
|
|
22
|
+
fatal: true
|
|
23
|
+
});
|
|
24
|
+
_state.label = 1;
|
|
25
|
+
case 1:
|
|
26
|
+
if (false)
|
|
27
|
+
return [
|
|
28
|
+
3,
|
|
29
|
+
3
|
|
30
|
+
];
|
|
31
|
+
return [
|
|
32
|
+
4,
|
|
33
|
+
reader.read()
|
|
34
|
+
];
|
|
35
|
+
case 2:
|
|
36
|
+
_ref = _state.sent(), done = _ref.done, value = _ref.value;
|
|
37
|
+
if (done)
|
|
38
|
+
return [
|
|
39
|
+
3,
|
|
40
|
+
3
|
|
41
|
+
];
|
|
42
|
+
try {
|
|
43
|
+
chunk = JSON.stringify(decoder.decode(value, {
|
|
44
|
+
stream: true
|
|
45
|
+
}));
|
|
46
|
+
scriptTag = "<script>(self.__FLIGHT_DATA||=[]).push(".concat(chunk, ")</script>");
|
|
47
|
+
controller.enqueue(encoder.encode(scriptTag));
|
|
48
|
+
} catch (err) {
|
|
49
|
+
;
|
|
50
|
+
base64 = JSON.stringify(btoa((_String = String).fromCodePoint.apply(_String, _to_consumable_array(value))));
|
|
51
|
+
scriptTag1 = "<script>(self.__FLIGHT_DATA||=[]).push(Uint8Array.from(atob(".concat(base64, "), m => m.codePointAt(0)))</script>");
|
|
52
|
+
controller.enqueue(encoder.encode(scriptTag1));
|
|
53
|
+
}
|
|
54
|
+
return [
|
|
55
|
+
3,
|
|
56
|
+
1
|
|
57
|
+
];
|
|
58
|
+
case 3:
|
|
59
|
+
remaining = decoder.decode();
|
|
60
|
+
if (remaining.length) {
|
|
61
|
+
scriptTag2 = "<script>(self.__FLIGHT_DATA||=[]).push(".concat(JSON.stringify(remaining), ")</script>");
|
|
62
|
+
controller.enqueue(encoder.encode(scriptTag2));
|
|
63
|
+
}
|
|
64
|
+
controller.close();
|
|
65
|
+
return [
|
|
66
|
+
3,
|
|
67
|
+
5
|
|
68
|
+
];
|
|
69
|
+
case 4:
|
|
70
|
+
error = _state.sent();
|
|
71
|
+
controller.error(error);
|
|
72
|
+
return [
|
|
73
|
+
3,
|
|
74
|
+
5
|
|
75
|
+
];
|
|
76
|
+
case 5:
|
|
77
|
+
return [
|
|
78
|
+
2
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
return _processRSCStream.apply(this, arguments);
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
processRSCStream
|
|
87
|
+
};
|