@modern-js/runtime 2.5.0 → 2.7.0
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/CHANGELOG.md +55 -0
- package/dist/cjs/cli/index.js +9 -0
- package/dist/cjs/common.js +6 -0
- package/dist/cjs/core/{app-config.js → appConfig.js} +8 -4
- package/dist/cjs/core/compatible.js +11 -10
- package/dist/cjs/core/index.js +6 -6
- package/dist/cjs/core/loader/index.js +4 -0
- package/dist/cjs/core/loader/loaderManager.js +6 -0
- package/dist/cjs/core/loader/useLoader.js +6 -2
- package/dist/cjs/core/types.js +15 -0
- package/dist/cjs/document/Body.js +6 -8
- package/dist/cjs/document/DocumentContext.js +4 -0
- package/dist/cjs/document/DocumentStructureContext.js +4 -0
- package/dist/cjs/document/Head.js +7 -11
- package/dist/cjs/document/Html.js +24 -22
- package/dist/cjs/document/Links.js +1 -3
- package/dist/cjs/document/Root.js +8 -14
- package/dist/cjs/document/Script.js +5 -7
- package/dist/cjs/document/Scripts.js +1 -3
- package/dist/cjs/document/cli/index.js +8 -2
- package/dist/cjs/exports/head.js +4 -0
- package/dist/cjs/exports/loadable.js +4 -0
- package/dist/cjs/exports/styled.js +4 -0
- package/dist/cjs/router/cli/index.js +3 -7
- package/dist/cjs/router/index.js +4 -0
- package/dist/cjs/router/runtime/DefaultNotFound.js +13 -10
- package/dist/cjs/router/runtime/DeferredDataScripts.js +165 -0
- package/dist/cjs/router/runtime/index.js +116 -3
- package/dist/cjs/router/runtime/plugin.js +18 -6
- package/dist/cjs/router/runtime/plugin.node.js +16 -49
- package/dist/cjs/router/runtime/utils.js +79 -101
- package/dist/cjs/router/runtime/withRouter.js +9 -6
- package/dist/cjs/{runtime-context.js → runtimeContext.js} +3 -3
- package/dist/cjs/ssr/cli/babel-plugin-ssr-loader-id.js +4 -0
- package/dist/cjs/ssr/cli/index.js +6 -0
- package/dist/cjs/ssr/index.js +10 -24
- package/dist/cjs/ssr/index.node.js +5 -0
- package/dist/cjs/ssr/prefetch.js +3 -6
- package/dist/cjs/ssr/react/nossr/index.js +4 -0
- package/dist/cjs/ssr/react/prerender/index.js +4 -0
- package/dist/cjs/ssr/serverRender/renderToStream/buildTemplate.after.js +7 -13
- package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +5 -0
- package/dist/cjs/ssr/serverRender/renderToStream/index.js +4 -0
- package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +22 -7
- package/dist/cjs/ssr/serverRender/renderToStream/template.js +2 -1
- package/dist/cjs/ssr/serverRender/renderToString/entry.js +28 -11
- package/dist/cjs/ssr/serverRender/renderToString/index.js +4 -0
- package/dist/cjs/ssr/serverRender/renderToString/loadable.js +1 -1
- package/dist/cjs/ssr/serverRender/time.worker.js +1 -1
- package/dist/cjs/ssr/serverRender/utils.js +1 -1
- package/dist/cjs/ssr/utils.js +16 -4
- package/dist/cjs/state/index.js +4 -0
- package/dist/cjs/state/plugins.js +4 -0
- package/dist/cjs/state/runtime/index.js +4 -0
- package/dist/cjs/state/runtime/plugin.js +5 -7
- package/dist/esm/cli/index.js +5 -1
- package/dist/esm/common.js +3 -1
- package/dist/esm/core/{app-config.js → appConfig.js} +3 -1
- package/dist/esm/core/compatible.js +4 -2
- package/dist/esm/core/index.js +2 -2
- package/dist/esm/core/loader/loaderManager.js +3 -1
- package/dist/esm/core/loader/useLoader.js +1 -1
- package/dist/esm/core/types.js +1 -0
- package/dist/esm/document/cli/index.js +4 -2
- package/dist/esm/router/cli/index.js +1 -5
- package/dist/esm/router/runtime/DeferredDataScripts.js +166 -0
- package/dist/esm/router/runtime/index.js +3 -2
- package/dist/esm/router/runtime/plugin.js +11 -3
- package/dist/esm/router/runtime/plugin.node.js +11 -68
- package/dist/esm/router/runtime/utils.js +138 -81
- package/dist/esm/ssr/cli/index.js +2 -0
- package/dist/esm/ssr/index.node.js +1 -0
- package/dist/esm/ssr/prefetch.js +1 -0
- package/dist/esm/ssr/serverRender/renderToStream/buildTemplate.after.js +37 -10
- package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +1 -0
- package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +19 -8
- package/dist/esm/ssr/serverRender/renderToStream/template.js +2 -1
- package/dist/esm/ssr/serverRender/renderToString/entry.js +37 -13
- package/dist/esm/ssr/serverRender/time.worker.js +1 -1
- package/dist/esm/ssr/utils.js +6 -4
- package/dist/esm-node/cli/index.js +5 -0
- package/dist/esm-node/common.js +4 -0
- package/dist/esm-node/core/{app-config.js → appConfig.js} +5 -1
- package/dist/esm-node/core/compatible.js +5 -8
- package/dist/esm-node/core/index.js +2 -2
- package/dist/esm-node/core/loader/loaderManager.js +2 -0
- package/dist/esm-node/core/loader/useLoader.js +1 -1
- package/dist/esm-node/core/types.js +0 -0
- package/dist/esm-node/document/Body.js +6 -8
- package/dist/esm-node/document/Head.js +7 -11
- package/dist/esm-node/document/Html.js +24 -22
- package/dist/esm-node/document/Links.js +1 -3
- package/dist/esm-node/document/Root.js +8 -14
- package/dist/esm-node/document/Script.js +5 -7
- package/dist/esm-node/document/Scripts.js +1 -3
- package/dist/esm-node/document/cli/index.js +4 -2
- package/dist/esm-node/router/cli/index.js +2 -5
- package/dist/esm-node/router/runtime/DefaultNotFound.js +13 -10
- package/dist/esm-node/router/runtime/DeferredDataScripts.js +148 -0
- package/dist/esm-node/router/runtime/index.js +113 -2
- package/dist/esm-node/router/runtime/plugin.js +15 -7
- package/dist/esm-node/router/runtime/plugin.node.js +12 -52
- package/dist/esm-node/router/runtime/utils.js +70 -101
- package/dist/esm-node/router/runtime/withRouter.js +9 -6
- package/dist/esm-node/ssr/cli/index.js +2 -0
- package/dist/esm-node/ssr/index.js +6 -24
- package/dist/esm-node/ssr/index.node.js +1 -0
- package/dist/esm-node/ssr/prefetch.js +3 -6
- package/dist/esm-node/ssr/serverRender/renderToStream/buildTemplate.after.js +7 -7
- package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +1 -0
- package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +22 -7
- package/dist/esm-node/ssr/serverRender/renderToStream/template.js +2 -1
- package/dist/esm-node/ssr/serverRender/renderToString/entry.js +24 -11
- package/dist/esm-node/ssr/serverRender/renderToString/loadable.js +1 -1
- package/dist/esm-node/ssr/serverRender/time.worker.js +1 -1
- package/dist/esm-node/ssr/serverRender/utils.js +1 -1
- package/dist/esm-node/ssr/utils.js +6 -4
- package/dist/esm-node/state/runtime/plugin.js +1 -7
- package/dist/types/common.d.ts +3 -1
- package/dist/types/core/compatible.d.ts +1 -1
- package/dist/types/core/index.d.ts +4 -3
- package/dist/types/core/loader/index.d.ts +1 -2
- package/dist/types/core/loader/useLoader.d.ts +1 -15
- package/dist/types/core/plugin.d.ts +15 -15
- package/dist/types/core/types.d.ts +22 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/router/runtime/DeferredDataScripts.d.ts +8 -0
- package/dist/types/router/runtime/index.d.ts +4 -2
- package/dist/types/router/runtime/utils.d.ts +23 -4
- package/dist/types/{runtime-context.d.ts → runtimeContext.d.ts} +2 -0
- package/dist/types/ssr/serverRender/renderToStream/buildTemplate.after.d.ts +2 -0
- package/dist/types/ssr/serverRender/renderToString/type.d.ts +1 -0
- package/package.json +14 -15
- package/dist/cjs/ssr/prefetch.worker.js +0 -67
- package/dist/cjs/ssr/serverRender/renderToStream/index.worker.js +0 -64
- package/dist/cjs/ssr/serverRender/renderToString/index.worker.js +0 -58
- package/dist/esm/ssr/prefetch.worker.js +0 -191
- package/dist/esm/ssr/serverRender/renderToStream/index.worker.js +0 -32
- package/dist/esm/ssr/serverRender/renderToString/index.worker.js +0 -167
- package/dist/esm-node/ssr/prefetch.worker.js +0 -46
- package/dist/esm-node/ssr/serverRender/renderToStream/index.worker.js +0 -35
- package/dist/esm-node/ssr/serverRender/renderToString/index.worker.js +0 -29
- package/dist/types/ssr/prefetch.worker.d.ts +0 -13
- package/dist/types/ssr/serverRender/renderToStream/index.worker.d.ts +0 -6
- package/dist/types/ssr/serverRender/renderToString/index.worker.d.ts +0 -6
- /package/dist/esm/{runtime-context.js → runtimeContext.js} +0 -0
- /package/dist/esm-node/{runtime-context.js → runtimeContext.js} +0 -0
- /package/dist/types/core/{app-config.d.ts → appConfig.d.ts} +0 -0
|
@@ -1,9 +1,120 @@
|
|
|
1
1
|
import { routerPlugin } from "./plugin";
|
|
2
2
|
var runtime_default = routerPlugin;
|
|
3
3
|
import { modifyRoutes } from "./plugin";
|
|
4
|
-
export * from "react-router-dom";
|
|
5
4
|
export * from "./withRouter";
|
|
5
|
+
import {
|
|
6
|
+
createBrowserRouter,
|
|
7
|
+
createHashRouter,
|
|
8
|
+
createMemoryRouter,
|
|
9
|
+
RouterProvider,
|
|
10
|
+
BrowserRouter,
|
|
11
|
+
HashRouter,
|
|
12
|
+
MemoryRouter,
|
|
13
|
+
Router,
|
|
14
|
+
Await,
|
|
15
|
+
Form,
|
|
16
|
+
Link,
|
|
17
|
+
NavLink,
|
|
18
|
+
Navigate,
|
|
19
|
+
Outlet,
|
|
20
|
+
Route,
|
|
21
|
+
Routes,
|
|
22
|
+
ScrollRestoration,
|
|
23
|
+
useActionData,
|
|
24
|
+
useAsyncError,
|
|
25
|
+
useAsyncValue,
|
|
26
|
+
useBeforeUnload,
|
|
27
|
+
useFetcher,
|
|
28
|
+
useFetchers,
|
|
29
|
+
useFormAction,
|
|
30
|
+
useHref,
|
|
31
|
+
useInRouterContext,
|
|
32
|
+
useLinkClickHandler,
|
|
33
|
+
useLoaderData,
|
|
34
|
+
useLocation,
|
|
35
|
+
useMatch,
|
|
36
|
+
useMatches,
|
|
37
|
+
useNavigate,
|
|
38
|
+
useNavigation,
|
|
39
|
+
useNavigationType,
|
|
40
|
+
useOutlet,
|
|
41
|
+
useOutletContext,
|
|
42
|
+
useParams,
|
|
43
|
+
useResolvedPath,
|
|
44
|
+
useRevalidator,
|
|
45
|
+
useRouteError,
|
|
46
|
+
useRouteLoaderData,
|
|
47
|
+
useRoutes,
|
|
48
|
+
useSearchParams,
|
|
49
|
+
useSubmit,
|
|
50
|
+
createRoutesFromChildren,
|
|
51
|
+
createRoutesFromElements,
|
|
52
|
+
createSearchParams,
|
|
53
|
+
generatePath,
|
|
54
|
+
isRouteErrorResponse,
|
|
55
|
+
matchPath,
|
|
56
|
+
matchRoutes,
|
|
57
|
+
renderMatches,
|
|
58
|
+
resolvePath
|
|
59
|
+
} from "react-router-dom";
|
|
60
|
+
import { defer, json, redirect } from "@modern-js/utils/remix-router";
|
|
6
61
|
export {
|
|
62
|
+
Await,
|
|
63
|
+
BrowserRouter,
|
|
64
|
+
Form,
|
|
65
|
+
HashRouter,
|
|
66
|
+
Link,
|
|
67
|
+
MemoryRouter,
|
|
68
|
+
NavLink,
|
|
69
|
+
Navigate,
|
|
70
|
+
Outlet,
|
|
71
|
+
Route,
|
|
72
|
+
Router,
|
|
73
|
+
RouterProvider,
|
|
74
|
+
Routes,
|
|
75
|
+
ScrollRestoration,
|
|
76
|
+
createBrowserRouter,
|
|
77
|
+
createHashRouter,
|
|
78
|
+
createMemoryRouter,
|
|
79
|
+
createRoutesFromChildren,
|
|
80
|
+
createRoutesFromElements,
|
|
81
|
+
createSearchParams,
|
|
7
82
|
runtime_default as default,
|
|
8
|
-
|
|
83
|
+
defer,
|
|
84
|
+
generatePath,
|
|
85
|
+
isRouteErrorResponse,
|
|
86
|
+
json,
|
|
87
|
+
matchPath,
|
|
88
|
+
matchRoutes,
|
|
89
|
+
modifyRoutes,
|
|
90
|
+
redirect,
|
|
91
|
+
renderMatches,
|
|
92
|
+
resolvePath,
|
|
93
|
+
useActionData,
|
|
94
|
+
useAsyncError,
|
|
95
|
+
useAsyncValue,
|
|
96
|
+
useBeforeUnload,
|
|
97
|
+
useFetcher,
|
|
98
|
+
useFetchers,
|
|
99
|
+
useFormAction,
|
|
100
|
+
useHref,
|
|
101
|
+
useInRouterContext,
|
|
102
|
+
useLinkClickHandler,
|
|
103
|
+
useLoaderData,
|
|
104
|
+
useLocation,
|
|
105
|
+
useMatch,
|
|
106
|
+
useMatches,
|
|
107
|
+
useNavigate,
|
|
108
|
+
useNavigation,
|
|
109
|
+
useNavigationType,
|
|
110
|
+
useOutlet,
|
|
111
|
+
useOutletContext,
|
|
112
|
+
useParams,
|
|
113
|
+
useResolvedPath,
|
|
114
|
+
useRevalidator,
|
|
115
|
+
useRouteError,
|
|
116
|
+
useRouteLoaderData,
|
|
117
|
+
useRoutes,
|
|
118
|
+
useSearchParams,
|
|
119
|
+
useSubmit
|
|
9
120
|
};
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
useLocation
|
|
9
9
|
} from "react-router-dom";
|
|
10
10
|
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
11
|
-
import { renderRoutes, urlJoin } from "./utils";
|
|
11
|
+
import { deserializeErrors, renderRoutes, urlJoin } from "./utils";
|
|
12
12
|
let finalRouteConfig = {
|
|
13
13
|
routes: []
|
|
14
14
|
};
|
|
@@ -55,13 +55,21 @@ const routerPlugin = ({
|
|
|
55
55
|
const routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes(finalRouteConfig));
|
|
56
56
|
const baseUrl = ((_a = window._SERVER_DATA) == null ? void 0 : _a.router.baseUrl) || select(location.pathname);
|
|
57
57
|
const _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
58
|
+
let hydrationData = window._ROUTER_DATA;
|
|
59
|
+
if (hydrationData == null ? void 0 : hydrationData.errors) {
|
|
60
|
+
hydrationData = {
|
|
61
|
+
...hydrationData,
|
|
62
|
+
errors: deserializeErrors(hydrationData.errors)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const router = supportHtml5History ? createBrowserRouter(routes, {
|
|
66
|
+
basename: _basename,
|
|
67
|
+
hydrationData
|
|
68
|
+
}) : createHashRouter(routes, {
|
|
69
|
+
basename: _basename,
|
|
70
|
+
hydrationData
|
|
64
71
|
});
|
|
72
|
+
return /* @__PURE__ */ jsx(App, { ...props, children: /* @__PURE__ */ jsx(RouterProvider, { router }) });
|
|
65
73
|
};
|
|
66
74
|
};
|
|
67
75
|
const RouteApp = getRouteApp();
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { jsx
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useContext } from "react";
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
createStaticHandler,
|
|
6
|
-
isRouteErrorResponse
|
|
7
|
-
} from "@remix-run/router";
|
|
3
|
+
import { createStaticHandler } from "@modern-js/utils/remix-router";
|
|
8
4
|
import {
|
|
9
5
|
createStaticRouter,
|
|
10
6
|
StaticRouterProvider
|
|
@@ -41,26 +37,6 @@ function createFetchHeaders(requestHeaders) {
|
|
|
41
37
|
}
|
|
42
38
|
return headers;
|
|
43
39
|
}
|
|
44
|
-
function serializeErrors(errors) {
|
|
45
|
-
if (!errors) {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
const entries = Object.entries(errors);
|
|
49
|
-
const serialized = {};
|
|
50
|
-
for (const [key, val] of entries) {
|
|
51
|
-
if (isRouteErrorResponse(val)) {
|
|
52
|
-
serialized[key] = { ...val, __type: "RouteErrorResponse" };
|
|
53
|
-
} else if (val instanceof Error) {
|
|
54
|
-
serialized[key] = {
|
|
55
|
-
message: val.message,
|
|
56
|
-
__type: "Error"
|
|
57
|
-
};
|
|
58
|
-
} else {
|
|
59
|
-
serialized[key] = val;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return serialized;
|
|
63
|
-
}
|
|
64
40
|
const routerPlugin = ({
|
|
65
41
|
basename = "",
|
|
66
42
|
routesConfig,
|
|
@@ -74,10 +50,10 @@ const routerPlugin = ({
|
|
|
74
50
|
if (!routesConfig && !createRoutes) {
|
|
75
51
|
return next({ context });
|
|
76
52
|
}
|
|
77
|
-
const { request } = context.ssrContext;
|
|
53
|
+
const { request, mode: ssrMode } = context.ssrContext;
|
|
78
54
|
const baseUrl = request.baseUrl;
|
|
79
55
|
const _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
|
|
80
|
-
const routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes(routesConfig));
|
|
56
|
+
const routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes(routesConfig, ssrMode));
|
|
81
57
|
const { query } = createStaticHandler(routes, {
|
|
82
58
|
basename: _basename
|
|
83
59
|
});
|
|
@@ -100,30 +76,14 @@ const routerPlugin = ({
|
|
|
100
76
|
const getRouteApp = () => {
|
|
101
77
|
return (props) => {
|
|
102
78
|
const { router, routerContext } = useContext(RuntimeReactContext);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
)};`;
|
|
112
|
-
return /* @__PURE__ */ jsxs(App, {
|
|
113
|
-
...props,
|
|
114
|
-
children: [
|
|
115
|
-
/* @__PURE__ */ jsx(StaticRouterProvider, {
|
|
116
|
-
router,
|
|
117
|
-
context: routerContext,
|
|
118
|
-
hydrate: false
|
|
119
|
-
}),
|
|
120
|
-
/* @__PURE__ */ jsx("script", {
|
|
121
|
-
suppressHydrationWarning: true,
|
|
122
|
-
id: "the-nonce",
|
|
123
|
-
dangerouslySetInnerHTML: { __html: hydrateScript }
|
|
124
|
-
})
|
|
125
|
-
]
|
|
126
|
-
});
|
|
79
|
+
return /* @__PURE__ */ jsx(App, { ...props, children: /* @__PURE__ */ jsx(
|
|
80
|
+
StaticRouterProvider,
|
|
81
|
+
{
|
|
82
|
+
router,
|
|
83
|
+
context: routerContext,
|
|
84
|
+
hydrate: false
|
|
85
|
+
}
|
|
86
|
+
) });
|
|
127
87
|
};
|
|
128
88
|
};
|
|
129
89
|
const RouteApp = getRouteApp();
|
|
@@ -1,103 +1,46 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Suspense } from "react";
|
|
3
2
|
import { Route } from "react-router-dom";
|
|
3
|
+
import { renderNestedRoute } from "@modern-js/utils/nestedRoutes";
|
|
4
|
+
import {
|
|
5
|
+
ErrorResponse,
|
|
6
|
+
isRouteErrorResponse
|
|
7
|
+
} from "@modern-js/utils/remix-router";
|
|
4
8
|
import { DefaultNotFound } from "./DefaultNotFound";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
path: nestedRoute.path,
|
|
11
|
-
id: nestedRoute.id,
|
|
12
|
-
loader: createLoader(nestedRoute),
|
|
13
|
-
action: nestedRoute.action,
|
|
14
|
-
hasErrorBoundary: nestedRoute.hasErrorBoundary,
|
|
15
|
-
shouldRevalidate: nestedRoute.shouldRevalidate,
|
|
16
|
-
handle: nestedRoute.handle,
|
|
17
|
-
index: nestedRoute.index,
|
|
18
|
-
element: nestedRoute.element,
|
|
19
|
-
errorElement: nestedRoute.errorElement
|
|
20
|
-
};
|
|
21
|
-
if (nestedRoute.error) {
|
|
22
|
-
const errorElement = /* @__PURE__ */ jsx(nestedRoute.error, {});
|
|
23
|
-
routeProps.errorElement = errorElement;
|
|
24
|
-
}
|
|
25
|
-
let element;
|
|
26
|
-
if (Component) {
|
|
27
|
-
if (parent == null ? void 0 : parent.loading) {
|
|
28
|
-
const Loading = parent.loading;
|
|
29
|
-
if (isLoadableComponent(Component)) {
|
|
30
|
-
element = /* @__PURE__ */ jsx(Component, {
|
|
31
|
-
fallback: /* @__PURE__ */ jsx(Loading, {})
|
|
32
|
-
});
|
|
33
|
-
} else {
|
|
34
|
-
element = /* @__PURE__ */ jsx(Suspense, {
|
|
35
|
-
fallback: /* @__PURE__ */ jsx(Loading, {}),
|
|
36
|
-
children: /* @__PURE__ */ jsx(Component, {})
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
} else if (isLoadableComponent(Component) || isRoot) {
|
|
40
|
-
element = /* @__PURE__ */ jsx(Component, {});
|
|
41
|
-
} else {
|
|
42
|
-
element = /* @__PURE__ */ jsx(Suspense, {
|
|
43
|
-
fallback: null,
|
|
44
|
-
children: /* @__PURE__ */ jsx(Component, {})
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
nestedRoute.loading = parent == null ? void 0 : parent.loading;
|
|
49
|
-
}
|
|
50
|
-
if (element) {
|
|
51
|
-
routeProps.element = element;
|
|
52
|
-
}
|
|
53
|
-
const childElements = children == null ? void 0 : children.map((childRoute) => {
|
|
54
|
-
return renderNestedRoute(childRoute, nestedRoute);
|
|
55
|
-
});
|
|
56
|
-
const routeElement = index ? /* @__PURE__ */ jsx(Route, {
|
|
57
|
-
...routeProps,
|
|
58
|
-
index: true
|
|
59
|
-
}, id) : /* @__PURE__ */ jsx(Route, {
|
|
60
|
-
...routeProps,
|
|
61
|
-
index: false,
|
|
62
|
-
children: childElements
|
|
63
|
-
}, id);
|
|
64
|
-
return routeElement;
|
|
65
|
-
};
|
|
66
|
-
function getRouteComponents(routes, globalApp) {
|
|
9
|
+
import DeferredDataScripts from "./DeferredDataScripts";
|
|
10
|
+
function getRouteComponents(routes, {
|
|
11
|
+
globalApp,
|
|
12
|
+
ssrMode
|
|
13
|
+
}) {
|
|
67
14
|
const Layout = ({ Component, ...props }) => {
|
|
68
15
|
const GlobalLayout = globalApp;
|
|
69
16
|
if (!GlobalLayout) {
|
|
70
|
-
return /* @__PURE__ */ jsx(Component, {
|
|
71
|
-
...props
|
|
72
|
-
});
|
|
17
|
+
return /* @__PURE__ */ jsx(Component, { ...props });
|
|
73
18
|
}
|
|
74
|
-
return /* @__PURE__ */ jsx(GlobalLayout, {
|
|
75
|
-
Component,
|
|
76
|
-
...props
|
|
77
|
-
});
|
|
19
|
+
return /* @__PURE__ */ jsx(GlobalLayout, { Component, ...props });
|
|
78
20
|
};
|
|
79
21
|
const routeElements = [];
|
|
80
22
|
for (const route of routes) {
|
|
81
23
|
if (route.type === "nested") {
|
|
82
|
-
const routeElement = renderNestedRoute(route
|
|
24
|
+
const routeElement = renderNestedRoute(route, {
|
|
25
|
+
DeferredDataComponent: ssrMode === "stream" ? DeferredDataScripts : void 0
|
|
26
|
+
});
|
|
83
27
|
routeElements.push(routeElement);
|
|
84
28
|
} else {
|
|
85
|
-
const routeElement = /* @__PURE__ */ jsx(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
29
|
+
const routeElement = /* @__PURE__ */ jsx(
|
|
30
|
+
Route,
|
|
31
|
+
{
|
|
32
|
+
path: route.path,
|
|
33
|
+
element: /* @__PURE__ */ jsx(Layout, { Component: route.component })
|
|
34
|
+
},
|
|
35
|
+
route.path
|
|
36
|
+
);
|
|
91
37
|
routeElements.push(routeElement);
|
|
92
38
|
}
|
|
93
39
|
}
|
|
94
|
-
routeElements.push(/* @__PURE__ */ jsx(Route, {
|
|
95
|
-
path: "*",
|
|
96
|
-
element: /* @__PURE__ */ jsx(DefaultNotFound, {})
|
|
97
|
-
}, "*"));
|
|
40
|
+
routeElements.push(/* @__PURE__ */ jsx(Route, { path: "*", element: /* @__PURE__ */ jsx(DefaultNotFound, {}) }, "*"));
|
|
98
41
|
return routeElements;
|
|
99
42
|
}
|
|
100
|
-
function renderRoutes(routesConfig) {
|
|
43
|
+
function renderRoutes(routesConfig, ssrMode) {
|
|
101
44
|
if (!routesConfig) {
|
|
102
45
|
return null;
|
|
103
46
|
}
|
|
@@ -105,7 +48,7 @@ function renderRoutes(routesConfig) {
|
|
|
105
48
|
if (!routes) {
|
|
106
49
|
return null;
|
|
107
50
|
}
|
|
108
|
-
const routeElements = getRouteComponents(routes, globalApp);
|
|
51
|
+
const routeElements = getRouteComponents(routes, { globalApp, ssrMode });
|
|
109
52
|
return routeElements;
|
|
110
53
|
}
|
|
111
54
|
function getLocation(serverContext) {
|
|
@@ -139,31 +82,57 @@ function standardSlash(str) {
|
|
|
139
82
|
}
|
|
140
83
|
return addr;
|
|
141
84
|
}
|
|
142
|
-
function
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return (args) => {
|
|
146
|
-
if (typeof route.lazyImport === "function") {
|
|
147
|
-
route.lazyImport();
|
|
148
|
-
}
|
|
149
|
-
return loader(args);
|
|
150
|
-
};
|
|
151
|
-
} else {
|
|
152
|
-
return () => {
|
|
153
|
-
if (typeof route.lazyImport === "function") {
|
|
154
|
-
route.lazyImport();
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
};
|
|
85
|
+
function serializeErrors(errors) {
|
|
86
|
+
if (!errors) {
|
|
87
|
+
return null;
|
|
158
88
|
}
|
|
89
|
+
const entries = Object.entries(errors);
|
|
90
|
+
const serialized = {};
|
|
91
|
+
for (const [key, val] of entries) {
|
|
92
|
+
if (isRouteErrorResponse(val)) {
|
|
93
|
+
serialized[key] = { ...val, __type: "RouteErrorResponse" };
|
|
94
|
+
} else if (val instanceof Error) {
|
|
95
|
+
serialized[key] = {
|
|
96
|
+
message: val.message,
|
|
97
|
+
stack: val.stack,
|
|
98
|
+
__type: "Error"
|
|
99
|
+
};
|
|
100
|
+
} else {
|
|
101
|
+
serialized[key] = val;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return serialized;
|
|
159
105
|
}
|
|
160
|
-
function
|
|
161
|
-
|
|
106
|
+
function deserializeErrors(errors) {
|
|
107
|
+
if (!errors) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const entries = Object.entries(errors);
|
|
111
|
+
const serialized = {};
|
|
112
|
+
for (const [key, val] of entries) {
|
|
113
|
+
if (val && val.__type === "RouteErrorResponse") {
|
|
114
|
+
serialized[key] = new ErrorResponse(
|
|
115
|
+
val.status,
|
|
116
|
+
val.statusText,
|
|
117
|
+
val.data,
|
|
118
|
+
val.internal === true
|
|
119
|
+
);
|
|
120
|
+
} else if (val && val.__type === "Error") {
|
|
121
|
+
const error = new Error(val.message);
|
|
122
|
+
error.stack = val.stack;
|
|
123
|
+
serialized[key] = error;
|
|
124
|
+
} else {
|
|
125
|
+
serialized[key] = val;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return serialized;
|
|
162
129
|
}
|
|
163
130
|
export {
|
|
131
|
+
deserializeErrors,
|
|
164
132
|
getLocation,
|
|
165
133
|
getRouteComponents,
|
|
166
134
|
renderRoutes,
|
|
135
|
+
serializeErrors,
|
|
167
136
|
standardSlash,
|
|
168
137
|
urlJoin
|
|
169
138
|
};
|
|
@@ -5,12 +5,15 @@ const withRouter = (Component) => {
|
|
|
5
5
|
const location = useLocation();
|
|
6
6
|
const params = useParams();
|
|
7
7
|
const navigate = useNavigate();
|
|
8
|
-
return /* @__PURE__ */ jsx(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
return /* @__PURE__ */ jsx(
|
|
9
|
+
Component,
|
|
10
|
+
{
|
|
11
|
+
...props,
|
|
12
|
+
location,
|
|
13
|
+
params,
|
|
14
|
+
navigate
|
|
15
|
+
}
|
|
16
|
+
);
|
|
14
17
|
};
|
|
15
18
|
};
|
|
16
19
|
export {
|
|
@@ -39,6 +39,8 @@ var cli_default = () => ({
|
|
|
39
39
|
return {
|
|
40
40
|
source: {
|
|
41
41
|
alias: {
|
|
42
|
+
// ensure that all packages use the same storage in @modern-js/utils/ssr
|
|
43
|
+
"@modern-js/utils/ssr": require.resolve("@modern-js/utils/ssr"),
|
|
42
44
|
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
|
|
43
45
|
}
|
|
44
46
|
},
|
|
@@ -27,51 +27,33 @@ const ssr = (config) => ({
|
|
|
27
27
|
return stringSSRHydrate();
|
|
28
28
|
function stringSSRHydrate() {
|
|
29
29
|
if (renderLevel === RenderLevel.CLIENT_RENDER || renderLevel === RenderLevel.SERVER_PREFETCH) {
|
|
30
|
-
ModernRender(/* @__PURE__ */ jsx(App, {
|
|
31
|
-
context
|
|
32
|
-
}));
|
|
30
|
+
ModernRender(/* @__PURE__ */ jsx(App, { context }));
|
|
33
31
|
} else if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
34
32
|
if (isReact18()) {
|
|
35
33
|
loadableReady(() => {
|
|
36
|
-
let SSRApp = () => /* @__PURE__ */ jsx(WithCallback, {
|
|
37
|
-
callback,
|
|
38
|
-
children: /* @__PURE__ */ jsx(App, {
|
|
39
|
-
context: hydrateContext
|
|
40
|
-
})
|
|
41
|
-
});
|
|
34
|
+
let SSRApp = () => /* @__PURE__ */ jsx(WithCallback, { callback, children: /* @__PURE__ */ jsx(App, { context: hydrateContext }) });
|
|
42
35
|
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
43
36
|
ModernHydrate(/* @__PURE__ */ jsx(SSRApp, {}));
|
|
44
37
|
});
|
|
45
38
|
} else {
|
|
46
39
|
loadableReady(() => {
|
|
47
|
-
ModernHydrate(/* @__PURE__ */ jsx(App, {
|
|
48
|
-
context: hydrateContext
|
|
49
|
-
}), callback);
|
|
40
|
+
ModernHydrate(/* @__PURE__ */ jsx(App, { context: hydrateContext }), callback);
|
|
50
41
|
});
|
|
51
42
|
}
|
|
52
43
|
} else {
|
|
53
44
|
console.warn(
|
|
54
45
|
`unknow render level: ${renderLevel}, execute render()`
|
|
55
46
|
);
|
|
56
|
-
ModernRender(/* @__PURE__ */ jsx(App, {
|
|
57
|
-
context
|
|
58
|
-
}));
|
|
47
|
+
ModernRender(/* @__PURE__ */ jsx(App, { context }));
|
|
59
48
|
}
|
|
60
49
|
}
|
|
61
50
|
function streamSSRHydrate() {
|
|
62
51
|
if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
63
|
-
let SSRApp = () => /* @__PURE__ */ jsx(WithCallback, {
|
|
64
|
-
callback,
|
|
65
|
-
children: /* @__PURE__ */ jsx(App, {
|
|
66
|
-
context: hydrateContext
|
|
67
|
-
})
|
|
68
|
-
});
|
|
52
|
+
let SSRApp = () => /* @__PURE__ */ jsx(WithCallback, { callback, children: /* @__PURE__ */ jsx(App, { context: hydrateContext }) });
|
|
69
53
|
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
70
54
|
ModernHydrate(/* @__PURE__ */ jsx(SSRApp, {}));
|
|
71
55
|
} else {
|
|
72
|
-
ModernRender(/* @__PURE__ */ jsx(App, {
|
|
73
|
-
context
|
|
74
|
-
}));
|
|
56
|
+
ModernRender(/* @__PURE__ */ jsx(App, { context }));
|
|
75
57
|
}
|
|
76
58
|
}
|
|
77
59
|
},
|
|
@@ -15,6 +15,7 @@ const ssr = (config = {}) => ({
|
|
|
15
15
|
init({ context }, next) {
|
|
16
16
|
const { request } = context.ssrContext;
|
|
17
17
|
context.ssrContext.request = formatServer(request);
|
|
18
|
+
context.ssrContext.mode = config.mode;
|
|
18
19
|
return next({ context });
|
|
19
20
|
},
|
|
20
21
|
pickContext: ({ context, pickedContext }, next) => {
|
|
@@ -11,13 +11,9 @@ const prefetch = async (App, context) => run(context.ssrContext.request.headers,
|
|
|
11
11
|
stats: loadableStats,
|
|
12
12
|
entrypoints: [ssrContext.entryName].filter(Boolean)
|
|
13
13
|
});
|
|
14
|
-
renderToStaticMarkup(extractor.collectChunks(/* @__PURE__ */ jsx(App, {
|
|
15
|
-
context
|
|
16
|
-
})));
|
|
14
|
+
renderToStaticMarkup(extractor.collectChunks(/* @__PURE__ */ jsx(App, { context })));
|
|
17
15
|
} else {
|
|
18
|
-
renderToStaticMarkup(/* @__PURE__ */ jsx(App, {
|
|
19
|
-
context
|
|
20
|
-
}));
|
|
16
|
+
renderToStaticMarkup(/* @__PURE__ */ jsx(App, { context }));
|
|
21
17
|
}
|
|
22
18
|
if (!context.loaderManager.hasPendingLoaders()) {
|
|
23
19
|
return {
|
|
@@ -38,6 +34,7 @@ const prefetch = async (App, context) => run(context.ssrContext.request.headers,
|
|
|
38
34
|
loadersData,
|
|
39
35
|
initialData: context.initialData,
|
|
40
36
|
i18nData: context.__i18nData__,
|
|
37
|
+
// todo: move to plugin state
|
|
41
38
|
storeState: (_a = context == null ? void 0 : context.store) == null ? void 0 : _a.getState()
|
|
42
39
|
};
|
|
43
40
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { serializeJson } from "@modern-js/utils/serialize";
|
|
2
2
|
import { buildTemplate } from "./buildTemplate.share";
|
|
3
3
|
function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
4
4
|
const callbacks = [injectSSRDataScript];
|
|
@@ -8,7 +8,10 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
|
8
8
|
return template.replace("<!--<?- SSRDataScript ?>-->", ssrDataScript);
|
|
9
9
|
function buildSSRDataScript() {
|
|
10
10
|
const { ssrContext, renderLevel } = options;
|
|
11
|
-
const { request } = ssrContext;
|
|
11
|
+
const { request, enableUnsafeCtx } = ssrContext;
|
|
12
|
+
const unsafeContext = {
|
|
13
|
+
headers: request.headers
|
|
14
|
+
};
|
|
12
15
|
const SSRData = {
|
|
13
16
|
context: {
|
|
14
17
|
request: {
|
|
@@ -17,16 +20,13 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
|
17
20
|
pathname: request.pathname,
|
|
18
21
|
host: request.host,
|
|
19
22
|
url: request.url,
|
|
20
|
-
|
|
21
|
-
cookieMap: request.cookieMap
|
|
23
|
+
...enableUnsafeCtx ? unsafeContext : {}
|
|
22
24
|
}
|
|
23
25
|
},
|
|
24
26
|
renderLevel
|
|
25
27
|
};
|
|
26
28
|
return `
|
|
27
|
-
<script>window._SSR_DATA = ${
|
|
28
|
-
isJSON: true
|
|
29
|
-
})}<\/script>
|
|
29
|
+
<script>window._SSR_DATA = ${serializeJson(SSRData)}</script>
|
|
30
30
|
`;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -12,6 +12,7 @@ function getHeadTemplate(beforeEntryTemplate, context) {
|
|
|
12
12
|
const helmetData = ReactHelmet.renderStatic();
|
|
13
13
|
return helmetData ? helmetReplace(headTemplate2, helmetData) : headTemplate2;
|
|
14
14
|
},
|
|
15
|
+
// @TODO: prefetch scripts of lazy component
|
|
15
16
|
injectCss
|
|
16
17
|
];
|
|
17
18
|
const [headTemplate = ""] = beforeEntryTemplate.match(HEAD_REG_EXP) || [];
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { Transform } from "stream";
|
|
2
2
|
import { RenderLevel } from "../types";
|
|
3
|
+
import { ESCAPED_SHELL_STREAM_END_MARK } from "../../../common";
|
|
3
4
|
import { getTemplates } from "./template";
|
|
5
|
+
var ShellChunkStatus = /* @__PURE__ */ ((ShellChunkStatus2) => {
|
|
6
|
+
ShellChunkStatus2[ShellChunkStatus2["IDLE"] = 0] = "IDLE";
|
|
7
|
+
ShellChunkStatus2[ShellChunkStatus2["START"] = 1] = "START";
|
|
8
|
+
ShellChunkStatus2[ShellChunkStatus2["FINIESH"] = 2] = "FINIESH";
|
|
9
|
+
return ShellChunkStatus2;
|
|
10
|
+
})(ShellChunkStatus || {});
|
|
4
11
|
function renderToPipe(rootElement, context, options) {
|
|
5
|
-
let
|
|
12
|
+
let shellChunkStatus = 0 /* IDLE */;
|
|
6
13
|
const { ssrContext } = context;
|
|
7
14
|
const forUserPipe = (stream) => {
|
|
8
15
|
return new Promise((resolve) => {
|
|
@@ -23,9 +30,20 @@ function renderToPipe(rootElement, context, options) {
|
|
|
23
30
|
const injectableTransform = new Transform({
|
|
24
31
|
transform(chunk, _encoding, callback) {
|
|
25
32
|
try {
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
if (shellChunkStatus !== 2 /* FINIESH */) {
|
|
34
|
+
let concatedChunk = chunk.toString();
|
|
35
|
+
if (shellChunkStatus === 0 /* IDLE */) {
|
|
36
|
+
concatedChunk = `${shellBefore}${concatedChunk}`;
|
|
37
|
+
shellChunkStatus = 1 /* START */;
|
|
38
|
+
}
|
|
39
|
+
if (shellChunkStatus === 1 /* START */ && concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
|
|
40
|
+
concatedChunk = concatedChunk.replace(
|
|
41
|
+
ESCAPED_SHELL_STREAM_END_MARK,
|
|
42
|
+
shellAfter
|
|
43
|
+
);
|
|
44
|
+
shellChunkStatus = 2 /* FINIESH */;
|
|
45
|
+
}
|
|
46
|
+
this.push(concatedChunk);
|
|
29
47
|
} else {
|
|
30
48
|
this.push(chunk);
|
|
31
49
|
}
|
|
@@ -68,9 +86,6 @@ function renderToPipe(rootElement, context, options) {
|
|
|
68
86
|
});
|
|
69
87
|
};
|
|
70
88
|
return forUserPipe;
|
|
71
|
-
function joinChunk(before = "", chunk, after = "") {
|
|
72
|
-
return `${before}${chunk.toString()}${after}`;
|
|
73
|
-
}
|
|
74
89
|
}
|
|
75
90
|
var renderToPipe_default = renderToPipe;
|
|
76
91
|
export {
|