@modern-js/runtime 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 (68) hide show
  1. package/dist/cjs/cli/index.js +5 -1
  2. package/dist/cjs/core/browser/hydrate.js +3 -1
  3. package/dist/cjs/core/browser/index.js +8 -9
  4. package/dist/cjs/core/compatible.js +28 -48
  5. package/dist/cjs/core/plugin/base.js +20 -9
  6. package/dist/cjs/core/plugin/index.js +8 -2
  7. package/dist/cjs/core/react/index.js +11 -18
  8. package/dist/cjs/core/server/requestHandler.js +10 -10
  9. package/dist/cjs/core/server/stream/shared.js +2 -2
  10. package/dist/cjs/core/server/string/index.js +3 -2
  11. package/dist/cjs/core/server/utils.js +2 -5
  12. package/dist/cjs/router/cli/code/index.js +40 -4
  13. package/dist/cjs/router/cli/code/templates.js +13 -8
  14. package/dist/cjs/router/cli/index.js +32 -3
  15. package/dist/cjs/router/runtime/plugin.js +29 -40
  16. package/dist/cjs/router/runtime/plugin.node.js +27 -38
  17. package/dist/cjs/state/runtime/plugin.js +10 -21
  18. package/dist/esm/cli/index.js +10 -11
  19. package/dist/esm/core/browser/hydrate.js +3 -1
  20. package/dist/esm/core/browser/index.js +28 -13
  21. package/dist/esm/core/compatible.js +47 -56
  22. package/dist/esm/core/plugin/base.js +20 -9
  23. package/dist/esm/core/plugin/index.js +12 -3
  24. package/dist/esm/core/react/index.js +12 -20
  25. package/dist/esm/core/server/requestHandler.js +33 -14
  26. package/dist/esm/core/server/stream/shared.js +3 -3
  27. package/dist/esm/core/server/string/index.js +4 -3
  28. package/dist/esm/core/server/utils.js +2 -5
  29. package/dist/esm/router/cli/code/index.js +66 -16
  30. package/dist/esm/router/cli/code/templates.js +13 -8
  31. package/dist/esm/router/cli/index.js +48 -4
  32. package/dist/esm/router/runtime/plugin.js +32 -47
  33. package/dist/esm/router/runtime/plugin.node.js +27 -42
  34. package/dist/esm/state/runtime/plugin.js +11 -24
  35. package/dist/esm-node/cli/index.js +5 -1
  36. package/dist/esm-node/core/browser/hydrate.js +3 -1
  37. package/dist/esm-node/core/browser/index.js +8 -9
  38. package/dist/esm-node/core/compatible.js +28 -48
  39. package/dist/esm-node/core/plugin/base.js +18 -9
  40. package/dist/esm-node/core/plugin/index.js +10 -4
  41. package/dist/esm-node/core/react/index.js +11 -18
  42. package/dist/esm-node/core/server/requestHandler.js +10 -10
  43. package/dist/esm-node/core/server/stream/shared.js +2 -2
  44. package/dist/esm-node/core/server/string/index.js +3 -2
  45. package/dist/esm-node/core/server/utils.js +2 -5
  46. package/dist/esm-node/router/cli/code/index.js +41 -5
  47. package/dist/esm-node/router/cli/code/templates.js +13 -8
  48. package/dist/esm-node/router/cli/index.js +23 -4
  49. package/dist/esm-node/router/runtime/plugin.js +29 -40
  50. package/dist/esm-node/router/runtime/plugin.node.js +27 -38
  51. package/dist/esm-node/state/runtime/plugin.js +10 -21
  52. package/dist/types/core/compatible.d.ts +1 -1
  53. package/dist/types/core/context/index.d.ts +1 -1
  54. package/dist/types/core/context/runtime.d.ts +1 -1
  55. package/dist/types/core/plugin/base.d.ts +26 -36
  56. package/dist/types/core/plugin/index.d.ts +5 -12
  57. package/dist/types/core/plugin/runner.d.ts +4 -11
  58. package/dist/types/core/react/index.d.ts +1 -5
  59. package/dist/types/core/server/utils.d.ts +1 -1
  60. package/dist/types/router/cli/code/templates.d.ts +2 -2
  61. package/dist/types/router/runtime/plugin.d.ts +1 -1
  62. package/dist/types/router/runtime/plugin.node.d.ts +1 -1
  63. package/dist/types/state/runtime/plugin.d.ts +1 -1
  64. package/package.json +10 -10
  65. package/dist/cjs/core/utils/merge.js +0 -53
  66. package/dist/esm/core/utils/merge.js +0 -32
  67. package/dist/esm-node/core/utils/merge.js +0 -29
  68. package/dist/types/core/utils/merge.d.ts +0 -6
