@modern-js/server-core 2.63.1 → 2.63.2

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.
@@ -38,6 +38,7 @@ module.exports = __toCommonJS(resource_exports);
38
38
  var import_path = __toESM(require("path"));
39
39
  var import_fileReader = require("@modern-js/runtime-utils/fileReader");
40
40
  var import_utils = require("@modern-js/utils");
41
+ var import_utils2 = require("../../../utils");
41
42
  async function getHtmlTemplates(pwd, routes) {
42
43
  const htmls = await Promise.all(routes.map(async (route) => {
43
44
  let html;
@@ -48,7 +49,7 @@ async function getHtmlTemplates(pwd, routes) {
48
49
  } catch (e) {
49
50
  }
50
51
  return [
51
- route.entryName,
52
+ (0, import_utils2.uniqueKeyByRoute)(route),
52
53
  html
53
54
  ];
54
55
  }) || []);
@@ -19,7 +19,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var customServer_exports = {};
20
20
  __export(customServer_exports, {
21
21
  CustomServer: () => CustomServer,
22
- getServerMidFromUnstableMid: () => getServerMidFromUnstableMid
22
+ getServerMidFromUnstableMid: () => getServerMidFromUnstableMid,
23
+ injectRoute: () => injectRoute
23
24
  });
24
25
  module.exports = __toCommonJS(customServer_exports);
25
26
  var import_time = require("@modern-js/runtime-utils/time");
@@ -95,13 +96,19 @@ class CustomServer {
95
96
  }
96
97
  };
97
98
  }
