@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
@@ -27,6 +27,7 @@ module.exports = __toCommonJS(plugin_exports);
27
27
  var import_jsx_runtime = require("react/jsx-runtime");
28
28
  var import_merge = require("@modern-js/runtime-utils/merge");
29
29
  var import_router = require("@modern-js/runtime-utils/router");
30
+ var import_url = require("@modern-js/runtime-utils/url");
30
31
  var import_react = require("react");
31
32
  var import_core = require("../../core");
32
33
  var import_context = require("../../core/context");
@@ -48,110 +49,120 @@ function modifyRoutes(modifyFunction) {
48
49
  const routerPlugin = (userConfig = {}) => {
49
50
  return {
50
51
  name: "@modern-js/plugin-router",
51
- registerHook: {
52
- modifyRoutes: import_hooks.modifyRoutes
52
+ registryHooks: {
53
+ modifyRoutes: import_hooks.modifyRoutes,
54
+ onBeforeCreateRoutes: import_hooks.onBeforeCreateRoutes
53
55
  },
54
56
  setup: (api) => {
55
57
  let routes = [];
56
- return {
57
- beforeRender(context) {
58
- context.router = {
59
- useMatches: import_router.useMatches,
60
- useLocation: import_router.useLocation,
61
- useHref: import_router.useHref
62
- };
63
- Object.defineProperty(context, "routes", {
64
- get() {
65
- return routes;
66
- }
67
- });
68
- },
69
- wrapRoot: (App) => {
70
- const pluginConfig = api.useRuntimeConfigContext();
71
- const { serverBase = [], supportHtml5History = true, basename = "", routesConfig, createRoutes, future } = (0, import_merge.merge)(pluginConfig.router || {}, userConfig);
72
- const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
73
- finalRouteConfig = {
74
- routes: (0, import_context.getGlobalRoutes)(),
75
- globalApp: (0, import_context.getGlobalLayoutApp)(),
76
- ...routesConfig
77
- };
78
- if (!finalRouteConfig.routes && !createRoutes) {
79
- return App;
58
+ api.onBeforeRender((context) => {
59
+ if (window._SSR_DATA && userConfig.unstable_reloadOnURLMismatch) {
60
+ var _ssrContext_request;
61
+ const { ssrContext } = context;
62
+ const currentPathname = (0, import_url.normalizePathname)(window.location.pathname);
63
+ const initialPathname = (ssrContext === null || ssrContext === void 0 ? void 0 : (_ssrContext_request = ssrContext.request) === null || _ssrContext_request === void 0 ? void 0 : _ssrContext_request.pathname) && (0, import_url.normalizePathname)(ssrContext.request.pathname);
64
+ if (initialPathname && initialPathname !== currentPathname) {
65
+ const errorMsg = `The initial URL ${initialPathname} and the URL ${currentPathname} to be hydrated do not match, reload.`;
66
+ console.error(errorMsg);
67
+ window.location.reload();
68
+ }
69
+ }
70
+ context.router = {
71
+ useMatches: import_router.useMatches,
72
+ useLocation: import_router.useLocation,
73
+ useHref: import_router.useHref
74
+ };
75
+ Object.defineProperty(context, "routes", {
76
+ get() {
77
+ return routes;
80
78
  }
81
- const getRouteApp = () => {
82
- const useCreateRouter = (props) => {
83
- const runtimeContext = (0, import_react.useContext)(import_core.RuntimeReactContext);
84
- const baseUrl = select(location.pathname).replace(/^\/*/, "/");
85
- const _basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
86
- let hydrationData = window._ROUTER_DATA;
87
- const { unstable_getBlockNavState: getBlockNavState } = runtimeContext;
88
- return (0, import_react.useMemo)(() => {
89
- if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
90
- hydrationData = {
91
- ...hydrationData,
92
- errors: (0, import_utils.deserializeErrors)(hydrationData.errors)
93
- };
94
- }
95
- routes = createRoutes ? createRoutes() : (0, import_router.createRoutesFromElements)((0, import_utils.renderRoutes)({
96
- routesConfig: finalRouteConfig,
97
- props
98
- }));
99
- const runner = api.useHookRunners();
100
- routes = runner.modifyRoutes(routes);
101
- const router = supportHtml5History ? (0, import_router.createBrowserRouter)(routes, {
102
- basename: _basename,
103
- hydrationData
104
- }) : (0, import_router.createHashRouter)(routes, {
105
- basename: _basename,
106
- hydrationData
107
- });
108
- const originSubscribe = router.subscribe;
109
- router.subscribe = (listener) => {
110
- const wrapedListener = (...args) => {
111
- const blockRoute = getBlockNavState ? getBlockNavState() : false;
112
- if (blockRoute) {
113
- return;
114
- }
115
- return listener(...args);
116
- };
117
- return originSubscribe(wrapedListener);
79
+ });
80
+ });
81
+ api.wrapRoot((App) => {
82
+ const pluginConfig = api.getRuntimeConfig();
83
+ const { serverBase = [], supportHtml5History = true, basename = "", routesConfig, createRoutes, future } = (0, import_merge.merge)(pluginConfig.router || {}, userConfig);
84
+ const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
85
+ finalRouteConfig = {
86
+ routes: (0, import_context.getGlobalRoutes)(),
87
+ globalApp: (0, import_context.getGlobalLayoutApp)(),
88
+ ...routesConfig
89
+ };
90
+ if (!finalRouteConfig.routes && !createRoutes) {
91
+ return App;
92
+ }
93
+ const getRouteApp = () => {
94
+ const useCreateRouter = (props) => {
95
+ const runtimeContext = (0, import_react.useContext)(import_core.RuntimeReactContext);
96
+ const baseUrl = select(location.pathname).replace(/^\/*/, "/");
97
+ const _basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
98
+ let hydrationData = window._ROUTER_DATA;
99
+ const { unstable_getBlockNavState: getBlockNavState } = runtimeContext;
100
+ return (0, import_react.useMemo)(() => {
101
+ if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
102
+ hydrationData = {
103
+ ...hydrationData,
104
+ errors: (0, import_utils.deserializeErrors)(hydrationData.errors)
105
+ };
106
+ }
107
+ routes = createRoutes ? createRoutes() : (0, import_router.createRoutesFromElements)((0, import_utils.renderRoutes)({
108
+ routesConfig: finalRouteConfig,
109
+ props
110
+ }));
111
+ const hooks = api.getHooks();
112
+ routes = hooks.modifyRoutes.call(routes);
113
+ const router = supportHtml5History ? (0, import_router.createBrowserRouter)(routes, {
114
+ basename: _basename,
115
+ hydrationData
116
+ }) : (0, import_router.createHashRouter)(routes, {
117
+ basename: _basename,
118
+ hydrationData
119
+ });
120
+ const originSubscribe = router.subscribe;
121
+ router.subscribe = (listener) => {
122
+ const wrapedListener = (...args) => {
123
+ const blockRoute = getBlockNavState ? getBlockNavState() : false;
124
+ if (blockRoute) {
125
+ return;
126
+ }
127
+ return listener(...args);
118
128
  };
119
- return router;
120
- }, [
121
- finalRouteConfig,
122
- props,
123
- _basename,
124
- hydrationData,
125
- getBlockNavState
126
- ]);
127
- };
128
- const Null = () => null;
129
- return (props) => {
130
- beforeCreateRouter = false;
131
- const router = useCreateRouter(props);
132
- const routerWrapper = (
133
- // 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
134
- // According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
135
- // To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
136
- /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
137
- children: [
138
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_router.RouterProvider, {
139
- router,
140
- future
141
- }),
142
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Null, {}),
143
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Null, {})
144
- ]
145
- })
146
- );
147
- return App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
148
- children: routerWrapper
149
- }) : routerWrapper;
150
- };
129
+ return originSubscribe(wrapedListener);
130
+ };
131
+ return router;
132
+ }, [
133
+ finalRouteConfig,
134
+ props,
135
+ _basename,
136
+ hydrationData,
137
+ getBlockNavState
138
+ ]);
151
139
  };
152
- return getRouteApp();
153
- }
154
- };
140
+ const Null = () => null;
141
+ return (props) => {
142
+ beforeCreateRouter = false;
143
+ const router = useCreateRouter(props);
144
+ const routerWrapper = (
145
+ // 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
146
+ // According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
147
+ // To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
148
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
149
+ children: [
150
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_router.RouterProvider, {
151
+ router,
152
+ future
153
+ }),
154
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Null, {}),
155
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Null, {})
156
+ ]
157
+ })
158
+ );
159
+ return App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
160
+ children: routerWrapper
161
+ }) : routerWrapper;
162
+ };
163
+ };
164
+ return getRouteApp();
165
+ });
155
166
  }
