@modern-js/runtime 2.63.7-alpha.0 → 2.64.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 (72) hide show
  1. package/dist/cjs/core/browser/hydrate.js +1 -10
  2. package/dist/cjs/core/browser/index.js +6 -4
  3. package/dist/cjs/core/compat/hooks.js +80 -0
  4. package/dist/cjs/core/{plugin/runner.js → compat/index.js} +19 -19
  5. package/dist/cjs/core/compatible.js +16 -16
  6. package/dist/cjs/core/context/index.js +12 -2
  7. package/dist/cjs/core/context/runtime.js +1 -2
  8. package/dist/cjs/core/index.js +1 -4
  9. package/dist/cjs/core/plugin/base.js +0 -35
  10. package/dist/cjs/core/plugin/index.js +17 -17
  11. package/dist/cjs/core/plugin/types.js +16 -0
  12. package/dist/cjs/core/react/index.js +3 -3
  13. package/dist/cjs/core/server/requestHandler.js +4 -4
  14. package/dist/cjs/core/server/stream/createReadableStream.js +6 -3
  15. package/dist/cjs/router/runtime/hooks.js +7 -7
  16. package/dist/cjs/router/runtime/plugin.js +109 -98
  17. package/dist/cjs/router/runtime/plugin.node.js +89 -91
  18. package/dist/esm/core/browser/hydrate.js +1 -10
  19. package/dist/esm/core/browser/index.js +9 -7
  20. package/dist/esm/core/compat/hooks.js +88 -0
  21. package/dist/esm/core/compat/index.js +20 -0
  22. package/dist/esm/core/compatible.js +18 -18
  23. package/dist/esm/core/context/index.js +9 -1
  24. package/dist/esm/core/context/runtime.js +2 -3
  25. package/dist/esm/core/index.js +1 -3
  26. package/dist/esm/core/plugin/base.js +0 -28
  27. package/dist/esm/core/plugin/index.js +14 -17
  28. package/dist/esm/core/plugin/types.js +0 -0
  29. package/dist/esm/core/react/index.js +4 -4
  30. package/dist/esm/core/server/requestHandler.js +6 -6
  31. package/dist/esm/core/server/stream/createReadableStream.js +8 -5
  32. package/dist/esm/router/runtime/hooks.js +5 -5
  33. package/dist/esm/router/runtime/plugin.js +116 -105
  34. package/dist/esm/router/runtime/plugin.node.js +112 -111
  35. package/dist/esm-node/core/browser/hydrate.js +1 -10
  36. package/dist/esm-node/core/browser/index.js +7 -5
  37. package/dist/esm-node/core/compat/hooks.js +54 -0
  38. package/dist/esm-node/core/compat/index.js +18 -0
  39. package/dist/esm-node/core/compatible.js +17 -17
  40. package/dist/esm-node/core/context/index.js +9 -1
  41. package/dist/esm-node/core/context/runtime.js +1 -2
  42. package/dist/esm-node/core/index.js +1 -3
  43. package/dist/esm-node/core/plugin/base.js +0 -24
  44. package/dist/esm-node/core/plugin/index.js +16 -14
  45. package/dist/esm-node/core/plugin/types.js +0 -0
  46. package/dist/esm-node/core/react/index.js +4 -4
  47. package/dist/esm-node/core/server/requestHandler.js +5 -5
  48. package/dist/esm-node/core/server/stream/createReadableStream.js +7 -4
  49. package/dist/esm-node/router/runtime/hooks.js +5 -5
  50. package/dist/esm-node/router/runtime/plugin.js +110 -99
  51. package/dist/esm-node/router/runtime/plugin.node.js +90 -92
  52. package/dist/types/core/browser/hydrate.d.ts +1 -1
  53. package/dist/types/core/compat/hooks.d.ts +4 -0
  54. package/dist/types/core/compat/index.d.ts +2 -0
  55. package/dist/types/core/compatible.d.ts +7 -6
  56. package/dist/types/core/context/index.d.ts +7 -2
  57. package/dist/types/core/context/runtime.d.ts +1 -3
  58. package/dist/types/core/index.d.ts +1 -2
  59. package/dist/types/core/plugin/base.d.ts +9 -33
  60. package/dist/types/core/plugin/index.d.ts +19 -12
  61. package/dist/types/core/plugin/types.d.ts +10 -0
  62. package/dist/types/core/types.d.ts +0 -1
  63. package/dist/types/index.d.ts +1 -1
  64. package/dist/types/router/runtime/hooks.d.ts +7 -3
  65. package/dist/types/router/runtime/index.d.ts +1 -0
  66. package/dist/types/router/runtime/plugin.d.ts +5 -2
  67. package/dist/types/router/runtime/plugin.node.d.ts +5 -2
  68. package/dist/types/router/runtime/types.d.ts +4 -0
  69. package/package.json +12 -11
  70. package/dist/esm/core/plugin/runner.js +0 -17
  71. package/dist/esm-node/core/plugin/runner.js +0 -17
  72. package/dist/types/core/plugin/runner.d.ts +0 -9
