@modern-js/runtime 2.22.0 → 2.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @modern-js/runtime
2
2
 
3
+ ## 2.22.1
4
+
5
+ ### Patch Changes
6
+
7
+ - e2848a2: fix: support nonce in streaming SSR
8
+ fix: 在 streaming SSR 中支持 nonce
9
+ - 9b69987: feat: support Style component to use style with some <> special chartset
10
+ feat: 支持 Style 组件,用于渲染含 <> 等特殊字符
11
+ - Updated dependencies [25b490a]
12
+ - Updated dependencies [e2848a2]
13
+ - Updated dependencies [d4045ed]
14
+ - @modern-js/types@2.22.1
15
+ - @modern-js/utils@2.22.1
16
+ - @modern-js/plugin@2.22.1
17
+
3
18
  ## 2.22.0
4
19
 
5
20
  ### Minor Changes
@@ -37,7 +37,7 @@ function findTargetElement(tag, children) {
37
37
  return findTargetElement(tag, nextChildren);
38
38
  }
39
39
  function Html(props) {
40
- const { children } = props;
40
+ const { children, ...rest } = props;
41
41
  const hasSetHead = Boolean(findTargetChild("Head", children));
42
42
  const hasSetScripts = Boolean(findTargetElement("Scripts", children));
43
43
  const hasSetLinks = Boolean(findTargetElement("Links", children));
@@ -70,6 +70,7 @@ function Html(props) {
70
70
  });
71
71
  }
