@modern-js/runtime 0.0.0-next-1680077090761 → 0.0.0-next-1680098769946

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.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # @modern-js/runtime
2
2
 
3
- ## 0.0.0-next-1680077090761
3
+ ## 0.0.0-next-1680098769946
4
4
 
5
5
  ### Patch Changes
6
6
 
7
+ - 5d624fdc1: feat: assets and data prefetching is supported
8
+ feat: 支持资源和数据预加载
7
9
  - e2466a107: fix: remove nestedRoutes file from @modern-js/utils
8
10
  fix: 将 nestedRoutes 从 @modern-js/utils 中移除
9
11
  - 02bb383cb: fix: fix lost props when using nested route in micro-front-end mode
@@ -12,14 +14,17 @@
12
14
 
13
15
  feat(utils): 将运行时使用的 utils 移动到 universal 目录
14
16
 
17
+ - Updated dependencies [cfb058fbf]
18
+ - Updated dependencies [5d624fdc1]
15
19
  - Updated dependencies [e2466a107]
16
20
  - Updated dependencies [02bb383cb]
17
21
  - Updated dependencies [381a3b9f3]
18
22
  - Updated dependencies [7a60f1071]
19
23
  - Updated dependencies [274b2e535]
20
- - @modern-js/utils@0.0.0-next-1680077090761
21
- - @modern-js/plugin@0.0.0-next-1680077090761
22
- - @modern-js/types@0.0.0-next-1680077090761
24
+ - Updated dependencies [b9e1c546b]
25
+ - @modern-js/utils@0.0.0-next-1680098769946
26
+ - @modern-js/plugin@0.0.0-next-1680098769946
27
+ - @modern-js/types@0.0.0-next-1680098769946
23
28
 
24
29
  ## 2.10.0
25
30
 
