@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
|
@@ -37,7 +37,7 @@ var routerPlugin = function() {
|
|
|
37
37
|
if ((nestedRoutesEntry || pageRoutesEntry) && !isRouterV5) {
|
|
38
38
|
plugins.push({
|
|
39
39
|
name: "router",
|
|
40
|
-
path: "@".concat(metaName2, "/runtime/router"),
|
|
40
|
+
path: "@".concat(metaName2, "/runtime/router/internal"),
|
|
41
41
|
config: typeof routerConfig === "boolean" ? {
|
|
42
42
|
serverBase
|
|
43
43
|
} : _object_spread_props(_object_spread({}, routerConfig), {
|
|
@@ -64,9 +64,10 @@ var routerPlugin = function() {
|
|
|
64
64
|
// react-router v6 is no longer support ie 11
|
|
65
65
|
// so we need to compile these packages to ensure the compatibility
|
|
66
66
|
// https://github.com/remix-run/react-router/commit/f6df0697e1b2064a2b3a12e8b39577326fdd945b
|
|
67
|
-
/node_modules
|
|
68
|
-
/node_modules
|
|
69
|
-
/node_modules
|
|
67
|
+
/[\\/]node_modules[\\/]react-router[\\/]/,
|
|
68
|
+
/[\\/]node_modules[\\/]react-router-dom[\\/]/,
|
|
69
|
+
/[\\/]node_modules[\\/]@remix-run[\\/]router[\\/]/,
|
|
70
|
+
path.resolve(__dirname, "../runtime").replace("cjs", "esm")
|
|
70
71
|
],
|
|
71
72
|
globalVars: {
|
|
72
73
|
"process.env._MODERN_ROUTER_VERSION": "v6"
|
|
@@ -127,7 +128,7 @@ var routerPlugin = function() {
|
|
|
127
128
|
var _api_useAppContext = api.useAppContext(), internalDirectory = _api_useAppContext.internalDirectory, metaName2 = _api_useAppContext.metaName;
|
|
128
129
|
var pluginsExportsUtils = createRuntimeExportsUtils(internalDirectory, "plugins");
|
|
129
130
|
if (!isRouterV5) {
|
|
130
|
-
pluginsExportsUtils.addExport("export { default as router } from '@".concat(metaName2, "/runtime/router'"));
|
|
131
|
+
pluginsExportsUtils.addExport("export { default as router } from '@".concat(metaName2, "/runtime/router/internal'"));
|
|
131
132
|
}
|
|
132
133
|
});
|
|
133
134
|
api.onFileChanged(function() {
|
package/dist/esm/router/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
3
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
4
|
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
|
|
@@ -242,6 +243,5 @@ var NavLink = createPrefetchLink(RouterNavLink);
|
|
|
242
243
|
NavLink.displayName = "NavLink";
|
|
243
244
|
export {
|
|
244
245
|
Link,
|
|
245
|
-
NavLink
|
|
246
|
-
composeEventHandlers
|
|
246
|
+
NavLink
|
|
247
247
|
};
|
|
@@ -2,9 +2,9 @@ import { ROUTER_DATA_JSON_ID } from "../../core/constants";
|
|
|
2
2
|
var setupFnStr = "function s(r,e){_ROUTER_DATA.r=_ROUTER_DATA.r||{},_ROUTER_DATA.r[r]=_ROUTER_DATA.r[r]||{};return new Promise((function(A,R){_ROUTER_DATA.r[r][e]={resolve:A,reject:R}}))};";
|
|
3
3
|
var resolveFnStr = "function r(e,r,o,A){A?_ROUTER_DATA.r[e][r].reject(A):_ROUTER_DATA.r[e][r].resolve(o)};";
|
|
4
4
|
var preResolvedFnStr = "function p(e,r){return void 0!==r?Promise.reject(new Error(r.message)):Promise.resolve(e)};";
|
|
5
|
-
var mergeLoaderDataStr = 'function mergeLoaderData(e,n){
|
|
5
|
+
var mergeLoaderDataStr = 'function mergeLoaderData(e,n){var r=n.reduce((function(e,{key:n,routerDataFnName:r,routerDataFnArgs:a}){var t=a.map((e=>{if("undefined"!==e&&null!==e)return JSON.parse(e)}));return console.info("args",t),{...e,[n]:_ROUTER_DATA[r](...t)}}),{});Object.assign(_ROUTER_DATA.loaderData[e],r)}';
|
|
6
6
|
var initRouterDataAttrs = "_ROUTER_DATA.s = ".concat(setupFnStr, "_ROUTER_DATA.r = ").concat(resolveFnStr, "_ROUTER_DATA.p = ").concat(preResolvedFnStr).concat(mergeLoaderDataStr);
|
|
7
|
-
var modernInline = `function runWindowFn(){window[document.currentScript.getAttribute("data-fn-name")](
|
|
7
|
+
var modernInline = `function runWindowFn(){window[document.currentScript.getAttribute("data-fn-name")].apply(window,JSON.parse(document.currentScript.getAttribute("data-fn-args")))}function runRouterDataFn(){_ROUTER_DATA[document.currentScript.getAttribute("data-fn-name")].apply(_ROUTER_DATA,JSON.parse(document.currentScript.getAttribute("data-fn-args")))}function initRouterData(e){var r=document.getElementById(e);if(r)try{_ROUTER_DATA=JSON.parse(r.textContent)}catch(r){console.error("parse ".concat(e," error"),t),_ROUTER_DATA={}}};initRouterData('`.concat(ROUTER_DATA_JSON_ID, "');").concat(initRouterDataAttrs);
|
|
8
8
|
var runRouterDataFnStr = "runRouterDataFn();";
|
|
9
9
|
var runWindowFnStr = "runWindowFn();";
|
|
10
10
|
export {
|
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
import { useRouteLoaderData as useRouteData } from "@modern-js/runtime-utils/router";
|
|
2
|
-
import { routerPlugin } from "./plugin";
|
|
3
2
|
export * from "@modern-js/runtime-utils/router";
|
|
4
|
-
import { renderRoutes } from "./utils";
|
|
5
|
-
var runtime_default = routerPlugin;
|
|
6
|
-
import { modifyRoutes } from "./plugin";
|
|
7
|
-
export * from "./withRouter";
|
|
8
3
|
import { Link, NavLink } from "./PrefetchLink";
|
|
9
4
|
var useRouteLoaderData = function(routeId) {
|
|
10
5
|
var realRouteId = routeId.replace(/\[(.*?)\]/g, "($1)");
|
|
11
6
|
return useRouteData(realRouteId);
|
|
12
7
|
};
|
|
13
8
|
import { createShouldRevalidate, handleRouteModule, handleRouteModuleError } from "./routeModule";
|
|
9
|
+
export * from "./withRouter";
|
|
14
10
|
export {
|
|
15
11
|
Link,
|
|
16
12
|
NavLink,
|
|
17
13
|
createShouldRevalidate,
|
|
18
|
-
runtime_default as default,
|
|
19
14
|
handleRouteModule,
|
|
20
15
|
handleRouteModuleError,
|
|
21
|
-
modifyRoutes,
|
|
22
|
-
renderRoutes,
|
|
23
|
-
routerPlugin,
|
|
24
16
|
useRouteLoaderData
|
|
25
17
|
};
|
|
@@ -5,11 +5,14 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
5
5
|
import { merge } from "@modern-js/runtime-utils/merge";
|
|
6
6
|
import { RouterProvider, createBrowserRouter, createHashRouter, createRoutesFromElements, useHref, useLocation, useMatches } from "@modern-js/runtime-utils/router";
|
|
7
7
|
import { normalizePathname } from "@modern-js/runtime-utils/url";
|
|
8
|
-
import
|
|
8
|
+
import * as React from "react";
|
|
9
|
+
import { useContext, useEffect, useMemo } from "react";
|
|
9
10
|
import { RuntimeReactContext } from "../../core";
|
|
10
11
|
import { getGlobalLayoutApp, getGlobalRoutes } from "../../core/context";
|
|
12
|
+
import { getGlobalIsRscClient } from "../../core/context";
|
|
11
13
|
import { modifyRoutes as modifyRoutesHook, onBeforeCreateRoutes as onBeforeCreateRoutesHook } from "./hooks";
|
|
12
|
-
import {
|
|
14
|
+
import { createClientRouterFromPayload } from "./rsc-router";
|
|
15
|
+
import { createRouteObjectsFromConfig, deserializeErrors, renderRoutes, urlJoin } from "./utils";
|
|
13
16
|
var finalRouteConfig = {
|
|
14
17
|
routes: []
|
|
15
18
|
};
|
|
@@ -32,7 +35,9 @@ var routerPlugin = function() {
|
|
|
32
35
|
onBeforeCreateRoutes: onBeforeCreateRoutesHook
|
|
33
36
|
},
|
|
34
37
|
setup: function(api) {
|
|
35
|
-
var
|
|
38
|
+
var routesContainer = {
|
|
39
|
+
current: []
|
|
40
|
+
};
|
|
36
41
|
api.onBeforeRender(function(context) {
|
|
37
42
|
if (window._SSR_DATA && userConfig.unstable_reloadOnURLMismatch) {
|
|
38
43
|
var _ssrContext_request;
|
|
@@ -52,19 +57,14 @@ var routerPlugin = function() {
|
|
|
52
57
|
};
|
|
53
58
|
Object.defineProperty(context, "routes", {
|
|
54
59
|
get: function get() {
|
|
55
|
-
return
|
|
60
|
+
return routesContainer.current;
|
|
56
61
|
},
|
|
57
62
|
enumerable: true
|
|
58
63
|
});
|
|
59
64
|
});
|
|
60
65
|
api.wrapRoot(function(App) {
|
|
61
|
-
var
|
|
62
|
-
var
|
|
63
|
-
var select = function(pathname) {
|
|
64
|
-
return serverBase.find(function(baseUrl) {
|
|
65
|
-
return pathname.search(baseUrl) === 0;
|
|
66
|
-
}) || "/";
|
|
67
|
-
};
|
|
66
|
+
var mergedConfig = merge(api.getRuntimeConfig().router || {}, userConfig);
|
|
67
|
+
var _mergedConfig_serverBase = mergedConfig.serverBase, serverBase = _mergedConfig_serverBase === void 0 ? [] : _mergedConfig_serverBase, _mergedConfig_supportHtml5History = mergedConfig.supportHtml5History, supportHtml5History = _mergedConfig_supportHtml5History === void 0 ? true : _mergedConfig_supportHtml5History, _mergedConfig_basename = mergedConfig.basename, basename = _mergedConfig_basename === void 0 ? "" : _mergedConfig_basename, routesConfig = mergedConfig.routesConfig, createRoutes = mergedConfig.createRoutes, future = mergedConfig.future;
|
|
68
68
|
finalRouteConfig = _object_spread({
|
|
69
69
|
routes: getGlobalRoutes(),
|
|
70
70
|
globalApp: getGlobalLayoutApp()
|
|
@@ -72,86 +72,136 @@ var routerPlugin = function() {
|
|
|
72
72
|
if (!finalRouteConfig.routes && !createRoutes) {
|
|
73
73
|
return App;
|
|
74
74
|
}
|
|
75
|
-
var
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (blockRoute) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
return listener.apply(void 0, _to_consumable_array(args));
|
|
112
|
-
};
|
|
113
|
-
return originSubscribe(wrapedListener);
|
|
114
|
-
};
|
|
115
|
-
return router;
|
|
116
|
-
}, [
|
|
117
|
-
finalRouteConfig,
|
|
118
|
-
props,
|
|
119
|
-
_basename,
|
|
120
|
-
hydrationData,
|
|
121
|
-
getBlockNavState
|
|
122
|
-
]);
|
|
123
|
-
};
|
|
124
|
-
var Null = function() {
|
|
125
|
-
return null;
|
|
126
|
-
};
|
|
127
|
-
return function(props) {
|
|
128
|
-
beforeCreateRouter = false;
|
|
129
|
-
var router = useCreateRouter(props);
|
|
130
|
-
var routerWrapper = (
|
|
131
|
-
// To match the node tree about https://github.com/web-infra-dev/modern.js/blob/v2.59.0/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx#L150-L168
|
|
132
|
-
// According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
|
|
133
|
-
// To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
|
|
134
|
-
/* @__PURE__ */ _jsxs(_Fragment, {
|
|
135
|
-
children: [
|
|
136
|
-
/* @__PURE__ */ _jsx(RouterProvider, {
|
|
137
|
-
router,
|
|
138
|
-
future
|
|
139
|
-
}),
|
|
140
|
-
/* @__PURE__ */ _jsx(Null, {}),
|
|
141
|
-
/* @__PURE__ */ _jsx(Null, {})
|
|
142
|
-
]
|
|
143
|
-
})
|
|
144
|
-
);
|
|
145
|
-
return App ? /* @__PURE__ */ _jsx(App, {
|
|
146
|
-
children: routerWrapper
|
|
147
|
-
}) : routerWrapper;
|
|
75
|
+
var selectBasePath = function(pathname) {
|
|
76
|
+
return serverBase.find(function(baseUrl) {
|
|
77
|
+
return pathname.search(baseUrl) === 0;
|
|
78
|
+
}) || "/";
|
|
79
|
+
};
|
|
80
|
+
var RouterWrapper = function(props) {
|
|
81
|
+
var _useRouterCreation = useRouterCreation(_object_spread_props(_object_spread({}, props), {
|
|
82
|
+
rscPayload: props === null || props === void 0 ? void 0 : props.rscPayload
|
|
83
|
+
}), {
|
|
84
|
+
api,
|
|
85
|
+
createRoutes,
|
|
86
|
+
supportHtml5History,
|
|
87
|
+
selectBasePath,
|
|
88
|
+
basename,
|
|
89
|
+
future
|
|
90
|
+
}), router = _useRouterCreation.router, routes = _useRouterCreation.routes;
|
|
91
|
+
useEffect(function() {
|
|
92
|
+
routesContainer.current = routes;
|
|
93
|
+
}, [
|
|
94
|
+
routes
|
|
95
|
+
]);
|
|
96
|
+
beforeCreateRouter = false;
|
|
97
|
+
var RouterContent = function() {
|
|
98
|
+
return /* @__PURE__ */ _jsxs(_Fragment, {
|
|
99
|
+
children: [
|
|
100
|
+
/* @__PURE__ */ _jsx(RouterProvider, {
|
|
101
|
+
router,
|
|
102
|
+
future
|
|
103
|
+
}),
|
|
104
|
+
/* @__PURE__ */ _jsx(EmptyComponent, {}),
|
|
105
|
+
/* @__PURE__ */ _jsx(EmptyComponent, {})
|
|
106
|
+
]
|
|
107
|
+
});
|
|
148
108
|
};
|
|
109
|
+
return App ? /* @__PURE__ */ _jsx(App, {
|
|
110
|
+
children: /* @__PURE__ */ _jsx(RouterContent, {})
|
|
111
|
+
}) : /* @__PURE__ */ _jsx(RouterContent, {});
|
|
149
112
|
};
|
|
150
|
-
return
|
|
113
|
+
return RouterWrapper;
|
|
151
114
|
});
|
|
152
115
|
}
|
|
153
116
|
};
|
|
154
117
|
};
|
|
118
|
+
var EmptyComponent = function() {
|
|
119
|
+
return null;
|
|
120
|
+
};
|
|
121
|
+
var safeUse = function(promise) {
|
|
122
|
+
var useProp = "use";
|
|
123
|
+
var useHook = React && React[useProp];
|
|
124
|
+
if (typeof useHook === "function") {
|
|
125
|
+
return useHook(promise);
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
};
|
|
129
|
+
function useRouterCreation(props, options) {
|
|
130
|
+
var api = options.api, createRoutes = options.createRoutes, supportHtml5History = options.supportHtml5History, selectBasePath = options.selectBasePath, basename = options.basename;
|
|
131
|
+
var runtimeContext = useContext(RuntimeReactContext);
|
|
132
|
+
var baseUrl = selectBasePath(location.pathname).replace(/^\/*/, "/");
|
|
133
|
+
var _basename = baseUrl === "/" ? urlJoin(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
|
|
134
|
+
var getBlockNavState = runtimeContext.unstable_getBlockNavState;
|
|
135
|
+
var rscPayload = (props === null || props === void 0 ? void 0 : props.rscPayload) ? safeUse(props.rscPayload) : null;
|
|
136
|
+
var hydrationData = window._ROUTER_DATA || rscPayload;
|
|
137
|
+
return useMemo(function() {
|
|
138
|
+
if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
|
|
139
|
+
hydrationData = _object_spread_props(_object_spread({}, hydrationData), {
|
|
140
|
+
errors: deserializeErrors(hydrationData.errors)
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
var isRscClient = getGlobalIsRscClient();
|
|
144
|
+
var routes = null;
|
|
145
|
+
if (isRscClient) {
|
|
146
|
+
routes = createRoutes ? createRoutes() : createRouteObjectsFromConfig({
|
|
147
|
+
routesConfig: finalRouteConfig
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
|
|
151
|
+
routesConfig: finalRouteConfig,
|
|
152
|
+
props
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
if (!routes) {
|
|
156
|
+
routes = [];
|
|
157
|
+
}
|
|
158
|
+
var hooks = api.getHooks();
|
|
159
|
+
if (rscPayload) {
|
|
160
|
+
try {
|
|
161
|
+
var router = createClientRouterFromPayload(rscPayload, routes, _basename);
|
|
162
|
+
return {
|
|
163
|
+
router,
|
|
164
|
+
routes: router.routes || []
|
|
165
|
+
};
|
|
166
|
+
} catch (e) {
|
|
167
|
+
console.error("Failed to create router from RSC payload:", e);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
var modifiedRoutes = hooks.modifyRoutes.call(routes);
|
|
171
|
+
console.log("modifiedRoutes111111", modifiedRoutes, _basename, hydrationData);
|
|
172
|
+
var router1 = supportHtml5History ? createBrowserRouter(modifiedRoutes, {
|
|
173
|
+
basename: _basename,
|
|
174
|
+
hydrationData
|
|
175
|
+
}) : createHashRouter(modifiedRoutes, {
|
|
176
|
+
basename: _basename,
|
|
177
|
+
hydrationData
|
|
178
|
+
});
|
|
179
|
+
var originSubscribe = router1.subscribe;
|
|
180
|
+
router1.subscribe = function(listener) {
|
|
181
|
+
var wrappedListener = function() {
|
|
182
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
183
|
+
args[_key] = arguments[_key];
|
|
184
|
+
}
|
|
185
|
+
var blockRoute = getBlockNavState ? getBlockNavState() : false;
|
|
186
|
+
if (blockRoute) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
return listener.apply(void 0, _to_consumable_array(args));
|
|
190
|
+
};
|
|
191
|
+
return originSubscribe(wrappedListener);
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
router: router1,
|
|
195
|
+
routes: modifiedRoutes
|
|
196
|
+
};
|
|
197
|
+
}, [
|
|
198
|
+
finalRouteConfig,
|
|
199
|
+
props,
|
|
200
|
+
_basename,
|
|
201
|
+
hydrationData,
|
|
202
|
+
getBlockNavState
|
|
203
|
+
]);
|
|
204
|
+
}
|
|
155
205
|
export {
|
|
156
206
|
beforeCreateRouter,
|
|
157
207
|
finalRouteConfig,
|
|
@@ -13,10 +13,12 @@ import { LOADER_REPORTER_NAME } from "@modern-js/utils/universal/constants";
|
|
|
13
13
|
import { useContext } from "react";
|
|
14
14
|
import { JSX_SHELL_STREAM_END_MARK } from "../../common";
|
|
15
15
|
import { RuntimeReactContext } from "../../core";
|
|
16
|
-
import { getGlobalLayoutApp, getGlobalRoutes } from "../../core/context";
|
|
16
|
+
import { getGlobalEnableRsc, getGlobalLayoutApp, getGlobalRoutes } from "../../core/context";
|
|
17
|
+
import { setServerPayload } from "../../core/context/serverPayload.server";
|
|
17
18
|
import DeferredDataScripts from "./DeferredDataScripts.node";
|
|
18
19
|
import { modifyRoutes as modifyRoutesHook, onBeforeCreateRoutes as onBeforeCreateRoutesHook } from "./hooks";
|
|
19
|
-
import {
|
|
20
|
+
import { RSCStaticRouter, createServerPayload, handleRSCRedirect, prepareRSCRoutes } from "./rsc-router";
|
|
21
|
+
import { createRouteObjectsFromConfig, renderRoutes, urlJoin } from "./utils";
|
|
20
22
|
function createRemixReuqest(request) {
|
|
21
23
|
var method = "GET";
|
|
22
24
|
var headers = request.headers;
|
|
@@ -39,7 +41,7 @@ var routerPlugin = function() {
|
|
|
39
41
|
var finalRouteConfig = {};
|
|
40
42
|
api.onBeforeRender(function() {
|
|
41
43
|
var _ref = _async_to_generator(function(context, interrupt) {
|
|
42
|
-
var _context_ssrContext, _context_ssrContext_onTiming, _context_ssrContext1, pluginConfig, _merge, _merge_basename, basename, routesConfig, createRoutes, _context_ssrContext2, request, ssrMode, nonce, _context_ssrContext_loaderFailureMode, loaderFailureMode, baseUrl, _basename, reporter, requestContext, hooks, routes, query, remixRequest, end, routerContext, cost, errors,
|
|
44
|
+
var _context_ssrContext, _context_ssrContext_onTiming, _context_ssrContext1, pluginConfig, _merge, _merge_basename, basename, routesConfig, createRoutes, enableRsc, _context_ssrContext2, request, ssrMode, nonce, _context_ssrContext_loaderFailureMode, loaderFailureMode, baseUrl, _basename, reporter, requestContext, hooks, routes, query, remixRequest, end, routerContext, cost, isRSCNavigation, errors, payload, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, match;
|
|
43
45
|
return _ts_generator(this, function(_state) {
|
|
44
46
|
switch (_state.label) {
|
|
45
47
|
case 0:
|
|
@@ -54,6 +56,20 @@ var routerPlugin = function() {
|
|
|
54
56
|
2
|
|
55
57
|
];
|
|
56
58
|
}
|
|
59
|
+
enableRsc = getGlobalEnableRsc();
|
|
60
|
+
if (!enableRsc)
|
|
61
|
+
return [
|
|
62
|
+
3,
|
|
63
|
+
2
|
|
64
|
+
];
|
|
65
|
+
return [
|
|
66
|
+
4,
|
|
67
|
+
prepareRSCRoutes(finalRouteConfig.routes)
|
|
68
|
+
];
|
|
69
|
+
case 1:
|
|
70
|
+
_state.sent();
|
|
71
|
+
_state.label = 2;
|
|
72
|
+
case 2:
|
|
57
73
|
_context_ssrContext2 = context.ssrContext, request = _context_ssrContext2.request, ssrMode = _context_ssrContext2.mode, nonce = _context_ssrContext2.nonce, _context_ssrContext_loaderFailureMode = _context_ssrContext2.loaderFailureMode, loaderFailureMode = _context_ssrContext_loaderFailureMode === void 0 ? "errorBoundary" : _context_ssrContext_loaderFailureMode;
|
|
58
74
|
baseUrl = request.baseUrl;
|
|
59
75
|
_basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
|
|
@@ -65,15 +81,22 @@ var routerPlugin = function() {
|
|
|
65
81
|
4,
|
|
66
82
|
hooks.onBeforeCreateRoutes.call(context)
|
|
67
83
|
];
|
|
68
|
-
case
|
|
84
|
+
case 3:
|
|
69
85
|
_state.sent();
|
|
70
|
-
routes =
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
86
|
+
routes = [];
|
|
87
|
+
if (enableRsc) {
|
|
88
|
+
routes = createRoutes ? createRoutes() : createRouteObjectsFromConfig({
|
|
89
|
+
routesConfig: finalRouteConfig
|
|
90
|
+
});
|
|
91
|
+
} else {
|
|
92
|
+
routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
|
|
93
|
+
routesConfig: finalRouteConfig,
|
|
94
|
+
ssrMode,
|
|
95
|
+
props: {
|
|
96
|
+
nonce
|
|
97
|
+
}
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
77
100
|
routes = hooks.modifyRoutes.call(routes);
|
|
78
101
|
query = createStaticHandler(routes, {
|
|
79
102
|
basename: _basename
|
|
@@ -86,15 +109,23 @@ var routerPlugin = function() {
|
|
|
86
109
|
requestContext
|
|
87
110
|
})
|
|
88
111
|
];
|
|
89
|
-
case
|
|
112
|
+
case 4:
|
|
90
113
|
routerContext = _state.sent();
|
|
91
114
|
cost = end();
|
|
92
115
|
(_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, LOADER_REPORTER_NAME, cost);
|
|
116
|
+
isRSCNavigation = remixRequest.headers.get("x-rsc-tree") === "true";
|
|
93
117
|
if (_instanceof(routerContext, Response)) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
118
|
+
if (enableRsc && isRSCNavigation) {
|
|
119
|
+
return [
|
|
120
|
+
2,
|
|
121
|
+
interrupt(handleRSCRedirect(routerContext.headers, _basename, routerContext.status))
|
|
122
|
+
];
|
|
123
|
+
} else {
|
|
124
|
+
return [
|
|
125
|
+
2,
|
|
126
|
+
interrupt(routerContext)
|
|
127
|
+
];
|
|
128
|
+
}
|
|
98
129
|
}
|
|
99
130
|
errors = Object.values(routerContext.errors || {});
|
|
100
131
|
if (
|
|
@@ -104,9 +135,35 @@ var routerPlugin = function() {
|
|
|
104
135
|
routerContext.statusCode = 200;
|
|
105
136
|
throw errors[0];
|
|
106
137
|
}
|
|
107
|
-
router = createStaticRouter(routes, routerContext);
|
|
108
138
|
context.routerContext = routerContext;
|
|
109
|
-
|
|
139
|
+
if (enableRsc) {
|
|
140
|
+
if (isRSCNavigation) {
|
|
141
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
142
|
+
try {
|
|
143
|
+
for (_iterator = routerContext.matches[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
144
|
+
match = _step.value;
|
|
145
|
+
if (match.route.hasClientLoader) {
|
|
146
|
+
delete routerContext.loaderData[match.route.id];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
} catch (err) {
|
|
150
|
+
_didIteratorError = true;
|
|
151
|
+
_iteratorError = err;
|
|
152
|
+
} finally {
|
|
153
|
+
try {
|
|
154
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
155
|
+
_iterator.return();
|
|
156
|
+
}
|
|
157
|
+
} finally {
|
|
158
|
+
if (_didIteratorError) {
|
|
159
|
+
throw _iteratorError;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
payload = createServerPayload(routerContext, routes);
|
|
165
|
+
setServerPayload(payload);
|
|
166
|
+
}
|
|
110
167
|
Object.defineProperty(context, "routes", {
|
|
111
168
|
get: function get() {
|
|
112
169
|
return routes;
|
|
@@ -128,30 +185,43 @@ var routerPlugin = function() {
|
|
|
128
185
|
return App;
|
|
129
186
|
}
|
|
130
187
|
var getRouteApp = function() {
|
|
131
|
-
|
|
188
|
+
var enableRsc = getGlobalEnableRsc();
|
|
189
|
+
return function(props) {
|
|
132
190
|
var context = useContext(RuntimeReactContext);
|
|
133
|
-
var
|
|
191
|
+
var routerContext = context.routerContext, ssrContext = context.ssrContext, routes = context.routes;
|
|
134
192
|
var nonce = ssrContext.nonce, mode = ssrContext.mode, useJsonScript = ssrContext.useJsonScript;
|
|
135
|
-
var
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
193
|
+
var basename = routerContext.basename;
|
|
194
|
+
var remixRouter = createStaticRouter(routes, routerContext);
|
|
195
|
+
if (!enableRsc) {
|
|
196
|
+
var routerWrapper = /* @__PURE__ */ _jsxs(_Fragment, {
|
|
197
|
+
children: [
|
|
198
|
+
/* @__PURE__ */ _jsx(StaticRouterProvider, {
|
|
199
|
+
router: remixRouter,
|
|
200
|
+
context: routerContext,
|
|
201
|
+
hydrate: false
|
|
202
|
+
}),
|
|
203
|
+
mode === "stream" && // ROUTER_DATA will inject in `packages/runtime/plugin-runtime/src/core/server/string/ssrData.ts` in string ssr
|
|
204
|
+
// So we can inject it only when streaming ssr
|
|
205
|
+
/* @__PURE__ */ _jsx(DeferredDataScripts, {
|
|
206
|
+
nonce,
|
|
207
|
+
context: routerContext,
|
|
208
|
+
useJsonScript
|
|
209
|
+
}),
|
|
210
|
+
mode === "stream" && JSX_SHELL_STREAM_END_MARK
|
|
211
|
+
]
|
|
212
|
+
});
|
|
213
|
+
return App ? /* @__PURE__ */ _jsx(App, {
|
|
214
|
+
children: routerWrapper
|
|
215
|
+
}) : routerWrapper;
|
|
216
|
+
} else {
|
|
217
|
+
return App ? /* @__PURE__ */ _jsx(App, {
|
|
218
|
+
children: /* @__PURE__ */ _jsx(RSCStaticRouter, {
|
|
219
|
+
basename
|
|
220
|
+
})
|
|
221
|
+
}) : /* @__PURE__ */ _jsx(RSCStaticRouter, {
|
|
222
|
+
basename
|
|
223
|
+
});
|
|
224
|
+
}
|
|
155
225
|
};
|
|
156
226
|
};
|
|
157
227
|
return getRouteApp();
|