@modern-js/runtime 2.56.3-alpha.0 → 2.57.1-alpha.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 (55) hide show
  1. package/dist/cjs/core/browser/hydrate.js +3 -1
  2. package/dist/cjs/core/browser/index.js +8 -9
  3. package/dist/cjs/core/compatible.js +28 -48
  4. package/dist/cjs/core/plugin/base.js +20 -9
  5. package/dist/cjs/core/plugin/index.js +8 -2
  6. package/dist/cjs/core/react/index.js +11 -18
  7. package/dist/cjs/core/server/requestHandler.js +11 -10
  8. package/dist/cjs/core/server/stream/shared.js +2 -2
  9. package/dist/cjs/core/server/string/index.js +3 -2
  10. package/dist/cjs/core/server/utils.js +2 -5
  11. package/dist/cjs/router/runtime/plugin.js +29 -40
  12. package/dist/cjs/router/runtime/plugin.node.js +27 -38
  13. package/dist/cjs/state/runtime/plugin.js +10 -21
  14. package/dist/esm/core/browser/hydrate.js +3 -1
  15. package/dist/esm/core/browser/index.js +28 -13
  16. package/dist/esm/core/compatible.js +47 -56
  17. package/dist/esm/core/plugin/base.js +20 -9
  18. package/dist/esm/core/plugin/index.js +12 -3
  19. package/dist/esm/core/react/index.js +12 -20
  20. package/dist/esm/core/server/requestHandler.js +34 -14
  21. package/dist/esm/core/server/stream/shared.js +3 -3
  22. package/dist/esm/core/server/string/index.js +4 -3
  23. package/dist/esm/core/server/utils.js +2 -5
  24. package/dist/esm/router/runtime/plugin.js +32 -47
  25. package/dist/esm/router/runtime/plugin.node.js +27 -42
  26. package/dist/esm/state/runtime/plugin.js +11 -24
  27. package/dist/esm-node/core/browser/hydrate.js +3 -1
  28. package/dist/esm-node/core/browser/index.js +8 -9
  29. package/dist/esm-node/core/compatible.js +28 -48
  30. package/dist/esm-node/core/plugin/base.js +18 -9
  31. package/dist/esm-node/core/plugin/index.js +10 -4
  32. package/dist/esm-node/core/react/index.js +11 -18
  33. package/dist/esm-node/core/server/requestHandler.js +11 -10
  34. package/dist/esm-node/core/server/stream/shared.js +2 -2
  35. package/dist/esm-node/core/server/string/index.js +3 -2
  36. package/dist/esm-node/core/server/utils.js +2 -5
  37. package/dist/esm-node/router/runtime/plugin.js +29 -40
  38. package/dist/esm-node/router/runtime/plugin.node.js +27 -38
  39. package/dist/esm-node/state/runtime/plugin.js +10 -21
  40. package/dist/types/core/compatible.d.ts +1 -1
  41. package/dist/types/core/context/index.d.ts +1 -1
  42. package/dist/types/core/context/runtime.d.ts +1 -1
  43. package/dist/types/core/plugin/base.d.ts +26 -36
  44. package/dist/types/core/plugin/index.d.ts +5 -12
  45. package/dist/types/core/plugin/runner.d.ts +4 -11
  46. package/dist/types/core/react/index.d.ts +1 -5
  47. package/dist/types/core/server/utils.d.ts +1 -1
  48. package/dist/types/router/runtime/plugin.d.ts +1 -1
  49. package/dist/types/router/runtime/plugin.node.d.ts +1 -1
  50. package/dist/types/state/runtime/plugin.d.ts +1 -1
  51. package/package.json +12 -11
  52. package/dist/cjs/core/utils/merge.js +0 -53
  53. package/dist/esm/core/utils/merge.js +0 -32
  54. package/dist/esm-node/core/utils/merge.js +0 -29
  55. package/dist/types/core/utils/merge.d.ts +0 -6
@@ -97,7 +97,9 @@ function hydrateRoot(App, context, ModernRender, ModernHydrate) {
97
97
  });
