@modern-js/server-core 2.56.2 → 2.57.0

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 (49) hide show
  1. package/dist/cjs/adapters/node/helper/loadConfig.js +2 -2
  2. package/dist/cjs/adapters/node/plugins/resource.js +4 -1
  3. package/dist/cjs/{plugins/customServer/loader.js → helper.js} +6 -5
  4. package/dist/cjs/index.js +3 -0
  5. package/dist/cjs/plugins/customServer/base.js +3 -0
  6. package/dist/cjs/plugins/customServer/index.js +2 -5
  7. package/dist/cjs/plugins/default.js +42 -0
  8. package/dist/cjs/plugins/index.js +6 -6
  9. package/dist/cjs/plugins/monitors.js +4 -3
  10. package/dist/cjs/plugins/render/index.js +10 -34
  11. package/dist/cjs/plugins/render/inject.js +75 -0
  12. package/dist/cjs/plugins/render/render.js +23 -2
  13. package/dist/esm/adapters/node/helper/loadConfig.js +2 -2
  14. package/dist/esm/adapters/node/plugins/resource.js +13 -3
  15. package/dist/esm/{plugins/customServer/loader.js → helper.js} +3 -2
  16. package/dist/esm/index.js +2 -0
  17. package/dist/esm/plugins/customServer/base.js +3 -0
  18. package/dist/esm/plugins/customServer/index.js +1 -3
  19. package/dist/esm/plugins/default.js +19 -0
  20. package/dist/esm/plugins/index.js +5 -5
  21. package/dist/esm/plugins/monitors.js +3 -3
  22. package/dist/esm/plugins/render/index.js +27 -70
  23. package/dist/esm/plugins/render/inject.js +85 -0
  24. package/dist/esm/plugins/render/render.js +23 -2
  25. package/dist/esm-node/adapters/node/helper/loadConfig.js +2 -2
  26. package/dist/esm-node/adapters/node/plugins/resource.js +5 -2
  27. package/dist/esm-node/{plugins/customServer/loader.js → helper.js} +3 -2
  28. package/dist/esm-node/index.js +2 -0
  29. package/dist/esm-node/plugins/customServer/base.js +3 -0
  30. package/dist/esm-node/plugins/customServer/index.js +1 -3
  31. package/dist/esm-node/plugins/default.js +18 -0
  32. package/dist/esm-node/plugins/index.js +5 -5
  33. package/dist/esm-node/plugins/monitors.js +4 -3
  34. package/dist/esm-node/plugins/render/index.js +7 -32
  35. package/dist/esm-node/plugins/render/inject.js +50 -0
  36. package/dist/esm-node/plugins/render/render.js +23 -2
  37. package/dist/types/{plugins/customServer/loader.d.ts → helper.d.ts} +1 -1
  38. package/dist/types/index.d.ts +1 -0
  39. package/dist/types/plugins/customServer/index.d.ts +0 -1
  40. package/dist/types/plugins/default.d.ts +7 -0
  41. package/dist/types/plugins/index.d.ts +3 -3
  42. package/dist/types/plugins/monitors.d.ts +2 -2
  43. package/dist/types/plugins/render/index.d.ts +3 -16
  44. package/dist/types/plugins/render/inject.d.ts +7 -0
  45. package/dist/types/types/config/output.d.ts +0 -1
  46. package/dist/types/types/config/server.d.ts +4 -0
  47. package/dist/types/types/plugin.d.ts +10 -0
  48. package/dist/types/types/server.d.ts +1 -0
  49. package/package.json +7 -7
@@ -3,22 +3,21 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
4
4
  import { initReporter } from "../monitors";
5
5
  import { sortRoutes } from "../../utils";
