@modern-js/runtime 2.68.0 → 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/code.js +6 -3
- 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/code.js +6 -3
- 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/code.js +6 -3
- 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 +21 -12
- package/static/modern-inline.js +1 -1
|
@@ -44,8 +44,10 @@ var import_react = require("react");
|
|
|
44
44
|
var import_common = require("../../common");
|
|
45
45
|
var import_core = require("../../core");
|
|
46
46
|
var import_context = require("../../core/context");
|
|
47
|
+
var import_serverPayload = require("../../core/context/serverPayload.server");
|
|
47
48
|
var import_DeferredDataScripts = __toESM(require("./DeferredDataScripts.node"));
|
|
48
49
|
var import_hooks = require("./hooks");
|
|
50
|
+
var import_rsc_router = require("./rsc-router");
|
|
49
51
|
var import_utils = require("./utils");
|
|
50
52
|
function createRemixReuqest(request) {
|
|
51
53
|
const method = "GET";
|
|
@@ -78,6 +80,10 @@ const routerPlugin = (userConfig = {}) => {
|
|
|
78
80
|
if (!finalRouteConfig.routes && !createRoutes) {
|
|
79
81
|
return;
|
|
80
82
|
}
|
|
83
|
+
const enableRsc = (0, import_context.getGlobalEnableRsc)();
|
|
84
|
+
if (enableRsc) {
|
|
85
|
+
await (0, import_rsc_router.prepareRSCRoutes)(finalRouteConfig.routes);
|
|
86
|
+
}
|
|
81
87
|
const { request, mode: ssrMode, nonce, loaderFailureMode = "errorBoundary" } = context.ssrContext;
|
|
82
88
|
const { baseUrl } = request;
|
|
83
89
|
const _basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, basename) : baseUrl;
|
|
@@ -86,13 +92,20 @@ const routerPlugin = (userConfig = {}) => {
|
|
|
86
92
|
requestContext.set(import_node.reporterCtx, reporter);
|
|
87
93
|
const hooks = api.getHooks();
|
|
88
94
|
await hooks.onBeforeCreateRoutes.call(context);
|
|
89
|
-
let routes =
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
let routes = [];
|
|
96
|
+
if (enableRsc) {
|
|
97
|
+
routes = createRoutes ? createRoutes() : (0, import_utils.createRouteObjectsFromConfig)({
|
|
98
|
+
routesConfig: finalRouteConfig
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
routes = createRoutes ? createRoutes() : (0, import_router2.createRoutesFromElements)((0, import_utils.renderRoutes)({
|
|
102
|
+
routesConfig: finalRouteConfig,
|
|
103
|
+
ssrMode,
|
|
104
|
+
props: {
|
|
105
|
+
nonce
|
|
106
|
+
}
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
96
109
|
routes = hooks.modifyRoutes.call(routes);
|
|
97
110
|
const { query } = (0, import_remix_router.createStaticHandler)(routes, {
|
|
98
111
|
basename: _basename
|
|
@@ -104,8 +117,13 @@ const routerPlugin = (userConfig = {}) => {
|
|
|
104
117
|
});
|
|
105
118
|
const cost = end();
|
|
106
119
|
(_context_ssrContext1 = context.ssrContext) === null || _context_ssrContext1 === void 0 ? void 0 : (_context_ssrContext_onTiming = _context_ssrContext1.onTiming) === null || _context_ssrContext_onTiming === void 0 ? void 0 : _context_ssrContext_onTiming.call(_context_ssrContext1, import_constants.LOADER_REPORTER_NAME, cost);
|
|
120
|
+
const isRSCNavigation = remixRequest.headers.get("x-rsc-tree") === "true";
|
|
107
121
|
if (routerContext instanceof Response) {
|
|
108
|
-
|
|
122
|
+
if (enableRsc && isRSCNavigation) {
|
|
123
|
+
return interrupt((0, import_rsc_router.handleRSCRedirect)(routerContext.headers, _basename, routerContext.status));
|
|
124
|
+
} else {
|
|
125
|
+
return interrupt(routerContext);
|
|
126
|
+
}
|
|
109
127
|
}
|
|
110
128
|
const errors = Object.values(routerContext.errors || {});
|
|
111
129
|
if (
|
|
@@ -115,9 +133,19 @@ const routerPlugin = (userConfig = {}) => {
|
|
|
115
133
|
routerContext.statusCode = 200;
|
|
116
134
|
throw errors[0];
|
|
117
135
|
}
|
|
118
|
-
const router = (0, import_router.createStaticRouter)(routes, routerContext);
|
|
119
136
|
context.routerContext = routerContext;
|
|
120
|
-
|
|
137
|
+
let payload;
|
|
138
|
+
if (enableRsc) {
|
|
139
|
+
if (isRSCNavigation) {
|
|
140
|
+
for (const match of routerContext.matches) {
|
|
141
|
+
if (match.route.hasClientLoader) {
|
|
142
|
+
delete routerContext.loaderData[match.route.id];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
payload = (0, import_rsc_router.createServerPayload)(routerContext, routes);
|
|
147
|
+
(0, import_serverPayload.setServerPayload)(payload);
|
|
148
|
+
}
|
|
121
149
|
Object.defineProperty(context, "routes", {
|
|
122
150
|
get() {
|
|
123
151
|
return routes;
|
|
@@ -130,30 +158,43 @@ const routerPlugin = (userConfig = {}) => {
|
|
|
130
158
|
return App;
|
|
131
159
|
}
|
|
132
160
|
const getRouteApp = () => {
|
|
133
|
-
|
|
161
|
+
const enableRsc = (0, import_context.getGlobalEnableRsc)();
|
|
162
|
+
return (props) => {
|
|
134
163
|
const context = (0, import_react.useContext)(import_core.RuntimeReactContext);
|
|
135
|
-
const {
|
|
164
|
+
const { routerContext, ssrContext, routes } = context;
|
|
136
165
|
const { nonce, mode, useJsonScript } = ssrContext;
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
166
|
+
const { basename } = routerContext;
|
|
167
|
+
const remixRouter = (0, import_router.createStaticRouter)(routes, routerContext);
|
|
168
|
+
if (!enableRsc) {
|
|
169
|
+
const routerWrapper = /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
|
|
170
|
+
children: [
|
|
171
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_router.StaticRouterProvider, {
|
|
172
|
+
router: remixRouter,
|
|
173
|
+
context: routerContext,
|
|
174
|
+
hydrate: false
|
|
175
|
+
}),
|
|
176
|
+
mode === "stream" && // ROUTER_DATA will inject in `packages/runtime/plugin-runtime/src/core/server/string/ssrData.ts` in string ssr
|
|
177
|
+
// So we can inject it only when streaming ssr
|
|
178
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DeferredDataScripts.default, {
|
|
179
|
+
nonce,
|
|
180
|
+
context: routerContext,
|
|
181
|
+
useJsonScript
|
|
182
|
+
}),
|
|
183
|
+
mode === "stream" && import_common.JSX_SHELL_STREAM_END_MARK
|
|
184
|
+
]
|
|
185
|
+
});
|
|
186
|
+
return App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
|
|
187
|
+
children: routerWrapper
|
|
188
|
+
}) : routerWrapper;
|
|
189
|
+
} else {
|
|
190
|
+
return App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
|
|
191
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_rsc_router.RSCStaticRouter, {
|
|
192
|
+
basename
|
|
193
|
+
})
|
|
194
|
+
}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_rsc_router.RSCStaticRouter, {
|
|
195
|
+
basename
|
|
196
|
+
});
|
|
197
|
+
}
|
|
157
198
|
};
|
|
158
199
|
};
|
|
159
200
|
return getRouteApp();
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var rsc_router_exports = {};
|
|
30
|
+
__export(rsc_router_exports, {
|
|
31
|
+
RSCStaticRouter: () => RSCStaticRouter,
|
|
32
|
+
createClientRouterFromPayload: () => createClientRouterFromPayload,
|
|
33
|
+
createServerPayload: () => createServerPayload,
|
|
34
|
+
handleRSCRedirect: () => handleRSCRedirect,
|
|
35
|
+
prepareRSCRoutes: () => prepareRSCRoutes
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(rsc_router_exports);
|
|
38
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
39
|
+
var import_client = require("@modern-js/render/client");
|
|
40
|
+
var import_router = require("@modern-js/runtime-utils/node/router");
|
|
41
|
+
var import_router2 = require("@modern-js/runtime-utils/router");
|
|
42
|
+
var import_react = __toESM(require("react"));
|
|
43
|
+
const safeUse = (promise) => {
|
|
44
|
+
if (typeof import_react.default.use === "function") {
|
|
45
|
+
return import_react.default.use(promise);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
};
|
|
49
|
+
const createServerPayload = (routerContext, routes) => {
|
|
50
|
+
return {
|
|
51
|
+
type: "render",
|
|
52
|
+
actionData: routerContext.actionData,
|
|
53
|
+
errors: routerContext.errors,
|
|
54
|
+
loaderData: routerContext.loaderData,
|
|
55
|
+
location: routerContext.location,
|
|
56
|
+
routes: routerContext.matches.map((match, index, matches) => {
|
|
57
|
+
const element = match.route.element;
|
|
58
|
+
const parentMatch = index > 0 ? matches[index - 1] : void 0;
|
|
59
|
+
let processedElement;
|
|
60
|
+
if (element) {
|
|
61
|
+
var _routerContext_loaderData, _routerContext_actionData;
|
|
62
|
+
const ElementComponent = element.type;
|
|
63
|
+
processedElement = /* @__PURE__ */ import_react.default.createElement(ElementComponent, {
|
|
64
|
+
loaderData: routerContext === null || routerContext === void 0 ? void 0 : (_routerContext_loaderData = routerContext.loaderData) === null || _routerContext_loaderData === void 0 ? void 0 : _routerContext_loaderData[match.route.id],
|
|
65
|
+
actionData: routerContext === null || routerContext === void 0 ? void 0 : (_routerContext_actionData = routerContext.actionData) === null || _routerContext_actionData === void 0 ? void 0 : _routerContext_actionData[match.route.id],
|
|
66
|
+
params: match.params,
|
|
67
|
+
matches: routerContext.matches.map((m) => {
|
|
68
|
+
var _routerContext_loaderData2;
|
|
69
|
+
const { route, pathname, params } = m;
|
|
70
|
+
return {
|
|
71
|
+
id: route.id,
|
|
72
|
+
pathname,
|
|
73
|
+
params,
|
|
74
|
+
data: routerContext === null || routerContext === void 0 ? void 0 : (_routerContext_loaderData2 = routerContext.loaderData) === null || _routerContext_loaderData2 === void 0 ? void 0 : _routerContext_loaderData2[route.id],
|
|
75
|
+
handle: route.handle
|
|
76
|
+
};
|
|
77
|
+
})
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
element: processedElement,
|
|
82
|
+
errorElement: match.route.errorElement,
|
|
83
|
+
handle: match.route.handle,
|
|
84
|
+
hasAction: !!match.route.action,
|
|
85
|
+
hasErrorBoundary: !!match.route.hasErrorBoundary,
|
|
86
|
+
hasLoader: !!match.route.loader,
|
|
87
|
+
hasClientLoader: !!match.route.hasClientLoader,
|
|
88
|
+
id: match.route.id,
|
|
89
|
+
index: match.route.index,
|
|
90
|
+
params: match.params,
|
|
91
|
+
parentId: (parentMatch === null || parentMatch === void 0 ? void 0 : parentMatch.route.id) || match.route.parentId,
|
|
92
|
+
path: match.route.path,
|
|
93
|
+
pathname: match.pathname,
|
|
94
|
+
pathnameBase: match.pathnameBase
|
|
95
|
+
};
|
|
96
|
+
})
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
const handleRSCRedirect = (headers, basename, status) => {
|
|
100
|
+
const newHeaders = new Headers(headers);
|
|
101
|
+
let redirectUrl = headers.get("Location");
|
|
102
|
+
if (basename !== "/") {
|
|
103
|
+
redirectUrl = redirectUrl.replace(basename, "");
|
|
104
|
+
}
|
|
105
|
+
newHeaders.set("X-Modernjs-Redirect", redirectUrl);
|
|
106
|
+
newHeaders.set("X-Modernjs-BaseUrl", basename);
|
|
107
|
+
newHeaders.delete("Location");
|
|
108
|
+
return new Response(null, {
|
|
109
|
+
status,
|
|
110
|
+
headers: newHeaders
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const prepareRSCRoutes = async (routes) => {
|
|
114
|
+
const isLazyComponent = (component) => {
|
|
115
|
+
return component && typeof component === "object" && component._init !== void 0 && component._payload !== void 0;
|
|
116
|
+
};
|
|
117
|
+
const processRoutes = async (routesList) => {
|
|
118
|
+
await Promise.all(routesList.map(async (route) => {
|
|
119
|
+
if ("lazyImport" in route && isLazyComponent(route.component)) {
|
|
120
|
+
route.component = (await route.lazyImport()).default;
|
|
121
|
+
}
|
|
122
|
+
if (route.children && Array.isArray(route.children)) {
|
|
123
|
+
await processRoutes(route.children);
|
|
124
|
+
}
|
|
125
|
+
}));
|
|
126
|
+
};
|
|
127
|
+
await processRoutes(routes);
|
|
128
|
+
};
|
|
129
|
+
const mergeRoutes = (routes, originalRoutes) => {
|
|
130
|
+
if (!originalRoutes || !Array.isArray(originalRoutes)) {
|
|
131
|
+
return routes;
|
|
132
|
+
}
|
|
133
|
+
const routesMap = /* @__PURE__ */ new Map();
|
|
134
|
+
const buildRoutesMap = (routesList) => {
|
|
135
|
+
routesList.forEach((route) => {
|
|
136
|
+
if (route.id) {
|
|
137
|
+
routesMap.set(route.id, route);
|
|
138
|
+
}
|
|
139
|
+
if ("children" in route && route.children && Array.isArray(route.children)) {
|
|
140
|
+
buildRoutesMap(route.children);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
buildRoutesMap(routes);
|
|
145
|
+
const mergeRoutesRecursive = (origRoutes) => {
|
|
146
|
+
return origRoutes.map((origRoute) => {
|
|
147
|
+
if (origRoute.id && routesMap.has(origRoute.id)) {
|
|
148
|
+
const matchedRoute = routesMap.get(origRoute.id);
|
|
149
|
+
const result = {
|
|
150
|
+
loader: origRoute.hasClientLoader ? origRoute.loader : void 0,
|
|
151
|
+
...matchedRoute
|
|
152
|
+
};
|
|
153
|
+
if (origRoute.children && Array.isArray(origRoute.children)) {
|
|
154
|
+
result.children = mergeRoutesRecursive(origRoute.children);
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
return origRoute;
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
return mergeRoutesRecursive(originalRoutes);
|
|
162
|
+
};
|
|
163
|
+
const findRouteInTree = (routes, routeId) => {
|
|
164
|
+
for (const route of routes) {
|
|
165
|
+
if (route.id === routeId) {
|
|
166
|
+
return route;
|
|
167
|
+
}
|
|
168
|
+
if (route.children && Array.isArray(route.children)) {
|
|
169
|
+
const found = findRouteInTree(route.children, routeId);
|
|
170
|
+
if (found) {
|
|
171
|
+
return found;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
};
|
|
177
|
+
const createClientRouterFromPayload = (payload, originalRoutes, basename = "") => {
|
|
178
|
+
const processedRoutes = payload.routes.reduceRight((previous, route) => {
|
|
179
|
+
if (previous.length > 0) {
|
|
180
|
+
return [
|
|
181
|
+
{
|
|
182
|
+
...route,
|
|
183
|
+
children: previous
|
|
184
|
+
}
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
return [
|
|
188
|
+
route
|
|
189
|
+
];
|
|
190
|
+
}, []);
|
|
191
|
+
const mergedRoutes = mergeRoutes(processedRoutes, originalRoutes);
|
|
192
|
+
const router = (0, import_router2.createBrowserRouter)(mergedRoutes, {
|
|
193
|
+
//@ts-ignore
|
|
194
|
+
hydrationData: payload,
|
|
195
|
+
basename,
|
|
196
|
+
dataStrategy: async (context) => {
|
|
197
|
+
const { request, matches } = context;
|
|
198
|
+
const results = {};
|
|
199
|
+
const clientMatches = matches.filter((match) => match.route.hasClientLoader);
|
|
200
|
+
const fetchPromise = fetch(request.url, {
|
|
201
|
+
headers: {
|
|
202
|
+
"x-rsc-tree": "true"
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
const clientLoadersPromise = clientMatches.length > 0 ? Promise.all(clientMatches.map(async (clientMatch) => {
|
|
206
|
+
const foundRoute = findRouteInTree(originalRoutes, clientMatch.route.id);
|
|
207
|
+
clientMatch.route.loader = foundRoute === null || foundRoute === void 0 ? void 0 : foundRoute.loader;
|
|
208
|
+
const res2 = await clientMatch.resolve();
|
|
209
|
+
return {
|
|
210
|
+
routeId: clientMatch.route.id,
|
|
211
|
+
result: res2
|
|
212
|
+
};
|
|
213
|
+
})) : Promise.resolve([]);
|
|
214
|
+
const res = await fetchPromise;
|
|
215
|
+
const redirectLocation = res.headers.get("X-Modernjs-Redirect");
|
|
216
|
+
if (redirectLocation) {
|
|
217
|
+
matches.forEach((match) => {
|
|
218
|
+
const routeId = match.route.id;
|
|
219
|
+
if (routeId) {
|
|
220
|
+
results[routeId] = {
|
|
221
|
+
type: "redirect",
|
|
222
|
+
result: (0, import_router2.redirect)(redirectLocation)
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
return results;
|
|
227
|
+
}
|
|
228
|
+
const [clientLoaderResults] = await Promise.all([
|
|
229
|
+
clientLoadersPromise
|
|
230
|
+
]);
|
|
231
|
+
clientLoaderResults.forEach(({ routeId, result }) => {
|
|
232
|
+
results[routeId] = result;
|
|
233
|
+
});
|
|
234
|
+
const payload2 = await (0, import_client.createFromReadableStream)(res.body);
|
|
235
|
+
if (typeof payload2.type === "undefined" || payload2.type !== "render") {
|
|
236
|
+
throw new Error("Unexpected payload type");
|
|
237
|
+
}
|
|
238
|
+
matches.forEach((match) => {
|
|
239
|
+
var _payload_loaderData;
|
|
240
|
+
const routeId = match.route.id;
|
|
241
|
+
const matchedRoute = payload2.routes.find((route) => route.id === routeId);
|
|
242
|
+
if (matchedRoute) {
|
|
243
|
+
router.patchRoutes(matchedRoute.parentId, [
|
|
244
|
+
matchedRoute
|
|
245
|
+
], true);
|
|
246
|
+
}
|
|
247
|
+
if ((_payload_loaderData = payload2.loaderData) === null || _payload_loaderData === void 0 ? void 0 : _payload_loaderData[routeId]) {
|
|
248
|
+
results[routeId] = {
|
|
249
|
+
type: "data",
|
|
250
|
+
result: payload2.loaderData[routeId]
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
return results;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
return router;
|
|
258
|
+
};
|
|
259
|
+
const createRSCStaticRouterComponent = (payload, basename) => {
|
|
260
|
+
const routerContext = {
|
|
261
|
+
actionData: payload.actionData,
|
|
262
|
+
actionHeaders: {},
|
|
263
|
+
activeDeferreds: {},
|
|
264
|
+
basename: basename || "",
|
|
265
|
+
errors: payload.errors,
|
|
266
|
+
loaderData: payload.loaderData,
|
|
267
|
+
loaderHeaders: {},
|
|
268
|
+
location: payload.location,
|
|
269
|
+
statusCode: 200,
|
|
270
|
+
matches: payload.routes.map((match) => ({
|
|
271
|
+
params: match.params,
|
|
272
|
+
pathname: match.pathname,
|
|
273
|
+
pathnameBase: match.pathnameBase,
|
|
274
|
+
route: {
|
|
275
|
+
id: match.id,
|
|
276
|
+
action: match.hasAction || !!match.clientAction,
|
|
277
|
+
handle: match.handle,
|
|
278
|
+
hasErrorBoundary: match.hasErrorBoundary,
|
|
279
|
+
loader: match.hasLoader || !!match.clientLoader,
|
|
280
|
+
index: match.index,
|
|
281
|
+
path: match.path,
|
|
282
|
+
shouldRevalidate: match.shouldRevalidate
|
|
283
|
+
}
|
|
284
|
+
}))
|
|
285
|
+
};
|
|
286
|
+
const processedRoutes = payload.routes.reduceRight((previous, route) => {
|
|
287
|
+
if (previous.length > 0) {
|
|
288
|
+
return [
|
|
289
|
+
{
|
|
290
|
+
...route,
|
|
291
|
+
children: previous
|
|
292
|
+
}
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
return [
|
|
296
|
+
route
|
|
297
|
+
];
|
|
298
|
+
}, []);
|
|
299
|
+
const router = (0, import_router.createStaticRouter)(processedRoutes, routerContext);
|
|
300
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_router.StaticRouterProvider, {
|
|
301
|
+
context: routerContext,
|
|
302
|
+
router,
|
|
303
|
+
hydrate: false
|
|
304
|
+
});
|
|
305
|
+
};
|
|
306
|
+
const RSCStaticRouter = ({ basename }) => {
|
|
307
|
+
const payload = safeUse(safeUse(import_client.ElementsContext));
|
|
308
|
+
if (!payload || payload.type !== "render") {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
|
|
312
|
+
children: createRSCStaticRouterComponent(payload, basename)
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
316
|
+
0 && (module.exports = {
|
|
317
|
+
RSCStaticRouter,
|
|
318
|
+
createClientRouterFromPayload,
|
|
319
|
+
createServerPayload,
|
|
320
|
+
handleRSCRedirect,
|
|
321
|
+
prepareRSCRoutes
|
|
322
|
+
});
|
|
@@ -28,9 +28,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
var utils_exports = {};
|
|
30
30
|
__export(utils_exports, {
|
|
31
|
+
createRouteObjectsFromConfig: () => createRouteObjectsFromConfig,
|
|
31
32
|
deserializeErrors: () => deserializeErrors,
|
|
32
33
|
getLocation: () => getLocation,
|
|
33
34
|
getRouteComponents: () => getRouteComponents,
|
|
35
|
+
getRouteObjects: () => getRouteObjects,
|
|
34
36
|
renderRoutes: () => renderRoutes,
|
|
35
37
|
serializeErrors: () => serializeErrors,
|
|
36
38
|
standardSlash: () => standardSlash,
|
|
@@ -41,6 +43,7 @@ var import_jsx_runtime = require("react/jsx-runtime");
|
|
|
41
43
|
var import_browser = require("@modern-js/runtime-utils/browser");
|
|
42
44
|
var import_remix_router = require("@modern-js/runtime-utils/remix-router");
|
|
43
45
|
var import_router = require("@modern-js/runtime-utils/router");
|
|
46
|
+
var import_react = __toESM(require("react"));
|
|
44
47
|
var import_DefaultNotFound = require("./DefaultNotFound");
|
|
45
48
|
var import_DeferredDataScripts = __toESM(require("./DeferredDataScripts"));
|
|
46
49
|
function getRouteComponents(routes, { globalApp, ssrMode, props }) {
|
|
@@ -80,6 +83,76 @@ function getRouteComponents(routes, { globalApp, ssrMode, props }) {
|
|
|
80
83
|
}, "*"));
|
|
81
84
|
return routeElements;
|
|
82
85
|
}
|
|
86
|
+
function getRouteObjects(routes, { globalApp, ssrMode, props }) {
|
|
87
|
+
const createLayoutElement = (Component) => {
|
|
88
|
+
const GlobalLayout = globalApp;
|
|
89
|
+
if (!GlobalLayout) {
|
|
90
|
+
return Component;
|
|
91
|
+
}
|
|
92
|
+
const LayoutWrapper = (props2) => {
|
|
93
|
+
const LayoutComponent = GlobalLayout;
|
|
94
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LayoutComponent, {
|
|
95
|
+
Component,
|
|
96
|
+
...props2
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
return LayoutWrapper;
|
|
100
|
+
};
|
|
101
|
+
const routeObjects = [];
|
|
102
|
+
for (const route of routes) {
|
|
103
|
+
if (route.type === "nested") {
|
|
104
|
+
const nestedRouteObject = {
|
|
105
|
+
path: route.path,
|
|
106
|
+
id: route.id,
|
|
107
|
+
loader: route.loader,
|
|
108
|
+
action: route.action,
|
|
109
|
+
hasErrorBoundary: route.hasErrorBoundary,
|
|
110
|
+
shouldRevalidate: route.shouldRevalidate,
|
|
111
|
+
handle: route.handle,
|
|
112
|
+
index: route.index,
|
|
113
|
+
hasClientLoader: !!route.clientData,
|
|
114
|
+
Component: route.component ? route.component : void 0,
|
|
115
|
+
errorElement: route.error ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(route.error, {}) : void 0,
|
|
116
|
+
children: route.children ? route.children.map((child) => getRouteObjects([
|
|
117
|
+
child
|
|
118
|
+
], {
|
|
119
|
+
globalApp,
|
|
120
|
+
ssrMode,
|
|
121
|
+
props
|
|
122
|
+
})[0]) : void 0
|
|
123
|
+
};
|
|
124
|
+
routeObjects.push(nestedRouteObject);
|
|
125
|
+
} else {
|
|
126
|
+
if (typeof route.component === "function" || typeof route.component === "object") {
|
|
127
|
+
const LayoutComponent = createLayoutElement(route.component);
|
|
128
|
+
const routeObject = {
|
|
129
|
+
path: route.path,
|
|
130
|
+
element: /* @__PURE__ */ import_react.default.createElement(LayoutComponent)
|
|
131
|
+
};
|
|
132
|
+
routeObjects.push(routeObject);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
routeObjects.push({
|
|
137
|
+
path: "*",
|
|
138
|
+
element: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DefaultNotFound.DefaultNotFound, {})
|
|
139
|
+
});
|
|
140
|
+
return routeObjects;
|
|
141
|
+
}
|
|
142
|
+
function createRouteObjectsFromConfig({ routesConfig, props, ssrMode }) {
|
|
143
|
+
if (!routesConfig) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
const { routes, globalApp } = routesConfig;
|
|
147
|
+
if (!routes) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
return getRouteObjects(routes, {
|
|
151
|
+
globalApp,
|
|
152
|
+
ssrMode,
|
|
153
|
+
props
|
|
154
|
+
});
|
|
155
|
+
}
|
|
83
156
|
function renderRoutes({ routesConfig, props, ssrMode }) {
|
|
84
157
|
if (!routesConfig) {
|
|
85
158
|
return null;
|
|
@@ -97,7 +170,7 @@ function renderRoutes({ routesConfig, props, ssrMode }) {
|
|
|
97
170
|
}
|
|
98
171
|
function getLocation(serverContext) {
|
|
99
172
|
var _url_replace;
|
|
100
|
-
const { pathname, url } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
|
|
173
|
+
const { pathname = "", url = "" } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
|
|
101
174
|
const cleanUrl = url === null || url === void 0 ? void 0 : (_url_replace = url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
|
|
102
175
|
const index = (cleanUrl || "").indexOf(pathname);
|
|
103
176
|
if (index === -1) {
|
|
@@ -171,9 +244,11 @@ function deserializeErrors(errors) {
|
|
|
171
244
|
}
|
|
172
245
|
// Annotate the CommonJS export names for ESM import in node:
|
|
173
246
|
0 && (module.exports = {
|
|
247
|
+
createRouteObjectsFromConfig,
|
|
174
248
|
deserializeErrors,
|
|
175
249
|
getLocation,
|
|
176
250
|
getRouteComponents,
|
|
251
|
+
getRouteObjects,
|
|
177
252
|
renderRoutes,
|
|
178
253
|
serializeErrors,
|
|
179
254
|
standardSlash,
|
package/dist/cjs/rsc/client.js
CHANGED
|
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
6
10
|
var __copyProps = (to, from, except, desc) => {
|
|
7
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -14,9 +18,16 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
18
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
20
|
var client_exports = {};
|
|
21
|
+
__export(client_exports, {
|
|
22
|
+
isRedirectResponse: () => isRedirectResponse
|
|
23
|
+
});
|
|
17
24
|
module.exports = __toCommonJS(client_exports);
|
|
18
25
|
__reExport(client_exports, require("@modern-js/render/client"), module.exports);
|
|
26
|
+
const isRedirectResponse = (res) => {
|
|
27
|
+
return res.headers.get("X-Modernjs-Redirect") != null;
|
|
28
|
+
};
|
|
19
29
|
// Annotate the CommonJS export names for ESM import in node:
|
|
20
30
|
0 && (module.exports = {
|
|
31
|
+
isRedirectResponse,
|
|
21
32
|
...require("@modern-js/render/client")
|
|
22
33
|
});
|
package/dist/cjs/rsc/server.js
CHANGED
|
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
6
10
|
var __copyProps = (to, from, except, desc) => {
|
|
7
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -14,9 +18,45 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
18
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
20
|
var server_exports = {};
|
|
21
|
+
__export(server_exports, {
|
|
22
|
+
processRSCStream: () => processRSCStream
|
|
23
|
+
});
|
|
17
24
|
module.exports = __toCommonJS(server_exports);
|
|
18
25
|
__reExport(server_exports, require("@modern-js/render/rsc"), module.exports);
|
|
26
|
+
async function processRSCStream(rscStream, controller, encoder) {
|
|
27
|
+
try {
|
|
28
|
+
const reader = rscStream.getReader();
|
|
29
|
+
const decoder = new TextDecoder("utf-8", {
|
|
30
|
+
fatal: true
|
|
31
|
+
});
|
|
32
|
+
while (true) {
|
|
33
|
+
const { done, value } = await reader.read();
|
|
34
|
+
if (done)
|
|
35
|
+
break;
|
|
36
|
+
try {
|
|
37
|
+
const chunk = JSON.stringify(decoder.decode(value, {
|
|
38
|
+
stream: true
|
|
39
|
+
}));
|
|
40
|
+
const scriptTag = `<script>(self.__FLIGHT_DATA||=[]).push(${chunk})</script>`;
|
|
41
|
+
controller.enqueue(encoder.encode(scriptTag));
|
|
42
|
+
} catch (err) {
|
|
43
|
+
const base64 = JSON.stringify(btoa(String.fromCodePoint(...value)));
|
|
44
|
+
const scriptTag = `<script>(self.__FLIGHT_DATA||=[]).push(Uint8Array.from(atob(${base64}), m => m.codePointAt(0)))</script>`;
|
|
45
|
+
controller.enqueue(encoder.encode(scriptTag));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const remaining = decoder.decode();
|
|
49
|
+
if (remaining.length) {
|
|
50
|
+
const scriptTag = `<script>(self.__FLIGHT_DATA||=[]).push(${JSON.stringify(remaining)})</script>`;
|
|
51
|
+
controller.enqueue(encoder.encode(scriptTag));
|
|
52
|
+
}
|
|
53
|
+
controller.close();
|
|
54
|
+
} catch (error) {
|
|
55
|
+
controller.error(error);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
19
58
|
// Annotate the CommonJS export names for ESM import in node:
|
|
20
59
|
0 && (module.exports = {
|
|
60
|
+
processRSCStream,
|
|
21
61
|
...require("@modern-js/render/rsc")
|
|
22
62
|
});
|