@modern-js/runtime 2.34.0 → 2.35.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.
Files changed (98) hide show
  1. package/dist/cjs/core/compatible.js +9 -10
  2. package/dist/cjs/core/loader/useLoader.js +8 -8
  3. package/dist/cjs/core/plugin.js +2 -3
  4. package/dist/cjs/document/Html.js +3 -4
  5. package/dist/cjs/document/cli/index.js +4 -6
  6. package/dist/cjs/router/cli/index.js +1 -2
  7. package/dist/cjs/router/runtime/DeferredDataScripts.node.js +9 -11
  8. package/dist/cjs/router/runtime/PrefetchLink.js +5 -9
  9. package/dist/cjs/router/runtime/index.js +5 -1
  10. package/dist/cjs/router/runtime/plugin.js +5 -7
  11. package/dist/cjs/router/runtime/plugin.node.js +8 -9
  12. package/dist/cjs/router/runtime/server.js +1 -1
  13. package/dist/cjs/router/runtime/utils.js +7 -7
  14. package/dist/cjs/router/runtime/withRouter.js +1 -1
  15. package/dist/cjs/ssr/cli/index.js +9 -11
  16. package/dist/cjs/ssr/index.js +3 -3
  17. package/dist/cjs/ssr/index.node.js +3 -2
  18. package/dist/cjs/ssr/prefetch.js +4 -4
  19. package/dist/cjs/ssr/react/prerender/util.js +2 -4
  20. package/dist/cjs/ssr/serverRender/renderToStream/buildTemplate.after.js +6 -3
  21. package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +3 -7
  22. package/dist/cjs/ssr/serverRender/renderToStream/index.js +14 -5
  23. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +5 -13
  24. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.worker.js +5 -8
  25. package/dist/cjs/ssr/serverRender/renderToString/entry.js +5 -5
  26. package/dist/cjs/ssr/serverRender/renderToString/index.js +3 -3
  27. package/dist/cjs/ssr/serverRender/renderToString/loadable.js +29 -18
  28. package/dist/cjs/ssr/serverRender/tracker.js +14 -0
  29. package/dist/esm/core/compatible.js +9 -10
  30. package/dist/esm/core/loader/useLoader.js +8 -8
  31. package/dist/esm/core/plugin.js +2 -3
  32. package/dist/esm/document/Html.js +3 -4
  33. package/dist/esm/document/cli/index.js +4 -5
  34. package/dist/esm/router/cli/index.js +1 -2
  35. package/dist/esm/router/runtime/DeferredDataScripts.node.js +4 -6
  36. package/dist/esm/router/runtime/PrefetchLink.js +5 -9
  37. package/dist/esm/router/runtime/index.js +3 -2
  38. package/dist/esm/router/runtime/plugin.js +4 -6
  39. package/dist/esm/router/runtime/plugin.node.js +6 -7
  40. package/dist/esm/router/runtime/server.js +1 -1
  41. package/dist/esm/router/runtime/utils.js +6 -6
  42. package/dist/esm/router/runtime/withRouter.js +1 -1
  43. package/dist/esm/ssr/cli/index.js +9 -11
  44. package/dist/esm/ssr/index.js +1 -1
  45. package/dist/esm/ssr/index.node.js +3 -2
  46. package/dist/esm/ssr/prefetch.js +3 -3
  47. package/dist/esm/ssr/react/prerender/util.js +2 -4
  48. package/dist/esm/ssr/serverRender/renderToStream/buildTemplate.after.js +5 -2
  49. package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +3 -5
  50. package/dist/esm/ssr/serverRender/renderToStream/index.js +14 -5
  51. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +5 -13
  52. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.worker.js +6 -7
  53. package/dist/esm/ssr/serverRender/renderToString/entry.js +4 -4
  54. package/dist/esm/ssr/serverRender/renderToString/index.js +2 -2
  55. package/dist/esm/ssr/serverRender/renderToString/loadable.js +32 -18
  56. package/dist/esm/ssr/serverRender/tracker.js +11 -0
  57. package/dist/esm-node/core/compatible.js +9 -10
  58. package/dist/esm-node/core/loader/useLoader.js +8 -8
  59. package/dist/esm-node/core/plugin.js +2 -3
  60. package/dist/esm-node/document/Html.js +3 -4
  61. package/dist/esm-node/document/cli/index.js +4 -6
  62. package/dist/esm-node/router/cli/index.js +1 -2
  63. package/dist/esm-node/router/runtime/DeferredDataScripts.node.js +4 -6
  64. package/dist/esm-node/router/runtime/PrefetchLink.js +5 -9
  65. package/dist/esm-node/router/runtime/index.js +3 -2
  66. package/dist/esm-node/router/runtime/plugin.js +4 -6
  67. package/dist/esm-node/router/runtime/plugin.node.js +6 -7
  68. package/dist/esm-node/router/runtime/server.js +1 -1
  69. package/dist/esm-node/router/runtime/utils.js +6 -6
  70. package/dist/esm-node/router/runtime/withRouter.js +1 -1
  71. package/dist/esm-node/ssr/cli/index.js +9 -11
  72. package/dist/esm-node/ssr/index.js +1 -1
  73. package/dist/esm-node/ssr/index.node.js +3 -2
  74. package/dist/esm-node/ssr/prefetch.js +3 -3
  75. package/dist/esm-node/ssr/react/prerender/util.js +2 -4
  76. package/dist/esm-node/ssr/serverRender/renderToStream/buildTemplate.after.js +5 -2
  77. package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +3 -7
  78. package/dist/esm-node/ssr/serverRender/renderToStream/index.js +13 -4
  79. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +5 -13
  80. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.worker.js +5 -8
  81. package/dist/esm-node/ssr/serverRender/renderToString/entry.js +4 -4
  82. package/dist/esm-node/ssr/serverRender/renderToString/index.js +2 -2
  83. package/dist/esm-node/ssr/serverRender/renderToString/loadable.js +29 -18
  84. package/dist/esm-node/ssr/serverRender/tracker.js +14 -0
  85. package/dist/types/router/runtime/PrefetchLink.d.ts +1 -1
  86. package/dist/types/router/runtime/hooks.d.ts +1 -1
  87. package/dist/types/router/runtime/index.d.ts +4 -3
  88. package/dist/types/router/runtime/server.d.ts +1 -1
  89. package/dist/types/router/runtime/types.d.ts +2 -2
  90. package/dist/types/router/runtime/utils.d.ts +1 -1
  91. package/dist/types/router/runtime/withRouter.d.ts +1 -1
  92. package/dist/types/runtimeContext.d.ts +1 -1
  93. package/dist/types/ssr/serverRender/renderToString/entry.d.ts +2 -2
  94. package/dist/types/ssr/serverRender/renderToString/loadable.d.ts +1 -0
  95. package/dist/types/ssr/serverRender/renderToString/type.d.ts +0 -10
  96. package/dist/types/ssr/serverRender/tracker.d.ts +5 -2
  97. package/dist/types/ssr/serverRender/types.d.ts +12 -1
  98. package/package.json +10 -9