98
98
  } else {
99
99
  (0, import_component.loadableReady)(() => {
100
- ModernHydrate(App, callback).then((root) => {
100
+ ModernHydrate(/* @__PURE__ */ import_react.default.cloneElement(App, {
101
+ _internal_context: hydrateContext
102
+ }), callback).then((root) => {
101
103
  resolve(root);
102
104
  });
103
105
  });
@@ -64,14 +64,11 @@ function isClientArgs(id) {
64
64
  async function render(App, id) {
65
65
  const runner = (0, import_runner.getGlobalRunner)();
66
66
  const context = (0, import_runtime.getInitialContext)(runner);
67
- const runInit = (_context) => runner.init({
68
- context: _context
69
- }, {
70
- onLast: ({ context: context1 }) => {
71
- const init = (0, import_context.getGlobalAppInit)();
72
- return init === null || init === void 0 ? void 0 : init(context1);
73
- }
74
- });
67
+ const runBeforeRender = async (context2) => {
68
+ await runner.beforeRender(context2);
69
+ const init = (0, import_context.getGlobalAppInit)();
70
+ return init === null || init === void 0 ? void 0 : init(context2);
71
+ };
75
72
  if (isClientArgs(id)) {
76
73
  var _ssrData_data, _ssrData_data1;
77
74
  const ssrData = getSSRData();
@@ -88,12 +85,14 @@ async function render(App, id) {
88
85
  loaderManager: (0, import_loaderManager.createLoaderManager)(initialLoadersState, {
89
86
  skipStatic: true
90
87
  }),
88
+ // garfish plugin params
89
+ _internalRouterBaseName: App.props.basename,
91
90
  ...ssrData ? {
92
91
  ssrContext: ssrData === null || ssrData === void 0 ? void 0 : ssrData.context
93
92
  } : {}
94
93
  });
95
94
  context.initialData = ssrData === null || ssrData === void 0 ? void 0 : (_ssrData_data1 = ssrData.data) === null || _ssrData_data1 === void 0 ? void 0 : _ssrData_data1.initialData;
96
- const initialData = await runInit(context);
95
+ const initialData = await runBeforeRender(context);
97
96
  if (initialData) {
98
97
  context.initialData = initialData;
99
98
  }
@@ -65,53 +65,38 @@ const createApp = ({ plugins, runtime, props: globalProps }) => {
65
65
  ...props
66
66
  }) : child));
67
67
  };
68
- const HOCApp = runner.hoc({
69
- App: WrapperComponent,
70
- config: globalProps || {}
71
- }, {
72
- onLast: ({ App: App2 }) => {
73
- const WrapComponent = ({ _internal_context, ...props }) => {
74
- let contextValue = _internal_context;
75
- if (!(contextValue === null || contextValue === void 0 ? void 0 : contextValue.runner)) {
76
- contextValue = getInitialContext(runner);
77
- runner.init({
78
- context: contextValue
79
- }, {
80
- onLast: ({ context: context1 }) => {
81
- var _getGlobalAppInit;
82
- return (_getGlobalAppInit = (0, import_context.getGlobalAppInit)()) === null || _getGlobalAppInit === void 0 ? void 0 : _getGlobalAppInit(context1);
83
- }
84
- });
85
- }
86
- const mergedProps = {
87
- ...props,
88
- ...globalProps
89
- };
90
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_runtime.RuntimeReactContext.Provider, {
91
- value: contextValue,
92
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App2, {
93
- ...mergedProps
94
- })
95
- });
96
- };
97
- return WrapComponent;
68
+ const WrapperApp = runner.wrapRoot(WrapperComponent);
69
+ const WrapComponent = ({ _internal_context, ...props }) => {
70
+ let contextValue = _internal_context;
71
+ if (!(contextValue === null || contextValue === void 0 ? void 0 : contextValue.runner)) {
72
+ var _getGlobalAppInit;
73
+ contextValue = getInitialContext(runner);
74
+ runner === null || runner === void 0 ? void 0 : runner.beforeRender(contextValue);
75
+ (_getGlobalAppInit = (0, import_context.getGlobalAppInit)()) === null || _getGlobalAppInit === void 0 ? void 0 : _getGlobalAppInit(contextValue);
98
76
  }
99
- });
100
- return HOCApp;
77
+ const mergedProps = {
78
+ ...props,
79
+ ...globalProps
80
+ };
81
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_runtime.RuntimeReactContext.Provider, {
82
+ value: contextValue,
83
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(WrapperApp, {
84
+ ...mergedProps
85
+ })
86
+ });
87
+ };
88
+ return WrapComponent;
101
89
  };