@@ -0,0 +1,205 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var PrefetchLink_exports = {};
29
+ __export(PrefetchLink_exports, {
30
+ PrefetchLink: () => Link,
31
+ PrefetchNavLink: () => NavLink,
32
+ composeEventHandlers: () => composeEventHandlers
33
+ });
34
+ module.exports = __toCommonJS(PrefetchLink_exports);
35
+ var import_jsx_runtime = require("react/jsx-runtime");
36
+ var import_react = __toESM(require("react"));
37
+ var import_react_router_dom = require("react-router-dom");
38
+ var import_core = require("../../core");
39
+ function composeEventHandlers(theirHandler, ourHandler) {
40
+ return (event) => {
41
+ theirHandler == null ? void 0 : theirHandler(event);
42
+ if (!event.defaultPrevented) {
43
+ ourHandler(event);
44
+ }
45
+ };
46
+ }
47
+ const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
48
+ function usePrefetchBehavior(prefetch, theirElementProps) {
49
+ const [maybePrefetch, setMaybePrefetch] = import_react.default.useState(false);
50
+ const [shouldPrefetch, setShouldPrefetch] = import_react.default.useState(false);
51
+ const { onFocus, onBlur, onMouseEnter, onMouseLeave, onTouchStart } = theirElementProps;
52
+ import_react.default.useEffect(() => {
53
+ if (prefetch === "render") {
54
+ setShouldPrefetch(true);
55
+ }
56
+ }, [prefetch]);
57
+ const setIntent = () => {
58
+ if (prefetch === "intent") {
59
+ setMaybePrefetch(true);
60
+ }
61
+ };
62
+ const cancelIntent = () => {
63
+ if (prefetch === "intent") {
64
+ setMaybePrefetch(false);
65
+ setShouldPrefetch(false);
66
+ }
67
+ };
68
+ import_react.default.useEffect(() => {
69
+ if (maybePrefetch) {
70
+ const id = setTimeout(() => {
71
+ setShouldPrefetch(true);
72
+ }, 100);
73
+ return () => {
74
+ clearTimeout(id);
75
+ };
76
+ }
77
+ }, [maybePrefetch]);
78
+ return [
79
+ shouldPrefetch,
80
+ {
81
+ onFocus: composeEventHandlers(onFocus, setIntent),
82
+ onBlur: composeEventHandlers(onBlur, cancelIntent),
83
+ onMouseEnter: composeEventHandlers(onMouseEnter, setIntent),
84
+ onMouseLeave: composeEventHandlers(onMouseLeave, cancelIntent),
85
+ onTouchStart: composeEventHandlers(onTouchStart, setIntent)
86
+ }
87
+ ];
88
+ }
89
+ async function loadRouteModule(route, routeAssets) {
90
+ const routeId = route.id;
91
+ if (!routeId) {
92
+ return;
93
+ }
94
+ if (!routeAssets[routeId]) {
95
+ return;
96
+ }
97
+ const { chunkIds } = routeAssets[routeId];
98
+ if (!chunkIds) {
99
+ return;
100
+ }
101
+ try {
102
+ await Promise.all(
103
+ chunkIds.map((chunkId) => {
104
+ return __webpack_chunk_load__ == null ? void 0 : __webpack_chunk_load__(String(chunkId));
105
+ })
106
+ );
107
+ } catch (error) {
108
+ console.error(error);
109
+ }
110
+ }
111
+ const getRequestUrl = (pathname, routeId) => {
112
+ const LOADER_ID_PARAM = "__loader";
113
+ const DIRECT_PARAM = "__ssrDirect";
114
+ const { protocol, host } = window.location;
115
+ const url = new URL(pathname, `${protocol}//${host}`);
116
+ url.searchParams.append(LOADER_ID_PARAM, routeId);
117
+ url.searchParams.append(DIRECT_PARAM, "true");
118
+ return url;
119
+ };
120
+ const createDataHref = (href) => {
121
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("link", { rel: "prefetch", as: "fetch", href }, href);
122
+ };
123
+ const getDataHref = (route, pathname, basename) => {
124
+ const { id } = route;
125
+ const path = basename === "/" ? pathname : `${basename}${pathname}`;
126
+ const url = getRequestUrl(path, id);
127
+ return createDataHref(url.toString());
128
+ };
129
+ const PrefetchPageLinks = ({ pathname }) => {
130
+ const context = (0, import_react.useContext)(import_core.RuntimeReactContext);
131
+ const { routeManifest, routes } = context;
132
+ const { routeAssets } = routeManifest;
133
+ const matches = Array.isArray(routes) ? (0, import_react_router_dom.matchRoutes)(routes, pathname) : [];
134
+ if (Array.isArray(matches)) {
135
+ matches == null ? void 0 : matches.forEach((match) => loadRouteModule(match.route, routeAssets));
136
+ }
137
+ if (!window._SSR_DATA) {
138
+ return null;
139
+ }
140
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
141
+ PrefetchDataLinks,
142
+ {
143
+ matches,
144
+ pathname,
145
+ routeManifest
146
+ }
147
+ );
148
+ };
149
+ const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
150
+ const currentMatches = (0, import_react_router_dom.useMatches)();
151
+ const basename = (0, import_react_router_dom.useHref)("/");
152
+ const dataHrefs = (0, import_react.useMemo)(() => {
153
+ return matches == null ? void 0 : matches.filter(
154
+ (match) => match.route.loader && typeof match.route.loader === "function" && match.route.loader.length > 0
155
+ ).filter((match, index) => {
156
+ const currentMatch = currentMatches[index];
157
+ if (!currentMatch || currentMatch.id !== match.route.id) {
158
+ return true;
159
+ }
160
+ if (currentMatch.pathname !== match.pathname) {
161
+ return true;
162
+ }
163
+ if (currentMatch.pathname.endsWith("*") && currentMatch.params["*"] !== match.params["*"]) {
164
+ return true;
165
+ }
166
+ return false;
167
+ }).map((match) => getDataHref(match.route, pathname, basename));
168
+ }, [matches, pathname, routeManifest]);
169
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: dataHrefs });
170
+ };
171
+ const createPrefetchLink = (Link2) => {
172
+ return import_react.default.forwardRef(
173
+ ({ to, prefetch = "none", ...props }, forwardedRef) => {
174
+ const isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);
175
+ const [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
176
+ prefetch,
177
+ props
178
+ );
179
+ const resolvedPath = (0, import_react_router_dom.useResolvedPath)(to);
180
+ const { pathname } = resolvedPath;
181
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
182
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
183
+ Link2,
184
+ {
185
+ ref: forwardedRef,
186
+ to,
187
+ ...props,
188
+ ...prefetchHandlers
189
+ }
190
+ ),
191
+ shouldPrefetch && __webpack_chunk_load__ && !isAbsolute ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PrefetchPageLinks, { pathname }) : null
192
+ ] });
193
+ }
194
+ );
195
+ };
196
+ const Link = createPrefetchLink(import_react_router_dom.Link);
197
+ Link.displayName = "Link";
198
+ const NavLink = createPrefetchLink(import_react_router_dom.NavLink);
199
+ NavLink.displayName = "NavLink";
200
+ // Annotate the CommonJS export names for ESM import in node:
201
+ 0 && (module.exports = {
202
+ PrefetchLink,
203
+ PrefetchNavLink,
204
+ composeEventHandlers
205
+ });
@@ -27,6 +27,8 @@ __export(runtime_exports, {
27
27
  NavLink: () => import_react_router_dom.NavLink,
28
28
  Navigate: () => import_react_router_dom.Navigate,
29
29
  Outlet: () => import_react_router_dom.Outlet,
30
+ PrefetchLink: () => import_PrefetchLink.PrefetchLink,
31
+ PrefetchNavLink: () => import_PrefetchLink.PrefetchNavLink,
30
32
  Route: () => import_react_router_dom.Route,
31
33
  Router: () => import_react_router_dom.Router,
32
34
  RouterProvider: () => import_react_router_dom.RouterProvider,
@@ -81,6 +83,7 @@ module.exports = __toCommonJS(runtime_exports);
81
83
  var import_plugin = require("./plugin");
82
84
  var import_plugin2 = require("./plugin");
83
85
  __reExport(runtime_exports, require("./withRouter"), module.exports);
86
+ var import_PrefetchLink = require("./PrefetchLink");
84
87
  var import_react_router_dom = require("react-router-dom");
85
88
  var import_remix_router = require("@modern-js/utils/universal/remix-router");
86
89
  var runtime_default = import_plugin.routerPlugin;
@@ -95,6 +98,8 @@ var runtime_default = import_plugin.routerPlugin;
95
98
  NavLink,
96
99
  Navigate,
97
100
  Outlet,
101
+ PrefetchLink,
102
+ PrefetchNavLink,
98
103
  Route,
99
104
  Router,
100
105
  RouterProvider,
@@ -60,6 +60,7 @@ const routerPlugin = ({
60
60
  createRoutes
61
61
  }) => {
62
62
  const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
63
+ let routes = [];
63
64
  finalRouteConfig = routesConfig;
64
65
  return {
65
66
  name: "@modern-js/plugin-router",
@@ -70,6 +71,11 @@ const routerPlugin = ({
70
71
  useMatches: import_react_router_dom.useMatches,
71
72
  useLocation: import_react_router_dom.useLocation
72
73
  };
74
+ Object.defineProperty(context, "routes", {
75
+ get() {
76
+ return routes;
77
+ }
78
+ });
73
79
  return next({ context });
74
80
  },
75
81
  hoc: ({ App }, next) => {
@@ -80,7 +86,7 @@ const routerPlugin = ({
80
86
  return (props) => {
81
87
  var _a;
82
88
  beforeCreateRouter = true;
83
- const routes = createRoutes ? createRoutes() : (0, import_react_router_dom.createRoutesFromElements)(
89
+ routes = createRoutes ? createRoutes() : (0, import_react_router_dom.createRoutesFromElements)(
84
90
  (0, import_utils.renderRoutes)({
85
91
  routesConfig: finalRouteConfig,
86
92
  props
@@ -39,7 +39,7 @@ module.exports = __toCommonJS(utils_exports);
39
39
  var import_jsx_runtime = require("react/jsx-runtime");
40
40
  var import_react_router_dom = require("react-router-dom");
41
41
  var import_remix_router = require("@modern-js/utils/universal/remix-router");
42
- var import_nestedRoutes = require("./nestedRoutes");
42
+ var import_nestedRoutes = require("@modern-js/utils/universal/nestedRoutes");
43
43
  var import_DefaultNotFound = require("./DefaultNotFound");
44
44
  var import_DeferredDataScripts = __toESM(require("./DeferredDataScripts"));
45
45
  function getRouteComponents(routes, {
@@ -48,6 +48,9 @@ const ssr = (config) => ({
48
48
  var _a;
49
49
  const hydrateContext = {
50
50
  ...context,
51
+ get routes() {
52
+ return context.routes;
53
+ },
51
54
  _hydration: true
52
55
  };
53
56
  const callback = () => {
@@ -71,8 +71,8 @@ function getHeadTemplate(beforeEntryTemplate, context) {
71
71
  if (routeId) {
72
72
  const routeManifest2 = routeAssets[routeId];
73
73
  if (routeManifest2) {
74
- const { assets = [] } = routeManifest2;
75
- const _cssChunks = assets.filter(
74
+ const { referenceCssAssets = [] } = routeManifest2;
75
+ const _cssChunks = referenceCssAssets.filter(
76
76
  (asset) => asset == null ? void 0 : asset.endsWith(".css")
77
77
  );
78
78
  cssChunks.push(..._cssChunks);