@@ -7,36 +7,35 @@ import { jsx as _jsx } from "react/jsx-runtime";
7
7
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
8
8
  import React, { useContext, useMemo } from "react";
9
9
  import { hydrateRoot as ModernHydrateRoot } from "./browser/hydrate";
10
- import { getGlobalAppInit } from "./context";
10
+ import { getGlobalAppInit, getGlobalInternalRuntimeContext } from "./context";
11
11
  import { RuntimeReactContext } from "./context/runtime";
12
12
  import { createLoaderManager } from "./loader/loaderManager";
13
13
  import { registerPlugin } from "./plugin";
14
- import { getGlobalRunner } from "./plugin/runner";
15
14
  import { wrapRuntimeContextProvider } from "./react/wrapper";
16
15
  var IS_REACT18 = process.env.IS_REACT18 === "true";
17
16
  function isClientArgs(id) {
18
17
  return typeof id === "string" || typeof HTMLElement !== "undefined" && _instanceof(id, HTMLElement);
19
18
  }
20
- var getInitialContext = function(runner) {
19
+ var getInitialContext = function() {
21
20
  return {
22
21
  loaderManager: createLoaderManager({}),
23
- runner,
24
22
  isBrowser: true,
25
23
  routeManifest: typeof window !== "undefined" && window[ROUTE_MANIFEST]
26
24
  };
27
25
  };
28
26
  var createApp = function(param) {
29
- var plugins = param.plugins, runtime = param.runtime, globalProps = param.props;
30
- var runner = registerPlugin(plugins, {
27
+ var plugins = param.plugins, globalProps = param.props;
28
+ var context = registerPlugin(plugins, {
31
29
  plugins: []
32
- }, runtime);
30
+ });
31
+ var hooks = context.hooks;
33
32
  return function(App) {
34
33
  var WrapperComponent = function(props) {
35
34
  return /* @__PURE__ */ React.createElement(App || React.Fragment, App ? _object_spread({}, props) : null, App ? props.children : React.Children.map(props.children, function(child) {
36
35
  return /* @__PURE__ */ React.isValidElement(child) ? /* @__PURE__ */ React.cloneElement(child, _object_spread({}, child.props, props)) : child;
37
36
  }));
38
37
  };
39
- var WrapperApp = runner.wrapRoot(WrapperComponent);
38
+ var WrapperApp = hooks.wrapRoot.call(WrapperComponent);
40
39
  var WrapComponent = function(props) {
41
40
  var mergedProps = _object_spread({}, props, globalProps);
42
41
  return /* @__PURE__ */ _jsx(WrapperApp, _object_spread({}, mergedProps));
@@ -46,13 +45,16 @@ var createApp = function(param) {
46
45
  };
47
46
  var bootstrap = function() {
48
47
  var _ref = _async_to_generator(function(BootApp, id, root, ReactDOM) {
49
- var App, runner, context, runBeforeRender, isBrowser, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement, ModernRender, ModernHydrate;
48
+ var App, internalRuntimeContext, api, hooks, context, runBeforeRender, isBrowser, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement, ModernRender, ModernHydrate;
50
49
  return _ts_generator(this, function(_state) {
51
50
  switch (_state.label) {
52
51
  case 0:
53
52
  App = BootApp;
54
- runner = getGlobalRunner();
55
- context = getInitialContext(runner);
53
+ internalRuntimeContext = getGlobalInternalRuntimeContext();
54
+ api = internalRuntimeContext.pluginAPI;
55
+ hooks = internalRuntimeContext.hooks;
56
+ context = getInitialContext();
57
+ api.updateRuntimeContext(context);
56
58
  runBeforeRender = function() {
57
59
  var _ref2 = _async_to_generator(function(context2) {
58
60
  var init;
@@ -61,7 +63,7 @@ var bootstrap = function() {
61
63
  case 0:
62
64
  return [
63
65
  4,
64
- runner.beforeRender(context2)
66
+ hooks.onBeforeRender.call(context2)
65
67
  ];
66
68
  case 1:
67
69
  _state2.sent();
@@ -202,18 +204,16 @@ var useRuntimeContext = function() {
202
204
  getInitData: function() {
203
205
  return Object.freeze(context.initialData);
204
206
  }
205
- } : {
206
- getInitData: function() {
207
- return Object.freeze(context.initialData);
208
- }
209
- };
207
+ } : {};
210
208
  var pickedContext = _object_spread_props(_object_spread({}, context), {
211
209
  context: tSSRContext,
212
210
  request: (_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.request,
213
211
  response: (_context_ssrContext1 = context.ssrContext) === null || _context_ssrContext1 === void 0 ? void 0 : _context_ssrContext1.response
214
212
  });
213
+ var internalRuntimeContext = getGlobalInternalRuntimeContext();
214
+ var hooks = internalRuntimeContext.hooks;
215
215
  var memoizedContext = useMemo(function() {
216
- return context.runner.pickContext(pickedContext);
216
+ return hooks.pickContext.call(pickedContext);
217
217
  }, [
218
218
  context
219
219
  ]);
@@ -7,6 +7,12 @@ function setGlobalContext(context) {
7
7
  globalContext.appConfig = typeof context.appConfig === "function" ? context.appConfig() : context.appConfig;
8
8
  globalContext.layoutApp = context.layoutApp;
9
9
  }
10
+ function setGlobalInternalRuntimeContext(context) {
11
+ globalContext.internalRuntimeContext = context;
12
+ }
13
+ function getGlobalInternalRuntimeContext() {
14
+ return globalContext.internalRuntimeContext;
15
+ }
10
16
  function getGlobalApp() {
11
17
  return globalContext.App;
12
18
  }
@@ -29,8 +35,10 @@ export {
29
35
  getGlobalApp,
30
36
  getGlobalAppConfig,
31
37
  getGlobalAppInit,
38
+ getGlobalInternalRuntimeContext,
32
39
  getGlobalLayoutApp,
33
40
  getGlobalRoutes,
34
41
  getInitialContext,
35
- setGlobalContext
42
+ setGlobalContext,
43
+ setGlobalInternalRuntimeContext
36
44
  };
@@ -3,11 +3,10 @@ import { createContext } from "react";
3
3
  import { createLoaderManager } from "../loader/loaderManager";
4
4
  var RuntimeReactContext = createContext({});
5
5
  var ServerRouterContext = createContext({});
6
- var getInitialContext = function(runner) {
7
- var isBrowser = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true, routeManifest = arguments.length > 2 ? arguments[2] : void 0;
6
+ var getInitialContext = function() {
7
+ var isBrowser = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true, routeManifest = arguments.length > 1 ? arguments[1] : void 0;
8
8
  return {
9
9
  loaderManager: createLoaderManager({}),
10
- runner,
11
10
  isBrowser,
12
11
  routeManifest: routeManifest || typeof window !== "undefined" && window[ROUTE_MANIFEST]
13
12
  };
@@ -1,4 +1,3 @@
1
- import { runtime } from "./plugin";
2
1
  import { defineConfig, getConfig, defineRuntimeConfig } from "./config";
3
2
  export * from "./compatible";
4
3
  import { RuntimeReactContext, ServerRouterContext } from "./context/runtime";
@@ -9,6 +8,5 @@ export {
9
8
  ServerRouterContext,
10
9
  defineConfig,
11
10
  defineRuntimeConfig,
12
- getConfig,
13
- runtime
11
+ getConfig
14
12
  };
@@ -1,28 +0,0 @@
1
- import { createAsyncInterruptWorkflow, createContext, createManager, createSyncParallelWorkflow, createWaterfall } from "@modern-js/plugin";
2
- var RuntimeConfigContext = createContext({});
3
- var useRuntimeConfigContext = function() {
4
- return RuntimeConfigContext.use().value;
5
- };
6
- var wrapRoot = createWaterfall();
7
- var beforeRender = createAsyncInterruptWorkflow();
8
- var pickContext = createWaterfall();
9
- var modifyRuntimeConfig = createSyncParallelWorkflow();
10
- var runtimeHooks = {
11
- beforeRender,
12
- wrapRoot,
13
- pickContext,
14
- modifyRuntimeConfig
15
- };
16
- var runtimePluginAPI = {
17
- useRuntimeConfigContext
18
- };
19
- var createRuntime = function() {
20
- return createManager(runtimeHooks, runtimePluginAPI);
21
- };
22
- var runtime = createRuntime();
23
- export {
24
- RuntimeConfigContext,
25
- createRuntime,
26
- runtime,
27
- useRuntimeConfigContext
28
- };
@@ -1,23 +1,20 @@
1
1
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import { runtime } from "@modern-js/plugin-v2/runtime";
2
3
  import { merge } from "@modern-js/runtime-utils/merge";
3
- import { RuntimeConfigContext, runtime } from "./base";
4
- import { getGlobalRunner, setGlobalRunner } from "./runner";
5
- export * from "./base";
6
- function setupConfigContext() {
7
- var runner = getGlobalRunner();
8
- var configs = runner.modifyRuntimeConfig();
9
- RuntimeConfigContext.set(merge.apply(void 0, [
10
- {}
11
- ].concat(_to_consumable_array(configs))));
12
- }
13
- function registerPlugin(internalPlugins, runtimeConfig, customRuntime) {
14
- var _instance;
4
+ import { compatPlugin } from "../compat";
5
+ import { handleSetupResult } from "../compat/hooks";
6
+ import { setGlobalInternalRuntimeContext } from "../context";
7
+ function registerPlugin(internalPlugins, runtimeConfig) {
15
8
  var _ref = runtimeConfig || {}, _ref_plugins = _ref.plugins, plugins = _ref_plugins === void 0 ? [] : _ref_plugins;
16
- (_instance = customRuntime || runtime).usePlugin.apply(_instance, _to_consumable_array(internalPlugins).concat(_to_consumable_array(plugins)));
17
- var runner = (customRuntime || runtime).init();
18
- setGlobalRunner(runner);
19
- setupConfigContext();
20
- return runner;
9
+ var runtimeContext = runtime.run({
10
+ plugins: [
11
+ compatPlugin()
12
+ ].concat(_to_consumable_array(internalPlugins), _to_consumable_array(plugins)),
13
+ config: runtimeConfig || {},
14
+ handleSetupResult
15
+ }).runtimeContext;
16
+ setGlobalInternalRuntimeContext(runtimeContext);
17
+ return runtimeContext;
21
18
  }
22
19
  function mergeConfig(config) {
23
20
  for (var _len = arguments.length, otherConfig = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
File without changes
@@ -1,16 +1,16 @@
1
1
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
2
2
  import { isBrowser } from "../../common";
3
3
  import { ROUTER_DATA_JSON_ID, SSR_DATA_JSON_ID } from "../constants";
4
- import { getGlobalApp } from "../context";
5
- import { getGlobalRunner } from "../plugin/runner";
4
+ import { getGlobalApp, getGlobalInternalRuntimeContext } from "../context";
6
5
  function createRoot(UserApp) {
7
6
  var App = UserApp || getGlobalApp();
8
7
  if (isBrowser()) {
9
8
  window._SSR_DATA = window._SSR_DATA || parsedJSONFromElement(SSR_DATA_JSON_ID);
10
9
  window._ROUTER_DATA = window._ROUTER_DATA || parsedJSONFromElement(ROUTER_DATA_JSON_ID);
11
10
  }
12
- var runner = getGlobalRunner();
13
- var WrapperApp = runner.wrapRoot(App);
11
+ var internalRuntimeContext = getGlobalInternalRuntimeContext();
12
+ var hooks = internalRuntimeContext.hooks;
13
+ var WrapperApp = hooks.wrapRoot.call(App);
14
14
  return WrapperApp;
15
15
  }
16
16
  export {
@@ -6,10 +6,9 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
6
  import { _ as _type_of } from "@swc/helpers/_/_type_of";
7
7
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
8
  import { getPathname, parseCookie, parseHeaders, parseQuery } from "@modern-js/runtime-utils/universal/request";
9
- import { getGlobalAppInit } from "../context";
9
+ import { getGlobalAppInit, getGlobalInternalRuntimeContext } from "../context";
10
10
  import { getInitialContext } from "../context/runtime";
11
11
  import { createLoaderManager } from "../loader/loaderManager";
12
- import { getGlobalRunner } from "../plugin/runner";
13
12
  import { createRoot } from "../react";
14
13
  import { CHUNK_CSS_PLACEHOLDER } from "./constants";
15
14
  import { getSSRConfigByEntry, getSSRMode } from "./utils";
@@ -75,14 +74,15 @@ var createRequestHandler = function() {
75
74
  return _ts_generator(this, function(_state) {
76
75
  requestHandler = function() {
77
76
  var _ref2 = _async_to_generator(function(request, options) {
78
- var _context_routerContext, _context_routerContext1, Root, runner, routeManifest, context, runBeforeRender, responseProxy, ssrContext, getRedirectResponse, initialData, _context_routerContext2, _context_ssrContext, redirectResponse, htmlTemplate, response;
77
+ var _context_routerContext, _context_routerContext1, Root, internalRuntimeContext, hooks, routeManifest, context, runBeforeRender, responseProxy, ssrContext, getRedirectResponse, initialData, _context_routerContext2, _context_ssrContext, redirectResponse, htmlTemplate, response;
79
78
  return _ts_generator(this, function(_state2) {
80
79
  switch (_state2.label) {
81
80
  case 0:
82
81
  Root = createRoot();
83
- runner = getGlobalRunner();
82
+ internalRuntimeContext = getGlobalInternalRuntimeContext();
83
+ hooks = internalRuntimeContext.hooks;
84
84
  routeManifest = options.resource.routeManifest;
85
- context = getInitialContext(runner, false, routeManifest);
85
+ context = getInitialContext(false, routeManifest);
86
86
  runBeforeRender = function() {
87
87
  var _ref3 = _async_to_generator(function(context2) {
88
88
  var result, init;
@@ -91,7 +91,7 @@ var createRequestHandler = function() {
91
91
  case 0:
92
92
  return [
93
93
  4,
94
- runner.beforeRender(context2)
94
+ hooks.onBeforeRender.call(context2)
95
95
  ];
96
96
  case 1:
97
97
  result = _state3.sent();
@@ -2,7 +2,7 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _define_property } from "@swc/helpers/_/_define_property";
3
3
  import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
4
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
- import { Transform } from "stream";
5
+ import { PassThrough, Transform } from "stream";
6
6
  import { createReadableStreamFromReadable } from "@modern-js/runtime-utils/node";
7
7
  import checkIsBot from "isbot";
8
8
  import { ServerStyleSheet } from "styled-components";
@@ -30,12 +30,12 @@ var createReadableStreamFromElement = function() {
30
30
  onReady = isbot || forceStream2String ? "onAllReady" : "onShellReady";
31
31
  sheet = new ServerStyleSheet();
32
32
  chunkVec = [];
33
- root = forceStream2String ? sheet.collectStyles(rootElement) : rootElement;
33
+ root = sheet.collectStyles(rootElement);
34
34
  return [
35
35
  2,
36
36
  new Promise(function(resolve) {
37
37
  var _obj;
38
- var pipe = renderToPipeableStream(root, (_obj = {
38
+ var _renderToPipeableStream = renderToPipeableStream(root, (_obj = {
39
39
  nonce: config.nonce
40
40
  }, _define_property(_obj, onReady, function() {
41
41
  var _options_onReady;
@@ -77,7 +77,10 @@ var createReadableStreamFromElement = function() {
77
77
  });
78
78
  var stream = createReadableStreamFromReadable(body);
79
79
  resolve(stream);
80
- pipe(body);
80
+ var passThrough = new PassThrough();
81
+ var styledStream = sheet.interleaveWithNodeStream(passThrough);
82
+ reactStreamingPipe(passThrough);
83
+ styledStream.pipe(body);
81
84
  });
82
85
  }), _define_property(_obj, "onShellError", function onShellError(error) {
83
86
  renderLevel = RenderLevel.CLIENT_RENDER;
@@ -100,7 +103,7 @@ var createReadableStreamFromElement = function() {
100
103
  var _options_onError;
101
104
  renderLevel = RenderLevel.CLIENT_RENDER;
102
105
  options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, error);
103
- }), _obj)).pipe;
106
+ }), _obj)), reactStreamingPipe = _renderToPipeableStream.pipe;
104
107
  })
105
108
  ];
106
109
  }
@@ -1,7 +1,7 @@
1
- import { createAsyncInterruptWorkflow, createWaterfall } from "@modern-js/plugin";
2
- var modifyRoutes = createWaterfall();
3
- var beforeCreateRoutes = createAsyncInterruptWorkflow();
1
+ import { createSyncHook } from "@modern-js/plugin-v2";
2
+ var modifyRoutes = createSyncHook();
3
+ var onBeforeCreateRoutes = createSyncHook();
4
4
  export {
5
- beforeCreateRoutes,
6
- modifyRoutes
5
+ modifyRoutes,
6
+ onBeforeCreateRoutes
7
7
  };
@@ -4,10 +4,11 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
4
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
5
5
  import { merge } from "@modern-js/runtime-utils/merge";
6
6
  import { RouterProvider, createBrowserRouter, createHashRouter, createRoutesFromElements, useHref, useLocation, useMatches } from "@modern-js/runtime-utils/router";
7
+ import { normalizePathname } from "@modern-js/runtime-utils/url";
7
8
  import { useContext, useMemo } from "react";
8
9
  import { RuntimeReactContext } from "../../core";
9
10
  import { getGlobalLayoutApp, getGlobalRoutes } from "../../core/context";
10
- import { modifyRoutes as modifyRoutesHook } from "./hooks";
11
+ import { modifyRoutes as modifyRoutesHook, onBeforeCreateRoutes as onBeforeCreateRoutesHook } from "./hooks";
11
12
  import { deserializeErrors, renderRoutes, urlJoin } from "./utils";
12
13
  var finalRouteConfig = {
13
14
  routes: []
@@ -26,117 +27,127 @@ var routerPlugin = function() {
26
27
  var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
27
28
  return {
28
29
  name: "@modern-js/plugin-router",
29
- registerHook: {
30
- modifyRoutes: modifyRoutesHook
30
+ registryHooks: {
31
+ modifyRoutes: modifyRoutesHook,
32
+ onBeforeCreateRoutes: onBeforeCreateRoutesHook
31
33
  },
32
34
  setup: function(api) {
33
35
  var routes = [];
34
- return {
35
- beforeRender: function beforeRender(context) {
36
- context.router = {
37
- useMatches,
38
- useLocation,
39
- useHref
40
- };
41
- Object.defineProperty(context, "routes", {
42
- get: function get() {
43
- return routes;
44
- }
45
- });
46
- },
47
- wrapRoot: function(App) {
48
- var pluginConfig = api.useRuntimeConfigContext();
49
- 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, future = _merge.future;
50
- var select = function(pathname) {
51
- return serverBase.find(function(baseUrl) {
52
- return pathname.search(baseUrl) === 0;
53
- }) || "/";
54
- };
55
- finalRouteConfig = _object_spread({
56
- routes: getGlobalRoutes(),
57
- globalApp: getGlobalLayoutApp()
58
- }, routesConfig);
59
- if (!finalRouteConfig.routes && !createRoutes) {
60
- return App;
36
+ api.onBeforeRender(function(context) {
37
+ if (window._SSR_DATA && userConfig.unstable_reloadOnURLMismatch) {
38
+ var _ssrContext_request;
39
+ var ssrContext = context.ssrContext;
40
+ var currentPathname = normalizePathname(window.location.pathname);
41
+ var initialPathname = (ssrContext === null || ssrContext === void 0 ? void 0 : (_ssrContext_request = ssrContext.request) === null || _ssrContext_request === void 0 ? void 0 : _ssrContext_request.pathname) && normalizePathname(ssrContext.request.pathname);
42
+ if (initialPathname && initialPathname !== currentPathname) {
43
+ var errorMsg = "The initial URL ".concat(initialPathname, " and the URL ").concat(currentPathname, " to be hydrated do not match, reload.");
44
+ console.error(errorMsg);
45
+ window.location.reload();
46
+ }
47
+ }
48
+ context.router = {
49
+ useMatches,
50
+ useLocation,
51
+ useHref
52
+ };
53
+ Object.defineProperty(context, "routes", {
54
+ get: function get() {
55
+ return routes;
61
56
  }
62
- var getRouteApp = function() {
63
- var useCreateRouter = function(props) {
64
- var runtimeContext = useContext(RuntimeReactContext);
65
- var baseUrl = select(location.pathname).replace(/^\/*/, "/");
66
- var _basename = baseUrl === "/" ? urlJoin(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
67
- var hydrationData = window._ROUTER_DATA;
68
- var getBlockNavState = runtimeContext.unstable_getBlockNavState;
69
- return useMemo(function() {
70
- if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
71
- hydrationData = _object_spread_props(_object_spread({}, hydrationData), {
72
- errors: deserializeErrors(hydrationData.errors)
73
- });
74
- }
75
- routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
76
- routesConfig: finalRouteConfig,
77
- props
78
- }));
79
- var runner = api.useHookRunners();
80
- routes = runner.modifyRoutes(routes);
81
- var router = supportHtml5History ? createBrowserRouter(routes, {
82
- basename: _basename,
83
- hydrationData
84
- }) : createHashRouter(routes, {
85
- basename: _basename,
86
- hydrationData
57
+ });
58
+ });
59
+ api.wrapRoot(function(App) {
60
+ var pluginConfig = api.getRuntimeConfig();
61
+ 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, future = _merge.future;
62
+ var select = function(pathname) {
63
+ return serverBase.find(function(baseUrl) {
64
+ return pathname.search(baseUrl) === 0;
65
+ }) || "/";
66
+ };
67
+ finalRouteConfig = _object_spread({
68
+ routes: getGlobalRoutes(),
69
+ globalApp: getGlobalLayoutApp()
70
+ }, routesConfig);
71
+ if (!finalRouteConfig.routes && !createRoutes) {
72
+ return App;
73
+ }
74
+ var getRouteApp = function() {
75
+ var useCreateRouter = function(props) {
76
+ var runtimeContext = useContext(RuntimeReactContext);
77
+ var baseUrl = select(location.pathname).replace(/^\/*/, "/");
78
+ var _basename = baseUrl === "/" ? urlJoin(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
79
+ var hydrationData = window._ROUTER_DATA;
80
+ var getBlockNavState = runtimeContext.unstable_getBlockNavState;
81
+ return useMemo(function() {
82
+ if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
83
+ hydrationData = _object_spread_props(_object_spread({}, hydrationData), {
84
+ errors: deserializeErrors(hydrationData.errors)
87
85
  });
88
- var originSubscribe = router.subscribe;
89
- router.subscribe = function(listener) {
90
- var wrapedListener = function() {
91
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
92
- args[_key] = arguments[_key];
93
- }
94
- var blockRoute = getBlockNavState ? getBlockNavState() : false;
95
- if (blockRoute) {
96
- return;
97
- }
98
- return listener.apply(void 0, _to_consumable_array(args));
99
- };
100
- return originSubscribe(wrapedListener);
86
+ }
87
+ routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
88
+ routesConfig: finalRouteConfig,
89
+ props
90
+ }));
91
+ var hooks = api.getHooks();
92
+ routes = hooks.modifyRoutes.call(routes);
93
+ var router = supportHtml5History ? createBrowserRouter(routes, {
94
+ basename: _basename,
95
+ hydrationData
96
+ }) : createHashRouter(routes, {
97
+ basename: _basename,
98
+ hydrationData
99
+ });
100
+ var originSubscribe = router.subscribe;
101
+ router.subscribe = function(listener) {
102
+ var wrapedListener = function() {
103
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
104
+ args[_key] = arguments[_key];
105
+ }
106
+ var blockRoute = getBlockNavState ? getBlockNavState() : false;
107
+ if (blockRoute) {
108
+ return;
109
+ }
110
+ return listener.apply(void 0, _to_consumable_array(args));
101
111
  };
102
- return router;
103
- }, [
104
- finalRouteConfig,
105
- props,
106
- _basename,
107
- hydrationData,
108
- getBlockNavState
109
- ]);
110
- };
111
- var Null = function() {
112
- return null;
113
- };
114
- return function(props) {
115
- beforeCreateRouter = false;
116
- var router = useCreateRouter(props);
117
- var routerWrapper = (
118
- // To match the node tree about https://github.com/web-infra-dev/modern.js/blob/v2.59.0/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx#L150-L168
119
- // According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
120
- // To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
121
- /* @__PURE__ */ _jsxs(_Fragment, {
122
- children: [
123
- /* @__PURE__ */ _jsx(RouterProvider, {
124
- router,
125
- future
126
- }),
127
- /* @__PURE__ */ _jsx(Null, {}),
128
- /* @__PURE__ */ _jsx(Null, {})
129
- ]
130
- })
131
- );
132
- return App ? /* @__PURE__ */ _jsx(App, {
133
- children: routerWrapper
134
- }) : routerWrapper;
135
- };
112
+ return originSubscribe(wrapedListener);
113
+ };
114
+ return router;
115
+ }, [
116
+ finalRouteConfig,
117
+ props,
118
+ _basename,
119
+ hydrationData,
120
+ getBlockNavState
121
+ ]);
136
122
  };
137
- return getRouteApp();
138
- }
139
- };
123
+ var Null = function() {
124
+ return null;
125
+ };
126
+ return function(props) {
127
+ beforeCreateRouter = false;
128
+ var router = useCreateRouter(props);
129
+ var routerWrapper = (
130
+ // To match the node tree about https://github.com/web-infra-dev/modern.js/blob/v2.59.0/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx#L150-L168
131
+ // According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
132
+ // To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
133
+ /* @__PURE__ */ _jsxs(_Fragment, {
134
+ children: [
135
+ /* @__PURE__ */ _jsx(RouterProvider, {
136
+ router,
137
+ future
138
+ }),
139
+ /* @__PURE__ */ _jsx(Null, {}),
140
+ /* @__PURE__ */ _jsx(Null, {})
141
+ ]
142
+ })
143
+ );
144
+ return App ? /* @__PURE__ */ _jsx(App, {
145
+ children: routerWrapper
146
+ }) : routerWrapper;
147
+ };
148
+ };
149
+ return getRouteApp();
150
+ });
140
151
  }
141
152
  };
142
153
  };