6
- import { getLoaderCtx, CustomServer, getServerMidFromUnstableMid } from "../customServer";
7
- import { createRender } from "./render";
6
+ import { CustomServer, getServerMidFromUnstableMid } from "../customServer";
7
+ import { getLoaderCtx } from "../../helper";
8
+ export * from "./inject";
8
9
  var renderPlugin = function() {
9
- var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
10
10
  return {
11
11
  name: "@modern-js/plugin-render",
12
12
  setup: function setup(api) {
13
- var staticGenerate = options.staticGenerate, cacheConfig = options.cacheConfig;
14
13
  return {
15
14
  prepare: function prepare() {
16
15
  return _async_to_generator(function() {
17
- var _config_render, _config_render1, _api_useAppContext, middlewares, routes, metaName, pwd, serverBase, runner, config, customServer, serverMiddleware, pageRoutes, render, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, _tmp, err;
16
+ var _config_render, _api_useAppContext, middlewares, routes, render, pwd, serverBase, runner, config, customServer, serverMiddleware, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, _tmp, err;
18
17
  return _ts_generator(this, function(_state) {
19
18
  switch (_state.label) {
20
19
  case 0:
21
- _api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, routes = _api_useAppContext.routes, metaName = _api_useAppContext.metaName, pwd = _api_useAppContext.distDirectory, serverBase = _api_useAppContext.serverBase;
20
+ _api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, routes = _api_useAppContext.routes, render = _api_useAppContext.render, pwd = _api_useAppContext.distDirectory, serverBase = _api_useAppContext.serverBase;
22
21
  runner = api.useHookRunners();
23
22
  config = api.useConfigContext();
24
23
  if (!routes) {
@@ -29,35 +28,22 @@ var renderPlugin = function() {
29
28
  customServer = new CustomServer(runner, serverBase, pwd);
30
29
  serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
31
30
  pageRoutes = getPageRoutes(routes);
32
- return [
33
- 4,
34
- getRenderHandler({
35
- pwd,
36
- routes,
37
- config,
38
- metaName,
39
- cacheConfig: ((_config_render1 = config.render) === null || _config_render1 === void 0 ? void 0 : _config_render1.cache) || cacheConfig,
40
- staticGenerate
41
- })
42
- ];
43
- case 1:
44
- render = _state.sent();
45
31
  _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
46
- _state.label = 2;
47
- case 2:
32
+ _state.label = 1;
33
+ case 1:
48
34
  _state.trys.push([
49
- 2,
35
+ 1,
36
+ 7,
50
37
  8,
51
- 9,
52
- 10
38
+ 9
53
39
  ]);
54
40
  _iterator = pageRoutes[Symbol.iterator]();
55
- _state.label = 3;
56
- case 3:
41
+ _state.label = 2;
42
+ case 2:
57
43
  if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
58
44
  return [
59
45
  3,
60
- 7
46
+ 6
61
47
  ];
62
48
  route = _step.value;
63
49
  originUrlPath = route.urlPath, entryName = route.entryName;
@@ -76,48 +62,48 @@ var renderPlugin = function() {
76
62
  if (_tmp)
77
63
  return [
78
64
  3,
79
- 5
65
+ 4
80
66
  ];
81
67
  return [
82
68
  4,
83
69
  customServer.getServerMiddleware()
84
70
  ];
85
- case 4:
71
+ case 3:
86
72
  _tmp = _state.sent();
87
- _state.label = 5;
88
- case 5:
73
+ _state.label = 4;
74
+ case 4:
89
75
  customServerMiddleware = _tmp;
90
76
  customServerMiddleware && middlewares.push({
91
77
  name: "custom-server-middleware",
92
78
  path: urlPath,
93
79
  handler: customServerMiddleware
94
80
  });
95
- middlewares.push({
81
+ render && middlewares.push({
96
82
  name: "render",
97
83
  path: urlPath,
98
84
  handler: createRenderHandler(render)
99
85
  });
100
- _state.label = 6;
101
- case 6:
86
+ _state.label = 5;
87
+ case 5:
102
88
  _iteratorNormalCompletion = true;
103
89
  return [
104
90
  3,
105
- 3
91
+ 2
106
92
  ];
107
- case 7:
93
+ case 6:
108
94
  return [
109
95
  3,
110
- 10
96
+ 9
111
97
  ];
112
- case 8:
98
+ case 7:
113
99
  err = _state.sent();
114
100
  _didIteratorError = true;
115
101
  _iteratorError = err;
116
102
  return [
117
103
  3,
118
- 10
104
+ 9
119
105
  ];
120
- case 9:
106
+ case 8:
121
107
  try {
122
108
  if (!_iteratorNormalCompletion && _iterator.return != null) {
123
109
  _iterator.return();
@@ -130,7 +116,7 @@ var renderPlugin = function() {
130
116
  return [
131
117
  7
132
118
  ];
133
- case 10:
119
+ case 9:
134
120
  return [
135
121
  2
136
122
  ];
@@ -197,35 +183,6 @@ function createRenderHandler(render) {
197
183
  };
198
184
  }();
199
185
  }
200
- function getRenderHandler(_) {
201
- return _getRenderHandler.apply(this, arguments);
202
- }
203
- function _getRenderHandler() {
204
- _getRenderHandler = _async_to_generator(function(param) {
205
- var pwd, routes, config, cacheConfig, metaName, staticGenerate, _config_server, _config_security, ssrConfig, forceCSR, render;
206
- return _ts_generator(this, function(_state) {
207
- pwd = param.pwd, routes = param.routes, config = param.config, cacheConfig = param.cacheConfig, metaName = param.metaName, staticGenerate = param.staticGenerate;
208
- ssrConfig = (_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr;
209
- forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
210
- render = createRender({
211
- routes,
212
- pwd,
213
- config,
214
- staticGenerate,
215
- cacheConfig,
216
- forceCSR,
217
- nonce: (_config_security = config.security) === null || _config_security === void 0 ? void 0 : _config_security.nonce,
218
- metaName: metaName || "modern-js"
219
- });
220
- return [
221
- 2,
222
- render
223
- ];
224
- });
225
- });
226
- return _getRenderHandler.apply(this, arguments);
227
- }
228
186
  export {
229
- getRenderHandler,
230
187
  renderPlugin
231
188
  };
@@ -0,0 +1,85 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
+ import { createRender } from "./render";
6
+ var injectRenderHandlerPlugin = function(param) {
7
+ var staticGenerate = param.staticGenerate, cacheConfig = param.cacheConfig;
8
+ return {
9
+ name: "@modern-js/plugin-inject-render",
10
+ setup: function setup(api) {
11
+ return {
12
+ prepare: function prepare() {
13
+ return _async_to_generator(function() {
14
+ var _config_render, _api_useAppContext, pwd, routes, metaName, config, getRenderHandlerOptions, render;
15
+ return _ts_generator(this, function(_state) {
16
+ switch (_state.label) {
17
+ case 0:
18
+ _api_useAppContext = api.useAppContext(), pwd = _api_useAppContext.distDirectory, routes = _api_useAppContext.routes, metaName = _api_useAppContext.metaName;
19
+ config = api.useConfigContext();
20
+ if (!routes) {
21
+ return [
22
+ 2
23
+ ];
24
+ }
25
+ getRenderHandlerOptions = {
26
+ pwd,
27
+ routes,
28
+ config,
29
+ metaName,
30
+ cacheConfig: ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.cache) || cacheConfig,
31
+ staticGenerate
32
+ };
33
+ return [
34
+ 4,
35
+ getRenderHandler(getRenderHandlerOptions)
36
+ ];
37
+ case 1:
38
+ render = _state.sent();
39
+ api.setAppContext(_object_spread_props(_object_spread({}, api.useAppContext()), {
40
+ render,
41
+ getRenderOptions: getRenderHandlerOptions
42
+ }));
43
+ return [
44
+ 2
45
+ ];
46
+ }
47
+ });
48
+ })();
49
+ }
50
+ };
51
+ }
52
+ };
53
+ };
54
+ function getRenderHandler(_) {
55
+ return _getRenderHandler.apply(this, arguments);
56
+ }
57
+ function _getRenderHandler() {
58
+ _getRenderHandler = _async_to_generator(function(param) {
59
+ var pwd, routes, config, cacheConfig, metaName, staticGenerate, _config_server, _config_security, ssrConfig, forceCSR, render;
60
+ return _ts_generator(this, function(_state) {
61
+ pwd = param.pwd, routes = param.routes, config = param.config, cacheConfig = param.cacheConfig, metaName = param.metaName, staticGenerate = param.staticGenerate;
62
+ ssrConfig = (_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr;
63
+ forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
64
+ render = createRender({
65
+ routes,
66
+ pwd,
67
+ config,
68
+ staticGenerate,
69
+ cacheConfig,
70
+ forceCSR,
71
+ nonce: (_config_security = config.security) === null || _config_security === void 0 ? void 0 : _config_security.nonce,
72
+ metaName: metaName || "modern-js"
73
+ });
74
+ return [
75
+ 2,
76
+ render
77
+ ];
78
+ });
79
+ });
80
+ return _getRenderHandler.apply(this, arguments);
81
+ }
82
+ export {
83
+ getRenderHandler,
84
+ injectRenderHandlerPlugin
85
+ };
@@ -8,6 +8,7 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
8
8
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
9
9
  import { cutNameByHyphen } from "@modern-js/utils/universal";
10
10
  import { TrieRouter } from "hono/router/trie-router";
11
+ import { matchRoutes } from "@modern-js/runtime-utils/router";
11
12
  import { parseQuery, getPathname, createErrorHtml, sortRoutes, transformResponse, onError as onErrorFn, ErrorDigest, parseHeaders } from "../../utils";
12
13
  import { REPLACE_REG, X_MODERNJS_RENDER } from "../../constants";
13
14
  import { dataHandler } from "./dataHandler";
@@ -257,7 +258,7 @@ function renderHandler(request, options, mode, onError) {
257
258
  }
258
259
  function _renderHandler() {
259
260
  _renderHandler = _async_to_generator(function(request, options, mode, onError) {
260
- var serverData, response, e, newRes, routeInfo;
261
+ var _options_config_server, serverData, response, serverManifest, ssrByRouteIds, nestedRoutesJson, routes, url, matchedRoutes, _lastMatch_route, lastMatch, e, newRes, routeInfo;
261
262
  function applyExtendHeaders(r, route) {
262
263
  Object.entries(route.responseHeaders || {}).forEach(function(param) {
263
264
  var _param = _sliced_to_array(param, 2), k = _param[0], v = _param[1];
@@ -273,7 +274,27 @@ function _renderHandler() {
273
274
  params: options.params
274
275
  }
275
276
  };
276
- if (!(mode === "ssr"))
277
+ response = null;
278
+ serverManifest = options.serverManifest;
279
+ ssrByRouteIds = (_options_config_server = options.config.server) === null || _options_config_server === void 0 ? void 0 : _options_config_server.ssrByRouteIds;
280
+ if (serverManifest.nestedRoutesJson && ssrByRouteIds && (ssrByRouteIds === null || ssrByRouteIds === void 0 ? void 0 : ssrByRouteIds.length) > 0) {
281
+ nestedRoutesJson = serverManifest.nestedRoutesJson;
282
+ routes = nestedRoutesJson === null || nestedRoutesJson === void 0 ? void 0 : nestedRoutesJson[options.routeInfo.entryName];
283
+ if (routes) {
284
+ url = new URL(request.url);
285
+ matchedRoutes = matchRoutes(routes, url.pathname, options.routeInfo.urlPath);
286
+ if (!matchedRoutes) {
287
+ response = csrRender(options.html);
288
+ } else {
289
+ ;
290
+ lastMatch = matchedRoutes[matchedRoutes.length - 1];
291
+ if (!(lastMatch === null || lastMatch === void 0 ? void 0 : (_lastMatch_route = lastMatch.route) === null || _lastMatch_route === void 0 ? void 0 : _lastMatch_route.id) || !ssrByRouteIds.includes(lastMatch.route.id)) {
292
+ response = csrRender(options.html);
293
+ }
294
+ }
295
+ }
296
+ }
297
+ if (!(mode === "ssr" && !response))
277
298
  return [
278
299
  3,
279
300
  6
@@ -27,8 +27,8 @@ function loadServerRuntimeConfig(pwd, oldServerFile = DEFAULT_SERVER_CONFIG, new
27
27
  return oldServerConfig;
28
28
  }
29
29
  function loadServerCliConfig(pwd, defaultConfig = {}) {
30
- var _defaultConfig_output;
31
- const cliConfigPath = ensureAbsolutePath(pwd, path.join(((_defaultConfig_output = defaultConfig.output) === null || _defaultConfig_output === void 0 ? void 0 : _defaultConfig_output.path) || "dist", OUTPUT_CONFIG_FILE));
30
+ var _defaultConfig_output_distPath, _defaultConfig_output;
31
+ const cliConfigPath = ensureAbsolutePath(pwd, path.join(((_defaultConfig_output = defaultConfig.output) === null || _defaultConfig_output === void 0 ? void 0 : (_defaultConfig_output_distPath = _defaultConfig_output.distPath) === null || _defaultConfig_output_distPath === void 0 ? void 0 : _defaultConfig_output_distPath.root) || "dist", OUTPUT_CONFIG_FILE));
32
32
  let cliConfig = {
33
33
  output: {},
34
34
  source: {},
@@ -1,6 +1,6 @@
1
1
  import path from "path";
2
2
  import { fileReader } from "@modern-js/runtime-utils/fileReader";
3
- import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs } from "@modern-js/utils";
3
+ import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs } from "@modern-js/utils";
4
4
  async function getHtmlTemplates(pwd, routes) {
5
5
  const htmls = await Promise.all(routes.map(async (route) => {
6
6
  let html;
@@ -60,11 +60,14 @@ async function getServerManifest(pwd, routes, logger) {
60
60
  const loadableStats = await import(loadableUri).catch((_) => ({}));
61
61
  const routesManifestUri = path.join(pwd, ROUTE_MANIFEST_FILE);
62
62
  const routeManifest = await import(routesManifestUri).catch((_) => ({}));
63
+ const nestedRoutesJsonPath = path.join(pwd, NESTED_ROUTE_SPEC_FILE);
64
+ const nestedRoutesJson = await import(nestedRoutesJsonPath).catch((_) => ({}));
63
65
  return {
64
66
  loaderBundles,
65
67
  renderBundles,
66
68
  loadableStats,
67
- routeManifest
69
+ routeManifest,
70
+ nestedRoutesJson
68
71
  };
69
72
  }
70
73
  function injectServerManifest(pwd, routes) {
@@ -1,10 +1,11 @@
1
+ const LOADER_CONTEXT = "loaderContext";
1
2
  function getLoaderCtx(c) {
2
- const loaderContext = c.get("loaderContext");
3
+ const loaderContext = c.get(LOADER_CONTEXT);
3
4
  if (loaderContext) {
4
5
  return loaderContext;
5
6
  } else {
6
7
  const loaderContext2 = /* @__PURE__ */ new Map();
7
- c.set("loaderContext", loaderContext2);
8
+ c.set(LOADER_CONTEXT, loaderContext2);
8
9
  return loaderContext2;
9
10
  }
10
11
  }
@@ -2,6 +2,7 @@ import { createErrorHtml, onError, ErrorDigest } from "./utils";
2
2
  import { AGGRED_DIR } from "./constants";
3
3
  import { createServerBase } from "./serverBase";
4
4
  import { PluginManager } from "./pluginManager";
5
+ import { getLoaderCtx } from "./helper";
5
6
  export * from "./plugins";
6
7
  export * from "./types/plugin";
7
8
  export * from "./types/render";
@@ -14,5 +15,6 @@ export {
14
15
  PluginManager,
15
16
  createErrorHtml,
16
17
  createServerBase,
18
+ getLoaderCtx,
17
19
  onError
18
20
  };
@@ -120,6 +120,9 @@ class BaseHookResponse {
120
120
  _class_private_field_get(this, _c1).status(code);
121
121
  _class_private_field_get(this, _resArgs) && (_class_private_field_get(this, _resArgs).status = code);
122
122
  }
123
+ getStatus() {
124
+ return _class_private_field_get(this, _c1).res.status;
125
+ }
123
126
  get cookies() {
124
127
  const setCookie = (key, value) => {
125
128
  _class_private_field_get(this, _c1).header("set-cookie", `${key}=${value}`, {
@@ -1,10 +1,9 @@
1
1
  import { time } from "@modern-js/runtime-utils/time";
2
2
  import { transformResponse } from "../../utils";
3
+ import { getLoaderCtx } from "../../helper";
3
4
  import { ServerTimings } from "../../constants";
4
- import { getLoaderCtx } from "./loader";
5
5
  import { getAfterMatchCtx, getAfterRenderCtx, createCustomMiddlewaresCtx, createAfterStreamingRenderContext } from "./context";
6
6
  import { createBaseHookContext } from "./base";
7
- import { getLoaderCtx as getLoaderCtx2 } from "./loader";
8
7
  const noop = () => {
9
8
  };
10
9
  const isHtmlResponse = (response) => {
@@ -178,6 +177,5 @@ async function createMiddlewareContextFromHono(c) {
178
177
  }
179
178
  export {
180
179
  CustomServer,
181
- getLoaderCtx2 as getLoaderCtx,
182
180
  getServerMidFromUnstableMid
183
181
  };
@@ -0,0 +1,18 @@
1
+ import { injectRenderHandlerPlugin } from "./render";
2
+ import { initMonitorsPlugin, injectloggerPluigin, injectServerTiming } from "./monitors";
3
+ import { processedByPlugin } from "./processedBy";
4
+ import { logPlugin } from "./log";
5
+ function createDefaultPlugins(options = {}) {
6
+ const plugins = [
7
+ initMonitorsPlugin(),
8
+ injectRenderHandlerPlugin(options),
9
+ injectloggerPluigin(options.logger),
10
+ injectServerTiming(),
11
+ logPlugin(),
12
+ processedByPlugin()
13
+ ];
14
+ return plugins;
15
+ }
16
+ export {
17
+ createDefaultPlugins
18
+ };
@@ -1,14 +1,14 @@
1
- import { renderPlugin, getRenderHandler } from "./render";
1
+ import { renderPlugin, injectRenderHandlerPlugin, getRenderHandler } from "./render";
2
2
  import { faviconPlugin } from "./favicon";
3
+ import { injectServerTiming, injectloggerPluigin } from "./monitors";
3
4
  import { processedByPlugin } from "./processedBy";
4
- import { getLoaderCtx } from "./customServer";
5
5
  import { logPlugin } from "./log";
6
- import { initMonitorsPlugin, injectServerTiming, injectloggerPluigin } from "./monitors";
6
+ import { createDefaultPlugins } from "./default";
7
7
  export {
8
+ createDefaultPlugins,
8
9
  faviconPlugin,
9
- getLoaderCtx,
10
10
  getRenderHandler,
11
- initMonitorsPlugin,
11
+ injectRenderHandlerPlugin,
12
12
  injectServerTiming,
13
13
  injectloggerPluigin,
14
14
  logPlugin,
@@ -64,9 +64,10 @@ const initMonitorsPlugin = () => ({
64
64
  };
65
65
  }
66
66
  });
67
- const injectloggerPluigin = (logger) => ({
67
+ const injectloggerPluigin = (inputLogger) => ({
68
68
  name: "@modern-js/inject-logger",
69
69
  setup(api) {
70
+ const logger = inputLogger || console;
70
71
  return {
71
72
  prepare() {
72
73
  const { middlewares } = api.useAppContext();
@@ -100,12 +101,12 @@ const injectloggerPluigin = (logger) => ({
100
101
  };
101
102
  }
102
103
  });
103
- const injectServerTiming = (metaName = "modern-js") => ({
104
+ const injectServerTiming = () => ({
104
105
  name: "@modern-js/inject-server-timing",
105
106
  setup(api) {
106
107
  return {
107
108
  prepare() {
108
- const { middlewares } = api.useAppContext();
109
+ const { middlewares, metaName } = api.useAppContext();
109
110
  middlewares.push({
110
111
  name: "inject-server-timing",
111
112
  handler: async (c, next) => {
@@ -1,16 +1,16 @@
1
1
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
2
2
  import { initReporter } from "../monitors";
3
3
  import { sortRoutes } from "../../utils";
4
- import { getLoaderCtx, CustomServer, getServerMidFromUnstableMid } from "../customServer";
5
- import { createRender } from "./render";
6
- const renderPlugin = (options = {}) => ({
4
+ import { CustomServer, getServerMidFromUnstableMid } from "../customServer";
5
+ import { getLoaderCtx } from "../../helper";
6
+ export * from "./inject";
7
+ const renderPlugin = () => ({
7
8
  name: "@modern-js/plugin-render",
8
9
  setup(api) {
9
- const { staticGenerate, cacheConfig } = options;
10
10
  return {
11
11
  async prepare() {
12
- var _config_render, _config_render1;
13
- const { middlewares, routes, metaName, distDirectory: pwd, serverBase } = api.useAppContext();
12
+ var _config_render;
13
+ const { middlewares, routes, render, distDirectory: pwd, serverBase } = api.useAppContext();
14
14
  const runner = api.useHookRunners();
15
15
  const config = api.useConfigContext();
16
16
  if (!routes) {
@@ -19,14 +19,6 @@ const renderPlugin = (options = {}) => ({
19
19
  const customServer = new CustomServer(runner, serverBase, pwd);
20
20
  const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
21
21
  const pageRoutes = getPageRoutes(routes);
22
- const render = await getRenderHandler({
23
- pwd,
24
- routes,
25
- config,
26
- metaName,
27
- cacheConfig: ((_config_render1 = config.render) === null || _config_render1 === void 0 ? void 0 : _config_render1.cache) || cacheConfig,
28
- staticGenerate
29
- });
30
22
  for (const route of pageRoutes) {
31
23
  const { urlPath: originUrlPath, entryName } = route;
32
24
  const urlPath = originUrlPath.endsWith("/") ? `${originUrlPath}*` : `${originUrlPath}/*`;
@@ -46,7 +38,7 @@ const renderPlugin = (options = {}) => ({
46
38
  path: urlPath,
47
39
  handler: customServerMiddleware
48
40
  });
49
- middlewares.push({
41
+ render && middlewares.push({
50
42
  name: `render`,
51
43
  path: urlPath,
52
44
  handler: createRenderHandler(render)
@@ -91,23 +83,6 @@ function createRenderHandler(render) {
91
83
  return c.body(body, status, headersData);
92
84
  };
93
85
  }
94
- async function getRenderHandler({ pwd, routes, config, cacheConfig, metaName, staticGenerate }) {
95
- var _config_server, _config_security;
96
- const ssrConfig = (_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr;
97
- const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
98
- const render = createRender({
99
- routes,
100
- pwd,
101
- config,
102
- staticGenerate,
103
- cacheConfig,
104
- forceCSR,
105
- nonce: (_config_security = config.security) === null || _config_security === void 0 ? void 0 : _config_security.nonce,
106
- metaName: metaName || "modern-js"
107
- });
108
- return render;
109
- }
110
86
  export {
111
- getRenderHandler,
112
87
  renderPlugin
113
88
  };
@@ -0,0 +1,50 @@
1
+ import { createRender } from "./render";
2
+ const injectRenderHandlerPlugin = ({ staticGenerate, cacheConfig }) => ({
3
+ name: "@modern-js/plugin-inject-render",
4
+ setup(api) {
5
+ return {
6
+ async prepare() {
7
+ var _config_render;
8
+ const { distDirectory: pwd, routes, metaName } = api.useAppContext();
9
+ const config = api.useConfigContext();
10
+ if (!routes) {
11
+ return;
12
+ }
13
+ const getRenderHandlerOptions = {
14
+ pwd,
15
+ routes,
16
+ config,
17
+ metaName,
18
+ cacheConfig: ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.cache) || cacheConfig,
19
+ staticGenerate
20
+ };
21
+ const render = await getRenderHandler(getRenderHandlerOptions);
22
+ api.setAppContext({
23
+ ...api.useAppContext(),
24
+ render,
25
+ getRenderOptions: getRenderHandlerOptions
26
+ });
27
+ }
28
+ };
29
+ }
30
+ });
31
+ async function getRenderHandler({ pwd, routes, config, cacheConfig, metaName, staticGenerate }) {
32
+ var _config_server, _config_security;
33
+ const ssrConfig = (_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr;
34
+ const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
35
+ const render = createRender({
36
+ routes,
37
+ pwd,
38
+ config,
39
+ staticGenerate,
40
+ cacheConfig,
41
+ forceCSR,
42
+ nonce: (_config_security = config.security) === null || _config_security === void 0 ? void 0 : _config_security.nonce,
43
+ metaName: metaName || "modern-js"
44
+ });
45
+ return render;
46
+ }
47
+ export {
48
+ getRenderHandler,
49
+ injectRenderHandlerPlugin
50
+ };
@@ -1,5 +1,6 @@
1
1
  import { cutNameByHyphen } from "@modern-js/utils/universal";
2
2
  import { TrieRouter } from "hono/router/trie-router";
3
+ import { matchRoutes } from "@modern-js/runtime-utils/router";
3
4
  import { parseQuery, getPathname, createErrorHtml, sortRoutes, transformResponse, onError as onErrorFn, ErrorDigest, parseHeaders } from "../../utils";
4
5
  import { REPLACE_REG, X_MODERNJS_RENDER } from "../../constants";
5
6
  import { dataHandler } from "./dataHandler";
@@ -117,14 +118,34 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
117
118
  };
118
119
  }
119
120
  async function renderHandler(request, options, mode, onError) {
121
+ var _options_config_server;
120
122
  const serverData = {
121
123
  router: {
122
124
  baseUrl: options.routeInfo.urlPath,
123
125
  params: options.params
124
126
  }
125
127
  };
126
- let response;
127
- if (mode === "ssr") {
128
+ let response = null;
129
+ const { serverManifest } = options;
130
+ const ssrByRouteIds = (_options_config_server = options.config.server) === null || _options_config_server === void 0 ? void 0 : _options_config_server.ssrByRouteIds;
131
+ if (serverManifest.nestedRoutesJson && ssrByRouteIds && (ssrByRouteIds === null || ssrByRouteIds === void 0 ? void 0 : ssrByRouteIds.length) > 0) {
132
+ const { nestedRoutesJson } = serverManifest;
133
+ const routes = nestedRoutesJson === null || nestedRoutesJson === void 0 ? void 0 : nestedRoutesJson[options.routeInfo.entryName];
134
+ if (routes) {
135
+ const url = new URL(request.url);
136
+ const matchedRoutes = matchRoutes(routes, url.pathname, options.routeInfo.urlPath);
137
+ if (!matchedRoutes) {
138
+ response = csrRender(options.html);
139
+ } else {
140
+ var _lastMatch_route;
141
+ const lastMatch = matchedRoutes[matchedRoutes.length - 1];
142
+ if (!(lastMatch === null || lastMatch === void 0 ? void 0 : (_lastMatch_route = lastMatch.route) === null || _lastMatch_route === void 0 ? void 0 : _lastMatch_route.id) || !ssrByRouteIds.includes(lastMatch.route.id)) {
143
+ response = csrRender(options.html);
144
+ }
145
+ }
146
+ }
147
+ }
148
+ if (mode === "ssr" && !response) {
128
149
  try {
129
150
  response = await ssrRender(request, options);
130
151
  } catch (e) {
@@ -1,4 +1,4 @@
1
- import type { Context } from '../../types';
1
+ import type { Context } from './types';
2
2
  type LoaderContext = Map<string, unknown>;
3
3
  type Var = {
4
4
  loaderContext: LoaderContext;
@@ -4,6 +4,7 @@ export type { ServerBase, ServerBaseOptions } from './serverBase';
4
4
  export { createServerBase } from './serverBase';
5
5
  export { PluginManager, type PluginManagerOptions } from './pluginManager';
6
6
  export type { Middleware, Context, Next, HonoRequest as InternalRequest, ServerEnv, ServerManifest, } from './types';
7
+ export { getLoaderCtx } from './helper';
7
8
  export * from './plugins';
8
9
  export * from './types/plugin';
9
10
  export * from './types/render';