@modern-js/runtime 3.10.1 → 3.10.3
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/core/server/helmet.js +7 -0
- package/dist/cjs/core/server/stream/beforeTemplate.js +1 -2
- package/dist/cjs/core/server/stream/createReadableStream.js +6 -10
- package/dist/cjs/core/server/stream/createReadableStream.worker.js +6 -9
- package/dist/cjs/core/server/stream/deferredScript.js +10 -1
- package/dist/cjs/core/server/stream/deferredScriptOutputCoordinator.js +9 -3
- package/dist/cjs/core/server/stream/shared.js +22 -0
- package/dist/cjs/core/server/string/index.js +21 -5
- package/dist/cjs/core/server/string/loadable.js +1 -2
- package/dist/cjs/core/server/utils.js +14 -0
- package/dist/cjs/router/cli/code/utils.js +5 -15
- package/dist/esm/core/server/helmet.mjs +5 -1
- package/dist/esm/core/server/stream/beforeTemplate.mjs +2 -3
- package/dist/esm/core/server/stream/createReadableStream.mjs +8 -12
- package/dist/esm/core/server/stream/createReadableStream.worker.mjs +8 -11
- package/dist/esm/core/server/stream/deferredScript.mjs +8 -2
- package/dist/esm/core/server/stream/deferredScriptOutputCoordinator.mjs +9 -3
- package/dist/esm/core/server/stream/shared.mjs +21 -2
- package/dist/esm/core/server/string/index.mjs +22 -6
- package/dist/esm/core/server/string/loadable.mjs +2 -3
- package/dist/esm/core/server/utils.mjs +12 -1
- package/dist/esm/router/cli/code/utils.mjs +5 -15
- package/dist/esm-node/core/server/helmet.mjs +5 -1
- package/dist/esm-node/core/server/stream/beforeTemplate.mjs +2 -3
- package/dist/esm-node/core/server/stream/createReadableStream.mjs +8 -12
- package/dist/esm-node/core/server/stream/createReadableStream.worker.mjs +8 -11
- package/dist/esm-node/core/server/stream/deferredScript.mjs +8 -2
- package/dist/esm-node/core/server/stream/deferredScriptOutputCoordinator.mjs +9 -3
- package/dist/esm-node/core/server/stream/shared.mjs +21 -2
- package/dist/esm-node/core/server/string/index.mjs +22 -6
- package/dist/esm-node/core/server/string/loadable.mjs +2 -3
- package/dist/esm-node/core/server/utils.mjs +12 -1
- package/dist/esm-node/router/cli/code/utils.mjs +5 -15
- package/dist/types/core/server/helmet.d.ts +2 -0
- package/dist/types/core/server/stream/deferredScript.d.ts +4 -0
- package/dist/types/core/server/stream/deferredScriptOutputCoordinator.d.ts +7 -0
- package/dist/types/core/server/stream/shared.d.ts +14 -0
- package/dist/types/core/server/utils.d.ts +6 -0
- package/package.json +10 -10
|
@@ -29,6 +29,7 @@ var __webpack_exports__ = {};
|
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
31
|
createReplaceHelemt: ()=>createReplaceHelemt,
|
|
32
|
+
hasTitleTagBefore: ()=>hasTitleTagBefore,
|
|
32
33
|
helmetReplace: ()=>helmetReplace
|
|
33
34
|
});
|
|
34
35
|
const external_os_namespaceObject = require("os");
|
|
@@ -38,6 +39,10 @@ const RE_BODY_ATTR = /<body[^>]*>/;
|
|
|
38
39
|
const RE_LAST_IN_HEAD = /<\/head>/;
|
|
39
40
|
const RE_TITLE = /<title[^>]*>([\s\S\n\r]*?)<\/title>/;
|
|
40
41
|
const TEST_TITLE_CONTENT = /(?<=<title[^>]*>)([\s\S\n\r]*?)([.|\S])([\s\S\n\r]*?)(?=<\/title>)/;
|
|
42
|
+
function hasTitleTagBefore(html, index) {
|
|
43
|
+
const match = RE_TITLE.exec(html);
|
|
44
|
+
return null !== match && match.index < index;
|
|
45
|
+
}
|
|
41
46
|
function createReplaceHelemt(helmetData) {
|
|
42
47
|
return helmetData ? (template)=>helmetReplace(template, helmetData) : (tempalte)=>tempalte;
|
|
43
48
|
}
|
|
@@ -69,9 +74,11 @@ function helmetReplace(content, helmetData) {
|
|
|
69
74
|
return (0, external_utils_js_namespaceObject.safeReplace)(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
|
|
70
75
|
}
|
|
71
76
|
exports.createReplaceHelemt = __webpack_exports__.createReplaceHelemt;
|
|
77
|
+
exports.hasTitleTagBefore = __webpack_exports__.hasTitleTagBefore;
|
|
72
78
|
exports.helmetReplace = __webpack_exports__.helmetReplace;
|
|
73
79
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
74
80
|
"createReplaceHelemt",
|
|
81
|
+
"hasTitleTagBefore",
|
|
75
82
|
"helmetReplace"
|
|
76
83
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
77
84
|
Object.defineProperty(exports, '__esModule', {
|
|
@@ -47,10 +47,9 @@ const external_helmet_js_namespaceObject = require("../helmet.js");
|
|
|
47
47
|
const external_shared_js_namespaceObject = require("../shared.js");
|
|
48
48
|
const external_utils_js_namespaceObject = require("../utils.js");
|
|
49
49
|
const readAsset = async (chunk)=>{
|
|
50
|
-
const fs = await import("fs/promises");
|
|
51
50
|
const path = await import("path");
|
|
52
51
|
const filepath = path.join(__dirname, chunk);
|
|
53
|
-
return
|
|
52
|
+
return (0, external_utils_js_namespaceObject.readAssetWithCache)(filepath);
|
|
54
53
|
};
|
|
55
54
|
const checkIsInline = (chunk, enableInline)=>{
|
|
56
55
|
if ('production' !== process.env.NODE_ENV) return false;
|
|
@@ -34,7 +34,6 @@ const external_stream_namespaceObject = require("stream");
|
|
|
34
34
|
const external_string_decoder_namespaceObject = require("string_decoder");
|
|
35
35
|
const node_namespaceObject = require("@modern-js/runtime-utils/node");
|
|
36
36
|
const constants_namespaceObject = require("@modern-js/utils/universal/constants");
|
|
37
|
-
const external_common_js_namespaceObject = require("../../../common.js");
|
|
38
37
|
const external_constants_js_namespaceObject = require("../../constants.js");
|
|
39
38
|
const index_js_namespaceObject = require("../../context/index.js");
|
|
40
39
|
const monitors_index_js_namespaceObject = require("../../context/monitors/index.js");
|
|
@@ -68,7 +67,7 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
68
67
|
extenders.forEach((extender)=>{
|
|
69
68
|
if (extender.modifyRootElement) processedRootElement = extender.modifyRootElement(processedRootElement);
|
|
70
69
|
});
|
|
71
|
-
const
|
|
70
|
+
const splitAtShellEndMark = (0, external_shared_js_namespaceObject.createShellEndMarkSplitter)();
|
|
72
71
|
return new Promise((resolve)=>{
|
|
73
72
|
const { pipe: reactStreamingPipe } = renderToPipeableStream(processedRootElement, {
|
|
74
73
|
nonce: config.nonce,
|
|
@@ -100,12 +99,9 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
100
99
|
transform (chunk, _encoding, callback) {
|
|
101
100
|
try {
|
|
102
101
|
if (shellChunkStatus !== external_shared_js_namespaceObject.ShellChunkStatus.FINISH) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (-1 !== markerIndex) {
|
|
107
|
-
const beforeMark = concatedChunk.slice(0, markerIndex);
|
|
108
|
-
const afterMark = concatedChunk.slice(markerIndex + external_common_js_namespaceObject.ESCAPED_SHELL_STREAM_END_MARK.length);
|
|
102
|
+
const shell = splitAtShellEndMark(decoder.write(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
|
|
103
|
+
if (shell) {
|
|
104
|
+
const { beforeMark, afterMark } = shell;
|
|
109
105
|
shellChunkStatus = external_shared_js_namespaceObject.ShellChunkStatus.FINISH;
|
|
110
106
|
writeReact(`${shellBefore}${beforeMark}${shellAfter}`);
|
|
111
107
|
if (afterMark) writeReact(afterMark);
|
|
@@ -162,14 +158,14 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
162
158
|
const activeDeferreds = storageContext?.activeDeferreds;
|
|
163
159
|
const deferredScriptKeys = storageContext?.deferredScriptKeys;
|
|
164
160
|
const entries = activeDeferreds instanceof Map ? Array.from(activeDeferreds.entries()) : [];
|
|
165
|
-
if (entries
|
|
161
|
+
if ((0, external_deferredScript_js_namespaceObject.hasDeferredResolvers)(entries, deferredScriptKeys)) {
|
|
166
162
|
const enqueueScript = (s)=>{
|
|
167
163
|
if (!outputState.coordinator) throw new Error("Deferred script coordinator is not ready");
|
|
168
164
|
if (shellChunkStatus === external_shared_js_namespaceObject.ShellChunkStatus.FINISH) outputState.coordinator.enqueueResolver(s);
|
|
169
165
|
else pendingScripts.push(s);
|
|
170
166
|
};
|
|
171
167
|
deferredResolversComplete = (0, external_deferredScript_js_namespaceObject.enqueueFromEntries)(entries, deferredScriptKeys, config.nonce, enqueueScript);
|
|
172
|
-
}
|
|
168
|
+
} else coordinator.markNoResolvers();
|
|
173
169
|
} catch (err) {
|
|
174
170
|
const monitors = (0, monitors_index_js_namespaceObject.getMonitors)();
|
|
175
171
|
monitors.error("cannot inject router data script", err);
|
|
@@ -32,7 +32,6 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
32
32
|
});
|
|
33
33
|
const ssr_namespaceObject = require("@modern-js/render/ssr");
|
|
34
34
|
const node_namespaceObject = require("@modern-js/runtime-utils/node");
|
|
35
|
-
const external_common_js_namespaceObject = require("../../../common.js");
|
|
36
35
|
const external_constants_js_namespaceObject = require("../../constants.js");
|
|
37
36
|
const external_deferredScript_js_namespaceObject = require("./deferredScript.js");
|
|
38
37
|
const external_deferredScriptOutputCoordinator_js_namespaceObject = require("./deferredScriptOutputCoordinator.js");
|
|
@@ -40,7 +39,7 @@ const external_shared_js_namespaceObject = require("./shared.js");
|
|
|
40
39
|
const external_template_js_namespaceObject = require("./template.js");
|
|
41
40
|
const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
42
41
|
let shellChunkStatus = external_shared_js_namespaceObject.ShellChunkStatus.START;
|
|
43
|
-
const
|
|
42
|
+
const splitAtShellEndMark = (0, external_shared_js_namespaceObject.createShellEndMarkSplitter)();
|
|
44
43
|
const { htmlTemplate, runtimeContext, config, ssrConfig, entryName, rscRoot } = options;
|
|
45
44
|
const { shellBefore, shellAfter } = await (0, external_template_js_namespaceObject.getTemplates)(htmlTemplate, {
|
|
46
45
|
renderLevel: external_constants_js_namespaceObject.RenderLevel.SERVER_RENDER,
|
|
@@ -98,11 +97,12 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
98
97
|
const activeDeferreds = storageContext?.activeDeferreds;
|
|
99
98
|
const deferredScriptKeys = storageContext?.deferredScriptKeys;
|
|
100
99
|
const entries = activeDeferreds instanceof Map ? Array.from(activeDeferreds.entries()) : [];
|
|
101
|
-
if (entries
|
|
100
|
+
if ((0, external_deferredScript_js_namespaceObject.hasDeferredResolvers)(entries, deferredScriptKeys)) deferredResolversComplete = (0, external_deferredScript_js_namespaceObject.enqueueFromEntries)(entries, deferredScriptKeys, config.nonce, (script)=>{
|
|
102
101
|
if (!coordinator) throw new Error("Deferred script coordinator is not ready");
|
|
103
102
|
if (shellChunkStatus === external_shared_js_namespaceObject.ShellChunkStatus.FINISH) coordinator.enqueueResolver(script);
|
|
104
103
|
else pendingScripts.push(script);
|
|
105
104
|
});
|
|
105
|
+
else coordinator.markNoResolvers();
|
|
106
106
|
async function push() {
|
|
107
107
|
try {
|
|
108
108
|
const { done, value } = await reader.read();
|
|
@@ -116,14 +116,11 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
116
116
|
}
|
|
117
117
|
if (isClosed) return;
|
|
118
118
|
if (shellChunkStatus !== external_shared_js_namespaceObject.ShellChunkStatus.FINISH) {
|
|
119
|
-
|
|
119
|
+
const shell = splitAtShellEndMark(decoder.decode(value, {
|
|
120
120
|
stream: true
|
|
121
121
|
}));
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (-1 !== markerIndex) {
|
|
125
|
-
const beforeMark = concatedChunk.slice(0, markerIndex);
|
|
126
|
-
const afterMark = concatedChunk.slice(markerIndex + external_common_js_namespaceObject.ESCAPED_SHELL_STREAM_END_MARK.length);
|
|
122
|
+
if (shell) {
|
|
123
|
+
const { beforeMark, afterMark } = shell;
|
|
127
124
|
shellChunkStatus = external_shared_js_namespaceObject.ShellChunkStatus.FINISH;
|
|
128
125
|
coordinator?.writeReact(`${shellBefore}${beforeMark}${shellAfter}`);
|
|
129
126
|
if (afterMark) coordinator?.writeReact(afterMark);
|
|
@@ -31,6 +31,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
31
31
|
buildDeferredDataScript: ()=>buildDeferredDataScript,
|
|
32
32
|
enqueueFromEntries: ()=>enqueueFromEntries,
|
|
33
33
|
escapeAttr: ()=>escapeAttr,
|
|
34
|
+
hasDeferredResolvers: ()=>hasDeferredResolvers,
|
|
34
35
|
isDeferredDataLike: ()=>isDeferredDataLike,
|
|
35
36
|
isPromiseLike: ()=>isPromiseLike,
|
|
36
37
|
isRecord: ()=>isRecord,
|
|
@@ -78,11 +79,17 @@ function buildDeferredDataScript(nonce, args) {
|
|
|
78
79
|
const nonceAttr = nonce ? ` nonce="${nonce}"` : '';
|
|
79
80
|
return `<script async${nonceAttr} data-fn-name="r" data-script-src="modern-run-router-data-fn" data-fn-args='${escaped}' suppressHydrationWarning>${constants_js_namespaceObject.runRouterDataFnStr}</script>`;
|
|
80
81
|
}
|
|
82
|
+
function getSerializedKeys(routeId, value, deferredScriptKeys) {
|
|
83
|
+
return deferredScriptKeys ? deferredScriptKeys.get(routeId) ?? [] : value.pendingKeys ?? [];
|
|
84
|
+
}
|
|
85
|
+
function hasDeferredResolvers(entries, deferredScriptKeys) {
|
|
86
|
+
return entries.some(([routeId, value])=>isDeferredDataLike(value) && getSerializedKeys(routeId, value, deferredScriptKeys).some((key)=>isPromiseLike(value.data?.[key])));
|
|
87
|
+
}
|
|
81
88
|
function enqueueFromEntries(entries, deferredScriptKeys, nonce, emit) {
|
|
82
89
|
const pendingResolvers = [];
|
|
83
90
|
entries.forEach(([routeId, value])=>{
|
|
84
91
|
if (!isDeferredDataLike(value)) return;
|
|
85
|
-
const serializedKeys =
|
|
92
|
+
const serializedKeys = getSerializedKeys(routeId, value, deferredScriptKeys);
|
|
86
93
|
serializedKeys.forEach((key)=>{
|
|
87
94
|
const tracked = value.data?.[key];
|
|
88
95
|
if (isPromiseLike(tracked)) pendingResolvers.push(Promise.resolve(tracked).then((val)=>{
|
|
@@ -106,6 +113,7 @@ function enqueueFromEntries(entries, deferredScriptKeys, nonce, emit) {
|
|
|
106
113
|
exports.buildDeferredDataScript = __webpack_exports__.buildDeferredDataScript;
|
|
107
114
|
exports.enqueueFromEntries = __webpack_exports__.enqueueFromEntries;
|
|
108
115
|
exports.escapeAttr = __webpack_exports__.escapeAttr;
|
|
116
|
+
exports.hasDeferredResolvers = __webpack_exports__.hasDeferredResolvers;
|
|
109
117
|
exports.isDeferredDataLike = __webpack_exports__.isDeferredDataLike;
|
|
110
118
|
exports.isPromiseLike = __webpack_exports__.isPromiseLike;
|
|
111
119
|
exports.isRecord = __webpack_exports__.isRecord;
|
|
@@ -114,6 +122,7 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
|
114
122
|
"buildDeferredDataScript",
|
|
115
123
|
"enqueueFromEntries",
|
|
116
124
|
"escapeAttr",
|
|
125
|
+
"hasDeferredResolvers",
|
|
117
126
|
"isDeferredDataLike",
|
|
118
127
|
"isPromiseLike",
|
|
119
128
|
"isRecord",
|
|
@@ -88,8 +88,12 @@ class DeferredScriptOutputCoordinator {
|
|
|
88
88
|
this.shellFinished = true;
|
|
89
89
|
this.flushResolvers();
|
|
90
90
|
}
|
|
91
|
+
markNoResolvers() {
|
|
92
|
+
this.expectsResolvers = false;
|
|
93
|
+
}
|
|
91
94
|
writeReact(chunk) {
|
|
92
95
|
if (this.closed || !chunk) return;
|
|
96
|
+
if (!this.expectsResolvers) return void this.emit(chunk);
|
|
93
97
|
this.flushResolvers();
|
|
94
98
|
let emittedUntil = 0;
|
|
95
99
|
for(let index = 0; index < chunk.length; index++){
|
|
@@ -107,7 +111,7 @@ class DeferredScriptOutputCoordinator {
|
|
|
107
111
|
if (this.closed) return;
|
|
108
112
|
this.flushResolvers();
|
|
109
113
|
this.closed = true;
|
|
110
|
-
if (this.pendingResolvers.length > 0) throw new Error("Cannot emit deferred resolver scripts because the React stream ended outside a safe HTML boundary.");
|
|
114
|
+
if (this.pendingResolvers.length > 0) throw new Error(this.expectsResolvers ? "Cannot emit deferred resolver scripts because the React stream ended outside a safe HTML boundary." : "Cannot emit deferred resolver scripts that were queued after the stream was marked as having no resolvers.");
|
|
111
115
|
}
|
|
112
116
|
abort() {
|
|
113
117
|
this.closed = true;
|
|
@@ -117,7 +121,7 @@ class DeferredScriptOutputCoordinator {
|
|
|
117
121
|
return this.shellFinished && 'data' === this.state && 0 === this.openElements.length && !this.awaitingContinuationProtocol;
|
|
118
122
|
}
|
|
119
123
|
flushResolvers() {
|
|
120
|
-
if (!this.isSafeBoundary() || 0 === this.pendingResolvers.length) return;
|
|
124
|
+
if (!this.expectsResolvers || !this.isSafeBoundary() || 0 === this.pendingResolvers.length) return;
|
|
121
125
|
while(this.pendingResolvers.length > 0){
|
|
122
126
|
const resolver = this.pendingResolvers.shift();
|
|
123
127
|
if (resolver) this.emit(resolver);
|
|
@@ -314,11 +318,12 @@ class DeferredScriptOutputCoordinator {
|
|
|
314
318
|
this.state = 'data';
|
|
315
319
|
return;
|
|
316
320
|
}
|
|
317
|
-
const endTagPrefix = `</${rawTextElement}`;
|
|
318
321
|
if (this.continuationProtocolScript) {
|
|
319
322
|
this.continuationProtocolTail = `${this.continuationProtocolTail}${char}`.slice(-4);
|
|
320
323
|
if (/\$(?:RC|RS|RX|RB|RR)\b/.test(this.continuationProtocolTail)) this.hasContinuationProtocolCall = true;
|
|
321
324
|
}
|
|
325
|
+
if (!this.rawTextSearch && '<' !== char) return;
|
|
326
|
+
const endTagPrefix = `</${rawTextElement}`;
|
|
322
327
|
this.rawTextSearch = getSuffix(`${this.rawTextSearch}${char.toLowerCase()}`, endTagPrefix);
|
|
323
328
|
if (this.rawTextSearch === endTagPrefix) this.state = 'raw-end-tag';
|
|
324
329
|
}
|
|
@@ -345,6 +350,7 @@ class DeferredScriptOutputCoordinator {
|
|
|
345
350
|
this.pendingResolvers = [];
|
|
346
351
|
this.openElements = [];
|
|
347
352
|
this.shellFinished = false;
|
|
353
|
+
this.expectsResolvers = true;
|
|
348
354
|
this.closed = false;
|
|
349
355
|
this.state = 'data';
|
|
350
356
|
this.currentTagName = '';
|
|
@@ -39,6 +39,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
39
39
|
__webpack_require__.d(__webpack_exports__, {
|
|
40
40
|
ShellChunkStatus: ()=>shared_ShellChunkStatus,
|
|
41
41
|
createRenderStreaming: ()=>createRenderStreaming,
|
|
42
|
+
createShellEndMarkSplitter: ()=>createShellEndMarkSplitter,
|
|
42
43
|
encodeForWebStream: ()=>encodeForWebStream,
|
|
43
44
|
getReadableStreamFromString: ()=>getReadableStreamFromString,
|
|
44
45
|
resolveStreamingMode: ()=>resolveStreamingMode
|
|
@@ -56,6 +57,25 @@ var shared_ShellChunkStatus = /*#__PURE__*/ function(ShellChunkStatus) {
|
|
|
56
57
|
ShellChunkStatus[ShellChunkStatus["FINISH"] = 1] = "FINISH";
|
|
57
58
|
return ShellChunkStatus;
|
|
58
59
|
}({});
|
|
60
|
+
function createShellEndMarkSplitter() {
|
|
61
|
+
const marker = external_common_js_namespaceObject.ESCAPED_SHELL_STREAM_END_MARK;
|
|
62
|
+
const shellChunks = [];
|
|
63
|
+
let tail = '';
|
|
64
|
+
return (text)=>{
|
|
65
|
+
const searchText = tail + text;
|
|
66
|
+
const markerIndex = searchText.indexOf(marker);
|
|
67
|
+
if (-1 === markerIndex) {
|
|
68
|
+
shellChunks.push(text);
|
|
69
|
+
tail = searchText.slice(-(marker.length - 1));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const buffered = shellChunks.join('');
|
|
73
|
+
return {
|
|
74
|
+
beforeMark: buffered.slice(0, buffered.length - tail.length) + searchText.slice(0, markerIndex),
|
|
75
|
+
afterMark: searchText.slice(markerIndex + marker.length)
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
}
|
|
59
79
|
const SHOULD_STREAM_ALL_HEADER = 'x-should-stream-all';
|
|
60
80
|
function parseShouldStreamAllFlag(value) {
|
|
61
81
|
if (!value) return;
|
|
@@ -153,12 +173,14 @@ function createRenderStreaming(createReadableStreamPromise) {
|
|
|
153
173
|
}
|
|
154
174
|
exports.ShellChunkStatus = __webpack_exports__.ShellChunkStatus;
|
|
155
175
|
exports.createRenderStreaming = __webpack_exports__.createRenderStreaming;
|
|
176
|
+
exports.createShellEndMarkSplitter = __webpack_exports__.createShellEndMarkSplitter;
|
|
156
177
|
exports.encodeForWebStream = __webpack_exports__.encodeForWebStream;
|
|
157
178
|
exports.getReadableStreamFromString = __webpack_exports__.getReadableStreamFromString;
|
|
158
179
|
exports.resolveStreamingMode = __webpack_exports__.resolveStreamingMode;
|
|
159
180
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
160
181
|
"ShellChunkStatus",
|
|
161
182
|
"createRenderStreaming",
|
|
183
|
+
"createShellEndMarkSplitter",
|
|
162
184
|
"encodeForWebStream",
|
|
163
185
|
"getReadableStreamFromString",
|
|
164
186
|
"resolveStreamingMode"
|
|
@@ -122,16 +122,32 @@ async function generateHtml(App, htmlTemplate, chunkSet, collectors, htmlModifie
|
|
|
122
122
|
}
|
|
123
123
|
await Promise.all(collectors.map((component)=>component.effect()));
|
|
124
124
|
const { ssrScripts, cssChunk, jsChunk } = chunkSet;
|
|
125
|
-
const
|
|
126
|
-
createReplaceHtml(html),
|
|
125
|
+
const replaceChunks = [
|
|
127
126
|
createReplaceChunkJs(jsChunk),
|
|
128
|
-
createReplaceChunkCss(cssChunk)
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
createReplaceChunkCss(cssChunk)
|
|
128
|
+
];
|
|
129
|
+
const replaceHelmet = (0, external_helmet_js_namespaceObject.createReplaceHelemt)(helmetData);
|
|
130
|
+
const appHtmlMayHaveFirstTitle = html.includes('<title') && !(0, external_helmet_js_namespaceObject.hasTitleTagBefore)(htmlTemplate, htmlTemplate.indexOf(external_constants_js_namespaceObject_1.HTML_PLACEHOLDER));
|
|
131
|
+
const finalHtml = await (0, external_shared_js_namespaceObject.buildHtml)(htmlTemplate, [
|
|
132
|
+
...appHtmlMayHaveFirstTitle ? [
|
|
133
|
+
createReplaceHtml(html),
|
|
134
|
+
...replaceChunks,
|
|
135
|
+
createReplaceSSRDataScript(ssrScripts),
|
|
136
|
+
replaceHelmet
|
|
137
|
+
] : [
|
|
138
|
+
(template)=>(0, external_utils_js_namespaceObject.safeReplace)((0, external_utils_js_namespaceObject.safeReplace)(template, external_constants_js_namespaceObject_1.HTML_PLACEHOLDER, HTML_MARKER), external_constants_js_namespaceObject_1.SSR_DATA_PLACEHOLDER, SSR_DATA_MARKER),
|
|
139
|
+
...replaceChunks,
|
|
140
|
+
replaceHelmet,
|
|
141
|
+
(template)=>(0, external_utils_js_namespaceObject.safeReplace)(template, SSR_DATA_MARKER, ssrScripts),
|
|
142
|
+
(template)=>(0, external_utils_js_namespaceObject.safeReplace)(template, HTML_MARKER, html)
|
|
143
|
+
],
|
|
131
144
|
...htmlModifiers
|
|
132
145
|
]);
|
|
133
146
|
return finalHtml;
|
|
134
147
|
}
|
|
148
|
+
const markerId = Math.random().toString(36).slice(2);
|
|
149
|
+
const HTML_MARKER = `<!--modern-html-${markerId}-->`;
|
|
150
|
+
const SSR_DATA_MARKER = `<!--modern-ssr-data-${markerId}-->`;
|
|
135
151
|
function createReplaceHtml(html) {
|
|
136
152
|
return (template)=>(0, external_utils_js_namespaceObject.safeReplace)(template, external_constants_js_namespaceObject_1.HTML_PLACEHOLDER, html);
|
|
137
153
|
}
|
|
@@ -43,10 +43,9 @@ const checkIsInline = (chunk, enableInline)=>{
|
|
|
43
43
|
else return Boolean(enableInline);
|
|
44
44
|
};
|
|
45
45
|
const readAsset = async (chunk)=>{
|
|
46
|
-
const fs = await import("fs/promises");
|
|
47
46
|
const path = await import("path");
|
|
48
47
|
const filepath = path.resolve(__dirname, chunk.filename);
|
|
49
|
-
return
|
|
48
|
+
return (0, external_utils_js_namespaceObject.readAssetWithCache)(filepath);
|
|
50
49
|
};
|
|
51
50
|
class LoadableCollector {
|
|
52
51
|
get existsAssets() {
|
|
@@ -33,6 +33,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
33
33
|
getSSRConfigByEntry: ()=>getSSRConfigByEntry,
|
|
34
34
|
getSSRMode: ()=>getSSRMode,
|
|
35
35
|
hasStylesheetLink: ()=>hasStylesheetLink,
|
|
36
|
+
readAssetWithCache: ()=>readAssetWithCache,
|
|
36
37
|
safeReplace: ()=>safeReplace,
|
|
37
38
|
serializeErrors: ()=>serializeErrors
|
|
38
39
|
});
|
|
@@ -46,6 +47,17 @@ function safeReplace(source, searchValue, replaceValue) {
|
|
|
46
47
|
function checkIsNode() {
|
|
47
48
|
return "u" > typeof process && process.release?.name === 'node';
|
|
48
49
|
}
|
|
50
|
+
const assetCache = new Map();
|
|
51
|
+
function readAssetWithCache(filepath) {
|
|
52
|
+
const cached = assetCache.get(filepath);
|
|
53
|
+
if (cached) return cached;
|
|
54
|
+
const content = import("fs/promises").then((fs)=>fs.readFile(filepath, 'utf-8'));
|
|
55
|
+
assetCache.set(filepath, content);
|
|
56
|
+
content.catch(()=>{
|
|
57
|
+
if (assetCache.get(filepath) === content) assetCache.delete(filepath);
|
|
58
|
+
});
|
|
59
|
+
return content;
|
|
60
|
+
}
|
|
49
61
|
function serializeErrors(errors) {
|
|
50
62
|
if (!errors) return null;
|
|
51
63
|
const entries = Object.entries(errors);
|
|
@@ -99,6 +111,7 @@ exports.checkIsNode = __webpack_exports__.checkIsNode;
|
|
|
99
111
|
exports.getSSRConfigByEntry = __webpack_exports__.getSSRConfigByEntry;
|
|
100
112
|
exports.getSSRMode = __webpack_exports__.getSSRMode;
|
|
101
113
|
exports.hasStylesheetLink = __webpack_exports__.hasStylesheetLink;
|
|
114
|
+
exports.readAssetWithCache = __webpack_exports__.readAssetWithCache;
|
|
102
115
|
exports.safeReplace = __webpack_exports__.safeReplace;
|
|
103
116
|
exports.serializeErrors = __webpack_exports__.serializeErrors;
|
|
104
117
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
@@ -107,6 +120,7 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
|
107
120
|
"getSSRConfigByEntry",
|
|
108
121
|
"getSSRMode",
|
|
109
122
|
"hasStylesheetLink",
|
|
123
|
+
"readAssetWithCache",
|
|
110
124
|
"safeReplace",
|
|
111
125
|
"serializeErrors"
|
|
112
126
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -113,30 +113,20 @@ const parseModule = async ({ source, filename })=>{
|
|
|
113
113
|
}
|
|
114
114
|
return (0, external_es_module_lexer_namespaceObject.parse)(content);
|
|
115
115
|
};
|
|
116
|
-
const
|
|
116
|
+
const hasExport = async (filename, exportName, source)=>{
|
|
117
117
|
let content = source;
|
|
118
118
|
if (!source) content = (await utils_namespaceObject.fs.readFile(filename, 'utf-8')).toString();
|
|
119
|
-
if (content) {
|
|
119
|
+
if (content && (content.includes(exportName) || content.includes('\\'))) {
|
|
120
120
|
const [, moduleExports] = await parseModule({
|
|
121
121
|
source: content.toString(),
|
|
122
122
|
filename
|
|
123
123
|
});
|
|
124
|
-
return moduleExports.some((e)=>e.n ===
|
|
125
|
-
}
|
|
126
|
-
return false;
|
|
127
|
-
};
|
|
128
|
-
const hasAction = async (filename, source)=>{
|
|
129
|
-
let content = source;
|
|
130
|
-
if (!source) content = (await utils_namespaceObject.fs.readFile(filename, 'utf-8')).toString();
|
|
131
|
-
if (content) {
|
|
132
|
-
const [, moduleExports] = await parseModule({
|
|
133
|
-
source: content.toString(),
|
|
134
|
-
filename
|
|
135
|
-
});
|
|
136
|
-
return moduleExports.some((e)=>e.n === external_constants_js_namespaceObject.ACTION_EXPORT_NAME);
|
|
124
|
+
return moduleExports.some((e)=>e.n === exportName);
|
|
137
125
|
}
|
|
138
126
|
return false;
|
|
139
127
|
};
|
|
128
|
+
const hasLoader = (filename, source)=>hasExport(filename, external_constants_js_namespaceObject.LOADER_EXPORT_NAME, source);
|
|
129
|
+
const hasAction = (filename, source)=>hasExport(filename, external_constants_js_namespaceObject.ACTION_EXPORT_NAME, source);
|
|
140
130
|
const getServerLoadersFile = (internalDirectory, entryName)=>external_path_default().join(internalDirectory, entryName, 'route-server-loaders.js');
|
|
141
131
|
const getServerCombinedModuleFile = (internalDirectory, entryName)=>external_path_default().join(internalDirectory, entryName, 'server-loader-combined.js');
|
|
142
132
|
const getPathWithoutExt = (filename)=>{
|
|
@@ -5,6 +5,10 @@ const RE_BODY_ATTR = /<body[^>]*>/;
|
|
|
5
5
|
const RE_LAST_IN_HEAD = /<\/head>/;
|
|
6
6
|
const RE_TITLE = /<title[^>]*>([\s\S\n\r]*?)<\/title>/;
|
|
7
7
|
const TEST_TITLE_CONTENT = /(?<=<title[^>]*>)([\s\S\n\r]*?)([.|\S])([\s\S\n\r]*?)(?=<\/title>)/;
|
|
8
|
+
function hasTitleTagBefore(html, index) {
|
|
9
|
+
const match = RE_TITLE.exec(html);
|
|
10
|
+
return null !== match && match.index < index;
|
|
11
|
+
}
|
|
8
12
|
function createReplaceHelemt(helmetData) {
|
|
9
13
|
return helmetData ? (template)=>helmetReplace(template, helmetData) : (tempalte)=>tempalte;
|
|
10
14
|
}
|
|
@@ -35,4 +39,4 @@ function helmetReplace(content, helmetData) {
|
|
|
35
39
|
].reduce((pre, cur)=>pre + (cur.length > 0 ? ` ${cur}${EOL}` : ''), '');
|
|
36
40
|
return safeReplace(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
|
|
37
41
|
}
|
|
38
|
-
export { createReplaceHelemt, helmetReplace };
|
|
42
|
+
export { createReplaceHelemt, hasTitleTagBefore, helmetReplace };
|
|
@@ -3,12 +3,11 @@ import react_helmet from "react-helmet";
|
|
|
3
3
|
import { CHUNK_CSS_PLACEHOLDER } from "../constants.mjs";
|
|
4
4
|
import { createReplaceHelemt } from "../helmet.mjs";
|
|
5
5
|
import { buildHtml } from "../shared.mjs";
|
|
6
|
-
import { checkIsNode, hasStylesheetLink, safeReplace } from "../utils.mjs";
|
|
6
|
+
import { checkIsNode, hasStylesheetLink, readAssetWithCache, safeReplace } from "../utils.mjs";
|
|
7
7
|
const readAsset = async (chunk)=>{
|
|
8
|
-
const fs = await import("fs/promises");
|
|
9
8
|
const path = await import("path");
|
|
10
9
|
const filepath = path.join(__dirname, chunk);
|
|
11
|
-
return
|
|
10
|
+
return readAssetWithCache(filepath);
|
|
12
11
|
};
|
|
13
12
|
const checkIsInline = (chunk, enableInline)=>{
|
|
14
13
|
if ('production' !== process.env.NODE_ENV) return false;
|
|
@@ -2,13 +2,12 @@ import { PassThrough, Readable, Transform } from "stream";
|
|
|
2
2
|
import { StringDecoder } from "string_decoder";
|
|
3
3
|
import { storage } from "@modern-js/runtime-utils/node";
|
|
4
4
|
import { SSR_HYDRATION_ID_PREFIX } from "@modern-js/utils/universal/constants";
|
|
5
|
-
import { ESCAPED_SHELL_STREAM_END_MARK } from "../../../common.mjs";
|
|
6
5
|
import { RenderLevel } from "../../constants.mjs";
|
|
7
6
|
import { getGlobalInternalRuntimeContext } from "../../context/index.mjs";
|
|
8
7
|
import { getMonitors } from "../../context/monitors/index.mjs";
|
|
9
|
-
import { enqueueFromEntries } from "./deferredScript.mjs";
|
|
8
|
+
import { enqueueFromEntries, hasDeferredResolvers } from "./deferredScript.mjs";
|
|
10
9
|
import { DeferredScriptOutputCoordinator } from "./deferredScriptOutputCoordinator.mjs";
|
|
11
|
-
import { ShellChunkStatus, getReadableStreamFromString, resolveStreamingMode } from "./shared.mjs";
|
|
10
|
+
import { ShellChunkStatus, createShellEndMarkSplitter, getReadableStreamFromString, resolveStreamingMode } from "./shared.mjs";
|
|
12
11
|
import { getTemplates } from "./template.mjs";
|
|
13
12
|
const defaultExtender = {
|
|
14
13
|
modifyRootElement: (rootElement)=>rootElement,
|
|
@@ -36,7 +35,7 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
36
35
|
extenders.forEach((extender)=>{
|
|
37
36
|
if (extender.modifyRootElement) processedRootElement = extender.modifyRootElement(processedRootElement);
|
|
38
37
|
});
|
|
39
|
-
const
|
|
38
|
+
const splitAtShellEndMark = createShellEndMarkSplitter();
|
|
40
39
|
return new Promise((resolve)=>{
|
|
41
40
|
const { pipe: reactStreamingPipe } = renderToPipeableStream(processedRootElement, {
|
|
42
41
|
nonce: config.nonce,
|
|
@@ -68,12 +67,9 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
68
67
|
transform (chunk, _encoding, callback) {
|
|
69
68
|
try {
|
|
70
69
|
if (shellChunkStatus !== ShellChunkStatus.FINISH) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (-1 !== markerIndex) {
|
|
75
|
-
const beforeMark = concatedChunk.slice(0, markerIndex);
|
|
76
|
-
const afterMark = concatedChunk.slice(markerIndex + ESCAPED_SHELL_STREAM_END_MARK.length);
|
|
70
|
+
const shell = splitAtShellEndMark(decoder.write(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
|
|
71
|
+
if (shell) {
|
|
72
|
+
const { beforeMark, afterMark } = shell;
|
|
77
73
|
shellChunkStatus = ShellChunkStatus.FINISH;
|
|
78
74
|
writeReact(`${shellBefore}${beforeMark}${shellAfter}`);
|
|
79
75
|
if (afterMark) writeReact(afterMark);
|
|
@@ -130,14 +126,14 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
130
126
|
const activeDeferreds = storageContext?.activeDeferreds;
|
|
131
127
|
const deferredScriptKeys = storageContext?.deferredScriptKeys;
|
|
132
128
|
const entries = activeDeferreds instanceof Map ? Array.from(activeDeferreds.entries()) : [];
|
|
133
|
-
if (entries
|
|
129
|
+
if (hasDeferredResolvers(entries, deferredScriptKeys)) {
|
|
134
130
|
const enqueueScript = (s)=>{
|
|
135
131
|
if (!outputState.coordinator) throw new Error("Deferred script coordinator is not ready");
|
|
136
132
|
if (shellChunkStatus === ShellChunkStatus.FINISH) outputState.coordinator.enqueueResolver(s);
|
|
137
133
|
else pendingScripts.push(s);
|
|
138
134
|
};
|
|
139
135
|
deferredResolversComplete = enqueueFromEntries(entries, deferredScriptKeys, config.nonce, enqueueScript);
|
|
140
|
-
}
|
|
136
|
+
} else coordinator.markNoResolvers();
|
|
141
137
|
} catch (err) {
|
|
142
138
|
const monitors = getMonitors();
|
|
143
139
|
monitors.error("cannot inject router data script", err);
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { renderSSRStream } from "@modern-js/render/ssr";
|
|
2
2
|
import { storage } from "@modern-js/runtime-utils/node";
|
|
3
|
-
import { ESCAPED_SHELL_STREAM_END_MARK } from "../../../common.mjs";
|
|
4
3
|
import { RenderLevel } from "../../constants.mjs";
|
|
5
|
-
import { enqueueFromEntries } from "./deferredScript.mjs";
|
|
4
|
+
import { enqueueFromEntries, hasDeferredResolvers } from "./deferredScript.mjs";
|
|
6
5
|
import { DeferredScriptOutputCoordinator } from "./deferredScriptOutputCoordinator.mjs";
|
|
7
|
-
import { ShellChunkStatus, encodeForWebStream, getReadableStreamFromString, resolveStreamingMode } from "./shared.mjs";
|
|
6
|
+
import { ShellChunkStatus, createShellEndMarkSplitter, encodeForWebStream, getReadableStreamFromString, resolveStreamingMode } from "./shared.mjs";
|
|
8
7
|
import { getTemplates } from "./template.mjs";
|
|
9
8
|
const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
10
9
|
let shellChunkStatus = ShellChunkStatus.START;
|
|
11
|
-
const
|
|
10
|
+
const splitAtShellEndMark = createShellEndMarkSplitter();
|
|
12
11
|
const { htmlTemplate, runtimeContext, config, ssrConfig, entryName, rscRoot } = options;
|
|
13
12
|
const { shellBefore, shellAfter } = await getTemplates(htmlTemplate, {
|
|
14
13
|
renderLevel: RenderLevel.SERVER_RENDER,
|
|
@@ -66,11 +65,12 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
66
65
|
const activeDeferreds = storageContext?.activeDeferreds;
|
|
67
66
|
const deferredScriptKeys = storageContext?.deferredScriptKeys;
|
|
68
67
|
const entries = activeDeferreds instanceof Map ? Array.from(activeDeferreds.entries()) : [];
|
|
69
|
-
if (entries
|
|
68
|
+
if (hasDeferredResolvers(entries, deferredScriptKeys)) deferredResolversComplete = enqueueFromEntries(entries, deferredScriptKeys, config.nonce, (script)=>{
|
|
70
69
|
if (!coordinator) throw new Error("Deferred script coordinator is not ready");
|
|
71
70
|
if (shellChunkStatus === ShellChunkStatus.FINISH) coordinator.enqueueResolver(script);
|
|
72
71
|
else pendingScripts.push(script);
|
|
73
72
|
});
|
|
73
|
+
else coordinator.markNoResolvers();
|
|
74
74
|
async function push() {
|
|
75
75
|
try {
|
|
76
76
|
const { done, value } = await reader.read();
|
|
@@ -84,14 +84,11 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
84
84
|
}
|
|
85
85
|
if (isClosed) return;
|
|
86
86
|
if (shellChunkStatus !== ShellChunkStatus.FINISH) {
|
|
87
|
-
|
|
87
|
+
const shell = splitAtShellEndMark(decoder.decode(value, {
|
|
88
88
|
stream: true
|
|
89
89
|
}));
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (-1 !== markerIndex) {
|
|
93
|
-
const beforeMark = concatedChunk.slice(0, markerIndex);
|
|
94
|
-
const afterMark = concatedChunk.slice(markerIndex + ESCAPED_SHELL_STREAM_END_MARK.length);
|
|
90
|
+
if (shell) {
|
|
91
|
+
const { beforeMark, afterMark } = shell;
|
|
95
92
|
shellChunkStatus = ShellChunkStatus.FINISH;
|
|
96
93
|
coordinator?.writeReact(`${shellBefore}${beforeMark}${shellAfter}`);
|
|
97
94
|
if (afterMark) coordinator?.writeReact(afterMark);
|
|
@@ -40,11 +40,17 @@ function buildDeferredDataScript(nonce, args) {
|
|
|
40
40
|
const nonceAttr = nonce ? ` nonce="${nonce}"` : '';
|
|
41
41
|
return `<script async${nonceAttr} data-fn-name="r" data-script-src="modern-run-router-data-fn" data-fn-args='${escaped}' suppressHydrationWarning>${runRouterDataFnStr}</script>`;
|
|
42
42
|
}
|
|
43
|
+
function getSerializedKeys(routeId, value, deferredScriptKeys) {
|
|
44
|
+
return deferredScriptKeys ? deferredScriptKeys.get(routeId) ?? [] : value.pendingKeys ?? [];
|
|
45
|
+
}
|
|
46
|
+
function hasDeferredResolvers(entries, deferredScriptKeys) {
|
|
47
|
+
return entries.some(([routeId, value])=>isDeferredDataLike(value) && getSerializedKeys(routeId, value, deferredScriptKeys).some((key)=>isPromiseLike(value.data?.[key])));
|
|
48
|
+
}
|
|
43
49
|
function enqueueFromEntries(entries, deferredScriptKeys, nonce, emit) {
|
|
44
50
|
const pendingResolvers = [];
|
|
45
51
|
entries.forEach(([routeId, value])=>{
|
|
46
52
|
if (!isDeferredDataLike(value)) return;
|
|
47
|
-
const serializedKeys =
|
|
53
|
+
const serializedKeys = getSerializedKeys(routeId, value, deferredScriptKeys);
|
|
48
54
|
serializedKeys.forEach((key)=>{
|
|
49
55
|
const tracked = value.data?.[key];
|
|
50
56
|
if (isPromiseLike(tracked)) pendingResolvers.push(Promise.resolve(tracked).then((val)=>{
|
|
@@ -65,4 +71,4 @@ function enqueueFromEntries(entries, deferredScriptKeys, nonce, emit) {
|
|
|
65
71
|
});
|
|
66
72
|
return Promise.all(pendingResolvers).then(()=>void 0);
|
|
67
73
|
}
|
|
68
|
-
export { buildDeferredDataScript, enqueueFromEntries, escapeAttr, isDeferredDataLike, isPromiseLike, isRecord, toErrorInfo };
|
|
74
|
+
export { buildDeferredDataScript, enqueueFromEntries, escapeAttr, hasDeferredResolvers, isDeferredDataLike, isPromiseLike, isRecord, toErrorInfo };
|