@modern-js/runtime 2.36.0 → 2.37.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.
@@ -133,7 +133,8 @@ const getDataHref = (route, pathname, basename) => {
133
133
  const url = getRequestUrl(path, id);
134
134
  return createDataHref(url.toString());
135
135
  };
136
- const PrefetchPageLinks = ({ pathname }) => {
136
+ const PrefetchPageLinks = ({ path }) => {
137
+ const { pathname } = path;
137
138
  const context = (0, import_react.useContext)(import_core.RuntimeReactContext);
138
139
  const { routeManifest, routes } = context;
139
140
  const { routeAssets } = routeManifest || {};
@@ -146,11 +147,12 @@ const PrefetchPageLinks = ({ pathname }) => {
146
147
  }
147
148
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PrefetchDataLinks, {
148
149
  matches,
149
- pathname,
150
+ path,
150
151
  routeManifest
151
152
  });
152
153
  };
153
- const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
154
+ const PrefetchDataLinks = ({ matches, path, routeManifest }) => {
155
+ const { pathname, search, hash } = path;
154
156
  const currentMatches = (0, import_router.useMatches)();
155
157
  const basename = (0, import_router.useHref)("/");
156
158
  const dataHrefs = (0, import_react.useMemo)(() => {
@@ -158,6 +160,21 @@ const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
158
160
  if (!match.route.loader || typeof match.route.loader !== "function" || match.route.loader.length === 0) {
159
161
  return false;
160
162
  }
163
+ if (match.route.shouldRevalidate) {
164
+ var _currentMatches_;
165
+ const currentUrl = new URL(location.pathname + location.search + location.hash, window.origin);
166
+ const nextUrl = new URL(pathname + search + hash, window.origin);
167
+ const shouldLoad = match.route.shouldRevalidate({
168
+ currentUrl,
169
+ currentParams: ((_currentMatches_ = currentMatches[0]) === null || _currentMatches_ === void 0 ? void 0 : _currentMatches_.params) || {},
170
+ nextUrl,
171
+ nextParams: match.params,
172
+ defaultShouldRevalidate: true
173
+ });
174
+ if (typeof shouldLoad === "boolean") {
175
+ return shouldLoad;
176
+ }
177
+ }
161
178
  const currentMatch = currentMatches[index];
162
179
  if (!currentMatch || currentMatch.id !== match.route.id) {
163
180
  return true;
@@ -184,7 +201,6 @@ const createPrefetchLink = (Link2) => {
184
201
  const isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);
185
202
  const [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(prefetch, props);
186
203
  const resolvedPath = (0, import_router.useResolvedPath)(to);
187
- const { pathname } = resolvedPath;
188
204
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
189
205
  children: [
190
206
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Link2, {
@@ -194,7 +210,7 @@ const createPrefetchLink = (Link2) => {
194
210
  ...prefetchHandlers
195
211
  }),
196
212
  shouldPrefetch && __webpack_chunk_load__ && !isAbsolute ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PrefetchPageLinks, {
197
- pathname
213
+ path: resolvedPath
198
214
  }) : null
199
215
  ]
200
216
  });
@@ -40,6 +40,7 @@ __export(runtime_exports, {
40
40
  createRoutesFromChildren: () => import_router2.createRoutesFromChildren,
41
41
  createRoutesFromElements: () => import_router2.createRoutesFromElements,
42
42
  createSearchParams: () => import_router2.createSearchParams,
43
+ createShouldRevalidate: () => import_shouldRevalidate.createShouldRevalidate,
43
44
  default: () => runtime_default,
44
45
  defer: () => import_router2.defer,
45
46
  generatePath: () => import_router2.generatePath,
@@ -89,6 +90,7 @@ var import_utils = require("./utils");
89
90
  var import_plugin2 = require("./plugin");
90
91
  __reExport(runtime_exports, require("./withRouter"), module.exports);
91
92
  var import_PrefetchLink = require("./PrefetchLink");
93
+ var import_shouldRevalidate = require("./shouldRevalidate");
92
94
  var import_router2 = require("@modern-js/runtime-utils/router");
93
95
  var runtime_default = import_plugin.routerPlugin;
94
96
  const useRouteLoaderData = (routeId) => {
@@ -118,6 +120,7 @@ const useRouteLoaderData = (routeId) => {
118
120
  createRoutesFromChildren,
119
121
  createRoutesFromElements,
120
122
  createSearchParams,
123
+ createShouldRevalidate,
121
124
  defer,
122
125
  generatePath,
123
126
  isRouteErrorResponse,
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var shouldRevalidate_exports = {};
20
+ __export(shouldRevalidate_exports, {
21
+ createShouldRevalidate: () => createShouldRevalidate
22
+ });
23
+ module.exports = __toCommonJS(shouldRevalidate_exports);
24
+ var import_constants = require("@modern-js/utils/universal/constants");
25
+ const createShouldRevalidate = (routeId) => {
26
+ return (arg) => {
27
+ var _window_ROUTE_MODULES, _window;
28
+ const routeModule = (_window = window) === null || _window === void 0 ? void 0 : (_window_ROUTE_MODULES = _window[import_constants.ROUTE_MODULES]) === null || _window_ROUTE_MODULES === void 0 ? void 0 : _window_ROUTE_MODULES[routeId];
29
+ if (routeModule && typeof routeModule.shouldRevalidate === "function") {
30
+ return routeModule.shouldRevalidate(arg);
31
+ }
32
+ return arg.defaultShouldRevalidate;
33
+ };
34
+ };
35
+ // Annotate the CommonJS export names for ESM import in node:
36
+ 0 && (module.exports = {
37
+ createShouldRevalidate
38
+ });
@@ -146,7 +146,8 @@ var getDataHref = function(route, pathname, basename) {
146
146
  return createDataHref(url.toString());
147
147
  };
148
148
  var PrefetchPageLinks = function(param) {
149
- var pathname = param.pathname;
149
+ var path = param.path;
150
+ var pathname = path.pathname;
150
151
  var context = useContext(RuntimeReactContext);
151
152
  var routeManifest = context.routeManifest, routes = context.routes;
152
153
  var routeAssets = (routeManifest || {}).routeAssets;
@@ -161,12 +162,13 @@ var PrefetchPageLinks = function(param) {
161
162
  }
162
163
  return /* @__PURE__ */ _jsx(PrefetchDataLinks, {
163
164
  matches,
164
- pathname,
165
+ path,
165
166
  routeManifest
166
167
  });
167
168
  };
168
169
  var PrefetchDataLinks = function(param) {
169
- var matches = param.matches, pathname = param.pathname, routeManifest = param.routeManifest;
170
+ var matches = param.matches, path = param.path, routeManifest = param.routeManifest;
171
+ var pathname = path.pathname, search = path.search, hash = path.hash;
170
172
  var currentMatches = useMatches();
171
173
  var basename = useHref("/");
172
174
  var dataHrefs = useMemo(function() {
@@ -174,6 +176,21 @@ var PrefetchDataLinks = function(param) {
174
176
  if (!match.route.loader || typeof match.route.loader !== "function" || match.route.loader.length === 0) {
175
177
  return false;
176
178
  }
179
+ if (match.route.shouldRevalidate) {
180
+ var _currentMatches_;
181
+ var currentUrl = new URL(location.pathname + location.search + location.hash, window.origin);
182
+ var nextUrl = new URL(pathname + search + hash, window.origin);
183
+ var shouldLoad = match.route.shouldRevalidate({
184
+ currentUrl,
185
+ currentParams: ((_currentMatches_ = currentMatches[0]) === null || _currentMatches_ === void 0 ? void 0 : _currentMatches_.params) || {},
186
+ nextUrl,
187
+ nextParams: match.params,
188
+ defaultShouldRevalidate: true
189
+ });
190
+ if (typeof shouldLoad === "boolean") {
191
+ return shouldLoad;
192
+ }
193
+ }
177
194
  var currentMatch = currentMatches[index];
178
195
  if (!currentMatch || currentMatch.id !== match.route.id) {
179
196
  return true;
@@ -206,7 +223,6 @@ var createPrefetchLink = function(Link2) {
206
223
  var isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);
207
224
  var _usePrefetchBehavior = _sliced_to_array(usePrefetchBehavior(prefetch, props), 2), shouldPrefetch = _usePrefetchBehavior[0], prefetchHandlers = _usePrefetchBehavior[1];
208
225
  var resolvedPath = useResolvedPath(to);
209
- var pathname = resolvedPath.pathname;
210
226
  return /* @__PURE__ */ _jsxs(_Fragment, {
211
227
  children: [
212
228
  /* @__PURE__ */ _jsx(Link2, _object_spread({
@@ -214,7 +230,7 @@ var createPrefetchLink = function(Link2) {
214
230
  to
215
231
  }, props, prefetchHandlers)),
216
232
  shouldPrefetch && __webpack_chunk_load__ && !isAbsolute ? /* @__PURE__ */ _jsx(PrefetchPageLinks, {
217
- pathname
233
+ path: resolvedPath
218
234
  }) : null
219
235
  ]
220
236
  });
@@ -9,6 +9,7 @@ var useRouteLoaderData = function(routeId) {
9
9
  var realRouteId = routeId.replace(/\[(.*?)\]/g, "($1)");
10
10
  return useRouteData(realRouteId);
11
11
  };
12
+ import { createShouldRevalidate } from "./shouldRevalidate";
12
13
  import {
13
14
  createBrowserRouter,
14
15
  createHashRouter,
@@ -89,6 +90,7 @@ export {
89
90
  createRoutesFromChildren,
90
91
  createRoutesFromElements,
91
92
  createSearchParams,
93
+ createShouldRevalidate,
92
94
  runtime_default as default,
93
95
  defer,
94
96
  generatePath,
@@ -0,0 +1,14 @@
1
+ import { ROUTE_MODULES } from "@modern-js/utils/universal/constants";
2
+ var createShouldRevalidate = function(routeId) {
3
+ return function(arg) {
4
+ var _window_ROUTE_MODULES, _window;
5
+ var routeModule = (_window = window) === null || _window === void 0 ? void 0 : (_window_ROUTE_MODULES = _window[ROUTE_MODULES]) === null || _window_ROUTE_MODULES === void 0 ? void 0 : _window_ROUTE_MODULES[routeId];
6
+ if (routeModule && typeof routeModule.shouldRevalidate === "function") {
7
+ return routeModule.shouldRevalidate(arg);
8
+ }
9
+ return arg.defaultShouldRevalidate;
10
+ };
11
+ };
12
+ export {
13
+ createShouldRevalidate
14
+ };
@@ -98,7 +98,8 @@ const getDataHref = (route, pathname, basename) => {
98
98
  const url = getRequestUrl(path, id);
99
99
  return createDataHref(url.toString());
100
100
  };
101
- const PrefetchPageLinks = ({ pathname }) => {
101
+ const PrefetchPageLinks = ({ path }) => {
102
+ const { pathname } = path;
102
103
  const context = useContext(RuntimeReactContext);
103
104
  const { routeManifest, routes } = context;
104
105
  const { routeAssets } = routeManifest || {};
@@ -111,11 +112,12 @@ const PrefetchPageLinks = ({ pathname }) => {
111
112
  }
112
113
  return /* @__PURE__ */ _jsx(PrefetchDataLinks, {
113
114
  matches,
114
- pathname,
115
+ path,
115
116
  routeManifest
116
117
  });
117
118
  };
118
- const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
119
+ const PrefetchDataLinks = ({ matches, path, routeManifest }) => {
120
+ const { pathname, search, hash } = path;
119
121
  const currentMatches = useMatches();
120
122
  const basename = useHref("/");
121
123
  const dataHrefs = useMemo(() => {
@@ -123,6 +125,21 @@ const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
123
125
  if (!match.route.loader || typeof match.route.loader !== "function" || match.route.loader.length === 0) {
124
126
  return false;
125
127
  }
128
+ if (match.route.shouldRevalidate) {
129
+ var _currentMatches_;
130
+ const currentUrl = new URL(location.pathname + location.search + location.hash, window.origin);
131
+ const nextUrl = new URL(pathname + search + hash, window.origin);
132
+ const shouldLoad = match.route.shouldRevalidate({
133
+ currentUrl,
134
+ currentParams: ((_currentMatches_ = currentMatches[0]) === null || _currentMatches_ === void 0 ? void 0 : _currentMatches_.params) || {},
135
+ nextUrl,
136
+ nextParams: match.params,
137
+ defaultShouldRevalidate: true
138
+ });
139
+ if (typeof shouldLoad === "boolean") {
140
+ return shouldLoad;
141
+ }
142
+ }
126
143
  const currentMatch = currentMatches[index];
127
144
  if (!currentMatch || currentMatch.id !== match.route.id) {
128
145
  return true;
@@ -149,7 +166,6 @@ const createPrefetchLink = (Link2) => {
149
166
  const isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);
150
167
  const [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(prefetch, props);
151
168
  const resolvedPath = useResolvedPath(to);
152
- const { pathname } = resolvedPath;
153
169
  return /* @__PURE__ */ _jsxs(_Fragment, {
154
170
  children: [
155
171
  /* @__PURE__ */ _jsx(Link2, {
@@ -159,7 +175,7 @@ const createPrefetchLink = (Link2) => {
159
175
  ...prefetchHandlers
160
176
  }),
161
177
  shouldPrefetch && __webpack_chunk_load__ && !isAbsolute ? /* @__PURE__ */ _jsx(PrefetchPageLinks, {
162
- pathname
178
+ path: resolvedPath
163
179
  }) : null
164
180
  ]
165
181
  });
@@ -9,6 +9,7 @@ const useRouteLoaderData = (routeId) => {
9
9
  const realRouteId = routeId.replace(/\[(.*?)\]/g, "($1)");
10
10
  return useRouteData(realRouteId);
11
11
  };
12
+ import { createShouldRevalidate } from "./shouldRevalidate";
12
13
  import {
13
14
  createBrowserRouter,
14
15
  createHashRouter,
@@ -89,6 +90,7 @@ export {
89
90
  createRoutesFromChildren,
90
91
  createRoutesFromElements,
91
92
  createSearchParams,
93
+ createShouldRevalidate,
92
94
  runtime_default as default,
93
95
  defer,
94
96
  generatePath,
@@ -0,0 +1,14 @@
1
+ import { ROUTE_MODULES } from "@modern-js/utils/universal/constants";
2
+ const createShouldRevalidate = (routeId) => {
3
+ return (arg) => {
4
+ var _window_ROUTE_MODULES, _window;
5
+ const routeModule = (_window = window) === null || _window === void 0 ? void 0 : (_window_ROUTE_MODULES = _window[ROUTE_MODULES]) === null || _window_ROUTE_MODULES === void 0 ? void 0 : _window_ROUTE_MODULES[routeId];
6
+ if (routeModule && typeof routeModule.shouldRevalidate === "function") {
7
+ return routeModule.shouldRevalidate(arg);
8
+ }
9
+ return arg.defaultShouldRevalidate;
10
+ };
11
+ };
12
+ export {
13
+ createShouldRevalidate
14
+ };
@@ -9,6 +9,7 @@ export * from './withRouter';
9
9
  export { Link, NavLink } from './PrefetchLink';
10
10
  export type { LinkProps, NavLinkProps } from './PrefetchLink';
11
11
  export declare const useRouteLoaderData: typeof useRouteData;
12
+ export { createShouldRevalidate } from './shouldRevalidate';
12
13
  export type { LoaderFunction, LoaderFunctionArgs } from './types';
13
14
  export type { FormEncType, FormMethod, GetScrollRestorationKeyFunction, ParamKeyValuePair, SubmitOptions, URLSearchParamsInit, FetcherWithComponents, BrowserRouterProps, HashRouterProps, HistoryRouterProps, FormProps, ScrollRestorationProps, SubmitFunction, ActionFunction, ActionFunctionArgs, AwaitProps, unstable_Blocker, unstable_BlockerFunction, DataRouteMatch, DataRouteObject, Fetcher, Hash, IndexRouteObject, IndexRouteProps, JsonFunction, LayoutRouteProps, Location, MemoryRouterProps, NavigateFunction, NavigateOptions, NavigateProps, Navigation, Navigator, NonIndexRouteObject, OutletProps, Params, ParamParseKey, Path, PathMatch, Pathname, PathPattern, PathRouteProps, RedirectFunction, RelativeRoutingType, RouteMatch, RouteObject, RouteProps, RouterProps, RouterProviderProps, RoutesProps, Search, ShouldRevalidateFunction, To } from '@modern-js/runtime-utils/router';
14
15
  export { createBrowserRouter, createHashRouter, createMemoryRouter, RouterProvider, BrowserRouter, HashRouter, MemoryRouter, Router, Await, Form, Navigate, Outlet, Route, Routes, ScrollRestoration, useActionData, useAsyncError, useAsyncValue, useBeforeUnload, useFetcher, useFetchers, useFormAction, useHref, useInRouterContext, useLinkClickHandler, useLoaderData, useLocation, useMatch, useMatches, useNavigate, useNavigation, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRevalidator, useRouteError, useRoutes, useSearchParams, useSubmit, createRoutesFromChildren, createRoutesFromElements, createSearchParams, generatePath, isRouteErrorResponse, matchPath, matchRoutes, renderMatches, resolvePath, createPath, unstable_useBlocker, unstable_usePrompt, defer, json, redirect } from '@modern-js/runtime-utils/router';
@@ -0,0 +1,2 @@
1
+ import { ShouldRevalidateFunction } from '@modern-js/runtime-utils/remix-router';
2
+ export declare const createShouldRevalidate: (routeId: string) => ShouldRevalidateFunction;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.36.0",
18
+ "version": "2.37.0",
19
19
  "engines": {
20
20
  "node": ">=14.17.6"
21
21
  },
@@ -171,10 +171,10 @@
171
171
  "react-side-effect": "^2.1.1",
172
172
  "styled-components": "^5.3.1",
173
173
  "@swc/helpers": "0.5.1",
174
- "@modern-js/plugin": "2.36.0",
175
- "@modern-js/types": "2.36.0",
176
- "@modern-js/utils": "2.36.0",
177
- "@modern-js/runtime-utils": "2.36.0"
174
+ "@modern-js/plugin": "2.37.0",
175
+ "@modern-js/utils": "2.37.0",
176
+ "@modern-js/types": "2.37.0",
177
+ "@modern-js/runtime-utils": "2.37.0"
178
178
  },
179
179
  "peerDependencies": {
180
180
  "react": ">=17",
@@ -195,11 +195,11 @@
195
195
  "ts-jest": "^29.1.0",
196
196
  "typescript": "^5",
197
197
  "webpack": "^5.88.1",
198
- "@modern-js/app-tools": "2.36.0",
199
- "@modern-js/core": "2.36.0",
200
- "@modern-js/server-core": "2.36.0",
201
- "@scripts/build": "2.36.0",
202
- "@scripts/jest-config": "2.36.0"
198
+ "@modern-js/core": "2.37.0",
199
+ "@modern-js/app-tools": "2.37.0",
200
+ "@modern-js/server-core": "2.37.0",
201
+ "@scripts/build": "2.37.0",
202
+ "@scripts/jest-config": "2.37.0"
203
203
  },
204
204
  "sideEffects": false,
205
205
  "publishConfig": {