@modern-js/runtime 2.65.1 → 2.65.2
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/cache/index.js +22 -0
- package/dist/cjs/cli/ssr/index.js +1 -1
- package/dist/cjs/core/context/monitors/default.js +43 -0
- package/dist/cjs/core/context/monitors/index.js +31 -0
- package/dist/cjs/core/context/monitors/index.server.js +33 -0
- package/dist/cjs/core/context/request/index.js +30 -0
- package/dist/cjs/core/context/request/index.server.js +32 -0
- package/dist/cjs/core/server/requestHandler.js +84 -76
- package/dist/cjs/core/server/stream/shared.js +40 -45
- package/dist/cjs/core/server/string/index.js +50 -55
- package/dist/cjs/core/server/string/prefetch.js +47 -52
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/react-server.js +28 -0
- package/dist/cjs/router/runtime/plugin.node.js +1 -2
- package/dist/cjs/router/runtime/utils.js +4 -6
- package/dist/esm/cache/index.js +1 -0
- package/dist/esm/cli/ssr/index.js +1 -1
- package/dist/esm/core/context/monitors/default.js +40 -0
- package/dist/esm/core/context/monitors/index.js +7 -0
- package/dist/esm/core/context/monitors/index.server.js +9 -0
- package/dist/esm/core/context/request/index.js +6 -0
- package/dist/esm/core/context/request/index.server.js +8 -0
- package/dist/esm/core/server/requestHandler.js +131 -117
- package/dist/esm/core/server/stream/shared.js +49 -63
- package/dist/esm/core/server/string/index.js +82 -93
- package/dist/esm/core/server/string/prefetch.js +83 -94
- package/dist/esm/index.js +4 -0
- package/dist/esm/react-server.js +4 -0
- package/dist/esm/router/runtime/plugin.node.js +1 -2
- package/dist/esm/router/runtime/utils.js +4 -6
- package/dist/esm-node/cache/index.js +1 -0
- package/dist/esm-node/cli/ssr/index.js +1 -1
- package/dist/esm-node/core/context/monitors/default.js +19 -0
- package/dist/esm-node/core/context/monitors/index.js +7 -0
- package/dist/esm-node/core/context/monitors/index.server.js +9 -0
- package/dist/esm-node/core/context/request/index.js +6 -0
- package/dist/esm-node/core/context/request/index.server.js +8 -0
- package/dist/esm-node/core/server/requestHandler.js +84 -76
- package/dist/esm-node/core/server/stream/shared.js +40 -45
- package/dist/esm-node/core/server/string/index.js +50 -55
- package/dist/esm-node/core/server/string/prefetch.js +47 -52
- package/dist/esm-node/index.js +4 -0
- package/dist/esm-node/react-server.js +4 -0
- package/dist/esm-node/router/runtime/plugin.node.js +1 -2
- package/dist/esm-node/router/runtime/utils.js +4 -6
- package/dist/types/cache/index.d.ts +1 -0
- package/dist/types/core/compatible.d.ts +2 -2
- package/dist/types/core/context/monitors/default.d.ts +2 -0
- package/dist/types/core/context/monitors/index.d.ts +1 -0
- package/dist/types/core/context/monitors/index.server.d.ts +2 -0
- package/dist/types/core/context/request/index.d.ts +1 -0
- package/dist/types/core/context/request/index.server.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/react-server.d.ts +1 -0
- package/dist/types/router/runtime/utils.d.ts +3 -5
- package/package.json +21 -11
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { storage } from "@modern-js/runtime-utils/node";
|
|
1
2
|
import { getPathname, parseCookie, parseHeaders, parseQuery } from "@modern-js/runtime-utils/universal/request";
|
|
2
3
|
import { getGlobalAppInit, getGlobalInternalRuntimeContext, getGlobalRSCRoot } from "../context";
|
|
3
4
|
import { getInitialContext } from "../context/runtime";
|
|
@@ -64,85 +65,92 @@ function createSSRContext(request, options) {
|
|
|
64
65
|
}
|
|
65
66
|
const createRequestHandler = async (handleRequest, createRequestOptions) => {
|
|
66
67
|
const requestHandler = async (request, options) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
responseProxy
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
68
|
+
const headersData = parseHeaders(request);
|
|
69
|
+
return storage.run({
|
|
70
|
+
headers: headersData,
|
|
71
|
+
request,
|
|
72
|
+
monitors: options.monitors
|
|
73
|
+
}, async () => {
|
|
74
|
+
var _context_routerContext, _context_routerContext1, _context_routerContext2;
|
|
75
|
+
const Root = createRoot();
|
|
76
|
+
const internalRuntimeContext = getGlobalInternalRuntimeContext();
|
|
77
|
+
const hooks = internalRuntimeContext.hooks;
|
|
78
|
+
const { routeManifest } = options.resource;
|
|
79
|
+
const context = getInitialContext(false, routeManifest);
|
|
80
|
+
const runBeforeRender = async (context2) => {
|
|
81
|
+
const result = await hooks.onBeforeRender.call(context2);
|
|
82
|
+
if (typeof Response !== "undefined" && result instanceof Response) {
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
const init = getGlobalAppInit();
|
|
86
|
+
return init === null || init === void 0 ? void 0 : init(context2);
|
|
87
|
+
};
|
|
88
|
+
const responseProxy = {
|
|
89
|
+
headers: {},
|
|
90
|
+
code: -1
|
|
91
|
+
};
|
|
92
|
+
const ssrContext = createSSRContext(request, {
|
|
93
|
+
...options,
|
|
94
|
+
responseProxy
|
|
95
|
+
});
|
|
96
|
+
Object.assign(context, {
|
|
97
|
+
ssrContext,
|
|
98
|
+
isBrowser: false,
|
|
99
|
+
loaderManager: createLoaderManager({}, {
|
|
100
|
+
skipNonStatic: options.staticGenerate,
|
|
101
|
+
// if not static generate, only non-static loader can exec on prod env
|
|
102
|
+
skipStatic: process.env.NODE_ENV === "production" && !options.staticGenerate
|
|
103
|
+
})
|
|
104
|
+
});
|
|
105
|
+
const getRedirectResponse = (result) => {
|
|
106
|
+
if (typeof Response !== "undefined" && // fix: ssg workflow doesn't inject Web Response
|
|
107
|
+
result instanceof Response && result.status >= 300 && result.status <= 399) {
|
|
108
|
+
const { status } = result;
|
|
109
|
+
const redirectUrl = result.headers.get("Location") || "/";
|
|
110
|
+
const { ssrContext: ssrContext2 } = context;
|
|
111
|
+
if (ssrContext2) {
|
|
112
|
+
return new Response(null, {
|
|
113
|
+
status,
|
|
114
|
+
headers: {
|
|
115
|
+
Location: redirectUrl
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
111
119
|
}
|
|
120
|
+
return void 0;
|
|
121
|
+
};
|
|
122
|
+
const initialData = await runBeforeRender(context);
|
|
123
|
+
if (((_context_routerContext = context.routerContext) === null || _context_routerContext === void 0 ? void 0 : _context_routerContext.statusCode) && ((_context_routerContext1 = context.routerContext) === null || _context_routerContext1 === void 0 ? void 0 : _context_routerContext1.statusCode) !== 200) {
|
|
124
|
+
var _context_routerContext3, _context_ssrContext;
|
|
125
|
+
(_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.response.status((_context_routerContext3 = context.routerContext) === null || _context_routerContext3 === void 0 ? void 0 : _context_routerContext3.statusCode);
|
|
112
126
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
options.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
const { htmlTemplate } = options.resource;
|
|
130
|
-
options.resource.htmlTemplate = htmlTemplate.replace("</head>", `${CHUNK_CSS_PLACEHOLDER}</head>`);
|
|
131
|
-
const response = await handleRequest(request, Root, {
|
|
132
|
-
...options,
|
|
133
|
-
runtimeContext: context,
|
|
134
|
-
RSCRoot: (createRequestOptions === null || createRequestOptions === void 0 ? void 0 : createRequestOptions.enableRsc) && getGlobalRSCRoot()
|
|
135
|
-
});
|
|
136
|
-
Object.entries(responseProxy.headers).forEach(([key, value]) => {
|
|
137
|
-
response.headers.set(key, value);
|
|
138
|
-
});
|
|
139
|
-
if (responseProxy.code !== -1) {
|
|
140
|
-
return new Response(response.body, {
|
|
141
|
-
status: responseProxy.code,
|
|
142
|
-
headers: response.headers
|
|
127
|
+
const errors = Object.values(((_context_routerContext2 = context.routerContext) === null || _context_routerContext2 === void 0 ? void 0 : _context_routerContext2.errors) || {});
|
|
128
|
+
if (errors.length > 0) {
|
|
129
|
+
options.onError(errors[0], SSRErrors.LOADER_ERROR);
|
|
130
|
+
}
|
|
131
|
+
context.initialData = initialData;
|
|
132
|
+
const redirectResponse = getRedirectResponse(initialData);
|
|
133
|
+
if (redirectResponse) {
|
|
134
|
+
return redirectResponse;
|
|
135
|
+
}
|
|
136
|
+
const { htmlTemplate } = options.resource;
|
|
137
|
+
options.resource.htmlTemplate = htmlTemplate.replace("</head>", `${CHUNK_CSS_PLACEHOLDER}</head>`);
|
|
138
|
+
const response = await handleRequest(request, Root, {
|
|
139
|
+
...options,
|
|
140
|
+
runtimeContext: context,
|
|
141
|
+
RSCRoot: (createRequestOptions === null || createRequestOptions === void 0 ? void 0 : createRequestOptions.enableRsc) && getGlobalRSCRoot()
|
|
143
142
|
});
|
|
144
|
-
|
|
145
|
-
|
|
143
|
+
Object.entries(responseProxy.headers).forEach(([key, value]) => {
|
|
144
|
+
response.headers.set(key, value);
|
|
145
|
+
});
|
|
146
|
+
if (responseProxy.code !== -1) {
|
|
147
|
+
return new Response(response.body, {
|
|
148
|
+
status: responseProxy.code,
|
|
149
|
+
headers: response.headers
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return response;
|
|
153
|
+
});
|
|
146
154
|
};
|
|
147
155
|
return requestHandler;
|
|
148
156
|
};
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
-
import { run } from "@modern-js/runtime-utils/node";
|
|
3
2
|
import { time } from "@modern-js/runtime-utils/time";
|
|
4
|
-
import { parseHeaders } from "@modern-js/runtime-utils/universal/request";
|
|
5
3
|
import { wrapRuntimeContextProvider } from "../../react/wrapper";
|
|
6
4
|
import { SSRErrors, SSRTimings } from "../tracer";
|
|
7
5
|
import { getSSRConfigByEntry } from "../utils";
|
|
@@ -32,51 +30,48 @@ function getReadableStreamFromString(content) {
|
|
|
32
30
|
function createRenderStreaming(createReadableStreamPromise) {
|
|
33
31
|
return async (request, serverRoot, options) => {
|
|
34
32
|
const createReadableStreamFromElement = await createReadableStreamPromise;
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
44
|
-
children
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
let rootElement = wrapRuntimeContextProvider(serverRoot, Object.assign(runtimeContext, {
|
|
48
|
-
ssr: true
|
|
49
|
-
}));
|
|
50
|
-
rootElement = /* @__PURE__ */ _jsx(RSCServerRoot, {
|
|
51
|
-
children: rootElement
|
|
33
|
+
const end = time();
|
|
34
|
+
const { runtimeContext, config, resource } = options;
|
|
35
|
+
const { onError, onTiming } = options;
|
|
36
|
+
const { htmlTemplate, entryName } = resource;
|
|
37
|
+
const ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
|
|
38
|
+
const RSCServerRoot = ({ children }) => {
|
|
39
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
40
|
+
children
|
|
52
41
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
42
|
+
};
|
|
43
|
+
let rootElement = wrapRuntimeContextProvider(serverRoot, Object.assign(runtimeContext, {
|
|
44
|
+
ssr: true
|
|
45
|
+
}));
|
|
46
|
+
rootElement = /* @__PURE__ */ _jsx(RSCServerRoot, {
|
|
47
|
+
children: rootElement
|
|
48
|
+
});
|
|
49
|
+
const stream = await createReadableStreamFromElement(request, rootElement, {
|
|
50
|
+
config,
|
|
51
|
+
htmlTemplate,
|
|
52
|
+
runtimeContext,
|
|
53
|
+
ssrConfig,
|
|
54
|
+
entryName,
|
|
55
|
+
rscClientManifest: options.rscClientManifest,
|
|
56
|
+
rscSSRManifest: options.rscSSRManifest,
|
|
57
|
+
rscServerManifest: options.rscServerManifest,
|
|
58
|
+
rscRoot: options.rscRoot,
|
|
59
|
+
onShellReady() {
|
|
60
|
+
const cost = end();
|
|
61
|
+
onTiming(SSRTimings.RENDER_SHELL, cost);
|
|
62
|
+
},
|
|
63
|
+
onAllReady() {
|
|
64
|
+
const cost = end();
|
|
65
|
+
onTiming(SSRTimings.RENDER_HTML, cost);
|
|
66
|
+
},
|
|
67
|
+
onShellError(error) {
|
|
68
|
+
onError(error, SSRErrors.RENDER_SHELL);
|
|
69
|
+
},
|
|
70
|
+
onError(error) {
|
|
71
|
+
onError(error, SSRErrors.RENDER_STREAM);
|
|
72
|
+
}
|
|
79
73
|
});
|
|
74
|
+
return stream;
|
|
80
75
|
};
|
|
81
76
|
}
|
|
82
77
|
export {
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { run } from "@modern-js/runtime-utils/node";
|
|
2
1
|
import { time } from "@modern-js/runtime-utils/time";
|
|
3
|
-
import { parseHeaders } from "@modern-js/runtime-utils/universal/request";
|
|
4
2
|
import ReactDomServer from "react-dom/server";
|
|
5
3
|
import ReactHelmet from "react-helmet";
|
|
6
4
|
import { RenderLevel } from "../../constants";
|
|
@@ -15,59 +13,56 @@ import { prefetch } from "./prefetch";
|
|
|
15
13
|
import { SSRDataCollector } from "./ssrData";
|
|
16
14
|
import { StyledCollector } from "./styledComponent";
|
|
17
15
|
const renderString = async (request, serverRoot, options) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const html = await generateHtml(rootElement, htmlTemplate, chunkSet, collectors, ((_runtimeContext_ssrContext = runtimeContext.ssrContext) === null || _runtimeContext_ssrContext === void 0 ? void 0 : _runtimeContext_ssrContext.htmlModifiers) || [], tracer);
|
|
69
|
-
return html;
|
|
70
|
-
});
|
|
16
|
+
var _runtimeContext_ssrContext;
|
|
17
|
+
const { resource, runtimeContext, config, onError, onTiming } = options;
|
|
18
|
+
const tracer = {
|
|
19
|
+
onError,
|
|
20
|
+
onTiming
|
|
21
|
+
};
|
|
22
|
+
const routerContext = runtimeContext.routerContext;
|
|
23
|
+
const { htmlTemplate, entryName, loadableStats, routeManifest } = resource;
|
|
24
|
+
const ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
|
|
25
|
+
const chunkSet = {
|
|
26
|
+
renderLevel: RenderLevel.CLIENT_RENDER,
|
|
27
|
+
ssrScripts: "",
|
|
28
|
+
jsChunk: "",
|
|
29
|
+
cssChunk: ""
|
|
30
|
+
};
|
|
31
|
+
let prefetchData = {};
|
|
32
|
+
try {
|
|
33
|
+
prefetchData = await prefetch(serverRoot, request, options, ssrConfig, tracer);
|
|
34
|
+
chunkSet.renderLevel = RenderLevel.SERVER_PREFETCH;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
chunkSet.renderLevel = RenderLevel.CLIENT_RENDER;
|
|
37
|
+
tracer.onError(e, SSRErrors.PRERENDER);
|
|
38
|
+
}
|
|
39
|
+
const collectors = [
|
|
40
|
+
new StyledCollector(chunkSet),
|
|
41
|
+
new LoadableCollector({
|
|
42
|
+
stats: loadableStats,
|
|
43
|
+
nonce: config.nonce,
|
|
44
|
+
routeManifest,
|
|
45
|
+
template: htmlTemplate,
|
|
46
|
+
entryName,
|
|
47
|
+
chunkSet,
|
|
48
|
+
config
|
|
49
|
+
}),
|
|
50
|
+
new SSRDataCollector({
|
|
51
|
+
request,
|
|
52
|
+
prefetchData,
|
|
53
|
+
ssrConfig,
|
|
54
|
+
ssrContext: runtimeContext.ssrContext,
|
|
55
|
+
chunkSet,
|
|
56
|
+
routerContext,
|
|
57
|
+
nonce: config.nonce,
|
|
58
|
+
useJsonScript: config.useJsonScript
|
|
59
|
+
})
|
|
60
|
+
];
|
|
61
|
+
const rootElement = wrapRuntimeContextProvider(serverRoot, Object.assign(runtimeContext, {
|
|
62
|
+
ssr: true
|
|
63
|
+
}));
|
|
64
|
+
const html = await generateHtml(rootElement, htmlTemplate, chunkSet, collectors, ((_runtimeContext_ssrContext = runtimeContext.ssrContext) === null || _runtimeContext_ssrContext === void 0 ? void 0 : _runtimeContext_ssrContext.htmlModifiers) || [], tracer);
|
|
65
|
+
return html;
|
|
71
66
|
};
|
|
72
67
|
async function generateHtml(App, htmlTemplate, chunkSet, collectors, htmlModifiers, { onError, onTiming }) {
|
|
73
68
|
let html = "";
|
|
@@ -1,73 +1,68 @@
|
|
|
1
1
|
import { ChunkExtractor } from "@loadable/server";
|
|
2
|
-
import { run } from "@modern-js/runtime-utils/node";
|
|
3
2
|
import { time } from "@modern-js/runtime-utils/time";
|
|
4
|
-
import { parseHeaders } from "@modern-js/runtime-utils/universal/request";
|
|
5
3
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
6
4
|
import { wrapRuntimeContextProvider } from "../../react/wrapper";
|
|
7
5
|
import { SSRErrors, SSRTimings } from "../tracer";
|
|
8
6
|
const prefetch = async (App, request, options, ssrConfig, { onError, onTiming }) => {
|
|
9
|
-
|
|
7
|
+
var _context_store;
|
|
10
8
|
const { runtimeContext: context, resource } = options;
|
|
11
9
|
const { entryName, loadableStats } = resource;
|
|
12
|
-
|
|
13
|
-
var _context_store;
|
|
14
|
-
if (typeof ssrConfig === "boolean" || !ssrConfig.disablePrerender) {
|
|
15
|
-
try {
|
|
16
|
-
const end = time();
|
|
17
|
-
if (loadableStats) {
|
|
18
|
-
const extractor = new ChunkExtractor({
|
|
19
|
-
stats: loadableStats,
|
|
20
|
-
entrypoints: [
|
|
21
|
-
entryName
|
|
22
|
-
].filter(Boolean)
|
|
23
|
-
});
|
|
24
|
-
renderToStaticMarkup(extractor.collectChunks(wrapRuntimeContextProvider(App, Object.assign(context, {
|
|
25
|
-
ssr: false
|
|
26
|
-
}))));
|
|
27
|
-
} else {
|
|
28
|
-
renderToStaticMarkup(wrapRuntimeContextProvider(App, Object.assign(context, {
|
|
29
|
-
ssr: false
|
|
30
|
-
})));
|
|
31
|
-
}
|
|
32
|
-
const cost = end();
|
|
33
|
-
onTiming(SSRTimings.PRERENDER, cost);
|
|
34
|
-
} catch (e) {
|
|
35
|
-
const error = e;
|
|
36
|
-
onError(error, SSRErrors.PRERENDER);
|
|
37
|
-
throw e;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
if (!context.loaderManager.hasPendingLoaders()) {
|
|
41
|
-
return {
|
|
42
|
-
initialData: context.initialData,
|
|
43
|
-
i18nData: context.__i18nData__
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
let loadersData = {};
|
|
10
|
+
if (typeof ssrConfig === "boolean" || !ssrConfig.disablePrerender) {
|
|
47
11
|
try {
|
|
48
12
|
const end = time();
|
|
49
|
-
|
|
13
|
+
if (loadableStats) {
|
|
14
|
+
const extractor = new ChunkExtractor({
|
|
15
|
+
stats: loadableStats,
|
|
16
|
+
entrypoints: [
|
|
17
|
+
entryName
|
|
18
|
+
].filter(Boolean)
|
|
19
|
+
});
|
|
20
|
+
renderToStaticMarkup(extractor.collectChunks(wrapRuntimeContextProvider(App, Object.assign(context, {
|
|
21
|
+
ssr: false
|
|
22
|
+
}))));
|
|
23
|
+
} else {
|
|
24
|
+
renderToStaticMarkup(wrapRuntimeContextProvider(App, Object.assign(context, {
|
|
25
|
+
ssr: false
|
|
26
|
+
})));
|
|
27
|
+
}
|
|
50
28
|
const cost = end();
|
|
51
|
-
onTiming(SSRTimings.
|
|
29
|
+
onTiming(SSRTimings.PRERENDER, cost);
|
|
52
30
|
} catch (e) {
|
|
53
|
-
|
|
31
|
+
const error = e;
|
|
32
|
+
onError(error, SSRErrors.PRERENDER);
|
|
54
33
|
throw e;
|
|
55
34
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (data._error) {
|
|
59
|
-
onError(data._error, SSRErrors.USE_LOADER);
|
|
60
|
-
delete data._error;
|
|
61
|
-
}
|
|
62
|
-
});
|
|
35
|
+
}
|
|
36
|
+
if (!context.loaderManager.hasPendingLoaders()) {
|
|
63
37
|
return {
|
|
64
|
-
loadersData,
|
|
65
38
|
initialData: context.initialData,
|
|
66
|
-
i18nData: context.__i18nData__
|
|
67
|
-
// todo: move to plugin state
|
|
68
|
-
storeState: context === null || context === void 0 ? void 0 : (_context_store = context.store) === null || _context_store === void 0 ? void 0 : _context_store.getState()
|
|
39
|
+
i18nData: context.__i18nData__
|
|
69
40
|
};
|
|
41
|
+
}
|
|
42
|
+
let loadersData = {};
|
|
43
|
+
try {
|
|
44
|
+
const end = time();
|
|
45
|
+
loadersData = await context.loaderManager.awaitPendingLoaders();
|
|
46
|
+
const cost = end();
|
|
47
|
+
onTiming(SSRTimings.USE_LOADER, cost);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
onError(e, SSRErrors.USE_LOADER);
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
Object.keys(loadersData).forEach((id) => {
|
|
53
|
+
const data = loadersData[id];
|
|
54
|
+
if (data._error) {
|
|
55
|
+
onError(data._error, SSRErrors.USE_LOADER);
|
|
56
|
+
delete data._error;
|
|
57
|
+
}
|
|
70
58
|
});
|
|
59
|
+
return {
|
|
60
|
+
loadersData,
|
|
61
|
+
initialData: context.initialData,
|
|
62
|
+
i18nData: context.__i18nData__,
|
|
63
|
+
// todo: move to plugin state
|
|
64
|
+
storeState: context === null || context === void 0 ? void 0 : (_context_store = context.store) === null || _context_store === void 0 ? void 0 : _context_store.getState()
|
|
65
|
+
};
|
|
71
66
|
};
|
|
72
67
|
export {
|
|
73
68
|
prefetch
|
package/dist/esm-node/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from "./common";
|
|
2
|
+
import { getMonitors } from "./core/context/monitors";
|
|
3
|
+
import { getRequest } from "./core/context/request";
|
|
2
4
|
import { createApp, useLoader, bootstrap, RuntimeReactContext, defineConfig, defineRuntimeConfig, useRuntimeContext } from "./core";
|
|
3
5
|
export {
|
|
4
6
|
RuntimeReactContext,
|
|
@@ -6,6 +8,8 @@ export {
|
|
|
6
8
|
createApp,
|
|
7
9
|
defineConfig,
|
|
8
10
|
defineRuntimeConfig,
|
|
11
|
+
getMonitors,
|
|
12
|
+
getRequest,
|
|
9
13
|
isBrowser,
|
|
10
14
|
useLoader,
|
|
11
15
|
useRuntimeContext
|
|
@@ -4,7 +4,7 @@ import { UNSAFE_ErrorResponseImpl as ErrorResponseImpl } from "@modern-js/runtim
|
|
|
4
4
|
import { Route, isRouteErrorResponse } from "@modern-js/runtime-utils/router";
|
|
5
5
|
import { DefaultNotFound } from "./DefaultNotFound";
|
|
6
6
|
import DeferredDataScripts from "./DeferredDataScripts";
|
|
7
|
-
function getRouteComponents(routes, { globalApp, ssrMode, props
|
|
7
|
+
function getRouteComponents(routes, { globalApp, ssrMode, props }) {
|
|
8
8
|
const Layout = ({ Component, ...props2 }) => {
|
|
9
9
|
const GlobalLayout = globalApp;
|
|
10
10
|
if (!GlobalLayout) {
|
|
@@ -22,8 +22,7 @@ function getRouteComponents(routes, { globalApp, ssrMode, props, reporter }) {
|
|
|
22
22
|
if (route.type === "nested") {
|
|
23
23
|
const routeElement = renderNestedRoute(route, {
|
|
24
24
|
DeferredDataComponent: ssrMode === "stream" ? DeferredDataScripts : void 0,
|
|
25
|
-
props
|
|
26
|
-
reporter
|
|
25
|
+
props
|
|
27
26
|
});
|
|
28
27
|
routeElements.push(routeElement);
|
|
29
28
|
} else {
|
|
@@ -42,7 +41,7 @@ function getRouteComponents(routes, { globalApp, ssrMode, props, reporter }) {
|
|
|
42
41
|
}, "*"));
|
|
43
42
|
return routeElements;
|
|
44
43
|
}
|
|
45
|
-
function renderRoutes({ routesConfig, props, ssrMode
|
|
44
|
+
function renderRoutes({ routesConfig, props, ssrMode }) {
|
|
46
45
|
if (!routesConfig) {
|
|
47
46
|
return null;
|
|
48
47
|
}
|
|
@@ -53,8 +52,7 @@ function renderRoutes({ routesConfig, props, ssrMode, reporter }) {
|
|
|
53
52
|
const routeElements = getRouteComponents(routes, {
|
|
54
53
|
globalApp,
|
|
55
54
|
ssrMode,
|
|
56
|
-
props
|
|
57
|
-
reporter
|
|
55
|
+
props
|
|
58
56
|
});
|
|
59
57
|
return routeElements;
|
|
60
58
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@modern-js/runtime-utils/cache';
|
|
@@ -2,7 +2,7 @@ import type { RuntimePlugin } from '@modern-js/plugin-v2';
|
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import type { Renderer } from 'react-dom';
|
|
4
4
|
import type { createRoot, hydrateRoot } from 'react-dom/client';
|
|
5
|
-
import { type RuntimeContext } from './context/runtime';
|
|
5
|
+
import { type RuntimeContext, type TRuntimeContext } from './context/runtime';
|
|
6
6
|
import { type Plugin } from './plugin';
|
|
7
7
|
import type { RuntimeExtends } from './plugin/types';
|
|
8
8
|
export type CreateAppOptions = {
|
|
@@ -20,5 +20,5 @@ type BootStrap<T = unknown> = (App: React.ComponentType, id: string | HTMLElemen
|
|
|
20
20
|
hydrateRoot?: typeof hydrateRoot;
|
|
21
21
|
}) => Promise<T>;
|
|
22
22
|
export declare const bootstrap: BootStrap;
|
|
23
|
-
export declare const useRuntimeContext: () =>
|
|
23
|
+
export declare const useRuntimeContext: () => TRuntimeContext;
|
|
24
24
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getMonitors: () => Omit<import("@modern-js/types").Monitors, "push">;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getRequest: () => Request;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getRequest: () => Request;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export type { AppConfig, RuntimeConfig } from './common';
|
|
|
5
5
|
export { isBrowser } from './common';
|
|
6
6
|
export type { RuntimeContext } from './core/context/runtime';
|
|
7
7
|
export type { RuntimeUserConfig } from './config';
|
|
8
|
+
export { getMonitors } from './core/context/monitors';
|
|
9
|
+
export { getRequest } from './core/context/request';
|
|
8
10
|
export { createApp, useLoader, bootstrap, RuntimeReactContext, defineConfig, defineRuntimeConfig, useRuntimeContext, } from './core';
|
|
9
11
|
export type { StateConfig, RouterConfig };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getRequest } from './core/context/request';
|