72
72
  return /* @__PURE__ */ (0, _jsxruntime.jsx)("html", {
73
+ ...rest,
73
74
  children: /* @__PURE__ */ (0, _jsxruntime.jsxs)(_DocumentStructureContext.DocumentStructureContext.Provider, {
74
75
  value: {
75
76
  hasSetHead,
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "Style", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return Style;
9
+ }
10
+ });
11
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
12
+ const _jsxruntime = require("react/jsx-runtime");
13
+ const _react = /* @__PURE__ */ _interop_require_default._(require("react"));
14
+ const _constants = require("./constants");
15
+ function Style(props) {
16
+ const { content, children } = props;
17
+ const contentStr = children || content;
18
+ const contentIIFE = encodeURIComponent(`(${contentStr})()`);
19
+ return /* @__PURE__ */ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, {
20
+ children: [
21
+ `${_constants.DOCUMENT_STYLE_PLACEHOLDER_START}`,
22
+ `${contentIIFE}`,
23
+ `${_constants.DOCUMENT_STYLE_PLACEHOLDER_END}`
24
+ ]
25
+ });
26
+ }
@@ -146,6 +146,9 @@ const _default = () => {
146
146
  const nonceAttr = nonce ? `nonce=${nonce}` : "";
147
147
  html = html.replace(new RegExp(`${_constants.DOCUMENT_SCRIPT_PLACEHOLDER_START}(.*?)${_constants.DOCUMENT_SCRIPT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `<script ${nonceAttr}>${decodeURIComponent($1)}</script>`);
148
148
  }
149
+ if (html.includes(_constants.DOCUMENT_STYLE_PLACEHOLDER_START) && html.includes(_constants.DOCUMENT_STYLE_PLACEHOLDER_END)) {
150
+ html = html.replace(new RegExp(`${_constants.DOCUMENT_STYLE_PLACEHOLDER_START}(.*?)${_constants.DOCUMENT_STYLE_PLACEHOLDER_END}`, "g"), (_styleStr, $1) => `<style>${decodeURIComponent($1)}</style>`);
151
+ }
149
152
  if (html.includes(_constants.DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(_constants.DOCUMENT_COMMENT_PLACEHOLDER_END)) {
150
153
  html = html.replace(new RegExp(`${_constants.DOCUMENT_COMMENT_PLACEHOLDER_START}(.*?)${_constants.DOCUMENT_COMMENT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `${decodeURIComponent($1)}`);
151
154
  }
@@ -52,6 +52,12 @@ _export(exports, {
52
52
  DOCUMENT_SCRIPT_PLACEHOLDER_END: function() {
53
53
  return DOCUMENT_SCRIPT_PLACEHOLDER_END;
54
54
  },
55
+ DOCUMENT_STYLE_PLACEHOLDER_START: function() {
56
+ return DOCUMENT_STYLE_PLACEHOLDER_START;
57
+ },
58
+ DOCUMENT_STYLE_PLACEHOLDER_END: function() {
59
+ return DOCUMENT_STYLE_PLACEHOLDER_END;
60
+ },
55
61
  DOCUMENT_COMMENT_PLACEHOLDER_START: function() {
56
62
  return DOCUMENT_COMMENT_PLACEHOLDER_START;
57
63
  },
@@ -81,6 +87,8 @@ const DOCUMENT_LINKS_PLACEHOLDER = encodeURIComponent("<!-- chunk links placehol
81
87
  const DOCUMENT_NO_SCRIPTE_PLACEHOLDER = encodeURIComponent("<!-- no-script -->");
82
88
  const DOCUMENT_SCRIPT_PLACEHOLDER_START = encodeURIComponent("<!-- script-start -->");
83
89
  const DOCUMENT_SCRIPT_PLACEHOLDER_END = encodeURIComponent("<!-- script-end -->");
90
+ const DOCUMENT_STYLE_PLACEHOLDER_START = encodeURIComponent("<!-- style-start -->");
91
+ const DOCUMENT_STYLE_PLACEHOLDER_END = encodeURIComponent("<!-- style-end -->");
84
92
  const DOCUMENT_COMMENT_PLACEHOLDER_START = encodeURIComponent("<!-- comment-start -->");
85
93
  const DOCUMENT_COMMENT_PLACEHOLDER_END = encodeURIComponent("<!-- comment-end -->");
86
94
  const PLACEHOLDER_REPLACER_MAP = {
@@ -11,6 +11,7 @@ _export_star._(require("./Body"), exports);
11
11
  _export_star._(require("./Root"), exports);
12
12
  _export_star._(require("./Links"), exports);
13
13
  _export_star._(require("./Comment"), exports);
14
+ _export_star._(require("./Style"), exports);
14
15
  _export_star._(require("./Scripts"), exports);
15
16
  _export_star._(require("./Script"), exports);
16
17
  _export_star._(require("./constants"), exports);
@@ -17,7 +17,7 @@ const _utils = require("./utils");
17
17
  const setupFnStr = `function s(r,e){_ROUTER_DATA.r=_ROUTER_DATA.r||{},_ROUTER_DATA.r[r]=_ROUTER_DATA.r[r]||{};return new Promise((function(A,R){_ROUTER_DATA.r[r][e]={resolve:A,reject:R}}))};`;
18
18
  const resolveFnStr = `function r(e,r,o,A){A?_ROUTER_DATA.r[e][r].reject(A):_ROUTER_DATA.r[e][r].resolve(o)};`;
19
19
  const preResolvedFnStr = `function p(e,r){return void 0!==r?Promise.reject(new Error(r.message)):Promise.resolve(e)};`;
20
- const DeferredDataScripts = () => {
20
+ const DeferredDataScripts = (props) => {
21
21
  const context = (0, _react.useContext)(_router.UNSAFE_DataRouterContext);
22
22
  const { staticContext } = context || {};
23
23
  const hydratedRef = (0, _react.useRef)(false);
@@ -46,6 +46,7 @@ const DeferredDataScripts = () => {
46
46
  const deferredKeyPromiseStr = deferredKeys.map((key) => {
47
47
  if (pendingKeys.has(key)) {
48
48
  deferredDataScripts.push(/* @__PURE__ */ (0, _jsxruntime.jsx)(DeferredDataScript, {
49
+ nonce: props === null || props === void 0 ? void 0 : props.nonce,
49
50
  data: deferredData.data[key],
50
51
  dataKey: key,
51
52
  routeId
@@ -81,6 +82,7 @@ const DeferredDataScripts = () => {
81
82
  children: [
82
83
  !hydratedRef.current && /* @__PURE__ */ (0, _jsxruntime.jsx)("script", {
83
84
  async: true,
85
+ nonce: props === null || props === void 0 ? void 0 : props.nonce,
84
86
  suppressHydrationWarning: true,
85
87
  dangerouslySetInnerHTML: {
86
88
  __html: deferredScripts[0]
@@ -91,16 +93,18 @@ const DeferredDataScripts = () => {
91
93
  ]
92
94
  });
93
95
  };
94
- const DeferredDataScript = ({ data, routeId, dataKey }) => {
96
+ const DeferredDataScript = ({ data, routeId, dataKey, nonce }) => {
95
97
  return /* @__PURE__ */ (0, _jsxruntime.jsx)(_react.Suspense, {
96
98
  children: typeof document === "undefined" && data && dataKey && routeId ? /* @__PURE__ */ (0, _jsxruntime.jsx)(_router.Await, {
97
99
  resolve: data,
98
100
  errorElement: /* @__PURE__ */ (0, _jsxruntime.jsx)(ErrorDeferredDataScript, {
99
101
  routeId,
100
- dataKey
102
+ dataKey,
103
+ nonce
101
104
  }),
102
105
  children: (data2) => /* @__PURE__ */ (0, _jsxruntime.jsx)("script", {
103
106
  async: true,
107
+ nonce,
104
108
  suppressHydrationWarning: true,
105
109
  dangerouslySetInnerHTML: {
106
110
  __html: `_ROUTER_DATA.r(${JSON.stringify(routeId)}, ${JSON.stringify(dataKey)}, ${(0, _runtimenode.serializeJson)(data2)});`
@@ -109,9 +113,10 @@ const DeferredDataScript = ({ data, routeId, dataKey }) => {
109
113
  }) : null
110
114
  });
111
115
  };
112
- const ErrorDeferredDataScript = ({ routeId, dataKey }) => {
116
+ const ErrorDeferredDataScript = ({ routeId, dataKey, nonce }) => {
113
117
  const error = (0, _router.useAsyncError)();
114
118
  return /* @__PURE__ */ (0, _jsxruntime.jsx)("script", {
119
+ nonce,
115
120
  suppressHydrationWarning: true,
116
121
  dangerouslySetInnerHTML: {
117
122
  __html: `_ROUTER_DATA.r(${JSON.stringify(routeId)}, ${JSON.stringify(dataKey)}, ${void 0}, ${(0, _runtimenode.serializeJson)({
@@ -73,12 +73,15 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
73
73
  context
74
74
  });
75
75
  }
76
- const { request, mode: ssrMode } = context.ssrContext;
76
+ const { request, mode: ssrMode, nonce } = context.ssrContext;
77
77
  const baseUrl = request.baseUrl;
78
78
  const _basename = baseUrl === "/" ? (0, _utils.urlJoin)(baseUrl, basename) : baseUrl;
79
79
  const routes = createRoutes ? createRoutes() : (0, _router1.createRoutesFromElements)((0, _utils.renderRoutes)({
80
80
  routesConfig,
81
- ssrMode
81
+ ssrMode,
82
+ props: {
83
+ nonce
84
+ }
82
85
  }));
83
86
  const { query } = (0, _remixrouter.createStaticHandler)(routes, {
84
87
  basename: _basename
@@ -1,3 +1,6 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
1
4
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
5
  import { Body } from "./Body";
3
6
  import { DocumentStructureContext } from "./DocumentStructureContext";
@@ -44,7 +47,9 @@ function findTargetElement(tag, children) {
44
47
  return findTargetElement(tag, nextChildren);
45
48
  }
46
49
  export function Html(props) {
47
- var children = props.children;
50
+ var children = props.children, rest = _object_without_properties(props, [
51
+ "children"
52
+ ]);
48
53
  var hasSetHead = Boolean(findTargetChild("Head", children));
49
54
  var hasSetScripts = Boolean(findTargetElement("Scripts", children));
50
55
  var hasSetLinks = Boolean(findTargetElement("Links", children));
@@ -78,7 +83,7 @@ export function Html(props) {
78
83
  })
79
84
  });
80
85
  }
81
- return /* @__PURE__ */ _jsx("html", {
86
+ return /* @__PURE__ */ _jsx("html", _object_spread_props(_object_spread({}, rest), {
82
87
  children: /* @__PURE__ */ _jsxs(DocumentStructureContext.Provider, {
83
88
  value: {
84
89
  hasSetHead: hasSetHead,
@@ -94,5 +99,5 @@ export function Html(props) {
94
99
  children
95
100
  ]
96
101
  })
97
- });
102
+ }));
98
103
  }
@@ -0,0 +1,14 @@
1
+ import { jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { DOCUMENT_STYLE_PLACEHOLDER_START, DOCUMENT_STYLE_PLACEHOLDER_END } from "./constants";
3
+ export function Style(props) {
4
+ var content = props.content, children = props.children;
5
+ var contentStr = children || content;
6
+ var contentIIFE = encodeURIComponent("(".concat(contentStr, ")()"));
7
+ return /* @__PURE__ */ _jsxs(_Fragment, {
8
+ children: [
9
+ "".concat(DOCUMENT_STYLE_PLACEHOLDER_START),
10
+ "".concat(contentIIFE),
11
+ "".concat(DOCUMENT_STYLE_PLACEHOLDER_END)
12
+ ]
13
+ });
14
+ }
@@ -8,7 +8,7 @@ import ReactDomServer from "react-dom/server";
8
8
  import { build } from "esbuild";
9
9
  import { createDebugger, findExists, fs } from "@modern-js/utils";
10
10
  import { DocumentContext } from "../DocumentContext";
11
- import { DOCUMENT_SCRIPTS_PLACEHOLDER, DOCUMENT_LINKS_PLACEHOLDER, DOCUMENT_FILE_NAME, DOCUMENT_META_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP, DOC_EXT, DOCUMENT_SSR_PLACEHOLDER, DOCUMENT_CHUNKSMAP_PLACEHOLDER, DOCUMENT_SSRDATASCRIPT_PLACEHOLDER, DOCUMENT_SCRIPT_PLACEHOLDER_START, DOCUMENT_SCRIPT_PLACEHOLDER_END, HTML_SEPARATOR, DOCUMENT_COMMENT_PLACEHOLDER_START, DOCUMENT_COMMENT_PLACEHOLDER_END } from "../constants";
11
+ import { DOCUMENT_SCRIPTS_PLACEHOLDER, DOCUMENT_LINKS_PLACEHOLDER, DOCUMENT_FILE_NAME, DOCUMENT_META_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP, DOC_EXT, DOCUMENT_SSR_PLACEHOLDER, DOCUMENT_CHUNKSMAP_PLACEHOLDER, DOCUMENT_SSRDATASCRIPT_PLACEHOLDER, DOCUMENT_SCRIPT_PLACEHOLDER_START, DOCUMENT_SCRIPT_PLACEHOLDER_END, HTML_SEPARATOR, DOCUMENT_COMMENT_PLACEHOLDER_START, DOCUMENT_COMMENT_PLACEHOLDER_END, DOCUMENT_STYLE_PLACEHOLDER_START, DOCUMENT_STYLE_PLACEHOLDER_END } from "../constants";
12
12
  var debug = createDebugger("html_genarate");
13
13
  export var getDocumenByEntryName = function getDocumenByEntryName2(entrypoints, entryName, fallbackDir) {
14
14
  var _entrypoints_find;
@@ -180,6 +180,11 @@ export default function() {
180
180
  return "<script ".concat(nonceAttr, ">").concat(decodeURIComponent($1), "</script>");
181
181
  });
182
182
  }
183
+ if (html.includes(DOCUMENT_STYLE_PLACEHOLDER_START) && html.includes(DOCUMENT_STYLE_PLACEHOLDER_END)) {
184
+ html = html.replace(new RegExp("".concat(DOCUMENT_STYLE_PLACEHOLDER_START, "(.*?)").concat(DOCUMENT_STYLE_PLACEHOLDER_END), "g"), function(_styleStr, $1) {
185
+ return "<style>".concat(decodeURIComponent($1), "</style>");
186
+ });
187
+ }
183
188
  if (html.includes(DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(DOCUMENT_COMMENT_PLACEHOLDER_END)) {
184
189
  html = html.replace(new RegExp("".concat(DOCUMENT_COMMENT_PLACEHOLDER_START, "(.*?)").concat(DOCUMENT_COMMENT_PLACEHOLDER_END), "g"), function(_scriptStr, $1) {
185
190
  return "".concat(decodeURIComponent($1));
@@ -18,6 +18,8 @@ export var DOCUMENT_LINKS_PLACEHOLDER = encodeURIComponent("<!-- chunk links pla
18
18
  export var DOCUMENT_NO_SCRIPTE_PLACEHOLDER = encodeURIComponent("<!-- no-script -->");
19
19
  export var DOCUMENT_SCRIPT_PLACEHOLDER_START = encodeURIComponent("<!-- script-start -->");
20
20
  export var DOCUMENT_SCRIPT_PLACEHOLDER_END = encodeURIComponent("<!-- script-end -->");
21
+ export var DOCUMENT_STYLE_PLACEHOLDER_START = encodeURIComponent("<!-- style-start -->");
22
+ export var DOCUMENT_STYLE_PLACEHOLDER_END = encodeURIComponent("<!-- style-end -->");
21
23
  export var DOCUMENT_COMMENT_PLACEHOLDER_START = encodeURIComponent("<!-- comment-start -->");
22
24
  export var DOCUMENT_COMMENT_PLACEHOLDER_END = encodeURIComponent("<!-- comment-end -->");
23
25
  var _obj;
@@ -6,6 +6,7 @@ export * from "./Body";
6
6
  export * from "./Root";
7
7
  export * from "./Links";
8
8
  export * from "./Comment";
9
+ export * from "./Style";
9
10
  export * from "./Scripts";
10
11
  export * from "./Script";
11
12
  export * from "./constants";
@@ -8,7 +8,7 @@ import { serializeErrors } from "./utils";
8
8
  var setupFnStr = "function s(r,e){_ROUTER_DATA.r=_ROUTER_DATA.r||{},_ROUTER_DATA.r[r]=_ROUTER_DATA.r[r]||{};return new Promise((function(A,R){_ROUTER_DATA.r[r][e]={resolve:A,reject:R}}))};";
9
9
  var resolveFnStr = "function r(e,r,o,A){A?_ROUTER_DATA.r[e][r].reject(A):_ROUTER_DATA.r[e][r].resolve(o)};";
10
10
  var preResolvedFnStr = "function p(e,r){return void 0!==r?Promise.reject(new Error(r.message)):Promise.resolve(e)};";
11
- var DeferredDataScripts = function() {
11
+ var DeferredDataScripts = function(props) {
12
12
  var context = useContext(DataRouterContext);
13
13
  var staticContext = (context || {}).staticContext;
14
14
  var hydratedRef = useRef(false);
@@ -38,6 +38,7 @@ var DeferredDataScripts = function() {
38
38
  var deferredKeyPromiseStr = deferredKeys.map(function(key) {
39
39
  if (pendingKeys.has(key)) {
40
40
  deferredDataScripts.push(/* @__PURE__ */ _jsx(DeferredDataScript, {
41
+ nonce: props === null || props === void 0 ? void 0 : props.nonce,
41
42
  data: deferredData.data[key],
42
43
  dataKey: key,
43
44
  routeId: routeId
@@ -73,6 +74,7 @@ var DeferredDataScripts = function() {
73
74
  children: [
74
75
  !hydratedRef.current && /* @__PURE__ */ _jsx("script", {
75
76
  async: true,
77
+ nonce: props === null || props === void 0 ? void 0 : props.nonce,
76
78
  suppressHydrationWarning: true,
77
79
  dangerouslySetInnerHTML: {
78
80
  __html: deferredScripts[0]
@@ -84,17 +86,19 @@ var DeferredDataScripts = function() {
84
86
  });
85
87
  };
86
88
  var DeferredDataScript = function(param) {
87
- var data = param.data, routeId = param.routeId, dataKey = param.dataKey;
89
+ var data = param.data, routeId = param.routeId, dataKey = param.dataKey, nonce = param.nonce;
88
90
  return /* @__PURE__ */ _jsx(Suspense, {
89
91
  children: typeof document === "undefined" && data && dataKey && routeId ? /* @__PURE__ */ _jsx(Await, {
90
92
  resolve: data,
91
93
  errorElement: /* @__PURE__ */ _jsx(ErrorDeferredDataScript, {
92
94
  routeId: routeId,
93
- dataKey: dataKey
95
+ dataKey: dataKey,
96
+ nonce: nonce
94
97
  }),
95
98
  children: function(data2) {
96
99
  return /* @__PURE__ */ _jsx("script", {
97
100
  async: true,
101
+ nonce: nonce,
98
102
  suppressHydrationWarning: true,
99
103
  dangerouslySetInnerHTML: {
100
104
  __html: "_ROUTER_DATA.r(".concat(JSON.stringify(routeId), ", ").concat(JSON.stringify(dataKey), ", ").concat(serializeJson(data2), ");")
@@ -105,9 +109,10 @@ var DeferredDataScript = function(param) {
105
109
  });
106
110
  };
107
111
  var ErrorDeferredDataScript = function(param) {
108
- var routeId = param.routeId, dataKey = param.dataKey;
112
+ var routeId = param.routeId, dataKey = param.dataKey, nonce = param.nonce;
109
113
  var error = useAsyncError();
110
114
  return /* @__PURE__ */ _jsx("script", {
115
+ nonce: nonce,
111
116
  suppressHydrationWarning: true,
112
117
  dangerouslySetInnerHTML: {
113
118
  __html: "_ROUTER_DATA.r(".concat(JSON.stringify(routeId), ", ").concat(JSON.stringify(dataKey), ", ").concat(void 0, ", ").concat(serializeJson({
@@ -87,7 +87,7 @@ export var routerPlugin = function(param) {
87
87
  init: function init(param2, next) {
88
88
  var context = param2.context;
89
89
  return _async_to_generator(function() {
90
- var _context_ssrContext, request, ssrMode, baseUrl, _basename, routes, query, remixRequest, routerContext, router, runner;
90
+ var _context_ssrContext, request, ssrMode, nonce, baseUrl, _basename, routes, query, remixRequest, routerContext, router, runner;
91
91
  return _ts_generator(this, function(_state) {
92
92
  switch (_state.label) {
93
93
  case 0:
@@ -99,12 +99,15 @@ export var routerPlugin = function(param) {
99
99
  })
100
100
  ];
101
101
  }
102
- _context_ssrContext = context.ssrContext, request = _context_ssrContext.request, ssrMode = _context_ssrContext.mode;
102
+ _context_ssrContext = context.ssrContext, request = _context_ssrContext.request, ssrMode = _context_ssrContext.mode, nonce = _context_ssrContext.nonce;
103
103
  baseUrl = request.baseUrl;
104
104
  _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
105
105
  routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
106
106
  routesConfig: routesConfig,
107
- ssrMode: ssrMode
107
+ ssrMode: ssrMode,
108
+ props: {
109
+ nonce: nonce
110
+ }
108
111
  }));
109
112
  query = createStaticHandler(routes, {
110
113
  basename: _basename
@@ -25,7 +25,7 @@ function findTargetElement(tag, children) {
25
25
  return findTargetElement(tag, nextChildren);
26
26
  }
27
27
  export function Html(props) {
28
- const { children } = props;
28
+ const { children, ...rest } = props;
29
29
  const hasSetHead = Boolean(findTargetChild("Head", children));
30
30
  const hasSetScripts = Boolean(findTargetElement("Scripts", children));
31
31
  const hasSetLinks = Boolean(findTargetElement("Links", children));
@@ -58,6 +58,7 @@ export function Html(props) {
58
58
  });
59
59
  }
60
60
  return /* @__PURE__ */ _jsx("html", {
61
+ ...rest,
61
62
  children: /* @__PURE__ */ _jsxs(DocumentStructureContext.Provider, {
62
63
  value: {
63
64
  hasSetHead,
@@ -0,0 +1,14 @@
1
+ import { jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { DOCUMENT_STYLE_PLACEHOLDER_START, DOCUMENT_STYLE_PLACEHOLDER_END } from "./constants";
3
+ export function Style(props) {
4
+ const { content, children } = props;
5
+ const contentStr = children || content;
6
+ const contentIIFE = encodeURIComponent(`(${contentStr})()`);
7
+ return /* @__PURE__ */ _jsxs(_Fragment, {
8
+ children: [
9
+ `${DOCUMENT_STYLE_PLACEHOLDER_START}`,
10
+ `${contentIIFE}`,
11
+ `${DOCUMENT_STYLE_PLACEHOLDER_END}`
12
+ ]
13
+ });
14
+ }
@@ -4,7 +4,7 @@ import ReactDomServer from "react-dom/server";
4
4
  import { build } from "esbuild";
5
5
  import { createDebugger, findExists, fs } from "@modern-js/utils";
6
6
  import { DocumentContext } from "../DocumentContext";
7
- import { DOCUMENT_SCRIPTS_PLACEHOLDER, DOCUMENT_LINKS_PLACEHOLDER, DOCUMENT_FILE_NAME, DOCUMENT_META_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP, DOC_EXT, DOCUMENT_SSR_PLACEHOLDER, DOCUMENT_CHUNKSMAP_PLACEHOLDER, DOCUMENT_SSRDATASCRIPT_PLACEHOLDER, DOCUMENT_SCRIPT_PLACEHOLDER_START, DOCUMENT_SCRIPT_PLACEHOLDER_END, HTML_SEPARATOR, DOCUMENT_COMMENT_PLACEHOLDER_START, DOCUMENT_COMMENT_PLACEHOLDER_END } from "../constants";
7
+ import { DOCUMENT_SCRIPTS_PLACEHOLDER, DOCUMENT_LINKS_PLACEHOLDER, DOCUMENT_FILE_NAME, DOCUMENT_META_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP, DOC_EXT, DOCUMENT_SSR_PLACEHOLDER, DOCUMENT_CHUNKSMAP_PLACEHOLDER, DOCUMENT_SSRDATASCRIPT_PLACEHOLDER, DOCUMENT_SCRIPT_PLACEHOLDER_START, DOCUMENT_SCRIPT_PLACEHOLDER_END, HTML_SEPARATOR, DOCUMENT_COMMENT_PLACEHOLDER_START, DOCUMENT_COMMENT_PLACEHOLDER_END, DOCUMENT_STYLE_PLACEHOLDER_START, DOCUMENT_STYLE_PLACEHOLDER_END } from "../constants";
8
8
  const debug = createDebugger("html_genarate");
9
9
  export const getDocumenByEntryName = function(entrypoints, entryName, fallbackDir) {
10
10
  var _entrypoints_find;
@@ -126,6 +126,9 @@ export default () => {
126
126
  const nonceAttr = nonce ? `nonce=${nonce}` : "";
127
127
  html = html.replace(new RegExp(`${DOCUMENT_SCRIPT_PLACEHOLDER_START}(.*?)${DOCUMENT_SCRIPT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `<script ${nonceAttr}>${decodeURIComponent($1)}</script>`);
128
128
  }
129
+ if (html.includes(DOCUMENT_STYLE_PLACEHOLDER_START) && html.includes(DOCUMENT_STYLE_PLACEHOLDER_END)) {
130
+ html = html.replace(new RegExp(`${DOCUMENT_STYLE_PLACEHOLDER_START}(.*?)${DOCUMENT_STYLE_PLACEHOLDER_END}`, "g"), (_styleStr, $1) => `<style>${decodeURIComponent($1)}</style>`);
131
+ }
129
132
  if (html.includes(DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(DOCUMENT_COMMENT_PLACEHOLDER_END)) {
130
133
  html = html.replace(new RegExp(`${DOCUMENT_COMMENT_PLACEHOLDER_START}(.*?)${DOCUMENT_COMMENT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `${decodeURIComponent($1)}`);
131
134
  }
@@ -17,6 +17,8 @@ export const DOCUMENT_LINKS_PLACEHOLDER = encodeURIComponent("<!-- chunk links p
17
17
  export const DOCUMENT_NO_SCRIPTE_PLACEHOLDER = encodeURIComponent("<!-- no-script -->");
18
18
  export const DOCUMENT_SCRIPT_PLACEHOLDER_START = encodeURIComponent("<!-- script-start -->");
19
19
  export const DOCUMENT_SCRIPT_PLACEHOLDER_END = encodeURIComponent("<!-- script-end -->");
20
+ export const DOCUMENT_STYLE_PLACEHOLDER_START = encodeURIComponent("<!-- style-start -->");
21
+ export const DOCUMENT_STYLE_PLACEHOLDER_END = encodeURIComponent("<!-- style-end -->");
20
22
  export const DOCUMENT_COMMENT_PLACEHOLDER_START = encodeURIComponent("<!-- comment-start -->");
21
23
  export const DOCUMENT_COMMENT_PLACEHOLDER_END = encodeURIComponent("<!-- comment-end -->");
22
24
  export const PLACEHOLDER_REPLACER_MAP = {
@@ -6,6 +6,7 @@ export * from "./Body";
6
6
  export * from "./Root";
7
7
  export * from "./Links";
8
8
  export * from "./Comment";
9
+ export * from "./Style";
9
10
  export * from "./Scripts";
10
11
  export * from "./Script";
11
12
  export * from "./constants";
@@ -7,7 +7,7 @@ import { serializeErrors } from "./utils";
7
7
  const setupFnStr = `function s(r,e){_ROUTER_DATA.r=_ROUTER_DATA.r||{},_ROUTER_DATA.r[r]=_ROUTER_DATA.r[r]||{};return new Promise((function(A,R){_ROUTER_DATA.r[r][e]={resolve:A,reject:R}}))};`;
8
8
  const resolveFnStr = `function r(e,r,o,A){A?_ROUTER_DATA.r[e][r].reject(A):_ROUTER_DATA.r[e][r].resolve(o)};`;
9
9
  const preResolvedFnStr = `function p(e,r){return void 0!==r?Promise.reject(new Error(r.message)):Promise.resolve(e)};`;
10
- const DeferredDataScripts = () => {
10
+ const DeferredDataScripts = (props) => {
11
11
  const context = useContext(DataRouterContext);
12
12
  const { staticContext } = context || {};
13
13
  const hydratedRef = useRef(false);
@@ -36,6 +36,7 @@ const DeferredDataScripts = () => {
36
36
  const deferredKeyPromiseStr = deferredKeys.map((key) => {
37
37
  if (pendingKeys.has(key)) {
38
38
  deferredDataScripts.push(/* @__PURE__ */ _jsx(DeferredDataScript, {
39
+ nonce: props === null || props === void 0 ? void 0 : props.nonce,
39
40
  data: deferredData.data[key],
40
41
  dataKey: key,
41
42
  routeId
@@ -71,6 +72,7 @@ const DeferredDataScripts = () => {
71
72
  children: [
72
73
  !hydratedRef.current && /* @__PURE__ */ _jsx("script", {
73
74
  async: true,
75
+ nonce: props === null || props === void 0 ? void 0 : props.nonce,
74
76
  suppressHydrationWarning: true,
75
77
  dangerouslySetInnerHTML: {
76
78
  __html: deferredScripts[0]
@@ -81,16 +83,18 @@ const DeferredDataScripts = () => {
81
83
  ]
82
84
  });
83
85
  };
84
- const DeferredDataScript = ({ data, routeId, dataKey }) => {
86
+ const DeferredDataScript = ({ data, routeId, dataKey, nonce }) => {
85
87
  return /* @__PURE__ */ _jsx(Suspense, {
86
88
  children: typeof document === "undefined" && data && dataKey && routeId ? /* @__PURE__ */ _jsx(Await, {
87
89
  resolve: data,
88
90
  errorElement: /* @__PURE__ */ _jsx(ErrorDeferredDataScript, {
89
91
  routeId,
90
- dataKey
92
+ dataKey,
93
+ nonce
91
94
  }),
92
95
  children: (data2) => /* @__PURE__ */ _jsx("script", {
93
96
  async: true,
97
+ nonce,
94
98
  suppressHydrationWarning: true,
95
99
  dangerouslySetInnerHTML: {
96
100
  __html: `_ROUTER_DATA.r(${JSON.stringify(routeId)}, ${JSON.stringify(dataKey)}, ${serializeJson(data2)});`
@@ -99,9 +103,10 @@ const DeferredDataScript = ({ data, routeId, dataKey }) => {
99
103
  }) : null
100
104
  });
101
105
  };
102
- const ErrorDeferredDataScript = ({ routeId, dataKey }) => {
106
+ const ErrorDeferredDataScript = ({ routeId, dataKey, nonce }) => {
103
107
  const error = useAsyncError();
104
108
  return /* @__PURE__ */ _jsx("script", {
109
+ nonce,
105
110
  suppressHydrationWarning: true,
106
111
  dangerouslySetInnerHTML: {
107
112
  __html: `_ROUTER_DATA.r(${JSON.stringify(routeId)}, ${JSON.stringify(dataKey)}, ${void 0}, ${serializeJson({
@@ -49,12 +49,15 @@ export const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
49
49
  context
50
50
  });
51
51
  }
52
- const { request, mode: ssrMode } = context.ssrContext;
52
+ const { request, mode: ssrMode, nonce } = context.ssrContext;
53
53
  const baseUrl = request.baseUrl;
54
54
  const _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
55
55
  const routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
56
56
  routesConfig,
57
- ssrMode
57
+ ssrMode,
58
+ props: {
59
+ nonce
60
+ }
58
61
  }));
59
62
  const { query } = createStaticHandler(routes, {
60
63
  basename: _basename
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ export declare function Style(props: {
3
+ children?: string;
4
+ content?: string;
5
+ }): JSX.Element;
@@ -12,6 +12,8 @@ export declare const DOCUMENT_LINKS_PLACEHOLDER: string;
12
12
  export declare const DOCUMENT_NO_SCRIPTE_PLACEHOLDER: string;
13
13
  export declare const DOCUMENT_SCRIPT_PLACEHOLDER_START: string;
14
14
  export declare const DOCUMENT_SCRIPT_PLACEHOLDER_END: string;
15
+ export declare const DOCUMENT_STYLE_PLACEHOLDER_START: string;
16
+ export declare const DOCUMENT_STYLE_PLACEHOLDER_END: string;
15
17
  export declare const DOCUMENT_COMMENT_PLACEHOLDER_START: string;
16
18
  export declare const DOCUMENT_COMMENT_PLACEHOLDER_END: string;
17
19
  export declare const PLACEHOLDER_REPLACER_MAP: {
@@ -6,6 +6,7 @@ export * from './Body';
6
6
  export * from './Root';
7
7
  export * from './Links';
8
8
  export * from './Comment';
9
+ export * from './Style';
9
10
  export * from './Scripts';
10
11
  export * from './Script';
11
12
  export * from './constants';
@@ -3,5 +3,7 @@
3
3
  * DeferredDataScripts only renders in server side,
4
4
  * it doesn't need to be hydrated in client side.
5
5
  */
6
- declare const DeferredDataScripts: () => JSX.Element | null;
6
+ declare const DeferredDataScripts: (props?: {
7
+ nonce?: string;
8
+ }) => JSX.Element | null;
7
9
  export default DeferredDataScripts;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.22.0",
18
+ "version": "2.22.1",
19
19
  "engines": {
20
20
  "node": ">=14.17.6"
21
21
  },
@@ -169,9 +169,9 @@
169
169
  "redux-logger": "^3.0.6",
170
170
  "styled-components": "^5.3.1",
171
171
  "@swc/helpers": "0.5.1",
172
- "@modern-js/plugin": "2.22.0",
173
- "@modern-js/types": "2.22.0",
174
- "@modern-js/utils": "2.22.0"
172
+ "@modern-js/plugin": "2.22.1",
173
+ "@modern-js/types": "2.22.1",
174
+ "@modern-js/utils": "2.22.1"
175
175
  },
176
176
  "peerDependencies": {
177
177
  "react": ">=17",
@@ -192,11 +192,11 @@
192
192
  "ts-jest": "^29.1.0",
193
193
  "typescript": "^5",
194
194
  "webpack": "^5.82.1",
195
- "@modern-js/app-tools": "2.22.0",
196
- "@modern-js/core": "2.22.0",
197
- "@modern-js/server-core": "2.22.0",
198
- "@scripts/build": "2.22.0",
199
- "@scripts/jest-config": "2.22.0"
195
+ "@modern-js/app-tools": "2.22.1",
196
+ "@modern-js/core": "2.22.1",
197
+ "@modern-js/server-core": "2.22.1",
198
+ "@scripts/build": "2.22.1",
199
+ "@scripts/jest-config": "2.22.1"
200
200
  },
201
201
  "sideEffects": false,
202
202
  "modernConfig": {},