@modern-js/runtime 2.48.6 → 2.49.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 (41) hide show
  1. package/dist/cjs/core/compatible.js +3 -2
  2. package/dist/cjs/router/runtime/plugin.node.js +1 -3
  3. package/dist/cjs/ssr/cli/index.js +10 -9
  4. package/dist/cjs/ssr/react/prerender/index.js +3 -1
  5. package/dist/cjs/ssr/serverRender/renderToStream/index.js +2 -2
  6. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +46 -47
  7. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.worker.js +6 -1
  8. package/dist/cjs/ssr/serverRender/tracker.js +14 -6
  9. package/dist/esm/core/compatible.js +3 -2
  10. package/dist/esm/router/runtime/plugin.node.js +1 -3
  11. package/dist/esm/ssr/cli/index.js +10 -9
  12. package/dist/esm/ssr/react/prerender/index.js +3 -1
  13. package/dist/esm/ssr/serverRender/renderToStream/index.js +3 -3
  14. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +47 -48
  15. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.worker.js +6 -1
  16. package/dist/esm/ssr/serverRender/tracker.js +16 -6
  17. package/dist/esm-node/core/compatible.js +3 -2
  18. package/dist/esm-node/router/runtime/plugin.node.js +1 -3
  19. package/dist/esm-node/ssr/cli/index.js +10 -9
  20. package/dist/esm-node/ssr/react/prerender/index.js +3 -1
  21. package/dist/esm-node/ssr/serverRender/renderToStream/index.js +2 -2
  22. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +46 -47
  23. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.worker.js +6 -1
  24. package/dist/esm-node/ssr/serverRender/tracker.js +14 -6
  25. package/dist/types/ssr/serverRender/renderToStream/index.d.ts +1 -1
  26. package/dist/types/ssr/serverRender/renderToStream/renderToPipe.d.ts +3 -4
  27. package/dist/types/ssr/serverRender/renderToStream/renderToPipe.worker.d.ts +5 -1
  28. package/dist/types/ssr/serverRender/tracker.d.ts +1 -1
  29. package/package.json +10 -11
  30. package/dist/cjs/router/runtime/fetch.js +0 -29
  31. package/dist/cjs/router/runtime/fetch.node.js +0 -28
  32. package/dist/cjs/router/runtime/fetch.worker.js +0 -29
  33. package/dist/esm/router/runtime/fetch.js +0 -5
  34. package/dist/esm/router/runtime/fetch.node.js +0 -4
  35. package/dist/esm/router/runtime/fetch.worker.js +0 -5
  36. package/dist/esm-node/router/runtime/fetch.js +0 -5
  37. package/dist/esm-node/router/runtime/fetch.node.js +0 -4
  38. package/dist/esm-node/router/runtime/fetch.worker.js +0 -5
  39. package/dist/types/router/runtime/fetch.d.ts +0 -2
  40. package/dist/types/router/runtime/fetch.node.d.ts +0 -2
  41. package/dist/types/router/runtime/fetch.worker.d.ts +0 -2