102
90
  };
103
91
  const bootstrap = async (BootApp, id, root, ReactDOM) => {
104
92
  const App = BootApp;
105
93
  const runner = (0, import_runner.getGlobalRunner)();
106
94
  const context = getInitialContext(runner);
107
- const runInit = (_context) => runner.init({
108
- context: _context
109
- }, {
110
- onLast: ({ context: context1 }) => {
111
- const init = (0, import_context.getGlobalAppInit)();
112
- return init === null || init === void 0 ? void 0 : init(context1);
113
- }
114
- });
95
+ const runBeforeRender = async (context2) => {
96
+ await runner.beforeRender(context2);
97
+ const init = (0, import_context.getGlobalAppInit)();
98
+ return init === null || init === void 0 ? void 0 : init(context2);
99
+ };
115
100
  if (!id) {
116
101
  return /* @__PURE__ */ import_react.default.createElement(App, {
117
102
  _internal_context: context
@@ -140,7 +125,7 @@ const bootstrap = async (BootApp, id, root, ReactDOM) => {
140
125
  } : {}
141
126
  });
142
127
  context.initialData = ssrData === null || ssrData === void 0 ? void 0 : (_ssrData_data1 = ssrData.data) === null || _ssrData_data1 === void 0 ? void 0 : _ssrData_data1.initialData;
143
- const initialData = await runInit(context);
128
+ const initialData = await runBeforeRender(context);
144
129
  if (initialData) {
145
130
  context.initialData = initialData;
146
131
  }
@@ -200,16 +185,11 @@ const useRuntimeContext = () => {
200
185
  var _context_ssrContext, _context_ssrContext1;
201
186
  const context = (0, import_react.useContext)(import_runtime.RuntimeReactContext);
202
187
  const pickedContext = {
203
- initialData: context.initialData,
188
+ ...context,
204
189
  request: (_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.request,
205
190
  response: (_context_ssrContext1 = context.ssrContext) === null || _context_ssrContext1 === void 0 ? void 0 : _context_ssrContext1.response
206
191
  };
207
- const memoizedContext = (0, import_react.useMemo)(() => context.runner.pickContext({
208
- context,
209
- pickedContext
210
- }, {
211
- onLast: ({ pickedContext: pickedContext2 }) => pickedContext2
212
- }), [
192
+ const memoizedContext = (0, import_react.useMemo)(() => context.runner.pickContext(pickedContext), [
213
193
  context
214
194
  ]);
215
195
  return memoizedContext;
@@ -18,23 +18,34 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var base_exports = {};
20
20
  __export(base_exports, {
21
+ RuntimeConfigContext: () => RuntimeConfigContext,
21
22
  createRuntime: () => createRuntime,
22
- runtime: () => runtime
23
+ runtime: () => runtime,
24
+ useRuntimeConfigContext: () => useRuntimeConfigContext
23
25
  });
24
26
  module.exports = __toCommonJS(base_exports);
25
27
  var import_plugin = require("@modern-js/plugin");
26
- const hoc = (0, import_plugin.createPipeline)();
27
- const init = (0, import_plugin.createAsyncPipeline)();
28
- const pickContext = (0, import_plugin.createPipeline)();
28
+ const RuntimeConfigContext = (0, import_plugin.createContext)({});
29
+ const useRuntimeConfigContext = () => RuntimeConfigContext.use().value;
30
+ const wrapRoot = (0, import_plugin.createWaterfall)();
31
+ const beforeRender = (0, import_plugin.createAsyncInterruptWorkflow)();
32
+ const pickContext = (0, import_plugin.createWaterfall)();
33
+ const modifyRuntimeConfig = (0, import_plugin.createSyncParallelWorkflow)();
29
34
  const runtimeHooks = {
30
- hoc,
31
- init,
32
- pickContext
35
+ beforeRender,
36
+ wrapRoot,
37
+ pickContext,
38
+ modifyRuntimeConfig
33
39
  };
34
- const createRuntime = () => (0, import_plugin.createManager)(runtimeHooks);
40
+ const runtimePluginAPI = {
41
+ useRuntimeConfigContext
42
+ };
43
+ const createRuntime = () => (0, import_plugin.createManager)(runtimeHooks, runtimePluginAPI);
35
44
  const runtime = createRuntime();
36
45
  // Annotate the CommonJS export names for ESM import in node:
37
46
  0 && (module.exports = {
47
+ RuntimeConfigContext,
38
48
  createRuntime,
39
- runtime
49
+ runtime,
50
+ useRuntimeConfigContext
40
51
  });
@@ -23,19 +23,25 @@ __export(plugin_exports, {
23
23
  registerPlugin: () => registerPlugin
24
24
  });
25
25
  module.exports = __toCommonJS(plugin_exports);
26
- var import_merge = require("../utils/merge");
26
+ var import_merge = require("@modern-js/runtime-utils/merge");
27
27
  var import_base = require("./base");
28
28
  var import_runner = require("./runner");
29
29
  __reExport(plugin_exports, require("./base"), module.exports);
30
+ function setupConfigContext() {
31
+ const runner = (0, import_runner.getGlobalRunner)();
32
+ const configs = runner.modifyRuntimeConfig();
33
+ import_base.RuntimeConfigContext.set((0, import_merge.merge)({}, ...configs));
34
+ }
30
35
  function registerPlugin(internalPlugins, runtimeConfig, customRuntime) {
31
36
  const { plugins = [] } = runtimeConfig || {};
32
37
  (customRuntime || import_base.runtime).usePlugin(...internalPlugins, ...plugins);
33
38
  const runner = (customRuntime || import_base.runtime).init();
34
39
  (0, import_runner.setGlobalRunner)(runner);
40
+ setupConfigContext();
35
41
  return runner;
36
42
  }
37
43
  function mergeConfig(config, ...otherConfig) {
38
- return (0, import_merge.merge)(config, ...otherConfig);
44
+ return (0, import_merge.merge)({}, config, ...otherConfig);
39
45
  }
40
46
  // Annotate the CommonJS export names for ESM import in node:
41
47
  0 && (module.exports = {
@@ -24,26 +24,19 @@ module.exports = __toCommonJS(react_exports);
24
24
  var import_jsx_runtime = require("react/jsx-runtime");
25
25
  var import_context = require("../context");
26
26
  var import_runner = require("../plugin/runner");
27
- function createRoot(UserApp, config) {
27
+ function createRoot(UserApp) {
28
28
  const App = UserApp || (0, import_context.getGlobalApp)();
29
29
  const runner = (0, import_runner.getGlobalRunner)();
30
- const HOCApp = runner.hoc({
31
- App,
32
- config: config || {}
33
- }, {
34
- onLast: ({ App: App2 }) => {
35
- const WrapComponent = ({ _internal_context, ...props }) => {
36
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_context.RuntimeReactContext.Provider, {
37
- value: _internal_context,
38
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App2, {
39
- ...props
40
- })
41
- });
42
- };
43
- return WrapComponent;
44
- }
45
- });
46
- return HOCApp;
30
+ const WrapperApp = runner.wrapRoot(App);
31
+ const WrapComponent = ({ _internal_context, ...props }) => {
32
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_context.RuntimeReactContext.Provider, {
33
+ value: _internal_context,
34
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(WrapperApp, {
35
+ ...props
36
+ })
37
+ });
38
+ };
39
+ return WrapComponent;
47
40
  }
48
41
  // Annotate the CommonJS export names for ESM import in node:
49
42
  0 && (module.exports = {
@@ -30,7 +30,7 @@ var import_loaderManager = require("../loader/loaderManager");
30
30
  var import_utils = require("./utils");
31
31
  var import_constants = require("./constants");
32
32
  function createSSRContext(request, options) {
33
- const { config, loaderContext, onError, onTiming, locals, resource, params, responseProxy, staticGenerate, logger, metrics, reporter } = options;
33
+ const { config, loaderContext, onError, onTiming, locals, resource, params, responseProxy, logger, metrics, reporter } = options;
34
34
  const { nonce } = config;
35
35
  const { entryName, route } = resource;
36
36
  const cookie = request.headers.get("cookie");
@@ -39,7 +39,7 @@ function createSSRContext(request, options) {
39
39
  const query = (0, import_request.parseQuery)(request);
40
40
  const headersData = (0, import_request.parseHeaders)(request);
41
41
  const url = new URL(request.url);
42
- const ssrConfig = (0, import_utils.getSSRConfigByEntry)(entryName, config.ssr, config.ssrByEntries, staticGenerate);
42
+ const ssrConfig = (0, import_utils.getSSRConfigByEntry)(entryName, config.ssr, config.ssrByEntries);
43
43
  const ssrMode = (0, import_utils.getSSRMode)(ssrConfig);
44
44
  const loaderFailureMode = typeof ssrConfig === "object" ? ssrConfig.loaderFailureMode : void 0;
45
45
  return {
@@ -59,6 +59,7 @@ function createSSRContext(request, options) {
59
59
  params,
60
60
  headers: headersData,
61
61
  host: url.host,
62
+ url: request.url,
62
63
  raw: request
63
64
  },
64
65
  response: {
@@ -83,14 +84,14 @@ const createRequestHandler = async (handleRequest) => {
83
84
  const runner = (0, import_runner.getGlobalRunner)();
84
85
  const { routeManifest } = options.resource;
85
86
  const context = (0, import_runtime.getInitialContext)(runner, false, routeManifest);
86
- const runInit = (_context) => runner.init({
87
- context: _context
88
- }, {
89
- onLast: ({ context: context1 }) => {
90
- const init = (0, import_context.getGlobalAppInit)();
91
- return init === null || init === void 0 ? void 0 : init(context1);
87
+ const runBeforeRender = async (context2) => {
88
+ const result = await runner.beforeRender(context2);
89
+ if (typeof Response !== "undefined" && result instanceof Response) {
90
+ return result;
92
91
  }
93
- });
92
+ const init = (0, import_context.getGlobalAppInit)();
93
+ return init === null || init === void 0 ? void 0 : init(context2);
94
+ };
94
95
  const responseProxy = {
95
96
  headers: {},
96
97
  code: -1
@@ -125,7 +126,7 @@ const createRequestHandler = async (handleRequest) => {
125
126
  }
126
127
  return void 0;
127
128
  };
128
- const initialData = await runInit(context);
129
+ const initialData = await runBeforeRender(context);
129
130
  context.initialData = initialData;
130
131
  const redirectResponse = getRedirectResponse(initialData);
131
132
  if (redirectResponse) {
@@ -69,11 +69,11 @@ function createRenderStreaming(createReadableStreamFromElement) {
69
69
  const headersData = (0, import_request.parseHeaders)(request);
70
70
  return (0, import_node.run)(headersData, async () => {
71
71
  const end = (0, import_time.time)();
72
- const { runtimeContext, config, resource, staticGenerate } = options;
72
+ const { runtimeContext, config, resource } = options;
73
73
  const onError = (0, import_tracer.createOnError)(options.onError);
74
74
  const onTiming = (0, import_tracer.createOnTiming)(options.onTiming);
75
75
  const { htmlTemplate, entryName } = resource;
76
- const ssrConfig = (0, import_utils.getSSRConfigByEntry)(entryName, config.ssr, config.ssrByEntries, staticGenerate);
76
+ const ssrConfig = (0, import_utils.getSSRConfigByEntry)(entryName, config.ssr, config.ssrByEntries);
77
77
  const rootElement = import_react.default.cloneElement(serverRoot, {
78
78
  _internal_context: Object.assign(runtimeContext, {
79
79
  ssr: true
@@ -51,14 +51,14 @@ const renderString = async (request, serverRoot, options) => {
51
51
  const headersData = (0, import_request.parseHeaders)(request);
52
52
  return (0, import_node.run)(headersData, async () => {
53
53
  var _runtimeContext_ssrContext;
54
- const { resource, runtimeContext, config, onError, onTiming, staticGenerate } = options;
54
+ const { resource, runtimeContext, config, onError, onTiming } = options;
55
55
  const tracer = {
56
56
  onError: (0, import_tracer.createOnError)(onError),
57
57
  onTiming: (0, import_tracer.createOnTiming)(onTiming)
58
58
  };
59
59
  const routerContext = runtimeContext.routerContext;
60
60
  const { htmlTemplate, entryName, loadableStats, routeManifest } = resource;
61
- const ssrConfig = (0, import_utils.getSSRConfigByEntry)(entryName, config.ssr, config.ssrByEntries, staticGenerate);
61
+ const ssrConfig = (0, import_utils.getSSRConfigByEntry)(entryName, config.ssr, config.ssrByEntries);
62
62
  const chunkSet = {
63
63
  renderLevel: import_constants2.RenderLevel.CLIENT_RENDER,
64
64
  ssrScripts: "",
@@ -71,6 +71,7 @@ const renderString = async (request, serverRoot, options) => {
71
71
  chunkSet.renderLevel = import_constants2.RenderLevel.SERVER_PREFETCH;
72
72
  } catch (e) {
73
73
  chunkSet.renderLevel = import_constants2.RenderLevel.CLIENT_RENDER;
74
+ tracer.onError(import_tracer.SSRErrors.PRERENDER, e);
74
75
  }
75
76
  const collectors = [
76
77
  new import_styledComponent.StyledCollector(chunkSet),
@@ -63,14 +63,11 @@ function serializeErrors(errors) {
63
63
  }
64
64
  return serialized;
65
65
  }
66
- function getSSRConfigByEntry(entryName, ssr, ssrByEntries, staticGenerate) {
67
- if (staticGenerate) {
68
- return true;
69
- }
66
+ function getSSRConfigByEntry(entryName, ssr, ssrByEntries) {
70
67
  if (ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[entryName]) {
71
68
  return ssrByEntries[entryName];
72
69
  }
73
- return ssr;
70
+ return ssr || true;
74
71
  }
75
72
  function getSSRMode(ssrConfig) {
76
73
  if (typeof ssrConfig === "boolean") {
@@ -28,6 +28,7 @@ var import_jsx_runtime = require("react/jsx-runtime");
28
28
  var import_react = require("react");
29
29
  var import_router = require("@modern-js/runtime-utils/router");
30
30
  var import_parsed = require("@modern-js/runtime-utils/parsed");
31
+ var import_merge = require("@modern-js/runtime-utils/merge");
31
32
  var import_context = require("../../core/context");
32
33
  var import_core = require("../../core");
33
34
  var import_hooks = require("./hooks");
@@ -45,23 +46,17 @@ function modifyRoutes(modifyFunction) {
45
46
  console.error("It is not allowed to modify routes config after create router.");
46
47
  }
47
48
  }
48
- const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename = "", routesConfig, createRoutes }) => {
49
- const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
50
- let routes = [];
51
- finalRouteConfig = {
52
- routes: (0, import_context.getGlobalRoutes)(),
53
- globalApp: (0, import_context.getGlobalLayoutApp)(),
54
- ...routesConfig
55
- };
56
- window._SERVER_DATA = (0, import_parsed.parsedJSONFromElement)("__MODERN_SERVER_DATA__");
49
+ const routerPlugin = (userConfig = {}) => {
57
50
  return {
58
51
  name: "@modern-js/plugin-router",
59
52
  registerHook: {
60
53
  modifyRoutes: import_hooks.modifyRoutes
61
54
  },
62
55
  setup: (api) => {
56
+ let routes = [];
57
+ window._SERVER_DATA = (0, import_parsed.parsedJSONFromElement)("__MODERN_SERVER_DATA__");
63
58
  return {
64
- init({ context }, next) {
59
+ beforeRender(context) {
65
60
  context.router = {
66
61
  useMatches: import_router.useMatches,
67
62
  useLocation: import_router.useLocation,
@@ -72,24 +67,26 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
72
67
  return routes;
73
68
  }
74
69
  });
75
- return next({
76
- context
77
- });
78
70
  },
79
- hoc: ({ App, config }, next) => {
71
+ wrapRoot: (App) => {
72
+ const pluginConfig = api.useRuntimeConfigContext();
73
+ const { serverBase = [], supportHtml5History = true, basename = "", routesConfig, createRoutes } = (0, import_merge.merge)(pluginConfig.router || {}, userConfig);
74
+ const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
75
+ finalRouteConfig = {
76
+ routes: (0, import_context.getGlobalRoutes)(),
77
+ globalApp: (0, import_context.getGlobalLayoutApp)(),
78
+ ...routesConfig
79
+ };
80
80
  if (!finalRouteConfig.routes && !createRoutes) {
81
- return next({
82
- App,
83
- config
84
- });
81
+ return App;
85
82
  }
86
83
  const getRouteApp = () => {
87
84
  const useCreateRouter = (props) => {
88
- var _config_router, _window__SERVER_DATA;
89
- const 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(/^\/*/, "/");
85
+ var _window__SERVER_DATA;
86
+ const runtimeContext = (0, import_react.useContext)(import_core.RuntimeReactContext);
87
+ const 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(/^\/*/, "/");
90
88
  const _basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, basename) : baseUrl;
91
89
  let hydrationData = window._ROUTER_DATA;
92
- const runtimeContext = (0, import_react.useContext)(import_core.RuntimeReactContext);
93
90
  const { unstable_getBlockNavState: getBlockNavState } = runtimeContext;
94
91
  return (0, import_react.useMemo)(() => {
95
92
  if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
@@ -126,7 +123,8 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
126
123
  get() {
127
124
  return router;
128
125
  },
129
- configurable: true
126
+ configurable: true,
127
+ enumerable: true
130
128
  });
131
129
  return router;
132
130
  }, [
@@ -145,33 +143,24 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
145
143
  });
146
144
  };
147
145
  };
148
- const RouteApp = getRouteApp();
149
- return next({
150
- App: RouteApp,
151
- config
152
- });
146
+ return getRouteApp();
153
147
  },
154
- pickContext: ({ context, pickedContext }, next) => {
155
- const { remixRouter } = context;
148
+ pickContext: (pickedContext) => {
149
+ const { remixRouter } = pickedContext;
156
150
  if (!remixRouter) {
157
- return next({
158
- context,
159
- pickedContext
160
- });
151
+ return pickedContext;
161
152
  }
162
153
  const router = {
154
+ ...pickedContext.router,
163
155
  navigate: remixRouter.navigate,
164
156
  get location() {
165
157
  return remixRouter.state.location;
166
158
  }
167
159
  };
168
- return next({
169
- context,
170
- pickedContext: {
171
- ...pickedContext,
172
- router
173
- }
174
- });
160
+ return {
161
+ ...pickedContext,
162
+ router
163
+ };
175
164
  }
176
165
  };
177
166
  }
@@ -40,6 +40,7 @@ var import_router2 = require("@modern-js/runtime-utils/router");
40
40
  var import_node = require("@modern-js/runtime-utils/node");
41
41
  var import_time = require("@modern-js/runtime-utils/time");
42
42
  var import_constants = require("@modern-js/utils/universal/constants");
43
+ var import_merge = require("@modern-js/runtime-utils/merge");
43
44
  var import_common = require("../../common");
44
45
  var import_core = require("../../core");
45
46
  var import_context = require("../../core/context");
@@ -56,25 +57,26 @@ function createRemixReuqest(request) {
56
57
  signal: controller.signal
57
58
  });
58
59
  }
59
- const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
60
+ const routerPlugin = (userConfig = {}) => {
60
61
  return {
61
62
  name: "@modern-js/plugin-router",
62
63
  registerHook: {
63
64
  modifyRoutes: import_hooks.modifyRoutes
64
65
  },
65
66
  setup: (api) => {
66
- const finalRouteConfig = {
67
- routes: (0, import_context.getGlobalRoutes)(),
68
- globalApp: (0, import_context.getGlobalLayoutApp)(),
69
- ...routesConfig
70
- };
67
+ let finalRouteConfig = {};
71
68
  return {
72
- async init({ context }, next) {
69
+ async beforeRender(context, interrupt) {
73
70
  var _context_ssrContext, _context_ssrContext_onTiming, _context_ssrContext1;
71
+ const pluginConfig = api.useRuntimeConfigContext();
72
+ const { basename = "", routesConfig, createRoutes } = (0, import_merge.merge)(pluginConfig.router || {}, userConfig);
73
+ finalRouteConfig = {
74
+ routes: (0, import_context.getGlobalRoutes)(),
75
+ globalApp: (0, import_context.getGlobalLayoutApp)(),
76
+ ...routesConfig
77
+ };
74
78
  if (!finalRouteConfig.routes && !createRoutes) {
75
- return next({
76
- context
77
- });
79
+ return;
78
80
  }
79
81
  const { request, mode: ssrMode, nonce, loaderFailureMode = "errorBoundary" } = context.ssrContext;
80
82
  const { baseUrl } = request;
@@ -103,7 +105,7 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
103
105
  const cost = end();
104
106
  (_context_ssrContext1 = context.ssrContext) === null || _context_ssrContext1 === void 0 ? void 0 : (_context_ssrContext_onTiming = _context_ssrContext1.onTiming) === null || _context_ssrContext_onTiming === void 0 ? void 0 : _context_ssrContext_onTiming.call(_context_ssrContext1, import_constants.LOADER_REPORTER_NAME, cost);
105
107
  if (routerContext instanceof Response) {
106
- return routerContext;
108
+ return interrupt(routerContext);
107
109
  }
108
110
  if (routerContext.statusCode >= 500 && routerContext.statusCode < 600 && loaderFailureMode === "clientRender") {
109
111
  routerContext.statusCode = 200;
@@ -113,20 +115,15 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
113
115
  context.remixRouter = router;
114
116
  context.routerContext = routerContext;
115
117
  context.routes = routes;
116
- return next({
117
- context
118
- });
119
118
  },
120
- hoc: ({ App, config }, next) => {
119
+ wrapRoot: (App) => {
121
120
  if (!finalRouteConfig) {
122
- return next({
123
- App,
124
- config
125
- });
121
+ return App;
126
122
  }
127
123
  const getRouteApp = () => {
128
124
  return () => {
129
- const { remixRouter, routerContext, ssrContext } = (0, import_react.useContext)(import_core.RuntimeReactContext);
125
+ const context = (0, import_react.useContext)(import_core.RuntimeReactContext);
126
+ const { remixRouter, routerContext, ssrContext } = context;
130
127
  const { nonce, mode } = ssrContext;
131
128
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
132
129
  children: [
@@ -135,6 +132,8 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
135
132
  context: routerContext,
136
133
  hydrate: false
137
134
  }),
135
+ mode === "stream" && // ROUTER_DATA will inject in `packages/runtime/plugin-runtime/src/core/server/string/ssrData.ts` in string ssr
136
+ // So we can inject it only when streaming ssr
138
137
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DeferredDataScripts.default, {
139
138
  nonce,
140
139
  context: routerContext
@@ -144,19 +143,12 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
144
143
  });
145
144
  };
146
145
  };
147
- const RouteApp = getRouteApp();
148
- return next({
149
- App: RouteApp,
150
- config
151
- });
146
+ return getRouteApp();
152
147
  },
153
- pickContext: ({ context, pickedContext }, next) => {
154
- const { remixRouter } = context;
148
+ pickContext: (pickedContext) => {
149
+ const { remixRouter } = pickedContext;
155
150
  if (!remixRouter) {
156
- return next({
157
- context,
158
- pickedContext
159
- });
151
+ return pickedContext;
160
152
  }
161
153
  const router = {
162
154
  navigate: remixRouter.navigate,
@@ -164,13 +156,10 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
164
156
  return remixRouter.state.location;
165
157
  }
166
158
  };
167
- return next({
168
- context,
169
- pickedContext: {
170
- ...pickedContext,
171
- router
172
- }
173
- });
159
+ return {
160
+ ...pickedContext,
161
+ router
162
+ };
174
163
  }
175
164
  };
176
165
  }