156
167
  };
157
168
  };
@@ -60,102 +60,100 @@ function createRemixReuqest(request) {
60
60
  const routerPlugin = (userConfig = {}) => {
61
61
  return {
62
62
  name: "@modern-js/plugin-router",
63
- registerHook: {
63
+ registryHooks: {
64
64
  modifyRoutes: import_hooks.modifyRoutes,
65
- beforeCreateRoutes: import_hooks.beforeCreateRoutes
65
+ onBeforeCreateRoutes: import_hooks.onBeforeCreateRoutes
66
66
  },
67
67
  setup: (api) => {
68
68
  let finalRouteConfig = {};
69
- return {
70
- async beforeRender(context, interrupt) {
71
- var _context_ssrContext, _context_ssrContext_onTiming, _context_ssrContext1;
72
- const pluginConfig = api.useRuntimeConfigContext();
73
- const { basename = "", routesConfig, createRoutes } = (0, import_merge.merge)(pluginConfig.router || {}, userConfig);
74
- finalRouteConfig = {
75
- routes: (0, import_context.getGlobalRoutes)(),
76
- globalApp: (0, import_context.getGlobalLayoutApp)(),
77
- ...routesConfig
78
- };
79
- if (!finalRouteConfig.routes && !createRoutes) {
80
- return;
81
- }
82
- const { request, mode: ssrMode, nonce, loaderFailureMode = "errorBoundary" } = context.ssrContext;
83
- const { baseUrl } = request;
84
- const _basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, basename) : baseUrl;
85
- const { reporter } = context.ssrContext;
86
- const requestContext = (0, import_node.createRequestContext)((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.loaderContext);
87
- requestContext.set(import_node.reporterCtx, reporter);
88
- const runner = api.useHookRunners();
89
- await runner.beforeCreateRoutes(context);
90
- let routes = createRoutes ? createRoutes() : (0, import_router2.createRoutesFromElements)((0, import_utils.renderRoutes)({
91
- routesConfig: finalRouteConfig,
92
- ssrMode,
93
- props: {
94
- nonce
95
- },
96
- reporter
97
- }));
98
- routes = runner.modifyRoutes(routes);
99
- const { query } = (0, import_remix_router.createStaticHandler)(routes, {
100
- basename: _basename
101
- });
102
- const remixRequest = createRemixReuqest(context.ssrContext.request.raw);
103
- const end = (0, import_time.time)();
104
- const routerContext = await query(remixRequest, {
105
- requestContext
106
- });
107
- const cost = end();
108
- (_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);
109
- if (routerContext instanceof Response) {
110
- return interrupt(routerContext);
111
- }
112
- const errors = Object.values(routerContext.errors || {});
113
- if (
114
- // TODO: if loaderFailureMode is not 'errroBoundary', error log will not be printed.
115
- errors.length > 0 && loaderFailureMode === "clientRender"
116
- ) {
117
- routerContext.statusCode = 200;
118
- throw errors[0];
119
- }
120
- const router = (0, import_router.createStaticRouter)(routes, routerContext);
121
- context.routerContext = routerContext;
122
- context.remixRouter = router;
123
- context.routes = routes;
124
- },
125
- wrapRoot: (App) => {
126
- if (!finalRouteConfig) {
127
- return App;
128
- }
129
- const getRouteApp = () => {
130
- return () => {
131
- const context = (0, import_react.useContext)(import_core.RuntimeReactContext);
132
- const { remixRouter, routerContext, ssrContext } = context;
133
- const { nonce, mode, useJsonScript } = ssrContext;
134
- const routerWrapper = /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
135
- children: [
136
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_router.StaticRouterProvider, {
137
- router: remixRouter,
138
- context: routerContext,
139
- hydrate: false
140
- }),
141
- mode === "stream" && // ROUTER_DATA will inject in `packages/runtime/plugin-runtime/src/core/server/string/ssrData.ts` in string ssr
142
- // So we can inject it only when streaming ssr
143
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DeferredDataScripts.default, {
144
- nonce,
145
- context: routerContext,
146
- useJsonScript
147
- }),
148
- mode === "stream" && import_common.JSX_SHELL_STREAM_END_MARK
149
- ]
150
- });
151
- return App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
152
- children: routerWrapper
153
- }) : routerWrapper;
154
- };
155
- };
156
- return getRouteApp();
69
+ api.onBeforeRender(async (context, interrupt) => {
70
+ var _context_ssrContext, _context_ssrContext_onTiming, _context_ssrContext1;
71
+ const pluginConfig = api.getRuntimeConfig();
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
+ };
78
+ if (!finalRouteConfig.routes && !createRoutes) {
79
+ return;
80
+ }
81
+ const { request, mode: ssrMode, nonce, loaderFailureMode = "errorBoundary" } = context.ssrContext;
82
+ const { baseUrl } = request;
83
+ const _basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, basename) : baseUrl;
84
+ const { reporter } = context.ssrContext;
85
+ const requestContext = (0, import_node.createRequestContext)((_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.loaderContext);
86
+ requestContext.set(import_node.reporterCtx, reporter);
87
+ const hooks = api.getHooks();
88
+ await hooks.onBeforeCreateRoutes.call(context);
89
+ let routes = createRoutes ? createRoutes() : (0, import_router2.createRoutesFromElements)((0, import_utils.renderRoutes)({
90
+ routesConfig: finalRouteConfig,
91
+ ssrMode,
92
+ props: {
93
+ nonce
94
+ },
95
+ reporter
96
+ }));
97
+ routes = hooks.modifyRoutes.call(routes);
98
+ const { query } = (0, import_remix_router.createStaticHandler)(routes, {
99
+ basename: _basename
100
+ });
101
+ const remixRequest = createRemixReuqest(context.ssrContext.request.raw);
102
+ const end = (0, import_time.time)();
103
+ const routerContext = await query(remixRequest, {
104
+ requestContext
105
+ });
106
+ const cost = end();
107
+ (_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);
108
+ if (routerContext instanceof Response) {
109
+ return interrupt(routerContext);
157
110
  }
158
- };
111
+ const errors = Object.values(routerContext.errors || {});
112
+ if (
113
+ // TODO: if loaderFailureMode is not 'errroBoundary', error log will not be printed.
114
+ errors.length > 0 && loaderFailureMode === "clientRender"
115
+ ) {
116
+ routerContext.statusCode = 200;
117
+ throw errors[0];
118
+ }
119
+ const router = (0, import_router.createStaticRouter)(routes, routerContext);
120
+ context.routerContext = routerContext;
121
+ context.remixRouter = router;
122
+ context.routes = routes;
123
+ });
124
+ api.wrapRoot((App) => {
125
+ if (!finalRouteConfig) {
126
+ return App;
127
+ }
128
+ const getRouteApp = () => {
129
+ return () => {
130
+ const context = (0, import_react.useContext)(import_core.RuntimeReactContext);
131
+ const { remixRouter, routerContext, ssrContext } = context;
132
+ const { nonce, mode, useJsonScript } = ssrContext;
133
+ const routerWrapper = /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
134
+ children: [
135
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_router.StaticRouterProvider, {
136
+ router: remixRouter,
137
+ context: routerContext,
138
+ hydrate: false
139
+ }),
140
+ mode === "stream" && // ROUTER_DATA will inject in `packages/runtime/plugin-runtime/src/core/server/string/ssrData.ts` in string ssr
141
+ // So we can inject it only when streaming ssr
142
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DeferredDataScripts.default, {
143
+ nonce,
144
+ context: routerContext,
145
+ useJsonScript
146
+ }),
147
+ mode === "stream" && import_common.JSX_SHELL_STREAM_END_MARK
148
+ ]
149
+ });
150
+ return App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
151
+ children: routerWrapper
152
+ }) : routerWrapper;
153
+ };
154
+ };
155
+ return getRouteApp();
156
+ });
159
157
  }