@@ -228,8 +228,9 @@ const bootstrap = async (BootApp, id, root, ReactDOM) => {
228
228
  const redirectUrl = result.headers.get("Location") || "/";
229
229
  const { ssrContext } = context;
230
230
  if (ssrContext) {
231
- ssrContext.res.statusCode = status;
232
- ssrContext.res.setHeader("Location", redirectUrl);
231
+ var _ssrContext_res;
232
+ ssrContext.res && (ssrContext.res.statusCode = status);
233
+ (_ssrContext_res = ssrContext.res) === null || _ssrContext_res === void 0 ? void 0 : _ssrContext_res.setHeader("Location", redirectUrl);
233
234
  ssrContext.redirection = ssrContext.redirection || {};
234
235
  ssrContext.redirection.status = status;
235
236
  ssrContext.redirection.url = redirectUrl;
@@ -44,9 +44,7 @@ var import_time = require("@modern-js/runtime-utils/time");
44
44
  var import_constants = require("@modern-js/utils/universal/constants");
45
45
  var import_core = require("../../core");
46
46
  var import_utils = require("./utils");
47
- var import_fetch = require("./fetch");
48
47
  var import_hooks = require("./hooks");
49
- (0, import_fetch.installGlobals)();
50
48
  function createFetchRequest(req) {
51
49
  const origin = `${req.protocol}://${req.host}`;
52
50
  const url = new URL(req.originalUrl || req.url, origin);
@@ -112,7 +110,7 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
112
110
  requestContext
113
111
  });
114
112
  const cost = end();
115
- reporter.reportTiming(import_constants.LOADER_REPORTER_NAME, cost);
113
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportTiming(import_constants.LOADER_REPORTER_NAME, cost);
116
114
  serverTiming.addServeTiming(import_constants.LOADER_REPORTER_NAME, cost);
117
115
  if (routerContext instanceof Response) {
118
116
  return routerContext;
@@ -43,7 +43,7 @@ const hasStringSSREntry = (userConfig) => {
43
43
  }
44
44
  if ((server === null || server === void 0 ? void 0 : server.ssrByEntries) && typeof server.ssrByEntries === "object") {
45
45
  for (const name of Object.keys(server.ssrByEntries)) {
46
- if (!isStreaming(server.ssrByEntries[name])) {
46
+ if (server.ssrByEntries[name] && !isStreaming(server.ssrByEntries[name])) {
47
47
  return true;
48
48
  }
49
49
  }
@@ -65,26 +65,27 @@ const ssrPlugin = () => ({
65
65
  return {
66
66
  config() {
67
67
  const appContext = api.useAppContext();
68
+ const userConfig = api.useConfigContext();
68
69
  pluginsExportsUtils = (0, import_utils.createRuntimeExportsUtils)(appContext.internalDirectory, "plugins");
69
70
  const { bundlerType = "webpack" } = api.useAppContext();
70
71
  const babelHandler = (() => {
71
72
  if (bundlerType === "webpack") {
72
73
  return (config) => {
73
74
  var _config_plugins;
74
- const userConfig = api.useResolvedConfigContext();
75
+ const userConfig2 = api.useResolvedConfigContext();
75
76
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(import_path.default.join(__dirname, "./babel-plugin-ssr-loader-id"));
76
- if ((0, import_utils.isUseSSRBundle)(userConfig) && checkUseStringSSR(userConfig)) {
77
+ if ((0, import_utils.isUseSSRBundle)(userConfig2) && checkUseStringSSR(userConfig2)) {
77
78
  var _config_plugins1;
78
79
  (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
79
80
  }
80
81
  };
81
82
  } else if (bundlerType === "rspack") {
82
83
  return (config) => {
83
- const userConfig = api.useResolvedConfigContext();
84
- if ((0, import_utils.isUseSSRBundle)(userConfig)) {
84
+ const userConfig2 = api.useResolvedConfigContext();
85
+ if ((0, import_utils.isUseSSRBundle)(userConfig2)) {
85
86
  var _config_plugins;
86
87
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(import_path.default.join(__dirname, "./babel-plugin-ssr-loader-id"));
87
- if (checkUseStringSSR(userConfig)) {
88
+ if (checkUseStringSSR(userConfig2)) {
88
89
  var _config_plugins1;
89
90
  (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
90
91
  }
@@ -105,8 +106,8 @@ const ssrPlugin = () => ({
105
106
  },
106
107
  tools: {
107
108
  bundlerChain(chain, { isServer, isServiceWorker }) {
108
- const userConfig = api.useResolvedConfigContext();
109
- if ((0, import_utils.isUseSSRBundle)(userConfig) && !isServer && !isServiceWorker && checkUseStringSSR(userConfig)) {
109
+ const userConfig2 = api.useResolvedConfigContext();
110
+ if ((0, import_utils.isUseSSRBundle)(userConfig2) && !isServer && !isServiceWorker && checkUseStringSSR(userConfig2)) {
110
111
  const LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
111
112
  chain.plugin("loadable").use(LoadableBundlerPlugin, [
112
113
  {
@@ -115,7 +116,7 @@ const ssrPlugin = () => ({
115
116
  ]);
116
117
  }
117
118
  },
118
- babel: babelHandler
119
+ babel: (0, import_utils.isUseSSRBundle)(userConfig) && checkUseStringSSR(userConfig) ? babelHandler : void 0
119
120
  }
120
121
  };
121
122
  },
@@ -99,7 +99,9 @@ function factory(Component) {
99
99
  if (!validate) {
100
100
  throw new Error("invalid props, check usage");
101
101
  }
102
- console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
102
+ if (process.env.NODE_ENV === "development") {
103
+ console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
104
+ }
103
105
  return (0, import_react.createElement)(Component, {
104
106
  ...newProps
105
107
  });
@@ -49,7 +49,7 @@ const render = ({ App, context, config }) => {
49
49
  })
50
50
  });
51
51
  const { tracker } = ssrContext;
52
- const pipe = (0, import_renderToPipe.default)(rootElement, context, config, {
52
+ const stream = (0, import_renderToPipe.default)(rootElement, context, config, {
53
53
  onShellReady() {
54
54
  const cost = end();
55
55
  tracker.trackTiming(import_tracker.SSRTimings.RENDER_SHELL, cost);
@@ -65,7 +65,7 @@ const render = ({ App, context, config }) => {
65
65
  tracker.trackError(import_tracker.SSRErrors.RENDER_STREAM, error);
66
66
  }
67
67
  });
68
- return pipe;
68
+ return stream;
69
69
  });
70
70
  };
71
71
  // Annotate the CommonJS export names for ESM import in node:
@@ -36,58 +36,57 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
36
36
  const onReady = ((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.isSpider) ? "onAllReady" : "onShellReady";
37
37
  const { ssrContext } = context;
38
38
  const chunkVec = [];
39
- const forUserPipe = (stream) => {
40
- return new Promise((resolve) => {
41
- let renderToPipeableStream;
42
- try {
43
- ({ renderToPipeableStream } = require("react-dom/server"));
44
- } catch (e) {
45
- }
46
- const { pipe } = renderToPipeableStream(rootElement, {
47
- ...options,
48
- nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
49
- [onReady]() {
50
- (0, import_template.getTemplates)(context, import_types.RenderLevel.SERVER_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
51
- var _options_onShellReady;
52
- options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
53
- const injectableTransform = new import_stream.Transform({
54
- transform(chunk, _encoding, callback) {
55
- try {
56
- if (shellChunkStatus !== 1) {
57
- chunkVec.push(chunk.toString());
58
- let concatedChunk = chunkVec.join("");
59
- if (concatedChunk.endsWith(import_common.ESCAPED_SHELL_STREAM_END_MARK)) {
60
- concatedChunk = concatedChunk.replace(import_common.ESCAPED_SHELL_STREAM_END_MARK, "");
61
- shellChunkStatus = 1;
62
- this.push(`${shellBefore}${concatedChunk}${shellAfter}`);
63
- }
64
- } else {
65
- this.push(chunk);
66
- }
67
- callback();
68
- } catch (e) {
69
- if (e instanceof Error) {
70
- callback(e);
71
- } else {
72
- callback(new Error("Received unkown error when streaming"));
39
+ const forUserPipe = new Promise((resolve) => {
40
+ let renderToPipeableStream;
41
+ try {
42
+ ({ renderToPipeableStream } = require("react-dom/server"));
43
+ } catch (e) {
44
+ }
45
+ const { pipe } = renderToPipeableStream(rootElement, {
46
+ ...options,
47
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
48
+ [onReady]() {
49
+ (0, import_template.getTemplates)(context, import_types.RenderLevel.SERVER_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
50
+ var _options_onShellReady;
51
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
52
+ const injectableTransform = new import_stream.Transform({
53
+ transform(chunk, _encoding, callback) {
54
+ try {
55
+ if (shellChunkStatus !== 1) {
56
+ chunkVec.push(chunk.toString());
57
+ let concatedChunk = chunkVec.join("");
58
+ if (concatedChunk.endsWith(import_common.ESCAPED_SHELL_STREAM_END_MARK)) {
59
+ concatedChunk = concatedChunk.replace(import_common.ESCAPED_SHELL_STREAM_END_MARK, "");
60
+ shellChunkStatus = 1;
61
+ this.push(`${shellBefore}${concatedChunk}${shellAfter}`);
73
62
  }
63
+ } else {
64
+ this.push(chunk);
65
+ }
66
+ callback();
67
+ } catch (e) {
68
+ if (e instanceof Error) {
69
+ callback(e);
70
+ } else {
71
+ callback(new Error("Received unkown error when streaming"));
74
72
  }
75
73
  }
76
- });
77
- resolve(pipe(injectableTransform).pipe(stream));
74
+ }
78
75
  });
79
- },
80
- onShellError(error) {
81
- (0, import_template.getTemplates)(context, import_types.RenderLevel.CLIENT_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
82
- var _options_onShellError;
83
- const fallbackHtml = `${shellBefore}${shellAfter}`;
84
- resolve(fallbackHtml);
85
- options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
86
- });
87
- }
88
- });
76
+ pipe(injectableTransform);
77
+ resolve(injectableTransform);
78
+ });
79
+ },
80
+ onShellError(error) {
81
+ (0, import_template.getTemplates)(context, import_types.RenderLevel.CLIENT_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
82
+ var _options_onShellError;
83
+ const fallbackHtml = `${shellBefore}${shellAfter}`;
84
+ resolve(fallbackHtml);
85
+ options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
86
+ });
87
+ }
89
88
  });
90
- };
89
+ });
91
90
  return forUserPipe;
92
91
  }
93
92
  var renderToPipe_default = renderToPipe;
@@ -42,7 +42,7 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
42
42
  }
43
43
  const { shellAfter, shellBefore } = await (0, import_template.getTemplates)(context, import_types.RenderLevel.SERVER_RENDER, pluginConfig);
44
44
  try {
45
- var _context_ssrContext;
45
+ var _options_onShellReady, _context_ssrContext;
46
46
  const readableOriginal = await renderToReadableStream(rootElement, {
47
47
  ...options,
48
48
  nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
@@ -51,6 +51,11 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
51
51
  options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, error);
52
52
  }
53
53
  });
54
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
55
+ readableOriginal.allReady.then(() => {
56
+ var _options_onAllReady;
57
+ options === null || options === void 0 ? void 0 : (_options_onAllReady = options.onAllReady) === null || _options_onAllReady === void 0 ? void 0 : _options_onAllReady.call(options);
58
+ });
54
59
  if ((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.isSpider) {
55
60
  await readableOriginal.allReady;
56
61
  }
@@ -78,21 +78,29 @@ const timings = {
78
78
  logger: "App run useLoader cost = %d ms"
79
79
  }
80
80
  };
81
- function createSSRTracker({ reporter, serverTiming, logger }) {
81
+ function getHeadersWithoutCookie(headers) {
82
+ const _headers = {
83
+ ...headers,
84
+ cookie: void 0
85
+ };
86
+ delete _headers.cookie;
87
+ return _headers;
88
+ }
89
+ function createSSRTracker({ reporter, serverTiming, logger, request }) {
82
90
  const tracker = {
83
91
  get sessionId() {
84
- return reporter.sessionId;
92
+ return reporter === null || reporter === void 0 ? void 0 : reporter.sessionId;
85
93
  },
86
94
  trackError(key, e) {
87
95
  const { reporter: reporterContent, logger: loggerContent } = errors[key];
88
- reporterContent && reporter.reportError(`SSR Error - ${reporterContent}`, e);
89
- loggerContent && logger.error(loggerContent, e);
96
+ reporterContent && (reporter === null || reporter === void 0 ? void 0 : reporter.reportError(`SSR Error - ${reporterContent}`, e));
97
+ loggerContent && logger.error(`SSR Error - ${loggerContent}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, request.pathname, getHeadersWithoutCookie(request.headers));
90
98
  },
91
99
  trackTiming(key, cost) {
92
100
  const { reporter: reporterName, serverTiming: serverTimingName, logger: loggerName } = timings[key];
93
- reporterName && reporter.reportTiming(reporterName, cost);
101
+ reporterName && (reporter === null || reporter === void 0 ? void 0 : reporter.reportTiming(reporterName, cost));
94
102
  serverTimingName && serverTiming.addServeTiming(serverTimingName, cost);
95
- loggerName && logger.debug(loggerName, cost);
103
+ loggerName && logger.debug(`SSR Debug - ${loggerName}, req.url = %s`, cost, request.pathname);
96
104
  }
97
105
  };
98
106
  return tracker;
@@ -229,8 +229,9 @@ var bootstrap = function() {
229
229
  var redirectUrl = result.headers.get("Location") || "/";
230
230
  var ssrContext = context.ssrContext;
231
231
  if (ssrContext) {
232
- ssrContext.res.statusCode = status;
233
- ssrContext.res.setHeader("Location", redirectUrl);
232
+ var _ssrContext_res;
233
+ ssrContext.res && (ssrContext.res.statusCode = status);
234
+ (_ssrContext_res = ssrContext.res) === null || _ssrContext_res === void 0 ? void 0 : _ssrContext_res.setHeader("Location", redirectUrl);
234
235
  ssrContext.redirection = ssrContext.redirection || {};
235
236
  ssrContext.redirection.status = status;
236
237
  ssrContext.redirection.url = redirectUrl;
@@ -15,9 +15,7 @@ import { time } from "@modern-js/runtime-utils/time";
15
15
  import { LOADER_REPORTER_NAME } from "@modern-js/utils/universal/constants";
16
16
  import { RuntimeReactContext } from "../../core";
17
17
  import { renderRoutes, urlJoin } from "./utils";
18
- import { installGlobals } from "./fetch";
19
18
  import { modifyRoutes as modifyRoutesHook } from "./hooks";
20
- installGlobals();
21
19
  function createFetchRequest(req) {
22
20
  var origin = "".concat(req.protocol, "://").concat(req.host);
23
21
  var url = new URL(req.originalUrl || req.url, origin);
@@ -132,7 +130,7 @@ var routerPlugin = function(param) {
132
130
  case 1:
133
131
  routerContext = _state.sent();
134
132
  cost = end();
135
- reporter.reportTiming(LOADER_REPORTER_NAME, cost);
133
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportTiming(LOADER_REPORTER_NAME, cost);
136
134
  serverTiming.addServeTiming(LOADER_REPORTER_NAME, cost);
137
135
  if (_instanceof(routerContext, Response)) {
138
136
  return [
@@ -16,7 +16,7 @@ var hasStringSSREntry = function(userConfig) {
16
16
  try {
17
17
  for (var _iterator = Object.keys(server.ssrByEntries)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
18
18
  var name = _step.value;
19
- if (!isStreaming(server.ssrByEntries[name])) {
19
+ if (server.ssrByEntries[name] && !isStreaming(server.ssrByEntries[name])) {
20
20
  return true;
21
21
  }
22
22
  }
@@ -53,26 +53,27 @@ var ssrPlugin = function() {
53
53
  return {
54
54
  config: function config() {
55
55
  var appContext = api.useAppContext();
56
+ var userConfig = api.useConfigContext();
56
57
  pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
57
58
  var _api_useAppContext = api.useAppContext(), _api_useAppContext_bundlerType = _api_useAppContext.bundlerType, bundlerType = _api_useAppContext_bundlerType === void 0 ? "webpack" : _api_useAppContext_bundlerType;
58
59
  var babelHandler = function() {
59
60
  if (bundlerType === "webpack") {
60
61
  return function(config2) {
61
62
  var _config_plugins;
62
- var userConfig = api.useResolvedConfigContext();
63
+ var userConfig2 = api.useResolvedConfigContext();
63
64
  (_config_plugins = config2.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
64
- if (isUseSSRBundle(userConfig) && checkUseStringSSR(userConfig)) {
65
+ if (isUseSSRBundle(userConfig2) && checkUseStringSSR(userConfig2)) {
65
66
  var _config_plugins1;
66
67
  (_config_plugins1 = config2.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
67
68
  }
68
69
  };
69
70
  } else if (bundlerType === "rspack") {
70
71
  return function(config2) {
71
- var userConfig = api.useResolvedConfigContext();
72
- if (isUseSSRBundle(userConfig)) {
72
+ var userConfig2 = api.useResolvedConfigContext();
73
+ if (isUseSSRBundle(userConfig2)) {
73
74
  var _config_plugins;
74
75
  (_config_plugins = config2.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
75
- if (checkUseStringSSR(userConfig)) {
76
+ if (checkUseStringSSR(userConfig2)) {
76
77
  var _config_plugins1;
77
78
  (_config_plugins1 = config2.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
78
79
  }
@@ -95,8 +96,8 @@ var ssrPlugin = function() {
95
96
  tools: {
96
97
  bundlerChain: function bundlerChain(chain, param) {
97
98
  var isServer = param.isServer, isServiceWorker = param.isServiceWorker;
98
- var userConfig = api.useResolvedConfigContext();
99
- if (isUseSSRBundle(userConfig) && !isServer && !isServiceWorker && checkUseStringSSR(userConfig)) {
99
+ var userConfig2 = api.useResolvedConfigContext();
100
+ if (isUseSSRBundle(userConfig2) && !isServer && !isServiceWorker && checkUseStringSSR(userConfig2)) {
100
101
  var LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
101
102
  chain.plugin("loadable").use(LoadableBundlerPlugin, [
102
103
  {
@@ -105,7 +106,7 @@ var ssrPlugin = function() {
105
106
  ]);
106
107
  }
107
108
  },
108
- babel: babelHandler
109
+ babel: isUseSSRBundle(userConfig) && checkUseStringSSR(userConfig) ? babelHandler : void 0
109
110
  }
110
111
  };
111
112
  },
@@ -70,7 +70,9 @@ function factory(Component) {
70
70
  if (!validate) {
71
71
  throw new Error("invalid props, check usage");
72
72
  }
73
- console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
73
+ if (process.env.NODE_ENV === "development") {
74
+ console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
75
+ }
74
76
  return createElement(Component, _object_spread({}, newProps));
75
77
  };
76
78
  _create_class(Spr2, null, [
@@ -12,7 +12,7 @@ var render = function(param) {
12
12
  throw new Error('The "ssrContext" must not be undefined, but received undefined');
13
13
  }
14
14
  return run(ssrContext.request.headers, /* @__PURE__ */ _async_to_generator(function() {
15
- var end, rootElement, tracker, pipe;
15
+ var end, rootElement, tracker, stream;
16
16
  return _ts_generator(this, function(_state) {
17
17
  end = time();
18
18
  rootElement = createElement(App, {
@@ -21,7 +21,7 @@ var render = function(param) {
21
21
  })
22
22
  });
23
23
  tracker = ssrContext.tracker;
24
- pipe = renderToPipe(rootElement, context, config, {
24
+ stream = renderToPipe(rootElement, context, config, {
25
25
  onShellReady: function onShellReady() {
26
26
  var cost = end();
27
27
  tracker.trackTiming(SSRTimings.RENDER_SHELL, cost);
@@ -39,7 +39,7 @@ var render = function(param) {
39
39
  });
40
40
  return [
41
41
  2,
42
- pipe
42
+ stream
43
43
  ];
44
44
  });
45
45
  }));
@@ -17,58 +17,57 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
17
17
  var onReady = ((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.isSpider) ? "onAllReady" : "onShellReady";
18
18
  var ssrContext = context.ssrContext;
19
19
  var chunkVec = [];
20
- var forUserPipe = function(stream) {
21
- return new Promise(function(resolve) {
22
- var renderToPipeableStream;
23
- try {
24
- renderToPipeableStream = require("react-dom/server").renderToPipeableStream;
25
- } catch (e) {
26
- }
27
- var _obj;
28
- var pipe = renderToPipeableStream(rootElement, _object_spread_props(_object_spread({}, options), (_obj = {
29
- nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce
30
- }, _define_property(_obj, onReady, function() {
31
- getTemplates(context, RenderLevel.SERVER_RENDER, pluginConfig).then(function(param) {
32
- var shellAfter = param.shellAfter, shellBefore = param.shellBefore;
33
- var _options_onShellReady;
34
- options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
35
- var injectableTransform = new Transform({
36
- transform: function transform(chunk, _encoding, callback) {
37
- try {
38
- if (shellChunkStatus !== 1) {
39
- chunkVec.push(chunk.toString());
40
- var concatedChunk = chunkVec.join("");
41
- if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
42
- concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, "");
43
- shellChunkStatus = 1;
44
- this.push("".concat(shellBefore).concat(concatedChunk).concat(shellAfter));
45
- }
46
- } else {
47
- this.push(chunk);
48
- }
49
- callback();
50
- } catch (e) {
51
- if (_instanceof(e, Error)) {
52
- callback(e);
53
- } else {
54
- callback(new Error("Received unkown error when streaming"));
20
+ var forUserPipe = new Promise(function(resolve) {
21
+ var renderToPipeableStream;
22
+ try {
23
+ renderToPipeableStream = require("react-dom/server").renderToPipeableStream;
24
+ } catch (e) {
25
+ }
26
+ var _obj;
27
+ var pipe = renderToPipeableStream(rootElement, _object_spread_props(_object_spread({}, options), (_obj = {
28
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce
29
+ }, _define_property(_obj, onReady, function() {
30
+ getTemplates(context, RenderLevel.SERVER_RENDER, pluginConfig).then(function(param) {
31
+ var shellAfter = param.shellAfter, shellBefore = param.shellBefore;
32
+ var _options_onShellReady;
33
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
34
+ var injectableTransform = new Transform({
35
+ transform: function transform(chunk, _encoding, callback) {
36
+ try {
37
+ if (shellChunkStatus !== 1) {
38
+ chunkVec.push(chunk.toString());
39
+ var concatedChunk = chunkVec.join("");
40
+ if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
41
+ concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, "");
42
+ shellChunkStatus = 1;
43
+ this.push("".concat(shellBefore).concat(concatedChunk).concat(shellAfter));
55
44
  }
45
+ } else {
46
+ this.push(chunk);
47
+ }
48
+ callback();
49
+ } catch (e) {
50
+ if (_instanceof(e, Error)) {
51
+ callback(e);
52
+ } else {
53
+ callback(new Error("Received unkown error when streaming"));
56
54
  }
57
55
  }
58
- });
59
- resolve(pipe(injectableTransform).pipe(stream));
60
- });
61
- }), _define_property(_obj, "onShellError", function onShellError(error) {
62
- getTemplates(context, RenderLevel.CLIENT_RENDER, pluginConfig).then(function(param) {
63
- var shellAfter = param.shellAfter, shellBefore = param.shellBefore;
64
- var _options_onShellError;
65
- var fallbackHtml = "".concat(shellBefore).concat(shellAfter);
66
- resolve(fallbackHtml);
67
- options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
56
+ }
68
57
  });
69
- }), _obj))).pipe;
70
- });
71
- };
58
+ pipe(injectableTransform);
59
+ resolve(injectableTransform);
60
+ });
61
+ }), _define_property(_obj, "onShellError", function onShellError(error) {
62
+ getTemplates(context, RenderLevel.CLIENT_RENDER, pluginConfig).then(function(param) {
63
+ var shellAfter = param.shellAfter, shellBefore = param.shellBefore;
64
+ var _options_onShellError;
65
+ var fallbackHtml = "".concat(shellBefore).concat(shellAfter);
66
+ resolve(fallbackHtml);
67
+ options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
68
+ });
69
+ }), _obj))).pipe;
70
+ });
72
71
  return forUserPipe;
73
72
  }
74
73
  var renderToPipe_default = renderToPipe;
@@ -17,7 +17,7 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
17
17
  var ssrContext = context.ssrContext;
18
18
  var forUserPipe = function() {
19
19
  var _ref = _async_to_generator(function() {
20
- var renderToReadableStream, _ref2, shellAfter, shellBefore, _context_ssrContext, readableOriginal, reader, injectableStream, err, _ref1, shellAfter1, shellBefore1, fallbackHtml;
20
+ var renderToReadableStream, _ref2, shellAfter, shellBefore, _options_onShellReady, _context_ssrContext, readableOriginal, reader, injectableStream, err, _ref1, shellAfter1, shellBefore1, fallbackHtml;
21
21
  return _ts_generator(this, function(_state) {
22
22
  switch (_state.label) {
23
23
  case 0:
@@ -51,6 +51,11 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
51
51
  ];
52
52
  case 3:
53
53
  readableOriginal = _state.sent();
54
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
55
+ readableOriginal.allReady.then(function() {
56
+ var _options_onAllReady;
57
+ options === null || options === void 0 ? void 0 : (_options_onAllReady = options.onAllReady) === null || _options_onAllReady === void 0 ? void 0 : _options_onAllReady.call(options);
58
+ });
54
59
  if (!((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.isSpider))
55
60
  return [
56
61
  3,
@@ -1,4 +1,7 @@
1
1
  import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
+ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
4
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
2
5
  var SSRTimings;
3
6
  (function(SSRTimings2) {
4
7
  SSRTimings2[SSRTimings2["PRERENDER"] = 0] = "PRERENDER";
@@ -45,22 +48,29 @@ var timings = (_obj1 = {}, _define_property(_obj1, 0, {
45
48
  serverTiming: "use-loader",
46
49
  logger: "App run useLoader cost = %d ms"
47
50
  }), _obj1);
51
+ function getHeadersWithoutCookie(headers) {
52
+ var _headers = _object_spread_props(_object_spread({}, headers), {
53
+ cookie: void 0
54
+ });
55
+ delete _headers.cookie;
56
+ return _headers;
57
+ }
48
58
  function createSSRTracker(param) {
49
- var reporter = param.reporter, serverTiming = param.serverTiming, logger = param.logger;
59
+ var reporter = param.reporter, serverTiming = param.serverTiming, logger = param.logger, request = param.request;
50
60
  var tracker = {
51
61
  get sessionId() {
52
- return reporter.sessionId;
62
+ return reporter === null || reporter === void 0 ? void 0 : reporter.sessionId;
53
63
  },
54
64
  trackError: function trackError(key, e) {
55
65
  var _errors_key = errors[key], reporterContent = _errors_key.reporter, loggerContent = _errors_key.logger;
56
- reporterContent && reporter.reportError("SSR Error - ".concat(reporterContent), e);
57
- loggerContent && logger.error(loggerContent, e);
66
+ reporterContent && (reporter === null || reporter === void 0 ? void 0 : reporter.reportError("SSR Error - ".concat(reporterContent), e));
67
+ loggerContent && logger.error("SSR Error - ".concat(loggerContent, ", error = %s, req.url = %s, req.headers = %o"), _instanceof(e, Error) ? e.stack || e.message : e, request.pathname, getHeadersWithoutCookie(request.headers));
58
68
  },
59
69
  trackTiming: function trackTiming(key, cost) {
60
70
  var _timings_key = timings[key], reporterName = _timings_key.reporter, serverTimingName = _timings_key.serverTiming, loggerName = _timings_key.logger;
61
- reporterName && reporter.reportTiming(reporterName, cost);
71
+ reporterName && (reporter === null || reporter === void 0 ? void 0 : reporter.reportTiming(reporterName, cost));
62
72
  serverTimingName && serverTiming.addServeTiming(serverTimingName, cost);
63
- loggerName && logger.debug(loggerName, cost);
73
+ loggerName && logger.debug("SSR Debug - ".concat(loggerName, ", req.url = %s"), cost, request.pathname);
64
74
  }
65
75
  };
66
76
  return tracker;
@@ -193,8 +193,9 @@ const bootstrap = async (BootApp, id, root, ReactDOM) => {
193
193
  const redirectUrl = result.headers.get("Location") || "/";
194
194
  const { ssrContext } = context;
195
195
  if (ssrContext) {
196
- ssrContext.res.statusCode = status;
197
- ssrContext.res.setHeader("Location", redirectUrl);
196
+ var _ssrContext_res;
197
+ ssrContext.res && (ssrContext.res.statusCode = status);
198
+ (_ssrContext_res = ssrContext.res) === null || _ssrContext_res === void 0 ? void 0 : _ssrContext_res.setHeader("Location", redirectUrl);
198
199
  ssrContext.redirection = ssrContext.redirection || {};
199
200
  ssrContext.redirection.status = status;
200
201
  ssrContext.redirection.url = redirectUrl;
@@ -9,9 +9,7 @@ import { time } from "@modern-js/runtime-utils/time";
9
9
  import { LOADER_REPORTER_NAME } from "@modern-js/utils/universal/constants";
10
10
  import { RuntimeReactContext } from "../../core";
11
11
  import { renderRoutes, urlJoin } from "./utils";
12
- import { installGlobals } from "./fetch";
13
12
  import { modifyRoutes as modifyRoutesHook } from "./hooks";
14
- installGlobals();
15
13
  function createFetchRequest(req) {
16
14
  const origin = `${req.protocol}://${req.host}`;
17
15
  const url = new URL(req.originalUrl || req.url, origin);
@@ -77,7 +75,7 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
77
75
  requestContext
78
76
  });
79
77
  const cost = end();
80
- reporter.reportTiming(LOADER_REPORTER_NAME, cost);
78
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportTiming(LOADER_REPORTER_NAME, cost);
81
79
  serverTiming.addServeTiming(LOADER_REPORTER_NAME, cost);
82
80
  if (routerContext instanceof Response) {
83
81
  return routerContext;
@@ -9,7 +9,7 @@ const hasStringSSREntry = (userConfig) => {
9
9
  }
10
10
  if ((server === null || server === void 0 ? void 0 : server.ssrByEntries) && typeof server.ssrByEntries === "object") {
11
11
  for (const name of Object.keys(server.ssrByEntries)) {
12
- if (!isStreaming(server.ssrByEntries[name])) {
12
+ if (server.ssrByEntries[name] && !isStreaming(server.ssrByEntries[name])) {
13
13
  return true;
14
14
  }
15
15
  }
@@ -31,26 +31,27 @@ const ssrPlugin = () => ({
31
31
  return {
32
32
  config() {
33
33
  const appContext = api.useAppContext();
34
+ const userConfig = api.useConfigContext();
34
35
  pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
35
36
  const { bundlerType = "webpack" } = api.useAppContext();
36
37
  const babelHandler = (() => {
37
38
  if (bundlerType === "webpack") {
38
39
  return (config) => {
39
40
  var _config_plugins;
40
- const userConfig = api.useResolvedConfigContext();
41
+ const userConfig2 = api.useResolvedConfigContext();
41
42
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
42
- if (isUseSSRBundle(userConfig) && checkUseStringSSR(userConfig)) {
43
+ if (isUseSSRBundle(userConfig2) && checkUseStringSSR(userConfig2)) {
43
44
  var _config_plugins1;
44
45
  (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
45
46
  }
46
47
  };
47
48
  } else if (bundlerType === "rspack") {
48
49
  return (config) => {
49
- const userConfig = api.useResolvedConfigContext();
50
- if (isUseSSRBundle(userConfig)) {
50
+ const userConfig2 = api.useResolvedConfigContext();
51
+ if (isUseSSRBundle(userConfig2)) {
51
52
  var _config_plugins;
52
53
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
53
- if (checkUseStringSSR(userConfig)) {
54
+ if (checkUseStringSSR(userConfig2)) {
54
55
  var _config_plugins1;
55
56
  (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
56
57
  }
@@ -71,8 +72,8 @@ const ssrPlugin = () => ({
71
72
  },
72
73
  tools: {
73
74
  bundlerChain(chain, { isServer, isServiceWorker }) {
74
- const userConfig = api.useResolvedConfigContext();
75
- if (isUseSSRBundle(userConfig) && !isServer && !isServiceWorker && checkUseStringSSR(userConfig)) {
75
+ const userConfig2 = api.useResolvedConfigContext();
76
+ if (isUseSSRBundle(userConfig2) && !isServer && !isServiceWorker && checkUseStringSSR(userConfig2)) {
76
77
  const LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
77
78
  chain.plugin("loadable").use(LoadableBundlerPlugin, [
78
79
  {
@@ -81,7 +82,7 @@ const ssrPlugin = () => ({
81
82
  ]);
82
83
  }
83
84
  },
84
- babel: babelHandler
85
+ babel: isUseSSRBundle(userConfig) && checkUseStringSSR(userConfig) ? babelHandler : void 0
85
86
  }
86
87
  };
87
88
  },
@@ -66,7 +66,9 @@ function factory(Component) {
66
66
  if (!validate) {
67
67
  throw new Error("invalid props, check usage");
68
68
  }
69
- console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
69
+ if (process.env.NODE_ENV === "development") {
70
+ console.error("[Warn] PreRender has been deprecated, please use SSR Cache instead. reference to docs: https://modernjs.dev/guides/advanced-features/ssr.html");
71
+ }
70
72
  return createElement(Component, {
71
73
  ...newProps
72
74
  });
@@ -16,7 +16,7 @@ const render = ({ App, context, config }) => {
16
16
  })
17
17
  });
18
18
  const { tracker } = ssrContext;
19
- const pipe = renderToPipe(rootElement, context, config, {
19
+ const stream = renderToPipe(rootElement, context, config, {
20
20
  onShellReady() {
21
21
  const cost = end();
22
22
  tracker.trackTiming(SSRTimings.RENDER_SHELL, cost);
@@ -32,7 +32,7 @@ const render = ({ App, context, config }) => {
32
32
  tracker.trackError(SSRErrors.RENDER_STREAM, error);
33
33
  }
34
34
  });
35
- return pipe;
35
+ return stream;
36
36
  });
37
37
  };
38
38
  export {
@@ -13,58 +13,57 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
13
13
  const onReady = ((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.isSpider) ? "onAllReady" : "onShellReady";
14
14
  const { ssrContext } = context;
15
15
  const chunkVec = [];
16
- const forUserPipe = (stream) => {
17
- return new Promise((resolve) => {
18
- let renderToPipeableStream;
19
- try {
20
- ({ renderToPipeableStream } = require("react-dom/server"));
21
- } catch (e) {
22
- }
23
- const { pipe } = renderToPipeableStream(rootElement, {
24
- ...options,
25
- nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
26
- [onReady]() {
27
- getTemplates(context, RenderLevel.SERVER_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
28
- var _options_onShellReady;
29
- options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
30
- const injectableTransform = new Transform({
31
- transform(chunk, _encoding, callback) {
32
- try {
33
- if (shellChunkStatus !== 1) {
34
- chunkVec.push(chunk.toString());
35
- let concatedChunk = chunkVec.join("");
36
- if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
37
- concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, "");
38
- shellChunkStatus = 1;
39
- this.push(`${shellBefore}${concatedChunk}${shellAfter}`);
40
- }
41
- } else {
42
- this.push(chunk);
43
- }
44
- callback();
45
- } catch (e) {
46
- if (e instanceof Error) {
47
- callback(e);
48
- } else {
49
- callback(new Error("Received unkown error when streaming"));
16
+ const forUserPipe = new Promise((resolve) => {
17
+ let renderToPipeableStream;
18
+ try {
19
+ ({ renderToPipeableStream } = require("react-dom/server"));
20
+ } catch (e) {
21
+ }
22
+ const { pipe } = renderToPipeableStream(rootElement, {
23
+ ...options,
24
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
25
+ [onReady]() {
26
+ getTemplates(context, RenderLevel.SERVER_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
27
+ var _options_onShellReady;
28
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
29
+ const injectableTransform = new Transform({
30
+ transform(chunk, _encoding, callback) {
31
+ try {
32
+ if (shellChunkStatus !== 1) {
33
+ chunkVec.push(chunk.toString());
34
+ let concatedChunk = chunkVec.join("");
35
+ if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
36
+ concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, "");
37
+ shellChunkStatus = 1;
38
+ this.push(`${shellBefore}${concatedChunk}${shellAfter}`);
50
39
  }
40
+ } else {
41
+ this.push(chunk);
42
+ }
43
+ callback();
44
+ } catch (e) {
45
+ if (e instanceof Error) {
46
+ callback(e);
47
+ } else {
48
+ callback(new Error("Received unkown error when streaming"));
51
49
  }
52
50
  }
53
- });
54
- resolve(pipe(injectableTransform).pipe(stream));
51
+ }
55
52
  });
56
- },
57
- onShellError(error) {
58
- getTemplates(context, RenderLevel.CLIENT_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
59
- var _options_onShellError;
60
- const fallbackHtml = `${shellBefore}${shellAfter}`;
61
- resolve(fallbackHtml);
62
- options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
63
- });
64
- }
65
- });
53
+ pipe(injectableTransform);
54
+ resolve(injectableTransform);
55
+ });
56
+ },
57
+ onShellError(error) {
58
+ getTemplates(context, RenderLevel.CLIENT_RENDER, pluginConfig).then(({ shellAfter, shellBefore }) => {
59
+ var _options_onShellError;
60
+ const fallbackHtml = `${shellBefore}${shellAfter}`;
61
+ resolve(fallbackHtml);
62
+ options === null || options === void 0 ? void 0 : (_options_onShellError = options.onShellError) === null || _options_onShellError === void 0 ? void 0 : _options_onShellError.call(options, error);
63
+ });
64
+ }
66
65
  });
67
- };
66
+ });
68
67
  return forUserPipe;
69
68
  }
70
69
  var renderToPipe_default = renderToPipe;
@@ -19,7 +19,7 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
19
19
  }
20
20
  const { shellAfter, shellBefore } = await getTemplates(context, RenderLevel.SERVER_RENDER, pluginConfig);
21
21
  try {
22
- var _context_ssrContext;
22
+ var _options_onShellReady, _context_ssrContext;
23
23
  const readableOriginal = await renderToReadableStream(rootElement, {
24
24
  ...options,
25
25
  nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
@@ -28,6 +28,11 @@ function renderToPipe(rootElement, context, pluginConfig, options) {
28
28
  options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, error);
29
29
  }
30
30
  });
31
+ options === null || options === void 0 ? void 0 : (_options_onShellReady = options.onShellReady) === null || _options_onShellReady === void 0 ? void 0 : _options_onShellReady.call(options);
32
+ readableOriginal.allReady.then(() => {
33
+ var _options_onAllReady;
34
+ options === null || options === void 0 ? void 0 : (_options_onAllReady = options.onAllReady) === null || _options_onAllReady === void 0 ? void 0 : _options_onAllReady.call(options);
35
+ });
31
36
  if ((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.isSpider) {
32
37
  await readableOriginal.allReady;
33
38
  }
@@ -53,21 +53,29 @@ const timings = {
53
53
  logger: "App run useLoader cost = %d ms"
54
54
  }
55
55
  };
56
- function createSSRTracker({ reporter, serverTiming, logger }) {
56
+ function getHeadersWithoutCookie(headers) {
57
+ const _headers = {
58
+ ...headers,
59
+ cookie: void 0
60
+ };
61
+ delete _headers.cookie;
62
+ return _headers;
63
+ }
64
+ function createSSRTracker({ reporter, serverTiming, logger, request }) {
57
65
  const tracker = {
58
66
  get sessionId() {
59
- return reporter.sessionId;
67
+ return reporter === null || reporter === void 0 ? void 0 : reporter.sessionId;
60
68
  },
61
69
  trackError(key, e) {
62
70
  const { reporter: reporterContent, logger: loggerContent } = errors[key];
63
- reporterContent && reporter.reportError(`SSR Error - ${reporterContent}`, e);
64
- loggerContent && logger.error(loggerContent, e);
71
+ reporterContent && (reporter === null || reporter === void 0 ? void 0 : reporter.reportError(`SSR Error - ${reporterContent}`, e));
72
+ loggerContent && logger.error(`SSR Error - ${loggerContent}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, request.pathname, getHeadersWithoutCookie(request.headers));
65
73
  },
66
74
  trackTiming(key, cost) {
67
75
  const { reporter: reporterName, serverTiming: serverTimingName, logger: loggerName } = timings[key];
68
- reporterName && reporter.reportTiming(reporterName, cost);
76
+ reporterName && (reporter === null || reporter === void 0 ? void 0 : reporter.reportTiming(reporterName, cost));
69
77
  serverTimingName && serverTiming.addServeTiming(serverTimingName, cost);
70
- loggerName && logger.debug(loggerName, cost);
78
+ loggerName && logger.debug(`SSR Debug - ${loggerName}, req.url = %s`, cost, request.pathname);
71
79
  }
72
80
  };
73
81
  return tracker;
@@ -1,3 +1,3 @@
1
1
  /// <reference types="node" />
2
2
  import { ServerRenderOptions } from '../types';
3
- export declare const render: ({ App, context, config }: ServerRenderOptions) => Promise<import("./renderToPipe").Pipe<import("stream").Writable>>;
3
+ export declare const render: ({ App, context, config }: ServerRenderOptions) => Promise<string | import("stream").Readable>;
@@ -1,8 +1,7 @@
1
- /// <reference types="node" />
2
1
  /// <reference types="react" />
3
- import { Writable } from 'stream';
2
+ /// <reference types="node" />
3
+ import { Readable } from 'stream';
4
4
  import type { RenderToPipeableStreamOptions } from 'react-dom/server';
5
5
  import { RuntimeContext, SSRPluginConfig } from '../types';
6
- export type Pipe<T extends Writable> = (output: T) => Promise<T | string>;
7
- declare function renderToPipe(rootElement: React.ReactElement, context: RuntimeContext, pluginConfig: SSRPluginConfig, options?: RenderToPipeableStreamOptions): Pipe<Writable>;
6
+ declare function renderToPipe(rootElement: React.ReactElement, context: RuntimeContext, pluginConfig: SSRPluginConfig, options?: RenderToPipeableStreamOptions): Promise<string | Readable>;
8
7
  export default renderToPipe;
@@ -4,5 +4,9 @@ import type { Writable } from 'stream';
4
4
  import type { RenderToReadableStreamOptions } from 'react-dom/server';
5
5
  import { RuntimeContext, SSRPluginConfig } from '../types';
6
6
  export type Pipe<T extends Writable> = (output: T) => Promise<T | string>;
7
- declare function renderToPipe(rootElement: React.ReactElement, context: RuntimeContext, pluginConfig: SSRPluginConfig, options?: RenderToReadableStreamOptions): Promise<string | ReadableStream<any>>;
7
+ declare function renderToPipe(rootElement: React.ReactElement, context: RuntimeContext, pluginConfig: SSRPluginConfig, options?: RenderToReadableStreamOptions & {
8
+ onShellReady?: () => void;
9
+ onAllReady?: () => void;
10
+ onShellError?: (e: unknown) => void;
11
+ }): Promise<string | ReadableStream<any>>;
8
12
  export default renderToPipe;
@@ -13,7 +13,7 @@ export declare enum SSRErrors {
13
13
  RENDER_STREAM = 3,
14
14
  RENDER_SHELL = 4
15
15
  }
16
- export declare function createSSRTracker({ reporter, serverTiming, logger, }: BaseSSRServerContext): {
16
+ export declare function createSSRTracker({ reporter, serverTiming, logger, request, }: BaseSSRServerContext): {
17
17
  readonly sessionId: string | undefined;
18
18
  trackError(key: SSRErrors, e: Error): void;
19
19
  trackTiming(key: SSRTimings, cost: number): void;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.48.6",
18
+ "version": "2.49.1",
19
19
  "engines": {
20
20
  "node": ">=14.17.6"
21
21
  },
@@ -159,7 +159,6 @@
159
159
  "@modern-js-reduck/plugin-immutable": "^1.1.10",
160
160
  "@modern-js-reduck/react": "^1.1.10",
161
161
  "@modern-js-reduck/store": "^1.1.10",
162
- "@remix-run/node": "^1.12.0",
163
162
  "@types/loadable__component": "^5.13.4",
164
163
  "@types/react-helmet": "^6.1.2",
165
164
  "@types/styled-components": "^5.1.14",
@@ -171,10 +170,10 @@
171
170
  "react-side-effect": "^2.1.1",
172
171
  "styled-components": "^5.3.1",
173
172
  "@swc/helpers": "0.5.3",
174
- "@modern-js/types": "2.48.6",
175
- "@modern-js/plugin": "2.48.6",
176
- "@modern-js/utils": "2.48.6",
177
- "@modern-js/runtime-utils": "2.48.6"
173
+ "@modern-js/plugin": "2.49.1",
174
+ "@modern-js/utils": "2.49.1",
175
+ "@modern-js/types": "2.49.1",
176
+ "@modern-js/runtime-utils": "2.49.1"
178
177
  },
179
178
  "peerDependencies": {
180
179
  "react": ">=17",
@@ -195,11 +194,11 @@
195
194
  "ts-jest": "^29.1.0",
196
195
  "typescript": "^5",
197
196
  "webpack": "^5.91.0",
198
- "@modern-js/app-tools": "2.48.6",
199
- "@modern-js/core": "2.48.6",
200
- "@modern-js/server-core": "2.48.6",
201
- "@scripts/jest-config": "2.48.6",
202
- "@scripts/build": "2.48.6"
197
+ "@modern-js/app-tools": "2.49.1",
198
+ "@modern-js/core": "2.49.1",
199
+ "@modern-js/server-core": "2.49.1",
200
+ "@scripts/build": "2.49.1",
201
+ "@scripts/jest-config": "2.49.1"
203
202
  },
204
203
  "sideEffects": false,
205
204
  "publishConfig": {
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var fetch_exports = {};
20
- __export(fetch_exports, {
21
- installGlobals: () => installGlobals
22
- });
23
- module.exports = __toCommonJS(fetch_exports);
24
- function installGlobals() {
25
- }
26
- // Annotate the CommonJS export names for ESM import in node:
27
- 0 && (module.exports = {
28
- installGlobals
29
- });
@@ -1,28 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var fetch_node_exports = {};
20
- __export(fetch_node_exports, {
21
- installGlobals: () => import_node.installGlobals
22
- });
23
- module.exports = __toCommonJS(fetch_node_exports);
24
- var import_node = require("@remix-run/node");
25
- // Annotate the CommonJS export names for ESM import in node:
26
- 0 && (module.exports = {
27
- installGlobals
28
- });
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var fetch_worker_exports = {};
20
- __export(fetch_worker_exports, {
21
- installGlobals: () => installGlobals
22
- });
23
- module.exports = __toCommonJS(fetch_worker_exports);
24
- function installGlobals() {
25
- }
26
- // Annotate the CommonJS export names for ESM import in node:
27
- 0 && (module.exports = {
28
- installGlobals
29
- });
@@ -1,5 +0,0 @@
1
- function installGlobals() {
2
- }
3
- export {
4
- installGlobals
5
- };
@@ -1,4 +0,0 @@
1
- import { installGlobals } from "@remix-run/node";
2
- export {
3
- installGlobals
4
- };
@@ -1,5 +0,0 @@
1
- function installGlobals() {
2
- }
3
- export {
4
- installGlobals
5
- };
@@ -1,5 +0,0 @@
1
- function installGlobals() {
2
- }
3
- export {
4
- installGlobals
5
- };
@@ -1,4 +0,0 @@
1
- import { installGlobals } from "@remix-run/node";
2
- export {
3
- installGlobals
4
- };
@@ -1,5 +0,0 @@
1
- function installGlobals() {
2
- }
3
- export {
4
- installGlobals
5
- };
@@ -1,2 +0,0 @@
1
- declare function installGlobals(): void;
2
- export { installGlobals };
@@ -1,2 +0,0 @@
1
- import { installGlobals } from '@remix-run/node';
2
- export { installGlobals };
@@ -1,2 +0,0 @@
1
- declare function installGlobals(): void;
2
- export { installGlobals };