@@ -55,9 +55,8 @@ const aggKeysFromPropsList = function aggKeysFromPropsList2(propsList, propName)
55
55
  }, {});
56
56
  const res = propsList.filter((props) => usefulObject(props[propName])).reduce((result, next) => {
57
57
  REQUEST_META.forEach((key) => {
58
- var _prop;
59
58
  const prop = next[propName];
60
- if (((_prop = prop) === null || _prop === void 0 ? void 0 : _prop.hasOwnProperty(key)) && usefulArray(prop[key])) {
59
+ if ((prop === null || prop === void 0 ? void 0 : prop.hasOwnProperty(key)) && usefulArray(prop[key])) {
61
60
  result[key] = unique(result[key].concat(prop[key]));
62
61
  }
63
62
  });
@@ -80,9 +79,8 @@ const aggMatchesFromPropsList = function aggMatchesFromPropsList2(propsList, pro
80
79
  }, {});
81
80
  const res = propsList.filter((props) => usefulObject(props[propName])).reduce((result, next) => {
82
81
  REQUEST_META.forEach((key) => {
83
- var _prop;
84
82
  const prop = next[propName];
85
- if (((_prop = prop) === null || _prop === void 0 ? void 0 : _prop.hasOwnProperty(key)) && usefulObject(prop[key])) {
83
+ if ((prop === null || prop === void 0 ? void 0 : prop.hasOwnProperty(key)) && usefulObject(prop[key])) {
86
84
  result[key] = Object.assign(result[key], prop[key]);
87
85
  }
88
86
  });
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "buildShellAfterTemplate", {
8
8
  return buildShellAfterTemplate;
9
9
  }
10
10
  });
11
- const _runtimenode = require("@modern-js/utils/runtime-node");
11
+ const _node = require("@modern-js/runtime-utils/node");
12
12
  const _utils = require("../utils");
13
13
  const _buildTemplateshare = require("./buildTemplate.share");
14
14
  function buildShellAfterTemplate(afterAppTemplate, options) {
@@ -21,7 +21,7 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
21
21
  return template.replace("<!--<?- SSRDataScript ?>-->", ssrDataScript);
22
22
  function buildSSRDataScript() {
23
23
  const { context: { ssrContext, initialData, __i18nData__ }, renderLevel } = options;
24
- const { request, enableUnsafeCtx, nonce } = ssrContext;
24
+ const { request, enableUnsafeCtx, nonce, tracker } = ssrContext;
25
25
  const unsafeContext = {
26
26
  headers: request.headers
27
27
  };
@@ -31,6 +31,9 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
31
31
  i18nData: __i18nData__
32
32
  },
33
33
  context: {
34
+ reporter: {
35
+ sessionId: tracker.sessionId
36
+ },
34
37
  request: {
35
38
  params: request.params,
36
39
  query: request.query,
@@ -46,7 +49,7 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
46
49
  nonce
47
50
  });
48
51
  return `
49
- <script${attrsStr}>window._SSR_DATA = ${(0, _runtimenode.serializeJson)(SSRData)}</script>
52
+ <script${attrsStr}>window._SSR_DATA = ${(0, _node.serializeJson)(SSRData)}</script>
50
53
  `;
51
54
  }
52
55
  }
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "buildShellBeforeTemplate", {
10
10
  });
11
11
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
12
12
  const _reacthelmet = /* @__PURE__ */ _interop_require_default._(require("react-helmet"));
13
- const _router = require("@modern-js/utils/runtime/router");
13
+ const _router = require("@modern-js/runtime-utils/router");
14
14
  const _helmet = /* @__PURE__ */ _interop_require_default._(require("../helmet"));
15
15
  const _utils = require("../utils");
16
16
  const _buildTemplateshare = require("./buildTemplate.share");
@@ -31,7 +31,6 @@ function getHeadTemplate(beforeEntryTemplate, context) {
31
31
  function injectCss(headTemplate2) {
32
32
  return headTemplate2.replace(_utils.CSS_CHUNKS_PLACEHOLDER, getCssChunks());
33
33
  function getCssChunks() {
34
- var _matches;
35
34
  const { routeManifest, routerContext, routes } = context;
36
35
  if (!routeManifest || !routerContext || !routes) {
37
36
  return "";
@@ -39,7 +38,7 @@ function getHeadTemplate(beforeEntryTemplate, context) {
39
38
  const { routeAssets } = routeManifest;
40
39
  const cssChunks = [];
41
40
  const matches = (0, _router.matchRoutes)(routes, routerContext.location, routerContext.basename);
42
- (_matches = matches) === null || _matches === void 0 ? void 0 : _matches.forEach((match, index) => {
41
+ matches === null || matches === void 0 ? void 0 : matches.forEach((match, index) => {
43
42
  if (!index) {
44
43
  return;
45
44
  }
@@ -48,10 +47,7 @@ function getHeadTemplate(beforeEntryTemplate, context) {
48
47
  const routeManifest2 = routeAssets[routeId];
49
48
  if (routeManifest2) {
50
49
  const { referenceCssAssets = [] } = routeManifest2;
51
- const _cssChunks = referenceCssAssets.filter((asset) => {
52
- var _asset;
53
- return ((_asset = asset) === null || _asset === void 0 ? void 0 : _asset.endsWith(".css")) && !headTemplate2.includes(asset);
54
- });
50
+ const _cssChunks = referenceCssAssets.filter((asset) => (asset === null || asset === void 0 ? void 0 : asset.endsWith(".css")) && !headTemplate2.includes(asset));
55
51
  cssChunks.push(..._cssChunks);
56
52
  }
57
53
  }
@@ -10,33 +10,42 @@ Object.defineProperty(exports, "render", {
10
10
  });
11
11
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
12
12
  const _react = require("react");
13
- const _runtimenode = require("@modern-js/utils/runtime-node");
14
- const _time = require("@modern-js/utils/universal/time");
13
+ const _node = require("@modern-js/runtime-utils/node");
14
+ const _time = require("@modern-js/runtime-utils/time");
15
15
  const _prerender = require("../../react/prerender");
16
+ const _tracker = require("../tracker");
16
17
  const _renderToPipe = /* @__PURE__ */ _interop_require_default._(require("./renderToPipe"));
17
18
  const render = ({ App, context }) => {
18
19
  const { ssrContext } = context;
19
20
  if (!ssrContext) {
20
21
  throw new Error('The "ssrContext" must not be undefined, but received undefined');
21
22
  }
22
- return (0, _runtimenode.run)(ssrContext.request.headers, async () => {
23
+ return (0, _node.run)(ssrContext.request.headers, async () => {
23
24
  const end = (0, _time.time)();
24
25
  const rootElement = (0, _react.createElement)(App, {
25
26
  context: Object.assign(context || {}, {
26
27
  ssr: true
27
28
  })
28
29
  });
30
+ const { tracker } = ssrContext;
29
31
  const pipe = (0, _renderToPipe.default)(rootElement, context, {
30
32
  onShellReady() {
31
33
  const cacheConfig = _prerender.PreRender.config();
32
34
  if (cacheConfig) {
33
35
  ssrContext.cacheConfig = cacheConfig;
34
36
  }
37
+ const cost = end();
38
+ tracker.trackTiming(_tracker.SSRTimings.SSR_RENDER_SHELL, cost);
35
39
  },
36
40
  onAllReady() {
37
41
  const cost = end();
38
- ssrContext.logger.debug("App Render To HTML cost = %d ms", cost);
39
- ssrContext.metrics.emitTimer("app.render.html.cost", cost);
42
+ tracker.trackTiming(_tracker.SSRTimings.SSR_RENDER_TOTAL, cost);
43
+ },
44
+ onShellError(e) {
45
+ tracker.trackError(_tracker.SSRErrors.RENDER_SHELL, e);
46
+ },
47
+ onError(error) {
48
+ tracker.trackError(_tracker.SSRErrors.RENDER_STREAM, error);
40
49
  }
41
50
  });
42
51
  return pipe;
@@ -23,7 +23,6 @@ function renderToPipe(rootElement, context, options) {
23
23
  const chunkVec = [];
24
24
  const forUserPipe = (stream) => {
25
25
  return new Promise((resolve) => {
26
- var _ssrContext;
27
26
  let renderToPipeableStream;
28
27
  try {
29
28
  ({ renderToPipeableStream } = require("react-dom/server"));
@@ -31,11 +30,11 @@ function renderToPipe(rootElement, context, options) {
31
30
  }
32
31
  const { pipe } = renderToPipeableStream(rootElement, {
33
32
  ...options,
34
- nonce: (_ssrContext = ssrContext) === null || _ssrContext === void 0 ? void 0 : _ssrContext.nonce,
33
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
35
34
  onShellReady() {
36
- var _options_onShellReady, _options;
35
+ var _options_onShellReady;
37
36
  const { shellAfter, shellBefore } = (0, _template.getTemplates)(context, _types.RenderLevel.SERVER_RENDER);
38
- (_options = options) === null || _options === void 0 ? void 0 : (_options_onShellReady = _options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(_options);
37
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
39
38
  const injectableTransform = new _stream.Transform({
40
39
  transform(chunk, _encoding, callback) {
41
40
  try {
@@ -63,18 +62,11 @@ function renderToPipe(rootElement, context, options) {
63
62
  resolve(pipe(injectableTransform).pipe(stream));
64
63
  },
65
64
  onShellError(error) {
66
- var _options_onShellError, _options;
67
- ssrContext.metrics.emitCounter("app.render.streaming.shell.error", 1);
65
+ var _options_onShellError;
68
66
  const { shellAfter, shellBefore } = (0, _template.getTemplates)(context, _types.RenderLevel.CLIENT_RENDER);
69
67
  const fallbackHtml = `${shellBefore}${shellAfter}`;
70
68
  resolve(fallbackHtml);
71
- (_options = options) === null || _options === void 0 ? void 0 : (_options_onShellError = _options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(_options, error);
72
- },
73
- onError(error) {
74
- var _options_onError, _options;
75
- ssrContext.logger.error("An error occurs during streaming SSR", error);
76
- ssrContext.metrics.emitCounter("app.render.streaming.error", 1);
77
- (_options = options) === null || _options === void 0 ? void 0 : (_options_onError = _options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(_options, error);
69
+ options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
78
70
  }
79
71
  });
80
72
  });
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "default", {
10
10
  });
11
11
  const _types = require("../types");
12
12
  const _common = require("../../../common");
13
+ const _tracker = require("../tracker");
13
14
  const _template = require("./template");
14
15
  var ShellChunkStatus;
15
16
  (function(ShellChunkStatus2) {
@@ -28,15 +29,12 @@ function renderToPipe(rootElement, context, options) {
28
29
  }
29
30
  const { shellAfter, shellBefore } = (0, _template.getTemplates)(context, _types.RenderLevel.SERVER_RENDER);
30
31
  try {
31
- var _ssrContext;
32
32
  const readableOriginal = await renderToReadableStream(rootElement, {
33
33
  ...options,
34
- nonce: (_ssrContext = ssrContext) === null || _ssrContext === void 0 ? void 0 : _ssrContext.nonce,
34
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
35
35
  onError(error) {
36
- var _ssrContext2, _ssrContext12, _options_onError, _options;
37
- (_ssrContext2 = ssrContext) === null || _ssrContext2 === void 0 ? void 0 : _ssrContext2.logger.error("An error occurs during streaming SSR", error);
38
- (_ssrContext12 = ssrContext) === null || _ssrContext12 === void 0 ? void 0 : _ssrContext12.metrics.emitCounter("app.render.streaming.error", 1);
39
- (_options = options) === null || _options === void 0 ? void 0 : (_options_onError = _options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(_options, error);
36
+ var _options_onError;
37
+ options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, error);
40
38
  }
41
39
  });
42
40
  const reader = readableOriginal.getReader();
@@ -67,8 +65,7 @@ function renderToPipe(rootElement, context, options) {
67
65
  });
68
66
  return injectableStream;
69
67
  } catch (err) {
70
- var _ssrContext1;
71
- (_ssrContext1 = ssrContext) === null || _ssrContext1 === void 0 ? void 0 : _ssrContext1.metrics.emitCounter("app.render.streaming.shell.error", 1);
68
+ ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.tracker.trackError(_tracker.SSRErrors.RENDER_SHELL, err);
72
69
  const { shellAfter: shellAfter2, shellBefore: shellBefore2 } = (0, _template.getTemplates)(context, _types.RenderLevel.CLIENT_RENDER);
73
70
  const fallbackHtml = `${shellBefore2}${shellAfter2}`;
74
71
  return fallbackHtml;
@@ -11,9 +11,9 @@ Object.defineProperty(exports, "default", {
11
11
  const _define_property = require("@swc/helpers/_/_define_property");
12
12
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
13
13
  const _react = /* @__PURE__ */ _interop_require_default._(require("react"));
14
- const _runtimenode = require("@modern-js/utils/runtime-node");
14
+ const _node = require("@modern-js/runtime-utils/node");
15
15
  const _reacthelmet = /* @__PURE__ */ _interop_require_default._(require("react-helmet"));
16
- const _time = require("@modern-js/utils/universal/time");
16
+ const _time = require("@modern-js/runtime-utils/time");
17
17
  const _utils = require("../../../router/runtime/utils");
18
18
  const _helmet = /* @__PURE__ */ _interop_require_default._(require("../helmet"));
19
19
  const _types = require("../types");
@@ -123,13 +123,13 @@ class Entry {
123
123
  }
124
124
  getSSRDataScript(templateData, routerData) {
125
125
  const useInlineScript = this.pluginConfig.inlineScript !== false;
126
- const ssrData = (0, _runtimenode.serializeJson)(templateData);
126
+ const ssrData = (0, _node.serializeJson)(templateData);
127
127
  const attrsStr = (0, _utils1.attributesToString)({
128
128
  nonce: this.nonce
129
129
  });
130
130
  let ssrDataScripts = useInlineScript ? `<script${attrsStr}>window._SSR_DATA = ${ssrData}</script>` : `<script type="application/json" id="${_utils1.SSR_DATA_JSON_ID}">${ssrData}</script>`;
131
131
  if (routerData) {
132
- const serializedRouterData = (0, _runtimenode.serializeJson)(routerData);
132
+ const serializedRouterData = (0, _node.serializeJson)(routerData);
133
133
  ssrDataScripts += useInlineScript ? `
134
134
  <script${attrsStr}>window._ROUTER_DATA = ${serializedRouterData}</script>` : `
135
135
  <script type="application/json" id="${_utils1.ROUTER_DATA_JSON_ID}">${serializedRouterData}</script>`;
@@ -152,7 +152,7 @@ class Entry {
152
152
  this.entryName = entryName;
153
153
  this.App = options.App;
154
154
  this.pluginConfig = config;
155
- this.tracker = (0, _tracker.createSSRTracker)(ctx);
155
+ this.tracker = ctx.tracker;
156
156
  this.metrics = ctx.metrics;
157
157
  this.htmlModifiers = ctx.htmlModifiers;
158
158
  this.nonce = nonce;
@@ -9,14 +9,14 @@ Object.defineProperty(exports, "render", {
9
9
  }
10
10
  });
11
11
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
12
- const _runtimenode = require("@modern-js/utils/runtime-node");
13
- const _time = require("@modern-js/utils/universal/time");
12
+ const _node = require("@modern-js/runtime-utils/node");
13
+ const _time = require("@modern-js/runtime-utils/time");
14
14
  const _prerender = require("../../react/prerender");
15
15
  const _tracker = require("../tracker");
16
16
  const _entry = /* @__PURE__ */ _interop_require_default._(require("./entry"));
17
17
  const render = ({ App, context, config }) => {
18
18
  const ssrContext = context.ssrContext;
19
- return (0, _runtimenode.run)(ssrContext.request.headers, async () => {
19
+ return (0, _node.run)(ssrContext.request.headers, async () => {
20
20
  const entry = new _entry.default({
21
21
  ctx: ssrContext,
22
22
  App,
@@ -12,11 +12,10 @@ const _define_property = require("@swc/helpers/_/_define_property");
12
12
  const _server = require("@loadable/server");
13
13
  const _utils = require("../utils");
14
14
  const extname = (uri) => {
15
- var _uri;
16
15
  if (typeof uri !== "string" || !uri.includes(".")) {
17
16
  return "";
18
17
  }
19
- return `.${(_uri = uri) === null || _uri === void 0 ? void 0 : _uri.split(".").pop()}` || "";
18
+ return `.${uri === null || uri === void 0 ? void 0 : uri.split(".").pop()}` || "";
20
19
  };
21
20
  class LoadableCollector {
22
21
  collect(comopnent) {
@@ -40,35 +39,47 @@ class LoadableCollector {
40
39
  const { extractor } = this;
41
40
  const chunks = extractor.getChunkAssets(extractor.chunks);
42
41
  chunksMap.js = (chunksMap.js || "") + (0, _utils.getLoadableScripts)(extractor);
42
+ const attributes = this.generateAttributes();
43
43
  for (const v of chunks) {
44
- const fileType = extname(v.url).slice(1);
45
- const attributes = {};
46
- const { crossorigin, scriptLoading = "defer" } = config;
47
- if (crossorigin) {
48
- attributes.crossorigin = crossorigin === true ? "anonymous" : crossorigin;
49
- }
50
- switch (scriptLoading) {
51
- case "defer":
52
- attributes.defer = true;
53
- break;
54
- case "module":
55
- attributes.type = "module";
56
- break;
57
- default:
44
+ if (!v.url) {
45
+ continue;
58
46
  }
47
+ const fileType = extname(v.url).slice(1);
59
48
  if (fileType === "js") {
60
49
  const jsChunkReg = new RegExp(`<script .*src="${v.url}".*>`);
61
50
  if (!jsChunkReg.test(template)) {
51
+ const { scriptLoading = "defer" } = config;
52
+ switch (scriptLoading) {
53
+ case "defer":
54
+ attributes.defer = true;
55
+ break;
56
+ case "module":
57
+ attributes.type = "module";
58
+ break;
59
+ default:
60
+ }
62
61
  attributes.nonce = nonce;
63
62
  const attrsStr = (0, _utils.attributesToString)(attributes);
64
63
  chunksMap[fileType] += `<script${attrsStr} src="${v.url}"></script>`;
65
64
  }
66
65
  } else if (fileType === "css") {
67
- const attrsStr = (0, _utils.attributesToString)(attributes);
68
- chunksMap[fileType] += `<link${attrsStr} href="${v.url}" rel="stylesheet" />`;
66
+ const cssChunkReg = new RegExp(`<link .*href="${v.url}".*>`);
67
+ if (!cssChunkReg.test(template)) {
68
+ const attrsStr = (0, _utils.attributesToString)(attributes);
69
+ chunksMap[fileType] += `<link${attrsStr} href="${v.url}" rel="stylesheet" />`;
70
+ }
69
71
  }
70
72
  }
71
73
  }
74
+ generateAttributes() {
75
+ const { config } = this.options;
76
+ const { crossorigin } = config;
77
+ const attributes = {};
78
+ if (crossorigin) {
79
+ attributes.crossorigin = crossorigin === true ? "anonymous" : crossorigin;
80
+ }
81
+ return attributes;
82
+ }
72
83
  constructor(options) {
73
84
  _define_property._(this, "options", void 0);
74
85
  _define_property._(this, "extractor", void 0);
@@ -25,11 +25,14 @@ var SSRTimings;
25
25
  SSRTimings2[SSRTimings2["SSR_RENDER_TOTAL"] = 0] = "SSR_RENDER_TOTAL";
26
26
  SSRTimings2[SSRTimings2["SSR_PREFETCH"] = 1] = "SSR_PREFETCH";
27
27
  SSRTimings2[SSRTimings2["SSR_RENDER_HTML"] = 2] = "SSR_RENDER_HTML";
28
+ SSRTimings2[SSRTimings2["SSR_RENDER_SHELL"] = 3] = "SSR_RENDER_SHELL";
28
29
  })(SSRTimings || (SSRTimings = {}));
29
30
  var SSRErrors;
30
31
  (function(SSRErrors2) {
31
32
  SSRErrors2[SSRErrors2["PREFETCH"] = 0] = "PREFETCH";
32
33
  SSRErrors2[SSRErrors2["RENDER_HTML"] = 1] = "RENDER_HTML";
34
+ SSRErrors2[SSRErrors2["RENDER_STREAM"] = 2] = "RENDER_STREAM";
35
+ SSRErrors2[SSRErrors2["RENDER_SHELL"] = 3] = "RENDER_SHELL";
33
36
  })(SSRErrors || (SSRErrors = {}));
34
37
  const errors = {
35
38
  [SSRErrors.PREFETCH]: {
@@ -41,6 +44,14 @@ const errors = {
41
44
  reporter: "SSR Error - App Render To HTML",
42
45
  logger: "App Render To HTML",
43
46
  metrics: "app.render.html.error"
47
+ },
48
+ [SSRErrors.RENDER_STREAM]: {
49
+ reporter: "SSR Error - App Render To Streaming",
50
+ logger: "An error occurs during streaming SSR",
51
+ metrics: "app.render.streaming.error"
52
+ },
53
+ [SSRErrors.RENDER_SHELL]: {
54
+ metrics: "app.render.streaming.shell.error"
44
55
  }
45
56
  };
46
57
  const timings = {
@@ -61,6 +72,9 @@ const timings = {
61
72
  serverTiming: "ssr-render-total",
62
73
  metrics: "app.render.cost",
63
74
  logger: "App Render Total cost = %d ms"
75
+ },
76
+ [SSRTimings.SSR_RENDER_SHELL]: {
77
+ reporter: "ssr-render-shell"
64
78
  }
65
79
  };
66
80
  function createSSRTracker({ reporter, serverTiming, metrics, logger }) {
@@ -58,17 +58,16 @@ export var createApp = function(param) {
58
58
  var context = _param.context, props = _object_without_properties(_param, [
59
59
  "context"
60
60
  ]);
61
- var _contextValue;
62
61
  var contextValue = context;
63
- if (!((_contextValue = contextValue) === null || _contextValue === void 0 ? void 0 : _contextValue.runner)) {
62
+ if (!(contextValue === null || contextValue === void 0 ? void 0 : contextValue.runner)) {
64
63
  contextValue = getInitialContext(runner);
65
64
  runner.init({
66
65
  context: contextValue
67
66
  }, {
68
67
  onLast: function(param3) {
69
68
  var context1 = param3.context;
70
- var _App_init, _App;
71
- return (_App = _$App) === null || _App === void 0 ? void 0 : (_App_init = _App.init) === null || _App_init === void 0 ? void 0 : _App_init.call(_App, context1);
69
+ var _App_init;
70
+ return _$App === null || _$App === void 0 ? void 0 : (_App_init = _$App.init) === null || _App_init === void 0 ? void 0 : _App_init.call(_$App, context1);
72
71
  }
73
72
  });
74
73
  }
@@ -87,7 +86,7 @@ export var createApp = function(param) {
87
86
  };
88
87
  export var bootstrap = function() {
89
88
  var _ref = _async_to_generator(function(BootApp, id, root, ReactDOM) {
90
- var App, runner, context, runInit, isBrowser, _ssrData_data, _ssrData, _ssrData1, _ssrData_data1, _ssrData2, ssrData, loadersData, initialLoadersState, initialData, rootElement, ModernRender, ModernHydrate, isRedirectResponse, initialData1;
89
+ var App, runner, context, runInit, isBrowser, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement, ModernRender, ModernHydrate, isRedirectResponse, initialData1;
91
90
  return _ts_generator(this, function(_state) {
92
91
  switch (_state.label) {
93
92
  case 0:
@@ -106,8 +105,8 @@ export var bootstrap = function() {
106
105
  }, {
107
106
  onLast: function(param) {
108
107
  var context1 = param.context;
109
- var _App_init, _App;
110
- return (_App = App) === null || _App === void 0 ? void 0 : (_App_init = _App.init) === null || _App_init === void 0 ? void 0 : _App_init.call(_App, context1);
108
+ var _App_init;
109
+ return App === null || App === void 0 ? void 0 : (_App_init = App.init) === null || _App_init === void 0 ? void 0 : _App_init.call(App, context1);
111
110
  }
112
111
  });
113
112
  };
@@ -131,7 +130,7 @@ export var bootstrap = function() {
131
130
  2
132
131
  ];
133
132
  ssrData = window._SSR_DATA;
134
- loadersData = ((_ssrData = ssrData) === null || _ssrData === void 0 ? void 0 : (_ssrData_data = _ssrData.data) === null || _ssrData_data === void 0 ? void 0 : _ssrData_data.loadersData) || {};
133
+ loadersData = (ssrData === null || ssrData === void 0 ? void 0 : (_ssrData_data = ssrData.data) === null || _ssrData_data === void 0 ? void 0 : _ssrData_data.loadersData) || {};
135
134
  initialLoadersState = Object.keys(loadersData).reduce(function(res, key) {
136
135
  var loaderData = loadersData[key];
137
136
  if (loaderData.loading !== false) {
@@ -145,9 +144,9 @@ export var bootstrap = function() {
145
144
  skipStatic: true
146
145
  })
147
146
  }, ssrData ? {
148
- ssrContext: (_ssrData1 = ssrData) === null || _ssrData1 === void 0 ? void 0 : _ssrData1.context
147
+ ssrContext: ssrData === null || ssrData === void 0 ? void 0 : ssrData.context
149
148
  } : {}));
150
- context.initialData = (_ssrData2 = ssrData) === null || _ssrData2 === void 0 ? void 0 : (_ssrData_data1 = _ssrData2.data) === null || _ssrData_data1 === void 0 ? void 0 : _ssrData_data1.initialData;
149
+ context.initialData = ssrData === null || ssrData === void 0 ? void 0 : (_ssrData_data1 = ssrData.data) === null || _ssrData_data1 === void 0 ? void 0 : _ssrData_data1.initialData;
151
150
  return [
152
151
  4,
153
152
  runInit(context)
@@ -19,7 +19,7 @@ var useLoader = function(loaderFn) {
19
19
  delete options._cache;
20
20
  }
21
21
  var load = useCallback(function(params) {
22
- var _unlistenLoaderChangeRef_current, _unlistenLoaderChangeRef, _window__SSR_DATA_data_loadersData_id, _window__SSR_DATA_data, _window__SSR_DATA, _window, _loaderRef_current;
22
+ var _unlistenLoaderChangeRef_current, _window__SSR_DATA_data_loadersData_id, _window__SSR_DATA_data, _window__SSR_DATA, _window, _loaderRef_current;
23
23
  if (typeof params === "undefined") {
24
24
  var _loaderRef_current1;
25
25
  return (_loaderRef_current1 = loaderRef.current) === null || _loaderRef_current1 === void 0 ? void 0 : _loaderRef_current1.load();
@@ -38,7 +38,7 @@ var useLoader = function(loaderFn) {
38
38
  params: params
39
39
  }));
40
40
  loaderRef.current = loaderManager.get(id);
41
- (_unlistenLoaderChangeRef_current = (_unlistenLoaderChangeRef = unlistenLoaderChangeRef).current) === null || _unlistenLoaderChangeRef_current === void 0 ? void 0 : _unlistenLoaderChangeRef_current.call(_unlistenLoaderChangeRef);
41
+ (_unlistenLoaderChangeRef_current = unlistenLoaderChangeRef.current) === null || _unlistenLoaderChangeRef_current === void 0 ? void 0 : _unlistenLoaderChangeRef_current.call(unlistenLoaderChangeRef);
42
42
  if (isSSRRender) {
43
43
  return void 0;
44
44
  }
@@ -52,12 +52,12 @@ var useLoader = function(loaderFn) {
52
52
  unlistenLoaderChangeRef.current = (_loaderRef_current = loaderRef.current) === null || _loaderRef_current === void 0 ? void 0 : _loaderRef_current.onChange(function(_status, _result) {
53
53
  setResult(_result);
54
54
  if (_status === LoaderStatus.fulfilled) {
55
- var _options_onSuccess, _options;
56
- (_options = options) === null || _options === void 0 ? void 0 : (_options_onSuccess = _options.onSuccess) === null || _options_onSuccess === void 0 ? void 0 : _options_onSuccess.call(_options, _result.data);
55
+ var _options_onSuccess;
56
+ options === null || options === void 0 ? void 0 : (_options_onSuccess = options.onSuccess) === null || _options_onSuccess === void 0 ? void 0 : _options_onSuccess.call(options, _result.data);
57
57
  }
58
58
  if (_status === LoaderStatus.rejected) {
59
- var _options_onError, _options1;
60
- (_options1 = options) === null || _options1 === void 0 ? void 0 : (_options_onError = _options1.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(_options1, _result.error);
59
+ var _options_onError;
60
+ options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, _result.error);
61
61
  }
62
62
  });
63
63
  return res;
@@ -66,8 +66,8 @@ var useLoader = function(loaderFn) {
66
66
  ]);
67
67
  useEffect(function() {
68
68
  return function() {
69
- var _unlistenLoaderChangeRef_current, _unlistenLoaderChangeRef;
70
- (_unlistenLoaderChangeRef_current = (_unlistenLoaderChangeRef = unlistenLoaderChangeRef).current) === null || _unlistenLoaderChangeRef_current === void 0 ? void 0 : _unlistenLoaderChangeRef_current.call(_unlistenLoaderChangeRef);
69
+ var _unlistenLoaderChangeRef_current;
70
+ (_unlistenLoaderChangeRef_current = unlistenLoaderChangeRef.current) === null || _unlistenLoaderChangeRef_current === void 0 ? void 0 : _unlistenLoaderChangeRef_current.call(unlistenLoaderChangeRef);
71
71
  };
72
72
  }, []);
73
73
  useMemo(function() {
@@ -23,7 +23,6 @@ var registerInit = function(App, _init) {
23
23
  var originalInit = App.init;
24
24
  App.init = function() {
25
25
  var _ref = _async_to_generator(function(context) {
26
- var _originalInit, _$_init;
27
26
  return _ts_generator(this, function(_state) {
28
27
  switch (_state.label) {
29
28
  case 0:
@@ -33,8 +32,8 @@ var registerInit = function(App, _init) {
33
32
  return [
34
33
  4,
35
34
  Promise.all([
36
- (_originalInit = originalInit) === null || _originalInit === void 0 ? void 0 : _originalInit(context),
37
- (_$_init = _init) === null || _$_init === void 0 ? void 0 : _$_init(context)
35
+ originalInit === null || originalInit === void 0 ? void 0 : originalInit(context),
36
+ _init === null || _init === void 0 ? void 0 : _init(context)
38
37
  ])
39
38
  ];
40
39
  case 1:
@@ -11,8 +11,7 @@ function findTargetChild(tag, children) {
11
11
  });
12
12
  }
13
13
  function getEleType(ele) {
14
- var _ele, _ele1;
15
- return typeof ((_ele = ele) === null || _ele === void 0 ? void 0 : _ele.type) === "function" ? ele.type.name : (_ele1 = ele) === null || _ele1 === void 0 ? void 0 : _ele1.type;
14
+ return typeof (ele === null || ele === void 0 ? void 0 : ele.type) === "function" ? ele.type.name : ele === null || ele === void 0 ? void 0 : ele.type;
16
15
  }
17
16
  function findTargetElement(tag, children) {
18
17
  if (children.length === 0) {
@@ -23,11 +22,11 @@ function findTargetElement(tag, children) {
23
22
  try {
24
23
  for (var _iterator = children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
25
24
  var item = _step.value;
26
- var _item_props, _item;
25
+ var _item_props;
27
26
  if (tag === getEleType(item)) {
28
27
  return item;
29
28
  }
30
- if ((_item = item) === null || _item === void 0 ? void 0 : (_item_props = _item.props) === null || _item_props === void 0 ? void 0 : _item_props.children) {
29
+ if (item === null || item === void 0 ? void 0 : (_item_props = item.props) === null || _item_props === void 0 ? void 0 : _item_props.children) {
31
30
  nextChildren = nextChildren.concat(item.props.children);
32
31
  }
33
32
  }
@@ -57,7 +57,7 @@ export var documentPlugin = function() {
57
57
  }
58
58
  return function() {
59
59
  var _ref2 = _async_to_generator(function(param) {
60
- var htmlWebpackPlugin, _tsConfig, _partialsByEntrypoint, config, documentParams, tempTsConfigFile, userTsConfigFilePath, tsConfig, err, htmlOutputFile, Document, HTMLElement, html, partialsByEntrypoint, scripts, partialsContent, links, metas, nonce, nonceAttr, finalHtml;
60
+ var htmlWebpackPlugin, config, documentParams, tempTsConfigFile, userTsConfigFilePath, tsConfig, err, htmlOutputFile, Document, HTMLElement, html, partialsByEntrypoint, scripts, partialsContent, links, metas, nonce, nonceAttr, finalHtml;
61
61
  return _ts_generator(this, function(_state2) {
62
62
  switch (_state2.label) {
63
63
  case 0:
@@ -96,7 +96,7 @@ export var documentPlugin = function() {
96
96
  4
97
97
  ];
98
98
  case 4:
99
- if ((_tsConfig = tsConfig) === null || _tsConfig === void 0 ? void 0 : _tsConfig.compilerOptions) {
99
+ if (tsConfig === null || tsConfig === void 0 ? void 0 : tsConfig.compilerOptions) {
100
100
  tsConfig.compilerOptions.jsx = "react-jsx";
101
101
  } else {
102
102
  tsConfig.compilerOptions = {
@@ -168,7 +168,7 @@ export var documentPlugin = function() {
168
168
  partialsHead: "",
169
169
  partialsBody: ""
170
170
  };
171
- if ((_partialsByEntrypoint = partialsByEntrypoint) === null || _partialsByEntrypoint === void 0 ? void 0 : _partialsByEntrypoint[entryName]) {
171
+ if (partialsByEntrypoint === null || partialsByEntrypoint === void 0 ? void 0 : partialsByEntrypoint[entryName]) {
172
172
  partialsContent.partialsTop = partialsByEntrypoint[entryName].top.join("\n");
173
173
  partialsContent.partialsHead = partialsByEntrypoint[entryName].head.join("\n");
174
174
  partialsContent.partialsBody = partialsByEntrypoint[entryName].body.join("\n");
@@ -222,8 +222,7 @@ export var documentPlugin = function() {
222
222
  return {
223
223
  tools: {
224
224
  htmlPlugin: function(options, entry) {
225
- var _options, _options1, _options2;
226
- var hackParameters = typeof ((_options = options) === null || _options === void 0 ? void 0 : _options.templateParameters) === "function" ? (_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.templateParameters({}, {}, {}, {}) : _object_spread({}, (_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.templateParameters);
225
+ var hackParameters = typeof (options === null || options === void 0 ? void 0 : options.templateParameters) === "function" ? options === null || options === void 0 ? void 0 : options.templateParameters({}, {}, {}, {}) : _object_spread({}, options === null || options === void 0 ? void 0 : options.templateParameters);
227
226
  var templateContent = documentEntry(
228
227
  entry.entryName,
229
228
  // options,
@@ -47,13 +47,12 @@ export var routerPlugin = function() {
47
47
  },
48
48
  modifyEntryImports: function modifyEntryImports(param) {
49
49
  var entrypoint = param.entrypoint, imports = param.imports;
50
- var _runtimeConfig;
51
50
  var entryName = entrypoint.entryName, isMainEntry = entrypoint.isMainEntry, fileSystemRoutes = entrypoint.fileSystemRoutes;
52
51
  var userConfig = api.useResolvedConfigContext();
53
52
  var packageName = api.useAppContext().packageName;
54
53
  var runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
55
54
  runtimeConfigMap.set(entryName, runtimeConfig);
56
- if ((_runtimeConfig = runtimeConfig) === null || _runtimeConfig === void 0 ? void 0 : _runtimeConfig.router) {
55
+ if (runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.router) {
57
56
  if (!isV5(userConfig)) {
58
57
  imports.push({
59
58
  value: "@modern-js/runtime/plugins",