98
- async getServerMiddleware() {
99
+ async getServerMiddleware(renderMiddlewares) {
99
100
  const serverMiddleware = await this.serverMiddlewarePromise;
100
101
  if (!serverMiddleware) {
101
- return;
102
+ return renderMiddlewares;
102
103
  }
103
104
  if (Array.isArray(serverMiddleware)) {
104
- return getServerMidFromUnstableMid(serverMiddleware);
105
+ const unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
106
+ return [
107
+ ...renderMiddlewares || [],
108
+ ...unstableMiddlewares
109
+ ];
110
+ } else if (renderMiddlewares) {
111
+ return renderMiddlewares;
105
112
  }
106
113
  return async (c, next) => {
107
114
  var _c_env_node_res, _c_env_node, _c_env;
@@ -189,6 +196,9 @@ async function createMiddlewareContextFromHono(c) {
189
196
  set response(newRes) {
190
197
  c.res = newRes;
191
198
  },
199
+ get route() {
200
+ return c.get("route");
201
+ },
192
202
  get(key) {
193
203
  return loaderContext.get(key);
194
204
  },
@@ -202,8 +212,17 @@ async function createMiddlewareContextFromHono(c) {
202
212
  redirect: c.redirect.bind(c)
203
213
  };
204
214
  }
215
+ function injectRoute(route) {
216
+ return async (c, next) => {
217
+ if (route && !c.get("route")) {
218
+ c.set("route", route);
219
+ }
220
+ await next();
221
+ };
222
+ }
205
223
  // Annotate the CommonJS export names for ESM import in node:
206
224
  0 && (module.exports = {
207
225
  CustomServer,
208
- getServerMidFromUnstableMid
226
+ getServerMidFromUnstableMid,
227
+ injectRoute
209
228
  });
@@ -44,19 +44,25 @@ const renderPlugin = () => ({
44
44
  const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && (0, import_customServer.getServerMidFromUnstableMid)(config.render.middleware);
45
45
  const pageRoutes = getPageRoutes(routes);
46
46
  for (const route of pageRoutes) {
47
- const { urlPath: originUrlPath, entryName } = route;
47
+ const { urlPath: originUrlPath, entryName = import_constants.MAIN_ENTRY_NAME } = route;
48
48
  const urlPath = originUrlPath.endsWith("/") ? `${originUrlPath}*` : `${originUrlPath}/*`;
49
49
  middlewares.push({
50
50
  name: "init-reporter",
51
- handler: (0, import_monitors.initReporter)(entryName || import_constants.MAIN_ENTRY_NAME)
51
+ handler: (0, import_monitors.initReporter)(entryName)
52
+ });
53
+ const customServerHookMiddleware = customServer.getHookMiddleware(entryName, routes);
54
+ middlewares.push({
55
+ name: "inject-route-info",
56
+ handler: (0, import_customServer.injectRoute)({
57
+ entryName
58
+ })
52
59
  });
53
- const customServerHookMiddleware = customServer.getHookMiddleware(entryName || "main", routes);
54
60
  middlewares.push({
55
61
  name: "custom-server-hook",
56
62
  path: urlPath,
57
63
  handler: customServerHookMiddleware
58
64
  });
59
- const customServerMiddleware = serverMiddleware || await customServer.getServerMiddleware();
65
+ const customServerMiddleware = await customServer.getServerMiddleware(serverMiddleware);
60
66
  customServerMiddleware && middlewares.push({
61
67
  name: "custom-server-middleware",
62
68
  path: urlPath,
@@ -35,6 +35,7 @@ var import_universal = require("@modern-js/utils/universal");
35
35
  var import_trie_router = require("hono/router/trie-router");
36
36
  var import_constants = require("../../constants");
37
37
  var import_utils = require("../../utils");
38
+ var import_utils2 = require("../../utils");
38
39
  var import_dataHandler = require("./dataHandler");
39
40
  var import_ssrRender = require("./ssrRender");
40
41
  const DYNAMIC_ROUTE_REG = /\/:./;
@@ -49,8 +50,8 @@ function getRouter(routes) {
49
50
  }
50
51
  });
51
52
  const finalRoutes = [
52
- ...normalRoutes.sort(import_utils.sortRoutes),
53
- ...dynamicRoutes.sort(import_utils.sortRoutes)
53
+ ...normalRoutes.sort(import_utils2.sortRoutes),
54
+ ...dynamicRoutes.sort(import_utils2.sortRoutes)
54
55
  ];
55
56
  const router = new import_trie_router.TrieRouter();
56
57
  for (const route of finalRoutes) {
@@ -76,7 +77,7 @@ function getHeadersWithoutCookie(headers) {
76
77
  async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig, forceCSR, config, onFallback: onFallbackFn }) {
77
78
  const router = getRouter(routes);
78
79
  return async (req, { logger, reporter, metrics, monitors, nodeReq, templates, serverManifest, locals, matchPathname, loaderContext }) => {
79
- const forMatchpathname = matchPathname !== null && matchPathname !== void 0 ? matchPathname : (0, import_utils.getPathname)(req);
80
+ const forMatchpathname = matchPathname !== null && matchPathname !== void 0 ? matchPathname : (0, import_utils2.getPathname)(req);
80
81
  const [routeInfo, params] = matchRoute(router, forMatchpathname);
81
82
  const framework = metaName || "modern-js";
82
83
  const fallbackHeader = `x-${(0, import_universal.cutNameByHyphen)(framework)}-ssr-fallback`;
@@ -90,16 +91,16 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
90
91
  }, error);
91
92
  };
92
93
  if (!routeInfo) {
93
- return new Response((0, import_utils.createErrorHtml)(404), {
94
+ return new Response((0, import_utils2.createErrorHtml)(404), {
94
95
  status: 404,
95
96
  headers: {
96
97
  "content-type": "text/html; charset=UTF-8"
97
98
  }
98
99
  });
99
100
  }
100
- const html = templates[routeInfo.entryName];
101
+ const html = templates[(0, import_utils.uniqueKeyByRoute)(routeInfo)];
101
102
  if (!html) {
102
- return new Response((0, import_utils.createErrorHtml)(404), {
103
+ return new Response((0, import_utils2.createErrorHtml)(404), {
103
104
  status: 404,
104
105
  headers: {
105
106
  "content-type": "text/html; charset=UTF-8"
@@ -107,7 +108,7 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
107
108
  });
108
109
  }
109
110
  const renderMode = await getRenderMode(req, fallbackHeader, routeInfo.isSSR, forceCSR, nodeReq, onFallback);
110
- const headerData = (0, import_utils.parseHeaders)(req);
111
+ const headerData = (0, import_utils2.parseHeaders)(req);
111
112
  const onError = (e) => {
112
113
  monitors === null || monitors === void 0 ? void 0 : monitors.error(`SSR Error - ${e instanceof Error ? e.name : e}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, forMatchpathname, getHeadersWithoutCookie(headerData));
113
114
  };
@@ -115,7 +116,7 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
115
116
  monitors === null || monitors === void 0 ? void 0 : monitors.timing(name, dur, "SSR");
116
117
  };
117
118
  const onBoundError = async (e) => {
118
- (0, import_utils.onError)(import_utils.ErrorDigest.ERENDER, e, monitors, req);
119
+ (0, import_utils2.onError)(import_utils2.ErrorDigest.ERENDER, e, monitors, req);
119
120
  await (onFallback === null || onFallback === void 0 ? void 0 : onFallback("error", e));
120
121
  };
121
122
  const renderOptions = {
@@ -166,7 +167,7 @@ async function renderHandler(request, options, mode, onError) {
166
167
  let response = null;
167
168
  const { serverManifest } = options;
168
169
  const ssrByRouteIds = (_options_config_server = options.config.server) === null || _options_config_server === void 0 ? void 0 : _options_config_server.ssrByRouteIds;
169
- const runtimeEnv = (0, import_utils.getRuntimeEnv)();
170
+ const runtimeEnv = (0, import_utils2.getRuntimeEnv)();
170
171
  if (serverManifest.nestedRoutesJson && ssrByRouteIds && (ssrByRouteIds === null || ssrByRouteIds === void 0 ? void 0 : ssrByRouteIds.length) > 0 && runtimeEnv === "node") {
171
172
  const { nestedRoutesJson } = serverManifest;
172
173
  const routes = nestedRoutesJson === null || nestedRoutesJson === void 0 ? void 0 : nestedRoutesJson[options.routeInfo.entryName];
@@ -197,7 +198,7 @@ async function renderHandler(request, options, mode, onError) {
197
198
  } else {
198
199
  response = csrRender(options.html);
199
200
  }
200
- const newRes = (0, import_utils.transformResponse)(response, injectServerData(serverData));
201
+ const newRes = (0, import_utils2.transformResponse)(response, injectServerData(serverData));
201
202
  const { routeInfo } = options;
202
203
  applyExtendHeaders(newRes, routeInfo);
203
204
  return newRes;
@@ -208,7 +209,7 @@ async function renderHandler(request, options, mode, onError) {
208
209
  }
209
210
  }
210
211
  async function getRenderMode(req, fallbackHeader, isSSR, forceCSR, nodeReq, onFallback) {
211
- const query = (0, import_utils.parseQuery)(req);
212
+ const query = (0, import_utils2.parseQuery)(req);
212
213
  if (isSSR) {
213
214
  if (query.__loader) {
214
215
  return "data";
@@ -99,7 +99,8 @@ function createRequestHandlerConfig(userConfig) {
99
99
  enableInlineScripts: output === null || output === void 0 ? void 0 : output.enableInlineScripts,
100
100
  enableInlineStyles: output === null || output === void 0 ? void 0 : output.enableInlineStyles,
101
101
  crossorigin: html === null || html === void 0 ? void 0 : html.crossorigin,
102
- scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading
102
+ scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading,
103
+ useJsonScript: server === null || server === void 0 ? void 0 : server.useJsonScript
103
104
  };
104
105
  }
105
106
  // Annotate the CommonJS export names for ESM import in node:
@@ -18,13 +18,18 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var entry_exports = {};
20
20
  __export(entry_exports, {
21
- sortRoutes: () => sortRoutes
21
+ sortRoutes: () => sortRoutes,
22
+ uniqueKeyByRoute: () => uniqueKeyByRoute
22
23
  });
23
24
  module.exports = __toCommonJS(entry_exports);
24
25
  const sortRoutes = (route1, route2) => {
25
26
  return route2.urlPath.length - route1.urlPath.length;
26
27
  };
28
+ const uniqueKeyByRoute = (route) => {
29
+ return `${route.entryName}-${route.urlPath}`;
30
+ };
27
31
  // Annotate the CommonJS export names for ESM import in node:
28
32
  0 && (module.exports = {
29
- sortRoutes
33
+ sortRoutes,
34
+ uniqueKeyByRoute
30
35
  });
@@ -4,6 +4,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import path from "path";
5
5
  import { fileReader } from "@modern-js/runtime-utils/fileReader";
6
6
  import { fs, LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, compatibleRequire } from "@modern-js/utils";
7
+ import { uniqueKeyByRoute } from "../../../utils";
7
8
  function getHtmlTemplates(pwd, routes) {
8
9
  return _getHtmlTemplates.apply(this, arguments);
9
10
  }
@@ -48,7 +49,7 @@ function _getHtmlTemplates() {
48
49
  return [
49
50
  2,
50
51
  [
51
- route.entryName,
52
+ uniqueKeyByRoute(route),
52
53
  html
53
54
  ]
54
55
  ];
@@ -1,5 +1,6 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
5
  import { time } from "@modern-js/runtime-utils/time";
5
6
  import { ServerTimings } from "../../constants";
@@ -155,10 +156,10 @@ var CustomServer = /* @__PURE__ */ function() {
155
156
  };
156
157
  }();
157
158
  };
158
- _proto.getServerMiddleware = function getServerMiddleware() {
159
+ _proto.getServerMiddleware = function getServerMiddleware(renderMiddlewares) {
159
160
  var _this = this;
160
161
  return _async_to_generator(function() {
161
- var serverMiddleware;
162
+ var serverMiddleware, unstableMiddlewares;
162
163
  return _ts_generator(this, function(_state) {
163
164
  switch (_state.label) {
164
165
  case 0:
@@ -170,13 +171,20 @@ var CustomServer = /* @__PURE__ */ function() {
170
171
  serverMiddleware = _state.sent();
171
172
  if (!serverMiddleware) {
172
173
  return [
173
- 2
174
+ 2,
175
+ renderMiddlewares
174
176
  ];
175
177
  }
176
178
  if (Array.isArray(serverMiddleware)) {
179
+ unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
180
+ return [
181
+ 2,
182
+ _to_consumable_array(renderMiddlewares || []).concat(_to_consumable_array(unstableMiddlewares))
183
+ ];
184
+ } else if (renderMiddlewares) {
177
185
  return [
178
186
  2,
179
- getServerMidFromUnstableMid(serverMiddleware)
187
+ renderMiddlewares
180
188
  ];
181
189
  }
182
190
  return [
@@ -325,6 +333,9 @@ function _createMiddlewareContextFromHono() {
325
333
  set response(newRes) {
326
334
  c.res = newRes;
327
335
  },
336
+ get route() {
337
+ return c.get("route");
338
+ },
328
339
  get: function get(key) {
329
340
  return loaderContext.get(key);
330
341
  },
@@ -343,7 +354,34 @@ function _createMiddlewareContextFromHono() {
343
354
  });
344
355
  return _createMiddlewareContextFromHono.apply(this, arguments);
345
356
  }
357
+ function injectRoute(route) {
358
+ return function() {
359
+ var _ref = _async_to_generator(function(c, next) {
360
+ return _ts_generator(this, function(_state) {
361
+ switch (_state.label) {
362
+ case 0:
363
+ if (route && !c.get("route")) {
364
+ c.set("route", route);
365
+ }
366
+ return [
367
+ 4,
368
+ next()
369
+ ];
370
+ case 1:
371
+ _state.sent();
372
+ return [
373
+ 2
374
+ ];
375
+ }
376
+ });
377
+ });
378
+ return function(c, next) {
379
+ return _ref.apply(this, arguments);
380
+ };
381
+ }();
382
+ }
346
383
  export {
347
384
  CustomServer,
348
- getServerMidFromUnstableMid
385
+ getServerMidFromUnstableMid,
386
+ injectRoute
349
387
  };
@@ -3,7 +3,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
4
4
  import { getLoaderCtx } from "../../helper";
5
5
  import { sortRoutes } from "../../utils";
6
- import { CustomServer, getServerMidFromUnstableMid } from "../customServer";
6
+ import { CustomServer, getServerMidFromUnstableMid, injectRoute } from "../customServer";
7
7
  import { initReporter } from "../monitors";
8
8
  export * from "./inject";
9
9
  var renderPlugin = function() {
@@ -13,7 +13,7 @@ var renderPlugin = function() {
13
13
  return {
14
14
  prepare: function prepare() {
15
15
  return _async_to_generator(function() {
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;
16
+ var _config_render, _api_useAppContext, middlewares, routes, render, pwd, serverBase, runner, config, customServer, serverMiddleware, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, _route_entryName, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, err;
17
17
  return _ts_generator(this, function(_state) {
18
18
  switch (_state.label) {
19
19
  case 0:
@@ -33,9 +33,9 @@ var renderPlugin = function() {
33
33
  case 1:
34
34
  _state.trys.push([
35
35
  1,
36
+ 6,
36
37
  7,
37
- 8,
38
- 9
38
+ 8
39
39
  ]);
40
40
  _iterator = pageRoutes[Symbol.iterator]();
41
41
  _state.label = 2;
@@ -43,36 +43,33 @@ var renderPlugin = function() {
43
43
  if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
44
44
  return [
45
45
  3,
46
- 6
46
+ 5
47
47
  ];
48
48
  route = _step.value;
49
- originUrlPath = route.urlPath, entryName = route.entryName;
49
+ originUrlPath = route.urlPath, _route_entryName = route.entryName, entryName = _route_entryName === void 0 ? MAIN_ENTRY_NAME : _route_entryName;
50
50
  urlPath = originUrlPath.endsWith("/") ? "".concat(originUrlPath, "*") : "".concat(originUrlPath, "/*");
51
51
  middlewares.push({
52
52
  name: "init-reporter",
53
- handler: initReporter(entryName || MAIN_ENTRY_NAME)
53
+ handler: initReporter(entryName)
54
+ });
55
+ customServerHookMiddleware = customServer.getHookMiddleware(entryName, routes);
56
+ middlewares.push({
57
+ name: "inject-route-info",
58
+ handler: injectRoute({
59
+ entryName
60
+ })
54
61
  });
55
- customServerHookMiddleware = customServer.getHookMiddleware(entryName || "main", routes);
56
62
  middlewares.push({
57
63
  name: "custom-server-hook",
58
64
  path: urlPath,
59
65
  handler: customServerHookMiddleware
60
66
  });
61
- _tmp = serverMiddleware;
62
- if (_tmp)
63
- return [
64
- 3,
65
- 4
66
- ];
67
67
  return [
68
68
  4,
69
- customServer.getServerMiddleware()
69
+ customServer.getServerMiddleware(serverMiddleware)
70
70
  ];
71
71
  case 3:
72
- _tmp = _state.sent();
73
- _state.label = 4;
74
- case 4:
75
- customServerMiddleware = _tmp;
72
+ customServerMiddleware = _state.sent();
76
73
  customServerMiddleware && middlewares.push({
77
74
  name: "custom-server-middleware",
78
75
  path: urlPath,
@@ -83,27 +80,27 @@ var renderPlugin = function() {
83
80
  path: urlPath,
84
81
  handler: createRenderHandler(render)
85
82
  });
86
- _state.label = 5;
87
- case 5:
83
+ _state.label = 4;
84
+ case 4:
88
85
  _iteratorNormalCompletion = true;
89
86
  return [
90
87
  3,
91
88
  2
92
89
  ];
93
- case 6:
90
+ case 5:
94
91
  return [
95
92
  3,
96
- 9
93
+ 8
97
94
  ];
98
- case 7:
95
+ case 6:
99
96
  err = _state.sent();
100
97
  _didIteratorError = true;
101
98
  _iteratorError = err;
102
99
  return [
103
100
  3,
104
- 9
101
+ 8
105
102
  ];
106
- case 8:
103
+ case 7:
107
104
  try {
108
105
  if (!_iteratorNormalCompletion && _iterator.return != null) {
109
106
  _iterator.return();
@@ -116,7 +113,7 @@ var renderPlugin = function() {
116
113
  return [
117
114
  7
118
115
  ];
119
- case 9:
116
+ case 8:
120
117
  return [
121
118
  2
122
119
  ];
@@ -9,6 +9,7 @@ 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
11
  import { REPLACE_REG, X_MODERNJS_RENDER } from "../../constants";
12
+ import { uniqueKeyByRoute } from "../../utils";
12
13
  import { ErrorDigest, createErrorHtml, getPathname, getRuntimeEnv, onError as onErrorFn, parseHeaders, parseQuery, sortRoutes, transformResponse } from "../../utils";
13
14
  import { dataHandler } from "./dataHandler";
14
15
  import { ssrRender } from "./ssrRender";
@@ -113,7 +114,7 @@ function _createRender() {
113
114
  })
114
115
  ];
115
116
  }
116
- html = templates[routeInfo.entryName];
117
+ html = templates[uniqueKeyByRoute(routeInfo)];
117
118
  if (!html) {
118
119
  return [
119
120
  2,
@@ -123,7 +123,8 @@ function createRequestHandlerConfig(userConfig) {
123
123
  enableInlineScripts: output === null || output === void 0 ? void 0 : output.enableInlineScripts,
124
124
  enableInlineStyles: output === null || output === void 0 ? void 0 : output.enableInlineStyles,
125
125
  crossorigin: html === null || html === void 0 ? void 0 : html.crossorigin,
126
- scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading
126
+ scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading,
127
+ useJsonScript: server === null || server === void 0 ? void 0 : server.useJsonScript
127
128
  };
128
129
  }
129
130
  export {
@@ -1,6 +1,10 @@
1
1
  var sortRoutes = function(route1, route2) {
2
2
  return route2.urlPath.length - route1.urlPath.length;
3
3
  };
4
+ var uniqueKeyByRoute = function(route) {
5
+ return "".concat(route.entryName, "-").concat(route.urlPath);
6
+ };
4
7
  export {
5
- sortRoutes
8
+ sortRoutes,
9
+ uniqueKeyByRoute
6
10
  };
@@ -1,6 +1,7 @@
1
1
  import path from "path";
2
2
  import { fileReader } from "@modern-js/runtime-utils/fileReader";
3
3
  import { fs, LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, compatibleRequire } from "@modern-js/utils";
4
+ import { uniqueKeyByRoute } from "../../../utils";
4
5
  async function getHtmlTemplates(pwd, routes) {
5
6
  const htmls = await Promise.all(routes.map(async (route) => {
6
7
  let html;
@@ -11,7 +12,7 @@ async function getHtmlTemplates(pwd, routes) {
11
12
  } catch (e) {
12
13
  }
13
14
  return [
14
- route.entryName,
15
+ uniqueKeyByRoute(route),
15
16
  html
16
17
  ];
17
18
  }) || []);
@@ -71,13 +71,19 @@ class CustomServer {
71
71
  }
72
72
  };
73
73
  }
74
- async getServerMiddleware() {
74
+ async getServerMiddleware(renderMiddlewares) {
75
75
  const serverMiddleware = await this.serverMiddlewarePromise;
76
76
  if (!serverMiddleware) {
77
- return;
77
+ return renderMiddlewares;
78
78
  }
79
79
  if (Array.isArray(serverMiddleware)) {
80
- return getServerMidFromUnstableMid(serverMiddleware);
80
+ const unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
81
+ return [
82
+ ...renderMiddlewares || [],
83
+ ...unstableMiddlewares
84
+ ];
85
+ } else if (renderMiddlewares) {
86
+ return renderMiddlewares;
81
87
  }
82
88
  return async (c, next) => {
83
89
  var _c_env_node_res, _c_env_node, _c_env;
@@ -165,6 +171,9 @@ async function createMiddlewareContextFromHono(c) {
165
171
  set response(newRes) {
166
172
  c.res = newRes;
167
173
  },
174
+ get route() {
175
+ return c.get("route");
176
+ },
168
177
  get(key) {
169
178
  return loaderContext.get(key);
170
179
  },
@@ -178,7 +187,16 @@ async function createMiddlewareContextFromHono(c) {
178
187
  redirect: c.redirect.bind(c)
179
188
  };
180
189
  }
190
+ function injectRoute(route) {
191
+ return async (c, next) => {
192
+ if (route && !c.get("route")) {
193
+ c.set("route", route);
194
+ }
195
+ await next();
196
+ };
197
+ }
181
198
  export {
182
199
  CustomServer,
183
- getServerMidFromUnstableMid
200
+ getServerMidFromUnstableMid,
201
+ injectRoute
184
202
  };
@@ -1,7 +1,7 @@
1
1
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
2
2
  import { getLoaderCtx } from "../../helper";
3
3
  import { sortRoutes } from "../../utils";
4
- import { CustomServer, getServerMidFromUnstableMid } from "../customServer";
4
+ import { CustomServer, getServerMidFromUnstableMid, injectRoute } from "../customServer";
5
5
  import { initReporter } from "../monitors";
6
6
  export * from "./inject";
7
7
  const renderPlugin = () => ({
@@ -20,19 +20,25 @@ const renderPlugin = () => ({
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
22
  for (const route of pageRoutes) {
23
- const { urlPath: originUrlPath, entryName } = route;
23
+ const { urlPath: originUrlPath, entryName = MAIN_ENTRY_NAME } = route;
24
24
  const urlPath = originUrlPath.endsWith("/") ? `${originUrlPath}*` : `${originUrlPath}/*`;
25
25
  middlewares.push({
26
26
  name: "init-reporter",
27
- handler: initReporter(entryName || MAIN_ENTRY_NAME)
27
+ handler: initReporter(entryName)
28
+ });
29
+ const customServerHookMiddleware = customServer.getHookMiddleware(entryName, routes);
30
+ middlewares.push({
31
+ name: "inject-route-info",
32
+ handler: injectRoute({
33
+ entryName
34
+ })
28
35
  });
29
- const customServerHookMiddleware = customServer.getHookMiddleware(entryName || "main", routes);
30
36
  middlewares.push({
31
37
  name: "custom-server-hook",
32
38
  path: urlPath,
33
39
  handler: customServerHookMiddleware
34
40
  });
35
- const customServerMiddleware = serverMiddleware || await customServer.getServerMiddleware();
41
+ const customServerMiddleware = await customServer.getServerMiddleware(serverMiddleware);
36
42
  customServerMiddleware && middlewares.push({
37
43
  name: "custom-server-middleware",
38
44
  path: urlPath,
@@ -1,6 +1,7 @@
1
1
  import { cutNameByHyphen } from "@modern-js/utils/universal";
2
2
  import { TrieRouter } from "hono/router/trie-router";
3
3
  import { REPLACE_REG, X_MODERNJS_RENDER } from "../../constants";
4
+ import { uniqueKeyByRoute } from "../../utils";
4
5
  import { ErrorDigest, createErrorHtml, getPathname, getRuntimeEnv, onError as onErrorFn, parseHeaders, parseQuery, sortRoutes, transformResponse } from "../../utils";
5
6
  import { dataHandler } from "./dataHandler";
6
7
  import { ssrRender } from "./ssrRender";
@@ -64,7 +65,7 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
64
65
  }
65
66
  });
66
67
  }
67
- const html = templates[routeInfo.entryName];
68
+ const html = templates[uniqueKeyByRoute(routeInfo)];
68
69
  if (!html) {
69
70
  return new Response(createErrorHtml(404), {
70
71
  status: 404,
@@ -76,7 +76,8 @@ function createRequestHandlerConfig(userConfig) {
76
76
  enableInlineScripts: output === null || output === void 0 ? void 0 : output.enableInlineScripts,
77
77
  enableInlineStyles: output === null || output === void 0 ? void 0 : output.enableInlineStyles,
78
78
  crossorigin: html === null || html === void 0 ? void 0 : html.crossorigin,
79
- scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading
79
+ scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading,
80
+ useJsonScript: server === null || server === void 0 ? void 0 : server.useJsonScript
80
81
  };
81
82
  }
82
83
  export {
@@ -1,6 +1,10 @@
1
1
  const sortRoutes = (route1, route2) => {
2
2
  return route2.urlPath.length - route1.urlPath.length;
3
3
  };
4
+ const uniqueKeyByRoute = (route) => {
5
+ return `${route.entryName}-${route.urlPath}`;
6
+ };
4
7
  export {
5
- sortRoutes
8
+ sortRoutes,
9
+ uniqueKeyByRoute
6
10
  };
@@ -8,6 +8,9 @@ export declare class CustomServer {
8
8
  private serverBase;
9
9
  constructor(runner: ServerHookRunner, serverBase: ServerBase, pwd: string);
10
10
  getHookMiddleware(entryName: string, routes: ServerRoute[]): Middleware<ServerEnv>;
11
- getServerMiddleware(): Promise<Middleware<ServerNodeEnv & ServerEnv> | Array<Middleware<ServerNodeEnv & ServerEnv>> | undefined>;
11
+ getServerMiddleware(renderMiddlewares?: Middleware<ServerNodeEnv & ServerEnv>[]): Promise<Middleware<ServerNodeEnv & ServerEnv> | Array<Middleware<ServerNodeEnv & ServerEnv>> | undefined>;
12
12
  }
13
13
  export declare function getServerMidFromUnstableMid(serverMiddleware: UnstableMiddleware[]): Array<Middleware<ServerNodeEnv & ServerEnv>>;
14
+ export declare function injectRoute(route: {
15
+ entryName: string;
16
+ }): Middleware<ServerEnv>;
@@ -27,10 +27,13 @@ export interface ServerUserConfig {
27
27
  ssrByEntries?: SSRByEntries;
28
28
  baseUrl?: string | string[];
29
29
  port?: number;
30
- enableMicroFrontendDebug?: boolean;
31
30
  watchOptions?: WatchOptions;
32
31
  compiler?: 'babel' | 'typescript';
33
- enableFrameworkExt?: boolean;
32
+ /**
33
+ * @description use json script tag instead of inline script
34
+ * @default false
35
+ */
36
+ useJsonScript?: boolean;
34
37
  }
35
38
  export type ServerNormalizedConfig = ServerUserConfig;
36
39
  export {};
@@ -16,6 +16,7 @@ export type RequestHandlerConfig = {
16
16
  enableInlineScripts?: boolean | RegExp;
17
17
  ssr?: ServerUserConfig['ssr'];
18
18
  ssrByEntries?: ServerUserConfig['ssrByEntries'];
19
+ useJsonScript?: ServerUserConfig['useJsonScript'];
19
20
  };
20
21
  export type LoaderContext = Map<string, any>;
21
22
  export type OnError = (err: unknown) => void;
@@ -43,6 +43,10 @@ type ServerVariables = {
43
43
  * Custom by ssrRuntime.
44
44
  */
45
45
  locals?: Record<string, any>;
46
+ /**
47
+ * The current matched route, now only expose entryName field.
48
+ */
49
+ route: Required<Pick<ServerRoute, 'entryName'>>;
46
50
  };
47
51
  export type ServerEnv = {
48
52
  Variables: ServerVariables;
@@ -1,2 +1,3 @@
1
1
  import type { ServerRoute } from '@modern-js/types';
2
2
  export declare const sortRoutes: (route1: ServerRoute, route2: ServerRoute) => number;
3
+ export declare const uniqueKeyByRoute: (route: ServerRoute) => string;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.63.1",
18
+ "version": "2.63.2",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -53,9 +53,9 @@
53
53
  "flatted": "^3.2.9",
54
54
  "hono": "^3.12.2",
55
55
  "ts-deepmerge": "7.0.2",
56
- "@modern-js/runtime-utils": "2.63.1",
57
- "@modern-js/plugin": "2.63.1",
58
- "@modern-js/utils": "2.63.1"
56
+ "@modern-js/runtime-utils": "2.63.2",
57
+ "@modern-js/plugin": "2.63.2",
58
+ "@modern-js/utils": "2.63.2"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/jest": "^29",
@@ -65,9 +65,9 @@
65
65
  "jest": "^29",
66
66
  "ts-jest": "^29.1.0",
67
67
  "typescript": "^5",
68
- "@modern-js/types": "2.63.1",
69
- "@scripts/build": "2.63.1",
70
- "@scripts/jest-config": "2.63.1"
68
+ "@scripts/build": "2.63.2",
69
+ "@modern-js/types": "2.63.2",
70
+ "@scripts/jest-config": "2.63.2"
71
71
  },
72
72
  "sideEffects": false,
73
73
  "publishConfig": {