@@ -3,28 +3,20 @@ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_
3
3
  import { jsx as _jsx } from "react/jsx-runtime";
4
4
  import { RuntimeReactContext, getGlobalApp } from "../context";
5
5
  import { getGlobalRunner } from "../plugin/runner";
6
- function createRoot(UserApp, config) {
6
+ function createRoot(UserApp) {
7
7
  var App = UserApp || getGlobalApp();
8
8
  var runner = getGlobalRunner();
9
- var HOCApp = runner.hoc({
10
- App,
11
- config: config || {}
12
- }, {
13
- onLast: function(param) {
14
- var App2 = param.App;
15
- var WrapComponent = function(_param) {
16
- var _internal_context = _param._internal_context, props = _object_without_properties(_param, [
17
- "_internal_context"
18
- ]);
19
- return /* @__PURE__ */ _jsx(RuntimeReactContext.Provider, {
20
- value: _internal_context,
21
- children: /* @__PURE__ */ _jsx(App2, _object_spread({}, props))
22
- });
23
- };
24
- return WrapComponent;
25
- }
26
- });
27
- return HOCApp;
9
+ var WrapperApp = runner.wrapRoot(App);
10
+ var WrapComponent = function(_param) {
11
+ var _internal_context = _param._internal_context, props = _object_without_properties(_param, [
12
+ "_internal_context"
13
+ ]);
14
+ return /* @__PURE__ */ _jsx(RuntimeReactContext.Provider, {
15
+ value: _internal_context,
16
+ children: /* @__PURE__ */ _jsx(WrapperApp, _object_spread({}, props))
17
+ });
18
+ };
19
+ return WrapComponent;
28
20
  }
29
21
  export {
30
22
  createRoot
@@ -13,7 +13,7 @@ import { createLoaderManager } from "../loader/loaderManager";
13
13
  import { getSSRConfigByEntry, getSSRMode } from "./utils";
14
14
  import { CHUNK_CSS_PLACEHOLDER } from "./constants";
15
15
  function createSSRContext(request, options) {
16
- var config = options.config, loaderContext = options.loaderContext, onError = options.onError, onTiming = options.onTiming, locals = options.locals, resource = options.resource, params = options.params, responseProxy = options.responseProxy, staticGenerate = options.staticGenerate, logger = options.logger, metrics = options.metrics, reporter = options.reporter;
16
+ var config = options.config, loaderContext = options.loaderContext, onError = options.onError, onTiming = options.onTiming, locals = options.locals, resource = options.resource, params = options.params, responseProxy = options.responseProxy, logger = options.logger, metrics = options.metrics, reporter = options.reporter;
17
17
  var nonce = config.nonce;
18
18
  var entryName = resource.entryName, route = resource.route;
19
19
  var cookie = request.headers.get("cookie");
@@ -22,7 +22,7 @@ function createSSRContext(request, options) {
22
22
  var query = parseQuery(request);
23
23
  var headersData = parseHeaders(request);
24
24
  var url = new URL(request.url);
25
- var ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries, staticGenerate);
25
+ var ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
26
26
  var ssrMode = getSSRMode(ssrConfig);
27
27
  var loaderFailureMode = typeof ssrConfig === "object" ? ssrConfig.loaderFailureMode : void 0;
28
28
  return {
@@ -66,7 +66,7 @@ var createRequestHandler = function() {
66
66
  return _ts_generator(this, function(_state) {
67
67
  requestHandler = function() {
68
68
  var _ref2 = _async_to_generator(function(request, options) {
69
- var Root, runner, routeManifest, context, runInit, responseProxy, ssrContext, getRedirectResponse, initialData, redirectResponse, htmlTemplate, response;
69
+ var Root, runner, routeManifest, context, runBeforeRender, responseProxy, ssrContext, getRedirectResponse, initialData, redirectResponse, htmlTemplate, response;
70
70
  return _ts_generator(this, function(_state2) {
71
71
  switch (_state2.label) {
72
72
  case 0:
@@ -74,17 +74,36 @@ var createRequestHandler = function() {
74
74
  runner = getGlobalRunner();
75
75
  routeManifest = options.resource.routeManifest;
76
76
  context = getInitialContext(runner, false, routeManifest);
77
- runInit = function(_context) {
78
- return runner.init({
79
- context: _context
80
- }, {
81
- onLast: function(param) {
82
- var context1 = param.context;
83
- var init = getGlobalAppInit();
84
- return init === null || init === void 0 ? void 0 : init(context1);
85
- }
77
+ runBeforeRender = function() {
78
+ var _ref3 = _async_to_generator(function(context2) {
79
+ var result, init;
80
+ return _ts_generator(this, function(_state3) {
81
+ switch (_state3.label) {
82
+ case 0:
83
+ return [
84
+ 4,
85
+ runner.beforeRender(context2)
86
+ ];
87
+ case 1:
88
+ result = _state3.sent();
89
+ if (typeof Response !== "undefined" && _instanceof(result, Response)) {
90
+ return [
91
+ 2,
92
+ result
93
+ ];
94
+ }
95
+ init = getGlobalAppInit();
96
+ return [
97
+ 2,
98
+ init === null || init === void 0 ? void 0 : init(context2)
99
+ ];
100
+ }
101
+ });
86
102
  });
87
- };
103
+ return function runBeforeRender2(context2) {
104
+ return _ref3.apply(this, arguments);
105
+ };
106
+ }();
88
107
  responseProxy = {
89
108
  headers: {},
90
109
  code: -1
@@ -120,7 +139,7 @@ var createRequestHandler = function() {
120
139
  };
121
140
  return [
122
141
  4,
123
- runInit(context)
142
+ runBeforeRender(context)
124
143
  ];
125
144
  case 1:
126
145
  initialData = _state2.sent();
@@ -39,16 +39,16 @@ function createRenderStreaming(createReadableStreamFromElement) {
39
39
  return [
40
40
  2,
41
41
  run(headersData, /* @__PURE__ */ _async_to_generator(function() {
42
- var end, runtimeContext, config, resource, staticGenerate, onError, onTiming, htmlTemplate, entryName, ssrConfig, rootElement, stream;
42
+ var end, runtimeContext, config, resource, onError, onTiming, htmlTemplate, entryName, ssrConfig, rootElement, stream;
43
43
  return _ts_generator(this, function(_state2) {
44
44
  switch (_state2.label) {
45
45
  case 0:
46
46
  end = time();
47
- runtimeContext = options.runtimeContext, config = options.config, resource = options.resource, staticGenerate = options.staticGenerate;
47
+ runtimeContext = options.runtimeContext, config = options.config, resource = options.resource;
48
48
  onError = createOnError(options.onError);
49
49
  onTiming = createOnTiming(options.onTiming);
50
50
  htmlTemplate = resource.htmlTemplate, entryName = resource.entryName;
51
- ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries, staticGenerate);
51
+ ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
52
52
  rootElement = React.cloneElement(serverRoot, {
53
53
  _internal_context: Object.assign(runtimeContext, {
54
54
  ssr: true
@@ -25,18 +25,18 @@ var renderString = function() {
25
25
  return [
26
26
  2,
27
27
  run(headersData, /* @__PURE__ */ _async_to_generator(function() {
28
- var _runtimeContext_ssrContext, resource, runtimeContext, config, onError, onTiming, staticGenerate, tracer, routerContext, htmlTemplate, entryName, loadableStats, routeManifest, ssrConfig, chunkSet, prefetchData, e, collectors, rootElement, html;
28
+ var _runtimeContext_ssrContext, resource, runtimeContext, config, onError, onTiming, tracer, routerContext, htmlTemplate, entryName, loadableStats, routeManifest, ssrConfig, chunkSet, prefetchData, e, collectors, rootElement, html;
29
29
  return _ts_generator(this, function(_state2) {
30
30
  switch (_state2.label) {
31
31
  case 0:
32
- resource = options.resource, runtimeContext = options.runtimeContext, config = options.config, onError = options.onError, onTiming = options.onTiming, staticGenerate = options.staticGenerate;
32
+ resource = options.resource, runtimeContext = options.runtimeContext, config = options.config, onError = options.onError, onTiming = options.onTiming;
33
33
  tracer = {
34
34
  onError: createOnError(onError),
35
35
  onTiming: createOnTiming(onTiming)
36
36
  };
37
37
  routerContext = runtimeContext.routerContext;
38
38
  htmlTemplate = resource.htmlTemplate, entryName = resource.entryName, loadableStats = resource.loadableStats, routeManifest = resource.routeManifest;
39
- ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries, staticGenerate);
39
+ ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
40
40
  chunkSet = {
41
41
  renderLevel: RenderLevel.CLIENT_RENDER,
42
42
  ssrScripts: "",
@@ -66,6 +66,7 @@ var renderString = function() {
66
66
  case 3:
67
67
  e = _state2.sent();
68
68
  chunkSet.renderLevel = RenderLevel.CLIENT_RENDER;
69
+ tracer.onError(SSRErrors.PRERENDER, e);
69
70
  return [
70
71
  3,
71
72
  4
@@ -58,14 +58,11 @@ function serializeErrors(errors) {
58
58
  }
59
59
  return serialized;
60
60
  }
61
- function getSSRConfigByEntry(entryName, ssr, ssrByEntries, staticGenerate) {
62
- if (staticGenerate) {
63
- return true;
64
- }
61
+ function getSSRConfigByEntry(entryName, ssr, ssrByEntries) {
65
62
  if (ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[entryName]) {
66
63
  return ssrByEntries[entryName];
67
64
  }
68
- return ssr;
65
+ return ssr || true;
69
66
  }
70
67
  function getSSRMode(ssrConfig) {
71
68
  if (typeof ssrConfig === "boolean") {
@@ -1,7 +1,8 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import path from "path";
4
- import { fs, getEntryOptions, isRouterV5, isSSGEntry, isUseSSRBundle, logger } from "@modern-js/utils";
4
+ import { fs, getEntryOptions, isRouterV5, isSSGEntry, isUseSSRBundle, logger, filterRoutesForServer, filterRoutesLoader } from "@modern-js/utils";
5
+ import { cloneDeep } from "@modern-js/utils/lodash";
5
6
  import { FILE_SYSTEM_ROUTES_FILE_NAME } from "../constants";
6
7
  import { ENTRY_POINT_RUNTIME_GLOBAL_CONTEXT_FILE_NAME } from "../../../cli/constants";
7
8
  import * as templates from "./templates";
@@ -16,7 +17,7 @@ var generateCode = function() {
16
17
  }
17
18
  function _generateEntryCode() {
18
19
  _generateEntryCode = _async_to_generator(function(entrypoint) {
19
- var entryName, isMainEntry, isAutoMount, pageRoutesEntry, nestedRoutesEntry, metaName, _config_output, initialRoutes, nestedRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, routes, _$config, ssr, useSSG, mode, hasPageRoute, code, _, _tmp, routesServerFile, code1, serverLoaderCombined, serverLoaderFile;
20
+ var entryName, isMainEntry, isAutoMount, pageRoutesEntry, nestedRoutesEntry, metaName, _config_output, initialRoutes, nestedRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, _$config, ssrByRouteIds, clonedRoutes, markedRoutes, routes, ssr, useSSG, mode, hasPageRoute, code, _, _tmp, _config_output1, routesServerFile, filtedRoutesForServer, routesForServerLoaderMatches, code1, serverRoutesCode, serverLoaderCombined, serverLoaderFile;
20
21
  return _ts_generator(this, function(_state) {
21
22
  switch (_state.label) {
22
23
  case 0:
@@ -25,12 +26,12 @@ var generateCode = function() {
25
26
  if (!isAutoMount)
26
27
  return [
27
28
  3,
28
- 11
29
+ 14
29
30
  ];
30
31
  if (!(pageRoutesEntry || nestedRoutesEntry))
31
32
  return [
32
33
  3,
33
- 11
34
+ 14
34
35
  ];
35
36
  initialRoutes = [];
36
37
  nestedRoutes = null;
@@ -86,16 +87,19 @@ var generateCode = function() {
86
87
  }
87
88
  _state.label = 2;
88
89
  case 2:
90
+ _$config = api.useResolvedConfigContext();
91
+ ssrByRouteIds = _$config.server.ssrByRouteIds || [];
92
+ clonedRoutes = cloneDeep(initialRoutes);
93
+ markedRoutes = ssrByRouteIds.length > 0 ? markRoutes(clonedRoutes, ssrByRouteIds) : initialRoutes;
89
94
  return [
90
95
  4,
91
96
  hookRunners.modifyFileSystemRoutes({
92
97
  entrypoint,
93
- routes: initialRoutes
98
+ routes: markedRoutes
94
99
  })
95
100
  ];
96
101
  case 3:
97
102
  routes = _state.sent().routes;
98
- _$config = api.useResolvedConfigContext();
99
103
  ssr = getEntryOptions(entryName, isMainEntry, _$config.server.ssr, _$config.server.ssrByEntries, packageName);
100
104
  useSSG = isSSGEntry(_$config, entryName, entrypoints);
101
105
  if (ssr) {
@@ -138,11 +142,13 @@ var generateCode = function() {
138
142
  if (!(entrypoint.nestedRoutesEntry && isUseSSRBundle(_$config)))
139
143
  return [
140
144
  3,
141
- 8
145
+ 10
142
146
  ];
143
147
  routesServerFile = getServerLoadersFile(internalDirectory, entryName);
148
+ filtedRoutesForServer = filterRoutesForServer(routes);
149
+ routesForServerLoaderMatches = filterRoutesLoader(routes);
144
150
  code1 = templates.routesForServer({
145
- routes
151
+ routesForServerLoaderMatches
146
152
  });
147
153
  return [
148
154
  4,
@@ -156,26 +162,51 @@ var generateCode = function() {
156
162
  ];
157
163
  case 7:
158
164
  _state.sent();
159
- _state.label = 8;
165
+ return [
166
+ 4,
167
+ templates.fileSystemRoutes({
168
+ metaName,
169
+ routes: filtedRoutesForServer,
170
+ ssrMode: useSSG ? "string" : mode,
171
+ nestedRoutesEntry: entrypoint.nestedRoutesEntry,
172
+ entryName: entrypoint.entryName,
173
+ internalDirectory,
174
+ splitRouteChunks: _$config === null || _$config === void 0 ? void 0 : (_config_output1 = _$config.output) === null || _config_output1 === void 0 ? void 0 : _config_output1.splitRouteChunks
175
+ })
176
+ ];
160
177
  case 8:
178
+ serverRoutesCode = _state.sent();
179
+ return [
180
+ 4,
181
+ fs.outputFile(path.resolve(internalDirectory, "./".concat(entryName, "/routes.server.js")), serverRoutesCode, "utf8")
182
+ ];
183
+ case 9:
184
+ _state.sent();
185
+ _state.label = 10;
186
+ case 10:
161
187
  serverLoaderCombined = templates.ssrLoaderCombinedModule(entrypoints, entrypoint, _$config, appContext);
162
188
  if (!serverLoaderCombined)
163
189
  return [
164
190
  3,
165
- 10
191
+ 12
166
192
  ];
167
193
  serverLoaderFile = getServerCombinedModueFile(internalDirectory, entryName);
168
194
  return [
169
195
  4,
170
196
  fs.outputFile(serverLoaderFile, serverLoaderCombined)
171
197
  ];
172
- case 9:
173
- _state.sent();
174
- _state.label = 10;
175
- case 10:
176
- fs.outputFileSync(path.resolve(internalDirectory, "./".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_FILE_NAME)), code, "utf8");
177
- _state.label = 11;
178
198
  case 11:
199
+ _state.sent();
200
+ _state.label = 12;
201
+ case 12:
202
+ return [
203
+ 4,
204
+ fs.outputFile(path.resolve(internalDirectory, "./".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_FILE_NAME)), code, "utf8")
205
+ ];
206
+ case 13:
207
+ _state.sent();
208
+ _state.label = 14;
209
+ case 14:
179
210
  return [
180
211
  2
181
212
  ];
@@ -208,6 +239,25 @@ var generateCode = function() {
208
239
  return _ref.apply(this, arguments);
209
240
  };
210
241
  }();
242
+ function markRoutes(routes, routeIds) {
243
+ return routes.map(function(route) {
244
+ if (route.type !== "nested") {
245
+ return route;
246
+ }
247
+ if (route.children && route.children.length > 0) {
248
+ route.children = markRoutes(route.children, routeIds);
249
+ }
250
+ if (route.children && route.children.length > 0) {
251
+ route.inValidSSRRoute = route.children.every(function(child) {
252
+ var _child_inValidSSRRoute;
253
+ return (_child_inValidSSRRoute = child.inValidSSRRoute) !== null && _child_inValidSSRRoute !== void 0 ? _child_inValidSSRRoute : false;
254
+ });
255
+ } else if (route.id) {
256
+ route.inValidSSRRoute = !routeIds.includes(route.id);
257
+ }
258
+ return route;
259
+ });
260
+ }
211
261
  function generatorRegisterCode(internalDirectory, entryName, code) {
212
262
  fs.outputFileSync(path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_POINT_RUNTIME_GLOBAL_CONTEXT_FILE_NAME)), code, "utf8");
213
263
  }
@@ -9,7 +9,7 @@ import { ROUTE_MODULES } from "@modern-js/utils/universal/constants";
9
9
  import { APP_CONFIG_NAME, APP_INIT_EXPORTED, TEMP_LOADERS_DIR } from "../constants";
10
10
  import { getPathWithoutExt, getServerLoadersFile, parseModule, replaceWithAlias } from "./utils";
11
11
  var routesForServer = function(param) {
12
- var routes = param.routes;
12
+ var routesForServerLoaderMatches = param.routesForServerLoaderMatches;
13
13
  var loaders = [];
14
14
  var actions = [];
15
15
  var loadersMap = {};
@@ -51,7 +51,7 @@ var routesForServer = function(param) {
51
51
  var routesCode = "\n export const routes = [\n ";
52
52
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
53
53
  try {
54
- for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
54
+ for (var _iterator = routesForServerLoaderMatches[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
55
55
  var route = _step.value;
56
56
  if ("type" in route) {
57
57
  var keywords = [
@@ -136,13 +136,14 @@ var fileSystemRoutes = function() {
136
136
  importLazyCode = '\n import { lazy } from "react";\n import loadable, { lazy as loadableLazy } from "@'.concat(metaName, '/runtime/loadable"\n ');
137
137
  rootLayoutCode = "";
138
138
  getDataLoaderPath = function(param2) {
139
- var loaderId = param2.loaderId, clientData = param2.clientData, action = param2.action, inline = param2.inline, routeId = param2.routeId;
139
+ var loaderId = param2.loaderId, clientData = param2.clientData, action = param2.action, inline = param2.inline, routeId = param2.routeId, inValidSSRRoute = param2.inValidSSRRoute;
140
140
  if (!ssrMode) {
141
141
  return "";
142
142
  }
143
143
  var clientDataStr = clientData ? "&clientData=".concat(clientData) : "";
144
+ var retain = inValidSSRRoute !== null && inValidSSRRoute !== void 0 ? inValidSSRRoute : false;
144
145
  if (nestedRoutesEntry) {
145
- return "?loaderId=".concat(loaderId).concat(clientDataStr, "&action=").concat(action ? slash(action) : action, "&inline=").concat(inline, "&routeId=").concat(routeId);
146
+ return "?loaderId=".concat(loaderId).concat(clientDataStr, "&action=").concat(action ? slash(action) : action, "&inline=").concat(inline, "&routeId=").concat(routeId, "&retain=").concat(retain);
146
147
  }
147
148
  return "";
148
149
  };
@@ -176,6 +177,7 @@ var fileSystemRoutes = function() {
176
177
  loadersMap[loader] = {
177
178
  loaderId,
178
179
  routeId: route2.id,
180
+ inValidSSRRoute: route2.inValidSSRRoute,
179
181
  filePath: route2.data || route2.loader,
180
182
  clientData: Boolean(route2.clientData),
181
183
  route: route2,
@@ -316,7 +318,8 @@ var fileSystemRoutes = function() {
316
318
  clientData: loaderInfo.clientData,
317
319
  action: route1.action,
318
320
  inline: loaderInfo.inline,
319
- routeId: loaderInfo.routeId
321
+ routeId: loaderInfo.routeId,
322
+ inValidSSRRoute: loaderInfo.inValidSSRRoute
320
323
  }), '";\n');
321
324
  } else {
322
325
  importLoadersCode += "import { loader as ".concat(key, ' } from "').concat(slash(loaderInfo.filePath)).concat(getDataLoaderPath({
@@ -324,7 +327,8 @@ var fileSystemRoutes = function() {
324
327
  clientData: loaderInfo.clientData,
325
328
  action: false,
326
329
  inline: loaderInfo.inline,
327
- routeId: route1.id
330
+ routeId: route1.id,
331
+ inValidSSRRoute: loaderInfo.inValidSSRRoute
328
332
  }), '";\n');
329
333
  }
330
334
  } else {
@@ -333,7 +337,8 @@ var fileSystemRoutes = function() {
333
337
  clientData: loaderInfo.clientData,
334
338
  action: false,
335
339
  inline: loaderInfo.inline,
336
- routeId: loaderInfo.routeId
340
+ routeId: loaderInfo.routeId,
341
+ inValidSSRRoute: loaderInfo.inValidSSRRoute
337
342
  }), '";\n');
338
343
  }
339
344
  }
@@ -496,7 +501,7 @@ var runtimeGlobalContext = function() {
496
501
  }
497
502
  return [
498
503
  2,
499
- "".concat(imports.join("\n"), "\n\nimport { routes } from './routes.js';\n\nsetGlobalContext({\n layoutApp,\n routes,\n appInit,\n appConfig,\n});")
504
+ "".concat(imports.join("\n"), "\n\nimport { routes } from './routes';\n\nsetGlobalContext({\n layoutApp,\n routes,\n appInit,\n appConfig,\n});")
500
505
  ];
501
506
  }
502
507
  });
@@ -2,7 +2,8 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
3
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
- import { createRuntimeExportsUtils, getEntryOptions, isRouterV5 as isV5 } from "@modern-js/utils";
5
+ import path from "node:path";
6
+ import { createRuntimeExportsUtils, filterRoutesForServer, fs, getEntryOptions, isRouterV5 as isV5, NESTED_ROUTE_SPEC_FILE } from "@modern-js/utils";
6
7
  import { isRouteEntry } from "./entry";
7
8
  import { handleFileChange, handleGeneratorEntryCode, handleModifyEntrypoints } from "./handler";
8
9
  import { isRouteEntry as isRouteEntry2 } from "./entry";
@@ -14,6 +15,8 @@ var routerPlugin = function() {
14
15
  "@modern-js/runtime"
15
16
  ],
16
17
  setup: function(api) {
18
+ var nestedRoutes = {};
19
+ var nestedRoutesForServer = {};
17
20
  return {
18
21
  _internalRuntimePlugins: function _internalRuntimePlugins(param) {
19
22
  var entrypoint = param.entrypoint, plugins = param.plugins;
@@ -45,10 +48,10 @@ var routerPlugin = function() {
45
48
  };
46
49
  },
47
50
  checkEntryPoint: function checkEntryPoint(param) {
48
- var path = param.path, entry = param.entry;
51
+ var path2 = param.path, entry = param.entry;
49
52
  return {
50
- path,
51
- entry: entry || isRouteEntry(path)
53
+ path: path2,
54
+ entry: entry || isRouteEntry(path2)
52
55
  };
53
56
  },
54
57
  config: function config() {
@@ -132,6 +135,47 @@ var routerPlugin = function() {
132
135
  }
133
136
  });
134
137
  })();
138
+ },
139
+ modifyFileSystemRoutes: function modifyFileSystemRoutes(param) {
140
+ var entrypoint = param.entrypoint, routes = param.routes;
141
+ return _async_to_generator(function() {
142
+ return _ts_generator(this, function(_state) {
143
+ nestedRoutes[entrypoint.entryName] = routes;
144
+ nestedRoutesForServer[entrypoint.entryName] = filterRoutesForServer(routes);
145
+ return [
146
+ 2,
147
+ {
148
+ entrypoint,
149
+ routes
150
+ }
151
+ ];
152
+ });
153
+ })();
154
+ },
155
+ beforeGenerateRoutes: function beforeGenerateRoutes(param) {
156
+ var entrypoint = param.entrypoint, code = param.code;
157
+ return _async_to_generator(function() {
158
+ var distDirectory;
159
+ return _ts_generator(this, function(_state) {
160
+ switch (_state.label) {
161
+ case 0:
162
+ distDirectory = api.useAppContext().distDirectory;
163
+ return [
164
+ 4,
165
+ fs.outputJSON(path.resolve(distDirectory, NESTED_ROUTE_SPEC_FILE), nestedRoutesForServer)
166
+ ];
167
+ case 1:
168
+ _state.sent();
169
+ return [
170
+ 2,
171
+ {
172
+ entrypoint,
173
+ code
174
+ }
175
+ ];
176
+ }
177
+ });
178
+ })();
135
179
  }
136
180
  };
137
181
  }
@@ -5,6 +5,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
5
5
  import { useContext, useMemo } from "react";
6
6
  import { createBrowserRouter, createHashRouter, RouterProvider, createRoutesFromElements, useMatches, useLocation, useHref } from "@modern-js/runtime-utils/router";
7
7
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
8
+ import { merge } from "@modern-js/runtime-utils/merge";
8
9
  import { getGlobalLayoutApp, getGlobalRoutes } from "../../core/context";
9
10
  import { RuntimeReactContext } from "../../core";
10
11
  import { modifyRoutes as modifyRoutesHook } from "./hooks";
@@ -22,28 +23,18 @@ function modifyRoutes(modifyFunction) {
22
23
  console.error("It is not allowed to modify routes config after create router.");
23
24
  }
24
25
  }
25
- var routerPlugin = function(param) {
26
- var _param_serverBase = param.serverBase, serverBase = _param_serverBase === void 0 ? [] : _param_serverBase, _param_supportHtml5History = param.supportHtml5History, supportHtml5History = _param_supportHtml5History === void 0 ? true : _param_supportHtml5History, _param_basename = param.basename, basename = _param_basename === void 0 ? "" : _param_basename, routesConfig = param.routesConfig, createRoutes = param.createRoutes;
27
- var select = function(pathname) {
28
- return serverBase.find(function(baseUrl) {
29
- return pathname.search(baseUrl) === 0;
30
- }) || "/";
31
- };
32
- var routes = [];
33
- finalRouteConfig = _object_spread({
34
- routes: getGlobalRoutes(),
35
- globalApp: getGlobalLayoutApp()
36
- }, routesConfig);
37
- window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
26
+ var routerPlugin = function() {
27
+ var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
38
28
  return {
39
29
  name: "@modern-js/plugin-router",
40
30
  registerHook: {
41
31
  modifyRoutes: modifyRoutesHook
42
32
  },
43
33
  setup: function(api) {
34
+ var routes = [];
35
+ window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
44
36
  return {
45
- init: function init(param2, next) {
46
- var context = param2.context;
37
+ beforeRender: function beforeRender(context) {
47
38
  context.router = {
48
39
  useMatches,
49
40
  useLocation,
@@ -54,25 +45,29 @@ var routerPlugin = function(param) {
54
45
  return routes;
55
46
  }
56
47
  });
57
- return next({
58
- context
59
- });
60
48
  },
61
- hoc: function(param2, next) {
62
- var App = param2.App, config = param2.config;
49
+ wrapRoot: function(App) {
50
+ var pluginConfig = api.useRuntimeConfigContext();
51
+ var _merge = merge(pluginConfig.router || {}, userConfig), _merge_serverBase = _merge.serverBase, serverBase = _merge_serverBase === void 0 ? [] : _merge_serverBase, _merge_supportHtml5History = _merge.supportHtml5History, supportHtml5History = _merge_supportHtml5History === void 0 ? true : _merge_supportHtml5History, _merge_basename = _merge.basename, basename = _merge_basename === void 0 ? "" : _merge_basename, routesConfig = _merge.routesConfig, createRoutes = _merge.createRoutes;
52
+ var select = function(pathname) {
53
+ return serverBase.find(function(baseUrl) {
54
+ return pathname.search(baseUrl) === 0;
55
+ }) || "/";
56
+ };
57
+ finalRouteConfig = _object_spread({
58
+ routes: getGlobalRoutes(),
59
+ globalApp: getGlobalLayoutApp()
60
+ }, routesConfig);
63
61
  if (!finalRouteConfig.routes && !createRoutes) {
64
- return next({
65
- App,
66
- config
67
- });
62
+ return App;
68
63
  }
69
64
  var getRouteApp = function() {
70
65
  var useCreateRouter = function(props) {
71
- var _config_router, _window__SERVER_DATA;
72
- var baseUrl = ((config === null || config === void 0 ? void 0 : (_config_router = config.router) === null || _config_router === void 0 ? void 0 : _config_router.basename) || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
66
+ var _window__SERVER_DATA;
67
+ var runtimeContext = useContext(RuntimeReactContext);
68
+ var baseUrl = (runtimeContext._internalRouterBaseName || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
73
69
  var _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
74
70
  var hydrationData = window._ROUTER_DATA;
75
- var runtimeContext = useContext(RuntimeReactContext);
76
71
  var getBlockNavState = runtimeContext.unstable_getBlockNavState;
77
72
  return useMemo(function() {
78
73
  if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
@@ -111,7 +106,8 @@ var routerPlugin = function(param) {
111
106
  get: function get() {
112
107
  return router;
113
108
  },
114
- configurable: true
109
+ configurable: true,
110
+ enumerable: true
115
111
  });
116
112
  return router;
117
113
  }, [
@@ -130,32 +126,21 @@ var routerPlugin = function(param) {
130
126
  });
131
127
  };
132
128
  };
133
- var RouteApp = getRouteApp();
134
- return next({
135
- App: RouteApp,
136
- config
137
- });
129
+ return getRouteApp();
138
130
  },
139
- pickContext: function(param2, next) {
140
- var context = param2.context, pickedContext = param2.pickedContext;
141
- var remixRouter = context.remixRouter;
131
+ pickContext: function(pickedContext) {
132
+ var remixRouter = pickedContext.remixRouter;
142
133
  if (!remixRouter) {
143
- return next({
144
- context,
145
- pickedContext
146
- });
134
+ return pickedContext;
147
135
  }
148
- var router = {
136
+ var router = _object_spread_props(_object_spread({}, pickedContext.router), {
149
137
  navigate: remixRouter.navigate,
150
138
  get location() {
151
139
  return remixRouter.state.location;
152
140
  }
153
- };
154
- return next({
155
- context,
156
- pickedContext: _object_spread_props(_object_spread({}, pickedContext), {
157
- router
158
- })
141
+ });
142
+ return _object_spread_props(_object_spread({}, pickedContext), {
143
+ router
159
144
  });
160
145
  }
161
146
  };