160
158
  };
161
159
  };
@@ -2,7 +2,6 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
2
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
3
  import { jsx as _jsx } from "react/jsx-runtime";
4
4
  import { loadableReady } from "@loadable/component";
5
- import { normalizePathname } from "@modern-js/runtime-utils/url";
6
5
  import { RenderLevel } from "../constants";
7
6
  import { wrapRuntimeContextProvider } from "../react/wrapper";
8
7
  import { WithCallback } from "./withCallback";
@@ -10,21 +9,13 @@ var isReact18 = function() {
10
9
  return process.env.IS_REACT18 === "true";
11
10
  };
12
11
  function hydrateRoot(App, context, ModernRender, ModernHydrate) {
13
- var _ssrContext_request, _window__SSR_DATA, _window, _window__SSR_DATA1, _window1;
12
+ var _window__SSR_DATA, _window, _window__SSR_DATA1, _window1;
14
13
  var hydrateContext = _object_spread_props(_object_spread({}, context), {
15
14
  get routes() {
16
15
  return context.routes;
17
16
  },
18
17
  _hydration: true
19
18
  });
20
- var ssrContext = hydrateContext.ssrContext;
21
- var currentPathname = normalizePathname(window.location.pathname);
22
- 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);
23
- if (initialPathname && initialPathname !== currentPathname && context.router) {
24
- var errorMsg = "The initial URL ".concat(initialPathname, " and the URL ").concat(currentPathname, " to be hydrated do not match, reload.");
25
- console.error(errorMsg);
26
- window.location.reload();
27
- }
28
19
  var callback = function() {
29
20
  delete hydrateContext._hydration;
30
21
  };
