@modern-js/runtime 0.0.0-nightly-20231217170625 → 0.0.0-nightly-20231219170633
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/index.js +2 -3
- package/dist/cjs/ssr/react/prerender/index.js +1 -0
- package/dist/cjs/ssr/serverRender/helmet.js +4 -4
- package/dist/cjs/ssr/serverRender/index.js +1 -1
- package/dist/cjs/ssr/serverRender/renderToStream/buildTemplate.after.js +1 -1
- package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +1 -1
- package/dist/cjs/ssr/serverRender/renderToStream/index.js +0 -5
- package/dist/cjs/ssr/serverRender/renderToString/buildHtml.js +4 -4
- package/dist/cjs/ssr/serverRender/renderToString/index.js +0 -5
- package/dist/cjs/ssr/serverRender/utils.js +4 -5
- package/dist/esm/cli/index.js +2 -3
- package/dist/esm/ssr/react/prerender/index.js +1 -0
- package/dist/esm/ssr/serverRender/helmet.js +5 -5
- package/dist/esm/ssr/serverRender/index.js +2 -2
- package/dist/esm/ssr/serverRender/renderToStream/buildTemplate.after.js +2 -2
- package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +2 -2
- package/dist/esm/ssr/serverRender/renderToStream/index.js +0 -5
- package/dist/esm/ssr/serverRender/renderToString/buildHtml.js +5 -5
- package/dist/esm/ssr/serverRender/renderToString/index.js +1 -6
- package/dist/esm/ssr/serverRender/utils.js +5 -4
- package/dist/esm-node/cli/index.js +2 -3
- package/dist/esm-node/ssr/react/prerender/index.js +1 -0
- package/dist/esm-node/ssr/serverRender/helmet.js +5 -5
- package/dist/esm-node/ssr/serverRender/index.js +2 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/buildTemplate.after.js +2 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +2 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/index.js +0 -5
- package/dist/esm-node/ssr/serverRender/renderToString/buildHtml.js +5 -5
- package/dist/esm-node/ssr/serverRender/renderToString/index.js +0 -5
- package/dist/esm-node/ssr/serverRender/utils.js +3 -4
- package/dist/types/exports/server.d.ts +1 -0
- package/dist/types/ssr/react/prerender/index.d.ts +4 -0
- package/dist/types/ssr/serverRender/utils.d.ts +1 -2
- package/package.json +10 -10
package/dist/cjs/cli/index.js
CHANGED
|
@@ -32,7 +32,6 @@ __export(cli_exports, {
|
|
|
32
32
|
runtimePlugin: () => runtimePlugin
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(cli_exports);
|
|
35
|
-
var import_path = __toESM(require("path"));
|
|
36
35
|
var import_utils = require("@modern-js/utils");
|
|
37
36
|
var import_cli = require("../state/cli");
|
|
38
37
|
var import_cli2 = require("../ssr/cli");
|
|
@@ -57,8 +56,8 @@ const runtimePlugin = () => ({
|
|
|
57
56
|
setup: (api) => {
|
|
58
57
|
return {
|
|
59
58
|
config() {
|
|
60
|
-
const
|
|
61
|
-
process.env.IS_REACT18 = (0, import_utils.isReact18)(
|
|
59
|
+
const appDir = api.useAppContext().appDirectory;
|
|
60
|
+
process.env.IS_REACT18 = (0, import_utils.isReact18)(appDir).toString();
|
|
62
61
|
return {
|
|
63
62
|
runtime: {},
|
|
64
63
|
runtimeByEntries: {},
|
|
@@ -101,6 +101,7 @@ function factory(Component) {
|
|
|
101
101
|
if (!validate) {
|
|
102
102
|
throw new Error("invalid props, check usage");
|
|
103
103
|
}
|
|
104
|
+
console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
|
|
104
105
|
return (0, import_react.createElement)(Component, {
|
|
105
106
|
...newProps
|
|
106
107
|
});
|
|
@@ -32,11 +32,11 @@ function helmet(content, helmetData) {
|
|
|
32
32
|
let result = content;
|
|
33
33
|
const bodyAttributes = helmetData.bodyAttributes.toString();
|
|
34
34
|
if (bodyAttributes) {
|
|
35
|
-
result = (0, import_utils.
|
|
35
|
+
result = (0, import_utils.safeReplace)(result, RE_BODY_ATTR, `<body ${bodyAttributes}>`);
|
|
36
36
|
}
|
|
37
37
|
const htmlAttributes = helmetData.htmlAttributes.toString();
|
|
38
38
|
if (htmlAttributes) {
|
|
39
|
-
result = (0, import_utils.
|
|
39
|
+
result = (0, import_utils.safeReplace)(result, RE_HTML_ATTR, `<html ${htmlAttributes}>`);
|
|
40
40
|
}
|
|
41
41
|
const base = helmetData.base.toString();
|
|
42
42
|
const link = helmetData.link.toString();
|
|
@@ -48,7 +48,7 @@ function helmet(content, helmetData) {
|
|
|
48
48
|
const existTitleTag = RE_TITLE.test(content);
|
|
49
49
|
const shouldReplaceTitle = existTitleTag && TEST_TITLE_CONTENT.test(title.trim());
|
|
50
50
|
if (shouldReplaceTitle) {
|
|
51
|
-
result =
|
|
51
|
+
result = (0, import_utils.safeReplace)(result, RE_TITLE, title);
|
|
52
52
|
}
|
|
53
53
|
const helmetStr = [
|
|
54
54
|
base,
|
|
@@ -61,5 +61,5 @@ function helmet(content, helmetData) {
|
|
|
61
61
|
].reduce((pre, cur) => {
|
|
62
62
|
return pre + (cur.length > 0 ? ` ${cur}${import_os.EOL}` : "");
|
|
63
63
|
}, "");
|
|
64
|
-
return (0, import_utils.
|
|
64
|
+
return (0, import_utils.safeReplace)(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
|
|
65
65
|
}
|
|
@@ -27,7 +27,7 @@ async function serverRender(options) {
|
|
|
27
27
|
var _options_context_ssrContext;
|
|
28
28
|
if ((_options_context_ssrContext = options.context.ssrContext) === null || _options_context_ssrContext === void 0 ? void 0 : _options_context_ssrContext.template) {
|
|
29
29
|
var _options_context_ssrContext1;
|
|
30
|
-
options.context.ssrContext.template = (_options_context_ssrContext1 = options.context.ssrContext) === null || _options_context_ssrContext1 === void 0 ? void 0 : _options_context_ssrContext1.template.replace("</head>", `${import_constants.
|
|
30
|
+
options.context.ssrContext.template = (_options_context_ssrContext1 = options.context.ssrContext) === null || _options_context_ssrContext1 === void 0 ? void 0 : _options_context_ssrContext1.template.replace("</head>", `${import_constants.CHUNK_CSS_PLACEHOLDER}</head>`);
|
|
31
31
|
}
|
|
32
32
|
if ((0, import_utils.isReact18)() && options.config.mode === "stream") {
|
|
33
33
|
const pipe = await require("./renderToStream").render(options);
|
|
@@ -32,7 +32,7 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
|
32
32
|
return (0, import_buildTemplate.buildTemplate)(afterAppTemplate, callbacks);
|
|
33
33
|
function injectSSRDataScript(template) {
|
|
34
34
|
const ssrDataScript = buildSSRDataScript();
|
|
35
|
-
return (0, import_utils.
|
|
35
|
+
return (0, import_utils.safeReplace)(template, import_constants.SSR_DATA_PLACEHOLDER, ssrDataScript);
|
|
36
36
|
function buildSSRDataScript() {
|
|
37
37
|
const { context: { ssrContext, initialData, __i18nData__ }, renderLevel } = options;
|
|
38
38
|
const { request, enableUnsafeCtx, nonce, tracker } = ssrContext;
|
|
@@ -52,7 +52,7 @@ function getHeadTemplate(beforeEntryTemplate, context) {
|
|
|
52
52
|
}
|
|
53
53
|
return (0, import_buildTemplate.buildTemplate)(headTemplate, callbacks);
|
|
54
54
|
function injectCss(headTemplate2) {
|
|
55
|
-
return (0, import_utils.
|
|
55
|
+
return (0, import_utils.safeReplace)(headTemplate2, import_constants.CHUNK_CSS_PLACEHOLDER, getCssChunks());
|
|
56
56
|
function getCssChunks() {
|
|
57
57
|
const { routeManifest, routerContext, routes } = context;
|
|
58
58
|
if (!routeManifest || !routerContext || !routes) {
|
|
@@ -34,7 +34,6 @@ module.exports = __toCommonJS(renderToStream_exports);
|
|
|
34
34
|
var import_react = require("react");
|
|
35
35
|
var import_node = require("@modern-js/runtime-utils/node");
|
|
36
36
|
var import_time = require("@modern-js/runtime-utils/time");
|
|
37
|
-
var import_prerender = require("../../react/prerender");
|
|
38
37
|
var import_tracker = require("../tracker");
|
|
39
38
|
var import_renderToPipe = __toESM(require("./renderToPipe"));
|
|
40
39
|
const render = ({ App, context }) => {
|
|
@@ -52,10 +51,6 @@ const render = ({ App, context }) => {
|
|
|
52
51
|
const { tracker } = ssrContext;
|
|
53
52
|
const pipe = (0, import_renderToPipe.default)(rootElement, context, {
|
|
54
53
|
onShellReady() {
|
|
55
|
-
const cacheConfig = import_prerender.PreRender.config();
|
|
56
|
-
if (cacheConfig) {
|
|
57
|
-
ssrContext.cacheConfig = cacheConfig;
|
|
58
|
-
}
|
|
59
54
|
const cost = end();
|
|
60
55
|
tracker.trackTiming(import_tracker.SSRTimings.RENDER_SHELL, cost);
|
|
61
56
|
},
|
|
@@ -31,16 +31,16 @@ function buildHtml(template, callbacks) {
|
|
|
31
31
|
return callbacks.reduce((tmp, cb) => cb(tmp), template);
|
|
32
32
|
}
|
|
33
33
|
function createReplaceHtml(html) {
|
|
34
|
-
return (template) => (0, import_utils.
|
|
34
|
+
return (template) => (0, import_utils.safeReplace)(template, import_constants.HTML_PLACEHOLDER, html);
|
|
35
35
|
}
|
|
36
36
|
function createReplaceSSRDataScript(data) {
|
|
37
|
-
return (template) => (0, import_utils.
|
|
37
|
+
return (template) => (0, import_utils.safeReplace)(template, import_constants.SSR_DATA_PLACEHOLDER, data);
|
|
38
38
|
}
|
|
39
39
|
function createReplaceChunkJs(js) {
|
|
40
|
-
return (template) => (0, import_utils.
|
|
40
|
+
return (template) => (0, import_utils.safeReplace)(template, import_constants.CHUNK_JS_PLACEHOLDER, js);
|
|
41
41
|
}
|
|
42
42
|
function createReplaceChunkCss(css) {
|
|
43
|
-
return (template) => (0, import_utils.
|
|
43
|
+
return (template) => (0, import_utils.safeReplace)(template, import_constants.CHUNK_CSS_PLACEHOLDER, css);
|
|
44
44
|
}
|
|
45
45
|
// Annotate the CommonJS export names for ESM import in node:
|
|
46
46
|
0 && (module.exports = {
|
|
@@ -32,7 +32,6 @@ __export(renderToString_exports, {
|
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(renderToString_exports);
|
|
34
34
|
var import_node = require("@modern-js/runtime-utils/node");
|
|
35
|
-
var import_prerender = require("../../react/prerender");
|
|
36
35
|
var import_entry = __toESM(require("./entry"));
|
|
37
36
|
const render = ({ App, context, config }) => {
|
|
38
37
|
const ssrContext = context.ssrContext;
|
|
@@ -44,10 +43,6 @@ const render = ({ App, context, config }) => {
|
|
|
44
43
|
});
|
|
45
44
|
entry.metrics.emitCounter("app.visit.count", 1);
|
|
46
45
|
const html = await entry.renderToHtml(context);
|
|
47
|
-
const cacheConfig = import_prerender.PreRender.config();
|
|
48
|
-
if (cacheConfig) {
|
|
49
|
-
context.ssrContext.cacheConfig = cacheConfig;
|
|
50
|
-
}
|
|
51
46
|
return html;
|
|
52
47
|
});
|
|
53
48
|
};
|
|
@@ -19,7 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var utils_exports = {};
|
|
20
20
|
__export(utils_exports, {
|
|
21
21
|
attributesToString: () => attributesToString,
|
|
22
|
-
|
|
22
|
+
safeReplace: () => safeReplace
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(utils_exports);
|
|
25
25
|
function attributesToString(attributes) {
|
|
@@ -27,12 +27,11 @@ function attributesToString(attributes) {
|
|
|
27
27
|
return value === void 0 ? str : `${str} ${key}="${value}"`;
|
|
28
28
|
}, "");
|
|
29
29
|
}
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
return s1 + replaceValue + s2;
|
|
30
|
+
function safeReplace(source, searchValue, replaceValue) {
|
|
31
|
+
return source.replace(searchValue, () => replaceValue);
|
|
33
32
|
}
|
|
34
33
|
// Annotate the CommonJS export names for ESM import in node:
|
|
35
34
|
0 && (module.exports = {
|
|
36
35
|
attributesToString,
|
|
37
|
-
|
|
36
|
+
safeReplace
|
|
38
37
|
});
|
package/dist/esm/cli/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
-
import path from "path";
|
|
4
3
|
import { isReact18, cleanRequireCache } from "@modern-js/utils";
|
|
5
4
|
import { statePlugin } from "../state/cli";
|
|
6
5
|
import { ssrPlugin } from "../ssr/cli";
|
|
@@ -26,8 +25,8 @@ var runtimePlugin = function() {
|
|
|
26
25
|
setup: function(api) {
|
|
27
26
|
return {
|
|
28
27
|
config: function config() {
|
|
29
|
-
var
|
|
30
|
-
process.env.IS_REACT18 = isReact18(
|
|
28
|
+
var appDir = api.useAppContext().appDirectory;
|
|
29
|
+
process.env.IS_REACT18 = isReact18(appDir).toString();
|
|
31
30
|
return {
|
|
32
31
|
runtime: {},
|
|
33
32
|
runtimeByEntries: {},
|
|
@@ -76,6 +76,7 @@ function factory(Component) {
|
|
|
76
76
|
if (!validate) {
|
|
77
77
|
throw new Error("invalid props, check usage");
|
|
78
78
|
}
|
|
79
|
+
console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
|
|
79
80
|
return createElement(Component, _object_spread({}, newProps));
|
|
80
81
|
}
|
|
81
82
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EOL } from "os";
|
|
2
|
-
import {
|
|
2
|
+
import { safeReplace } from "./utils";
|
|
3
3
|
var RE_HTML_ATTR = /<html[^>]*>/;
|
|
4
4
|
var RE_BODY_ATTR = /<body[^>]*>/;
|
|
5
5
|
var RE_LAST_IN_HEAD = /<\/head>/;
|
|
@@ -9,11 +9,11 @@ function helmet(content, helmetData) {
|
|
|
9
9
|
var result = content;
|
|
10
10
|
var bodyAttributes = helmetData.bodyAttributes.toString();
|
|
11
11
|
if (bodyAttributes) {
|
|
12
|
-
result =
|
|
12
|
+
result = safeReplace(result, RE_BODY_ATTR, "<body ".concat(bodyAttributes, ">"));
|
|
13
13
|
}
|
|
14
14
|
var htmlAttributes = helmetData.htmlAttributes.toString();
|
|
15
15
|
if (htmlAttributes) {
|
|
16
|
-
result =
|
|
16
|
+
result = safeReplace(result, RE_HTML_ATTR, "<html ".concat(htmlAttributes, ">"));
|
|
17
17
|
}
|
|
18
18
|
var base = helmetData.base.toString();
|
|
19
19
|
var link = helmetData.link.toString();
|
|
@@ -25,7 +25,7 @@ function helmet(content, helmetData) {
|
|
|
25
25
|
var existTitleTag = RE_TITLE.test(content);
|
|
26
26
|
var shouldReplaceTitle = existTitleTag && TEST_TITLE_CONTENT.test(title.trim());
|
|
27
27
|
if (shouldReplaceTitle) {
|
|
28
|
-
result = result
|
|
28
|
+
result = safeReplace(result, RE_TITLE, title);
|
|
29
29
|
}
|
|
30
30
|
var helmetStr = [
|
|
31
31
|
base,
|
|
@@ -38,7 +38,7 @@ function helmet(content, helmetData) {
|
|
|
38
38
|
].reduce(function(pre, cur) {
|
|
39
39
|
return pre + (cur.length > 0 ? " ".concat(cur).concat(EOL) : "");
|
|
40
40
|
}, "");
|
|
41
|
-
return
|
|
41
|
+
return safeReplace(result, RE_LAST_IN_HEAD, "".concat(helmetStr, "</head>"));
|
|
42
42
|
}
|
|
43
43
|
export {
|
|
44
44
|
helmet as default
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
3
|
import { isReact18 } from "../utils";
|
|
4
|
-
import {
|
|
4
|
+
import { CHUNK_CSS_PLACEHOLDER } from "./constants";
|
|
5
5
|
function serverRender(options) {
|
|
6
6
|
return _serverRender.apply(this, arguments);
|
|
7
7
|
}
|
|
@@ -13,7 +13,7 @@ function _serverRender() {
|
|
|
13
13
|
case 0:
|
|
14
14
|
if ((_options_context_ssrContext = options.context.ssrContext) === null || _options_context_ssrContext === void 0 ? void 0 : _options_context_ssrContext.template) {
|
|
15
15
|
;
|
|
16
|
-
options.context.ssrContext.template = (_options_context_ssrContext1 = options.context.ssrContext) === null || _options_context_ssrContext1 === void 0 ? void 0 : _options_context_ssrContext1.template.replace("</head>", "".concat(
|
|
16
|
+
options.context.ssrContext.template = (_options_context_ssrContext1 = options.context.ssrContext) === null || _options_context_ssrContext1 === void 0 ? void 0 : _options_context_ssrContext1.template.replace("</head>", "".concat(CHUNK_CSS_PLACEHOLDER, "</head>"));
|
|
17
17
|
}
|
|
18
18
|
if (!(isReact18() && options.config.mode === "stream"))
|
|
19
19
|
return [
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
2
|
import { serializeJson } from "@modern-js/runtime-utils/node";
|
|
3
|
-
import { attributesToString,
|
|
3
|
+
import { attributesToString, safeReplace } from "../utils";
|
|
4
4
|
import { SSR_DATA_PLACEHOLDER } from "../constants";
|
|
5
5
|
import { buildTemplate } from "./buildTemplate.share";
|
|
6
6
|
function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
@@ -10,7 +10,7 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
|
10
10
|
return buildTemplate(afterAppTemplate, callbacks);
|
|
11
11
|
function injectSSRDataScript(template) {
|
|
12
12
|
var ssrDataScript = buildSSRDataScript();
|
|
13
|
-
return
|
|
13
|
+
return safeReplace(template, SSR_DATA_PLACEHOLDER, ssrDataScript);
|
|
14
14
|
function buildSSRDataScript() {
|
|
15
15
|
var _options_context = options.context, ssrContext = _options_context.ssrContext, initialData = _options_context.initialData, __i18nData__ = _options_context.__i18nData__, renderLevel = options.renderLevel;
|
|
16
16
|
var request = ssrContext.request, enableUnsafeCtx = ssrContext.enableUnsafeCtx, nonce = ssrContext.nonce, tracker = ssrContext.tracker;
|
|
@@ -4,7 +4,7 @@ import ReactHelmet from "react-helmet";
|
|
|
4
4
|
import { matchRoutes } from "@modern-js/runtime-utils/router";
|
|
5
5
|
import helmetReplace from "../helmet";
|
|
6
6
|
import { CHUNK_CSS_PLACEHOLDER } from "../constants";
|
|
7
|
-
import {
|
|
7
|
+
import { safeReplace } from "../utils";
|
|
8
8
|
import { HEAD_REG_EXP, buildTemplate } from "./buildTemplate.share";
|
|
9
9
|
function getHeadTemplate(beforeEntryTemplate, context) {
|
|
10
10
|
var callbacks = [
|
|
@@ -21,7 +21,7 @@ function getHeadTemplate(beforeEntryTemplate, context) {
|
|
|
21
21
|
}
|
|
22
22
|
return buildTemplate(headTemplate, callbacks);
|
|
23
23
|
function injectCss(headTemplate2) {
|
|
24
|
-
return
|
|
24
|
+
return safeReplace(headTemplate2, CHUNK_CSS_PLACEHOLDER, getCssChunks());
|
|
25
25
|
function getCssChunks() {
|
|
26
26
|
var routeManifest = context.routeManifest, routerContext = context.routerContext, routes = context.routes;
|
|
27
27
|
if (!routeManifest || !routerContext || !routes) {
|
|
@@ -3,7 +3,6 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
|
3
3
|
import { createElement } from "react";
|
|
4
4
|
import { run } from "@modern-js/runtime-utils/node";
|
|
5
5
|
import { time } from "@modern-js/runtime-utils/time";
|
|
6
|
-
import { PreRender } from "../../react/prerender";
|
|
7
6
|
import { SSRErrors, SSRTimings } from "../tracker";
|
|
8
7
|
import renderToPipe from "./renderToPipe";
|
|
9
8
|
var render = function(param) {
|
|
@@ -24,10 +23,6 @@ var render = function(param) {
|
|
|
24
23
|
tracker = ssrContext.tracker;
|
|
25
24
|
pipe = renderToPipe(rootElement, context, {
|
|
26
25
|
onShellReady: function onShellReady() {
|
|
27
|
-
var cacheConfig = PreRender.config();
|
|
28
|
-
if (cacheConfig) {
|
|
29
|
-
ssrContext.cacheConfig = cacheConfig;
|
|
30
|
-
}
|
|
31
26
|
var cost = end();
|
|
32
27
|
tracker.trackTiming(SSRTimings.RENDER_SHELL, cost);
|
|
33
28
|
},
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { safeReplace } from "../utils";
|
|
2
2
|
import { HTML_PLACEHOLDER, SSR_DATA_PLACEHOLDER, CHUNK_JS_PLACEHOLDER, CHUNK_CSS_PLACEHOLDER } from "../constants";
|
|
3
3
|
function buildHtml(template, callbacks) {
|
|
4
4
|
return callbacks.reduce(function(tmp, cb) {
|
|
@@ -7,22 +7,22 @@ function buildHtml(template, callbacks) {
|
|
|
7
7
|
}
|
|
8
8
|
function createReplaceHtml(html) {
|
|
9
9
|
return function(template) {
|
|
10
|
-
return
|
|
10
|
+
return safeReplace(template, HTML_PLACEHOLDER, html);
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
function createReplaceSSRDataScript(data) {
|
|
14
14
|
return function(template) {
|
|
15
|
-
return
|
|
15
|
+
return safeReplace(template, SSR_DATA_PLACEHOLDER, data);
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
function createReplaceChunkJs(js) {
|
|
19
19
|
return function(template) {
|
|
20
|
-
return
|
|
20
|
+
return safeReplace(template, CHUNK_JS_PLACEHOLDER, js);
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
function createReplaceChunkCss(css) {
|
|
24
24
|
return function(template) {
|
|
25
|
-
return
|
|
25
|
+
return safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
export {
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
3
|
import { run } from "@modern-js/runtime-utils/node";
|
|
4
|
-
import { PreRender } from "../../react/prerender";
|
|
5
4
|
import SSREntry from "./entry";
|
|
6
5
|
var render = function(param) {
|
|
7
6
|
var App = param.App, context = param.context, config = param.config;
|
|
8
7
|
var ssrContext = context.ssrContext;
|
|
9
8
|
return run(ssrContext.request.headers, /* @__PURE__ */ _async_to_generator(function() {
|
|
10
|
-
var entry, html
|
|
9
|
+
var entry, html;
|
|
11
10
|
return _ts_generator(this, function(_state) {
|
|
12
11
|
switch (_state.label) {
|
|
13
12
|
case 0:
|
|
@@ -23,10 +22,6 @@ var render = function(param) {
|
|
|
23
22
|
];
|
|
24
23
|
case 1:
|
|
25
24
|
html = _state.sent();
|
|
26
|
-
cacheConfig = PreRender.config();
|
|
27
|
-
if (cacheConfig) {
|
|
28
|
-
context.ssrContext.cacheConfig = cacheConfig;
|
|
29
|
-
}
|
|
30
25
|
return [
|
|
31
26
|
2,
|
|
32
27
|
html
|
|
@@ -5,11 +5,12 @@ function attributesToString(attributes) {
|
|
|
5
5
|
return value === void 0 ? str : "".concat(str, " ").concat(key, '="').concat(value, '"');
|
|
6
6
|
}, "");
|
|
7
7
|
}
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
function safeReplace(source, searchValue, replaceValue) {
|
|
9
|
+
return source.replace(searchValue, function() {
|
|
10
|
+
return replaceValue;
|
|
11
|
+
});
|
|
11
12
|
}
|
|
12
13
|
export {
|
|
13
14
|
attributesToString,
|
|
14
|
-
|
|
15
|
+
safeReplace
|
|
15
16
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import path from "path";
|
|
2
1
|
import { isReact18, cleanRequireCache } from "@modern-js/utils";
|
|
3
2
|
import { statePlugin } from "../state/cli";
|
|
4
3
|
import { ssrPlugin } from "../ssr/cli";
|
|
@@ -23,8 +22,8 @@ const runtimePlugin = () => ({
|
|
|
23
22
|
setup: (api) => {
|
|
24
23
|
return {
|
|
25
24
|
config() {
|
|
26
|
-
const
|
|
27
|
-
process.env.IS_REACT18 = isReact18(
|
|
25
|
+
const appDir = api.useAppContext().appDirectory;
|
|
26
|
+
process.env.IS_REACT18 = isReact18(appDir).toString();
|
|
28
27
|
return {
|
|
29
28
|
runtime: {},
|
|
30
29
|
runtimeByEntries: {},
|
|
@@ -68,6 +68,7 @@ function factory(Component) {
|
|
|
68
68
|
if (!validate) {
|
|
69
69
|
throw new Error("invalid props, check usage");
|
|
70
70
|
}
|
|
71
|
+
console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
|
|
71
72
|
return createElement(Component, {
|
|
72
73
|
...newProps
|
|
73
74
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EOL } from "os";
|
|
2
|
-
import {
|
|
2
|
+
import { safeReplace } from "./utils";
|
|
3
3
|
const RE_HTML_ATTR = /<html[^>]*>/;
|
|
4
4
|
const RE_BODY_ATTR = /<body[^>]*>/;
|
|
5
5
|
const RE_LAST_IN_HEAD = /<\/head>/;
|
|
@@ -9,11 +9,11 @@ function helmet(content, helmetData) {
|
|
|
9
9
|
let result = content;
|
|
10
10
|
const bodyAttributes = helmetData.bodyAttributes.toString();
|
|
11
11
|
if (bodyAttributes) {
|
|
12
|
-
result =
|
|
12
|
+
result = safeReplace(result, RE_BODY_ATTR, `<body ${bodyAttributes}>`);
|
|
13
13
|
}
|
|
14
14
|
const htmlAttributes = helmetData.htmlAttributes.toString();
|
|
15
15
|
if (htmlAttributes) {
|
|
16
|
-
result =
|
|
16
|
+
result = safeReplace(result, RE_HTML_ATTR, `<html ${htmlAttributes}>`);
|
|
17
17
|
}
|
|
18
18
|
const base = helmetData.base.toString();
|
|
19
19
|
const link = helmetData.link.toString();
|
|
@@ -25,7 +25,7 @@ function helmet(content, helmetData) {
|
|
|
25
25
|
const existTitleTag = RE_TITLE.test(content);
|
|
26
26
|
const shouldReplaceTitle = existTitleTag && TEST_TITLE_CONTENT.test(title.trim());
|
|
27
27
|
if (shouldReplaceTitle) {
|
|
28
|
-
result = result
|
|
28
|
+
result = safeReplace(result, RE_TITLE, title);
|
|
29
29
|
}
|
|
30
30
|
const helmetStr = [
|
|
31
31
|
base,
|
|
@@ -38,7 +38,7 @@ function helmet(content, helmetData) {
|
|
|
38
38
|
].reduce((pre, cur) => {
|
|
39
39
|
return pre + (cur.length > 0 ? ` ${cur}${EOL}` : "");
|
|
40
40
|
}, "");
|
|
41
|
-
return
|
|
41
|
+
return safeReplace(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
|
|
42
42
|
}
|
|
43
43
|
export {
|
|
44
44
|
helmet as default
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isReact18 } from "../utils";
|
|
2
|
-
import {
|
|
2
|
+
import { CHUNK_CSS_PLACEHOLDER } from "./constants";
|
|
3
3
|
async function serverRender(options) {
|
|
4
4
|
var _options_context_ssrContext;
|
|
5
5
|
if ((_options_context_ssrContext = options.context.ssrContext) === null || _options_context_ssrContext === void 0 ? void 0 : _options_context_ssrContext.template) {
|
|
6
6
|
var _options_context_ssrContext1;
|
|
7
|
-
options.context.ssrContext.template = (_options_context_ssrContext1 = options.context.ssrContext) === null || _options_context_ssrContext1 === void 0 ? void 0 : _options_context_ssrContext1.template.replace("</head>", `${
|
|
7
|
+
options.context.ssrContext.template = (_options_context_ssrContext1 = options.context.ssrContext) === null || _options_context_ssrContext1 === void 0 ? void 0 : _options_context_ssrContext1.template.replace("</head>", `${CHUNK_CSS_PLACEHOLDER}</head>`);
|
|
8
8
|
}
|
|
9
9
|
if (isReact18() && options.config.mode === "stream") {
|
|
10
10
|
const pipe = await require("./renderToStream").render(options);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { serializeJson } from "@modern-js/runtime-utils/node";
|
|
2
|
-
import { attributesToString,
|
|
2
|
+
import { attributesToString, safeReplace } from "../utils";
|
|
3
3
|
import { SSR_DATA_PLACEHOLDER } from "../constants";
|
|
4
4
|
import { buildTemplate } from "./buildTemplate.share";
|
|
5
5
|
function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
@@ -9,7 +9,7 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
|
|
|
9
9
|
return buildTemplate(afterAppTemplate, callbacks);
|
|
10
10
|
function injectSSRDataScript(template) {
|
|
11
11
|
const ssrDataScript = buildSSRDataScript();
|
|
12
|
-
return
|
|
12
|
+
return safeReplace(template, SSR_DATA_PLACEHOLDER, ssrDataScript);
|
|
13
13
|
function buildSSRDataScript() {
|
|
14
14
|
const { context: { ssrContext, initialData, __i18nData__ }, renderLevel } = options;
|
|
15
15
|
const { request, enableUnsafeCtx, nonce, tracker } = ssrContext;
|
|
@@ -2,7 +2,7 @@ import ReactHelmet from "react-helmet";
|
|
|
2
2
|
import { matchRoutes } from "@modern-js/runtime-utils/router";
|
|
3
3
|
import helmetReplace from "../helmet";
|
|
4
4
|
import { CHUNK_CSS_PLACEHOLDER } from "../constants";
|
|
5
|
-
import {
|
|
5
|
+
import { safeReplace } from "../utils";
|
|
6
6
|
import { HEAD_REG_EXP, buildTemplate } from "./buildTemplate.share";
|
|
7
7
|
function getHeadTemplate(beforeEntryTemplate, context) {
|
|
8
8
|
const callbacks = [
|
|
@@ -19,7 +19,7 @@ function getHeadTemplate(beforeEntryTemplate, context) {
|
|
|
19
19
|
}
|
|
20
20
|
return buildTemplate(headTemplate, callbacks);
|
|
21
21
|
function injectCss(headTemplate2) {
|
|
22
|
-
return
|
|
22
|
+
return safeReplace(headTemplate2, CHUNK_CSS_PLACEHOLDER, getCssChunks());
|
|
23
23
|
function getCssChunks() {
|
|
24
24
|
const { routeManifest, routerContext, routes } = context;
|
|
25
25
|
if (!routeManifest || !routerContext || !routes) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { createElement } from "react";
|
|
2
2
|
import { run } from "@modern-js/runtime-utils/node";
|
|
3
3
|
import { time } from "@modern-js/runtime-utils/time";
|
|
4
|
-
import { PreRender } from "../../react/prerender";
|
|
5
4
|
import { SSRErrors, SSRTimings } from "../tracker";
|
|
6
5
|
import renderToPipe from "./renderToPipe";
|
|
7
6
|
const render = ({ App, context }) => {
|
|
@@ -19,10 +18,6 @@ const render = ({ App, context }) => {
|
|
|
19
18
|
const { tracker } = ssrContext;
|
|
20
19
|
const pipe = renderToPipe(rootElement, context, {
|
|
21
20
|
onShellReady() {
|
|
22
|
-
const cacheConfig = PreRender.config();
|
|
23
|
-
if (cacheConfig) {
|
|
24
|
-
ssrContext.cacheConfig = cacheConfig;
|
|
25
|
-
}
|
|
26
21
|
const cost = end();
|
|
27
22
|
tracker.trackTiming(SSRTimings.RENDER_SHELL, cost);
|
|
28
23
|
},
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { safeReplace } from "../utils";
|
|
2
2
|
import { HTML_PLACEHOLDER, SSR_DATA_PLACEHOLDER, CHUNK_JS_PLACEHOLDER, CHUNK_CSS_PLACEHOLDER } from "../constants";
|
|
3
3
|
function buildHtml(template, callbacks) {
|
|
4
4
|
return callbacks.reduce((tmp, cb) => cb(tmp), template);
|
|
5
5
|
}
|
|
6
6
|
function createReplaceHtml(html) {
|
|
7
|
-
return (template) =>
|
|
7
|
+
return (template) => safeReplace(template, HTML_PLACEHOLDER, html);
|
|
8
8
|
}
|
|
9
9
|
function createReplaceSSRDataScript(data) {
|
|
10
|
-
return (template) =>
|
|
10
|
+
return (template) => safeReplace(template, SSR_DATA_PLACEHOLDER, data);
|
|
11
11
|
}
|
|
12
12
|
function createReplaceChunkJs(js) {
|
|
13
|
-
return (template) =>
|
|
13
|
+
return (template) => safeReplace(template, CHUNK_JS_PLACEHOLDER, js);
|
|
14
14
|
}
|
|
15
15
|
function createReplaceChunkCss(css) {
|
|
16
|
-
return (template) =>
|
|
16
|
+
return (template) => safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
|
|
17
17
|
}
|
|
18
18
|
export {
|
|
19
19
|
buildHtml,
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { run } from "@modern-js/runtime-utils/node";
|
|
2
|
-
import { PreRender } from "../../react/prerender";
|
|
3
2
|
import SSREntry from "./entry";
|
|
4
3
|
const render = ({ App, context, config }) => {
|
|
5
4
|
const ssrContext = context.ssrContext;
|
|
@@ -11,10 +10,6 @@ const render = ({ App, context, config }) => {
|
|
|
11
10
|
});
|
|
12
11
|
entry.metrics.emitCounter("app.visit.count", 1);
|
|
13
12
|
const html = await entry.renderToHtml(context);
|
|
14
|
-
const cacheConfig = PreRender.config();
|
|
15
|
-
if (cacheConfig) {
|
|
16
|
-
context.ssrContext.cacheConfig = cacheConfig;
|
|
17
|
-
}
|
|
18
13
|
return html;
|
|
19
14
|
});
|
|
20
15
|
};
|
|
@@ -3,11 +3,10 @@ function attributesToString(attributes) {
|
|
|
3
3
|
return value === void 0 ? str : `${str} ${key}="${value}"`;
|
|
4
4
|
}, "");
|
|
5
5
|
}
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
return s1 + replaceValue + s2;
|
|
6
|
+
function safeReplace(source, searchValue, replaceValue) {
|
|
7
|
+
return source.replace(searchValue, () => replaceValue);
|
|
9
8
|
}
|
|
10
9
|
export {
|
|
11
10
|
attributesToString,
|
|
12
|
-
|
|
11
|
+
safeReplace
|
|
13
12
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AfterMatchContext, AfterRenderContext, MiddlewareContext, NextFunction } from '@modern-js/types';
|
|
2
|
+
export type { Container, CacheControl, CacheOptionProvider, CacheOption, } from '@modern-js/types';
|
|
2
3
|
export declare const hook: (attacher: ({ addMiddleware, afterMatch, afterRender, }: {
|
|
3
4
|
addMiddleware: (mid: Middleware) => void;
|
|
4
5
|
afterRender: (hook: AfterRenderHook) => void;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export declare function attributesToString(attributes: Record<string, any>): string;
|
|
2
2
|
/**
|
|
3
|
-
* It is unsafe unsafeReplace, only support serachValue exsit one time.
|
|
4
3
|
* @param source
|
|
5
4
|
* @param searchValue
|
|
6
5
|
* @param replaceValue
|
|
7
6
|
* @returns
|
|
8
7
|
*/
|
|
9
|
-
export declare function
|
|
8
|
+
export declare function safeReplace(source: string, searchValue: string | RegExp, replaceValue: string): string;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-nightly-
|
|
18
|
+
"version": "0.0.0-nightly-20231219170633",
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">=14.17.6"
|
|
21
21
|
},
|
|
@@ -171,10 +171,10 @@
|
|
|
171
171
|
"react-side-effect": "^2.1.1",
|
|
172
172
|
"styled-components": "^5.3.1",
|
|
173
173
|
"@swc/helpers": "0.5.3",
|
|
174
|
-
"@modern-js/
|
|
175
|
-
"@modern-js/
|
|
176
|
-
"@modern-js/
|
|
177
|
-
"@modern-js/utils": "0.0.0-nightly-
|
|
174
|
+
"@modern-js/plugin": "0.0.0-nightly-20231219170633",
|
|
175
|
+
"@modern-js/types": "0.0.0-nightly-20231219170633",
|
|
176
|
+
"@modern-js/utils": "0.0.0-nightly-20231219170633",
|
|
177
|
+
"@modern-js/runtime-utils": "0.0.0-nightly-20231219170633"
|
|
178
178
|
},
|
|
179
179
|
"peerDependencies": {
|
|
180
180
|
"react": ">=17",
|
|
@@ -195,11 +195,11 @@
|
|
|
195
195
|
"ts-jest": "^29.1.0",
|
|
196
196
|
"typescript": "^5",
|
|
197
197
|
"webpack": "^5.89.0",
|
|
198
|
-
"@modern-js/
|
|
199
|
-
"@modern-js/
|
|
200
|
-
"@scripts/
|
|
201
|
-
"@
|
|
202
|
-
"@
|
|
198
|
+
"@modern-js/core": "0.0.0-nightly-20231219170633",
|
|
199
|
+
"@modern-js/app-tools": "0.0.0-nightly-20231219170633",
|
|
200
|
+
"@scripts/build": "0.0.0-nightly-20231219170633",
|
|
201
|
+
"@modern-js/server-core": "0.0.0-nightly-20231219170633",
|
|
202
|
+
"@scripts/jest-config": "0.0.0-nightly-20231219170633"
|
|
203
203
|
},
|
|
204
204
|
"sideEffects": false,
|
|
205
205
|
"publishConfig": {
|