@@ -5,10 +5,9 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
5
5
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
6
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
7
  import cookieTool from "cookie";
8
- import { getGlobalAppInit } from "../context";
8
+ import { getGlobalAppInit, getGlobalInternalRuntimeContext } from "../context";
9
9
  import { getInitialContext } from "../context/runtime";
10
10
  import { createLoaderManager } from "../loader/loaderManager";
11
- import { getGlobalRunner } from "../plugin/runner";
12
11
  import { wrapRuntimeContextProvider } from "../react/wrapper";
13
12
  import { hydrateRoot } from "./hydrate";
14
13
  var IS_REACT18 = process.env.IS_REACT18 === "true";
@@ -54,7 +53,7 @@ function render(App, id) {
54
53
  }
55
54
  function _render() {
56
55
  _render = _async_to_generator(function(App, id) {
57
- var runner, context, runBeforeRender, ModernRender, ModernHydrate, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement;
56
+ var context, runBeforeRender, ModernRender, ModernHydrate, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement;
58
57
  function _ModernRender() {
59
58
  _ModernRender = _async_to_generator(function(App2) {
60
59
  var renderFunc;
@@ -84,17 +83,20 @@ function _render() {
84
83
  return _ts_generator(this, function(_state) {
85
84
  switch (_state.label) {
86
85
  case 0:
87
- runner = getGlobalRunner();
88
- context = getInitialContext(runner);
86
+ context = getInitialContext();
89
87
  runBeforeRender = function() {
90
88
  var _ref = _async_to_generator(function(context2) {
91
- var init;
89
+ var internalRuntimeContext, api, hooks, init;
92
90
  return _ts_generator(this, function(_state2) {
93
91
  switch (_state2.label) {
94
92
  case 0:
93
+ internalRuntimeContext = getGlobalInternalRuntimeContext();
94
+ api = internalRuntimeContext.pluginAPI;
95
+ api.updateRuntimeContext(context2);
96
+ hooks = internalRuntimeContext.hooks;
95
97
  return [
96
98
  4,
97
- runner.beforeRender(context2)
99
+ hooks.onBeforeRender.call(context2)
98
100
  ];
99
101
  case 1:
100
102
  _state2.sent();
@@ -0,0 +1,88 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ function transformHookRunner(hookRunnerName) {
5
+ switch (hookRunnerName) {
6
+ case "beforeRender":
7
+ return "onBeforeRender";
8
+ default:
9
+ return hookRunnerName;
10
+ }
11
+ }
12
+ function handleSetupResult(setupResult, api) {
13
+ if (!setupResult) {
14
+ return;
15
+ }
16
+ Object.keys(setupResult).forEach(function(key) {
17
+ var fn = setupResult[key];
18
+ if (typeof fn === "function") {
19
+ var newAPI = transformHookRunner(key);
20
+ if (api[newAPI]) {
21
+ if (key === "beforeRender") {
22
+ api[newAPI](/* @__PURE__ */ _async_to_generator(function() {
23
+ var _len, params, _key;
24
+ var _arguments = arguments;
25
+ return _ts_generator(this, function(_state) {
26
+ switch (_state.label) {
27
+ case 0:
28
+ for (_len = _arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
29
+ params[_key] = _arguments[_key];
30
+ }
31
+ return [
32
+ 4,
33
+ fn.apply(void 0, _to_consumable_array(params))
34
+ ];
35
+ case 1:
36
+ _state.sent();
37
+ return [
38
+ 2
39
+ ];
40
+ }
41
+ });
42
+ }));
43
+ } else {
44
+ api[newAPI](function() {
45
+ for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
46
+ params[_key] = arguments[_key];
47
+ }
48
+ var res = fn.apply(void 0, _to_consumable_array(params));
49
+ return res;
50
+ });
51
+ }
52
+ }
53
+ }
54
+ });
55
+ }
56
+ function getHookRunners(runtimeContext) {
57
+ var _internalContext = runtimeContext._internalContext;
58
+ var hooks = _internalContext.hooks;
59
+ return {
60
+ beforeRender: function() {
61
+ var _ref = _async_to_generator(function(context) {
62
+ return _ts_generator(this, function(_state) {
63
+ return [
64
+ 2,
65
+ hooks.onBeforeRender.call(context)
66
+ ];
67
+ });
68
+ });
69
+ return function(context) {
70
+ return _ref.apply(this, arguments);
71
+ };
72
+ }(),
73
+ wrapRoot: function(App) {
74
+ return hooks.wrapRoot.call(App);
75
+ },
76
+ pickContext: function(context) {
77
+ return hooks.pickContext.call(context);
78
+ },
79
+ modifyRuntimeConfig: function(config) {
80
+ return hooks.modifyRuntimeConfig.call(config);
81
+ }
82
+ };
83
+ }
84
+ export {
85
+ getHookRunners,
86
+ handleSetupResult,
87
+ transformHookRunner
88
+ };
@@ -0,0 +1,20 @@
1
+ import { getHookRunners } from "./hooks";
2
+ var compatPlugin = function() {
3
+ return {
4
+ name: "@modern-js/runtime-plugin-compat",
5
+ _registryApi: function(getRuntimeContext) {
6
+ return {
7
+ useRuntimeConfigContext: function() {
8
+ var _internalContext = getRuntimeContext()._internalContext;
9
+ return _internalContext.config;
10
+ },
11
+ useHookRunners: function() {
12
+ return getHookRunners(getRuntimeContext());
13
+ }
14
+ };
15
+ }
16
+ };
17
+ };
18
+ export {
19
+ compatPlugin
20
+ };