react-router 8.5.0 → 8.5.2
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 +70 -1
- package/dist/development/dom-export.js +1 -1
- package/dist/development/index-react-server-client.js +1 -1
- package/dist/development/index-react-server.d.ts +75 -415
- package/dist/development/index-react-server.js +106 -20
- package/dist/development/index.js +1 -1
- package/dist/development/lib/actions.js +1 -1
- package/dist/development/lib/await.js +58 -0
- package/dist/development/lib/components.d.ts +65 -25
- package/dist/development/lib/components.js +69 -15
- package/dist/development/lib/context.d.ts +2 -2
- package/dist/development/lib/context.js +1 -1
- package/dist/development/lib/dom/dom.js +13 -4
- package/dist/development/lib/dom/lib.d.ts +2 -2
- package/dist/development/lib/dom/lib.js +11 -4
- package/dist/development/lib/dom/server.js +1 -1
- package/dist/development/lib/dom/ssr/components.js +1 -1
- package/dist/development/lib/dom/ssr/data.js +1 -1
- package/dist/development/lib/dom/ssr/errorBoundaries.js +1 -1
- package/dist/development/lib/dom/ssr/fallback.js +1 -1
- package/dist/development/lib/dom/ssr/fog-of-war.js +12 -3
- package/dist/development/lib/dom/ssr/hydration.d.ts +1 -0
- package/dist/development/lib/dom/ssr/hydration.js +1 -1
- package/dist/development/lib/dom/ssr/invariant.js +1 -1
- package/dist/development/lib/dom/ssr/links.js +1 -1
- package/dist/development/lib/dom/ssr/markup.js +1 -1
- package/dist/development/lib/dom/ssr/route-discovery.d.ts +2 -2
- package/dist/development/lib/dom/ssr/route-discovery.js +3 -3
- package/dist/development/lib/dom/ssr/routeModules.js +1 -1
- package/dist/development/lib/dom/ssr/routes-test-stub.js +1 -1
- package/dist/development/lib/dom/ssr/routes.js +1 -1
- package/dist/development/lib/dom/ssr/server.js +1 -1
- package/dist/development/lib/dom/ssr/single-fetch.d.ts +1 -1
- package/dist/development/lib/dom/ssr/single-fetch.js +7 -7
- package/dist/development/lib/dom-export/dom-router-provider.js +1 -1
- package/dist/development/lib/dom-export/hydrated-router.d.ts +1 -1
- package/dist/development/lib/dom-export/hydrated-router.js +5 -3
- package/dist/development/lib/errors.js +1 -1
- package/dist/development/lib/hooks.d.ts +0 -3
- package/dist/development/lib/hooks.js +13 -9
- package/dist/development/lib/href.js +4 -4
- package/dist/development/lib/router/history.js +1 -1
- package/dist/development/lib/router/instrumentation.js +1 -1
- package/dist/development/lib/router/matcher-route-pattern.js +1 -1
- package/dist/development/lib/router/matcher-route-pattern.preload.js +1 -1
- package/dist/development/lib/router/matcher.js +6 -1
- package/dist/development/lib/router/navigation.js +17 -2
- package/dist/development/lib/router/router.d.ts +2 -0
- package/dist/development/lib/router/router.js +62 -29
- package/dist/development/lib/router/url.js +1 -1
- package/dist/development/lib/router/utils.d.ts +13 -5
- package/dist/development/lib/router/utils.js +17 -8
- package/dist/development/lib/rsc/browser.js +28 -5
- package/dist/development/lib/rsc/errorBoundaries.js +1 -1
- package/dist/development/lib/rsc/html-stream/browser.js +1 -1
- package/dist/development/lib/rsc/html-stream/server.js +1 -1
- package/dist/development/lib/rsc/route-modules.js +1 -1
- package/dist/development/lib/rsc/server.rsc.d.ts +2 -0
- package/dist/development/lib/rsc/server.ssr.js +1 -1
- package/dist/development/lib/server-runtime/cookies.js +1 -1
- package/dist/development/lib/server-runtime/crypto.js +1 -1
- package/dist/development/lib/server-runtime/data.js +1 -1
- package/dist/development/lib/server-runtime/dev.js +1 -1
- package/dist/development/lib/server-runtime/entry.js +1 -1
- package/dist/development/lib/server-runtime/errors.js +1 -1
- package/dist/development/lib/server-runtime/headers.js +1 -1
- package/dist/development/lib/server-runtime/invariant.js +1 -1
- package/dist/development/lib/server-runtime/mode.js +1 -1
- package/dist/development/lib/server-runtime/routeMatching.js +1 -1
- package/dist/development/lib/server-runtime/routes.js +1 -1
- package/dist/development/lib/server-runtime/server.js +21 -10
- package/dist/development/lib/server-runtime/serverHandoff.js +1 -1
- package/dist/development/lib/server-runtime/sessions/cookieStorage.js +1 -1
- package/dist/development/lib/server-runtime/sessions/memoryStorage.js +1 -1
- package/dist/development/lib/server-runtime/sessions.js +2 -2
- package/dist/development/lib/server-runtime/single-fetch.js +1 -1
- package/dist/development/lib/server-runtime/urls.js +1 -1
- package/dist/development/lib/server-runtime/warnings.js +1 -1
- package/dist/development/lib/types/internal.js +1 -1
- package/dist/development/lib/types/route-module-annotations.d.ts +1 -1
- package/dist/development/route-pattern.js +1 -1
- package/dist/development/vendor/turbo-stream-v2/flatten.js +1 -1
- package/dist/development/vendor/turbo-stream-v2/turbo-stream.js +75 -66
- package/dist/development/vendor/turbo-stream-v2/unflatten.js +23 -3
- package/dist/development/vendor/turbo-stream-v2/utils.js +1 -1
- package/dist/production/dom-export.js +1 -1
- package/dist/production/index-react-server-client.js +1 -1
- package/dist/production/index-react-server.d.ts +75 -415
- package/dist/production/index-react-server.js +106 -20
- package/dist/production/index.js +1 -1
- package/dist/production/lib/actions.js +1 -1
- package/dist/production/lib/await.js +58 -0
- package/dist/production/lib/components.d.ts +65 -25
- package/dist/production/lib/components.js +69 -15
- package/dist/production/lib/context.d.ts +2 -2
- package/dist/production/lib/context.js +1 -1
- package/dist/production/lib/dom/dom.js +13 -4
- package/dist/production/lib/dom/lib.d.ts +2 -2
- package/dist/production/lib/dom/lib.js +11 -4
- package/dist/production/lib/dom/server.js +1 -1
- package/dist/production/lib/dom/ssr/components.js +1 -1
- package/dist/production/lib/dom/ssr/data.js +1 -1
- package/dist/production/lib/dom/ssr/errorBoundaries.js +1 -1
- package/dist/production/lib/dom/ssr/fallback.js +1 -1
- package/dist/production/lib/dom/ssr/fog-of-war.js +12 -3
- package/dist/production/lib/dom/ssr/hydration.d.ts +1 -0
- package/dist/production/lib/dom/ssr/hydration.js +1 -1
- package/dist/production/lib/dom/ssr/invariant.js +1 -1
- package/dist/production/lib/dom/ssr/links.js +1 -1
- package/dist/production/lib/dom/ssr/markup.js +1 -1
- package/dist/production/lib/dom/ssr/route-discovery.d.ts +2 -2
- package/dist/production/lib/dom/ssr/route-discovery.js +3 -3
- package/dist/production/lib/dom/ssr/routeModules.js +1 -1
- package/dist/production/lib/dom/ssr/routes-test-stub.js +1 -1
- package/dist/production/lib/dom/ssr/routes.js +1 -1
- package/dist/production/lib/dom/ssr/server.js +1 -1
- package/dist/production/lib/dom/ssr/single-fetch.d.ts +1 -1
- package/dist/production/lib/dom/ssr/single-fetch.js +7 -7
- package/dist/production/lib/dom-export/dom-router-provider.js +1 -1
- package/dist/production/lib/dom-export/hydrated-router.d.ts +1 -1
- package/dist/production/lib/dom-export/hydrated-router.js +5 -3
- package/dist/production/lib/errors.js +1 -1
- package/dist/production/lib/hooks.d.ts +0 -3
- package/dist/production/lib/hooks.js +13 -9
- package/dist/production/lib/href.js +4 -4
- package/dist/production/lib/router/history.js +1 -1
- package/dist/production/lib/router/instrumentation.js +1 -1
- package/dist/production/lib/router/matcher-route-pattern.js +1 -1
- package/dist/production/lib/router/matcher-route-pattern.preload.js +1 -1
- package/dist/production/lib/router/matcher.js +6 -1
- package/dist/production/lib/router/navigation.js +17 -2
- package/dist/production/lib/router/router.d.ts +2 -0
- package/dist/production/lib/router/router.js +62 -29
- package/dist/production/lib/router/url.js +1 -1
- package/dist/production/lib/router/utils.d.ts +13 -5
- package/dist/production/lib/router/utils.js +17 -8
- package/dist/production/lib/rsc/browser.js +28 -5
- package/dist/production/lib/rsc/errorBoundaries.js +1 -1
- package/dist/production/lib/rsc/html-stream/browser.js +1 -1
- package/dist/production/lib/rsc/html-stream/server.js +1 -1
- package/dist/production/lib/rsc/route-modules.js +1 -1
- package/dist/production/lib/rsc/server.rsc.d.ts +2 -0
- package/dist/production/lib/rsc/server.ssr.js +1 -1
- package/dist/production/lib/server-runtime/cookies.js +1 -1
- package/dist/production/lib/server-runtime/crypto.js +1 -1
- package/dist/production/lib/server-runtime/data.js +1 -1
- package/dist/production/lib/server-runtime/dev.js +1 -1
- package/dist/production/lib/server-runtime/entry.js +1 -1
- package/dist/production/lib/server-runtime/errors.js +1 -1
- package/dist/production/lib/server-runtime/headers.js +1 -1
- package/dist/production/lib/server-runtime/invariant.js +1 -1
- package/dist/production/lib/server-runtime/mode.js +1 -1
- package/dist/production/lib/server-runtime/routeMatching.js +1 -1
- package/dist/production/lib/server-runtime/routes.js +1 -1
- package/dist/production/lib/server-runtime/server.js +21 -10
- package/dist/production/lib/server-runtime/serverHandoff.js +1 -1
- package/dist/production/lib/server-runtime/sessions/cookieStorage.js +1 -1
- package/dist/production/lib/server-runtime/sessions/memoryStorage.js +1 -1
- package/dist/production/lib/server-runtime/sessions.js +2 -2
- package/dist/production/lib/server-runtime/single-fetch.js +1 -1
- package/dist/production/lib/server-runtime/urls.js +1 -1
- package/dist/production/lib/server-runtime/warnings.js +1 -1
- package/dist/production/lib/types/internal.js +1 -1
- package/dist/production/lib/types/route-module-annotations.d.ts +1 -1
- package/dist/production/route-pattern.js +1 -1
- package/dist/production/vendor/turbo-stream-v2/flatten.js +1 -1
- package/dist/production/vendor/turbo-stream-v2/turbo-stream.js +75 -66
- package/dist/production/vendor/turbo-stream-v2/unflatten.js +23 -3
- package/dist/production/vendor/turbo-stream-v2/utils.js +1 -1
- package/docs/explanation/backend-for-frontend.md +0 -3
- package/docs/explanation/code-splitting.md +0 -3
- package/docs/explanation/concurrency.md +0 -3
- package/docs/explanation/hot-module-replacement.md +0 -3
- package/docs/explanation/lazy-route-discovery.md +0 -3
- package/docs/explanation/progressive-enhancement.md +0 -3
- package/docs/explanation/race-conditions.md +0 -3
- package/docs/explanation/react-transitions.md +0 -3
- package/docs/explanation/sessions-and-cookies.md +1 -1
- package/docs/explanation/state-management.md +0 -3
- package/docs/explanation/styling.md +0 -3
- package/docs/explanation/type-safety.md +0 -3
- package/docs/how-to/accessibility.md +0 -6
- package/docs/how-to/client-data.md +0 -3
- package/docs/how-to/data-strategy.md +0 -3
- package/docs/how-to/error-boundary.md +0 -3
- package/docs/how-to/error-reporting.md +0 -3
- package/docs/how-to/fetchers.md +0 -3
- package/docs/how-to/file-route-conventions.md +0 -3
- package/docs/how-to/file-uploads.md +0 -3
- package/docs/how-to/form-validation.md +0 -3
- package/docs/how-to/headers.md +0 -3
- package/docs/how-to/instrumentation.md +0 -3
- package/docs/how-to/middleware.md +6 -3
- package/docs/how-to/navigation-blocking.md +0 -3
- package/docs/how-to/optimize-revalidation.md +0 -3
- package/docs/how-to/pre-rendering.md +0 -3
- package/docs/how-to/presets.md +0 -3
- package/docs/how-to/react-server-components.md +0 -3
- package/docs/how-to/resource-routes.md +0 -3
- package/docs/how-to/route-discovery.md +4 -2
- package/docs/how-to/route-module-type-safety.md +0 -3
- package/docs/how-to/security.md +0 -3
- package/docs/how-to/server-bundles.md +0 -3
- package/docs/how-to/spa.md +0 -3
- package/docs/how-to/status.md +0 -3
- package/docs/how-to/suspense.md +22 -6
- package/docs/how-to/using-handle.md +0 -3
- package/docs/how-to/view-transitions.md +0 -3
- package/docs/start/framework/data-loading.md +9 -0
- package/docs/upgrading/future.md +0 -12
- package/docs/upgrading/v7.md +14 -33
- package/package.json +1 -1
- package/dist/development/lib/dom/global.d.ts +0 -35
- package/dist/production/lib/dom/global.d.ts +0 -35
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* react-router v8.5.
|
|
2
|
+
* react-router v8.5.2
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -83,6 +83,46 @@ function parsePath(path) {
|
|
|
83
83
|
return parsedPath;
|
|
84
84
|
}
|
|
85
85
|
//#endregion
|
|
86
|
+
//#region lib/router/navigation.ts
|
|
87
|
+
const DEFAULT_NAVIGATION_URL = new URL("http://localhost");
|
|
88
|
+
function isSameOrigin(a, b) {
|
|
89
|
+
return a.origin === b.origin && (a.origin !== "null" || a.protocol === b.protocol && a.host === b.host);
|
|
90
|
+
}
|
|
91
|
+
function isExplicitUrl(destination, target) {
|
|
92
|
+
if (destination.startsWith("//")) return true;
|
|
93
|
+
let protocol = target.protocol.toLowerCase();
|
|
94
|
+
if (!destination.toLowerCase().startsWith(protocol)) return false;
|
|
95
|
+
return target.host === "" || destination.slice(protocol.length).startsWith("//");
|
|
96
|
+
}
|
|
97
|
+
function validateDocumentNavigationTarget(destination) {
|
|
98
|
+
let normalized = destination.replace(/[\t\n\r]/g, "");
|
|
99
|
+
let start = 0;
|
|
100
|
+
while (normalized.charCodeAt(start) <= 32) start++;
|
|
101
|
+
normalized = normalized.slice(start);
|
|
102
|
+
if (!ABSOLUTE_URL_REGEX.test(normalized)) return;
|
|
103
|
+
let target;
|
|
104
|
+
try {
|
|
105
|
+
target = new URL(destination, DEFAULT_NAVIGATION_URL);
|
|
106
|
+
} catch {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!isExplicitUrl(destination, target)) throw new Error("External navigation is not allowed");
|
|
110
|
+
}
|
|
111
|
+
function validateNavigationTarget(original, resolved, currentUrl, externalPolicy) {
|
|
112
|
+
let originalUrl = null;
|
|
113
|
+
try {
|
|
114
|
+
originalUrl = original == null ? null : new URL(original, currentUrl);
|
|
115
|
+
} catch {}
|
|
116
|
+
let resolvedUrl = new URL(resolved, currentUrl);
|
|
117
|
+
let originalIsExternal = originalUrl != null && !isSameOrigin(originalUrl, currentUrl);
|
|
118
|
+
let resolvedIsExternal = !isSameOrigin(resolvedUrl, currentUrl);
|
|
119
|
+
if (externalPolicy === "reject") {
|
|
120
|
+
if (originalIsExternal || resolvedIsExternal) throw new Error("External navigation is not allowed");
|
|
121
|
+
} else if (resolvedIsExternal) {
|
|
122
|
+
if (originalUrl == null || !isExplicitUrl(original, originalUrl) || !isSameOrigin(originalUrl, resolvedUrl)) throw new Error("External navigation is not allowed");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
86
126
|
//#region lib/router/utils.ts
|
|
87
127
|
/**
|
|
88
128
|
* Creates a type-safe {@link RouterContext} object that can be used to
|
|
@@ -721,11 +761,15 @@ function data(data, init) {
|
|
|
721
761
|
* header.
|
|
722
762
|
*/
|
|
723
763
|
const redirect$1 = (url, init = 302) => {
|
|
764
|
+
validateDocumentNavigationTarget(url);
|
|
724
765
|
let responseInit = init;
|
|
725
766
|
if (typeof responseInit === "number") responseInit = { status: responseInit };
|
|
726
|
-
else if (typeof responseInit.status === "undefined") responseInit.status = 302;
|
|
727
767
|
let headers = new Headers(responseInit.headers);
|
|
728
768
|
headers.set("Location", url);
|
|
769
|
+
if (typeof responseInit.status === "undefined") responseInit = {
|
|
770
|
+
...responseInit,
|
|
771
|
+
status: 302
|
|
772
|
+
};
|
|
729
773
|
return new Response(null, {
|
|
730
774
|
...responseInit,
|
|
731
775
|
headers
|
|
@@ -1038,12 +1082,17 @@ var V6RegExMatcher = class {
|
|
|
1038
1082
|
#routes = [];
|
|
1039
1083
|
#branches = [];
|
|
1040
1084
|
#basename;
|
|
1085
|
+
#matchCost = 0;
|
|
1041
1086
|
constructor(basename) {
|
|
1042
1087
|
this.#basename = basename;
|
|
1043
1088
|
}
|
|
1044
1089
|
update(routes) {
|
|
1045
1090
|
this.#routes = routes;
|
|
1046
1091
|
this.#branches = flattenAndRankRoutes(routes);
|
|
1092
|
+
this.#matchCost = this.#branches.reduce((total, branch) => total + branch.routesMeta.length, 0);
|
|
1093
|
+
}
|
|
1094
|
+
get matchCost() {
|
|
1095
|
+
return this.#matchCost;
|
|
1047
1096
|
}
|
|
1048
1097
|
match(locationArg, allowPartial = false) {
|
|
1049
1098
|
return matchRoutesImpl(this.#routes, locationArg, this.#basename, allowPartial, this.#branches);
|
|
@@ -1469,6 +1518,9 @@ function createStaticHandler(routes, opts) {
|
|
|
1469
1518
|
}
|
|
1470
1519
|
return {
|
|
1471
1520
|
dataRoutes,
|
|
1521
|
+
get _matchCost() {
|
|
1522
|
+
return dataRouteMatcher instanceof V6RegExMatcher ? dataRouteMatcher.matchCost : void 0;
|
|
1523
|
+
},
|
|
1472
1524
|
match,
|
|
1473
1525
|
query,
|
|
1474
1526
|
queryRoute
|
|
@@ -1580,6 +1632,9 @@ const loadLazyRouteProperty = ({ key, route, manifest, mapRouteProperties }) =>
|
|
|
1580
1632
|
}
|
|
1581
1633
|
})();
|
|
1582
1634
|
cache[key] = propertyPromise;
|
|
1635
|
+
propertyPromise.catch(() => {
|
|
1636
|
+
if (cache[key] === propertyPromise) delete cache[key];
|
|
1637
|
+
});
|
|
1583
1638
|
return propertyPromise;
|
|
1584
1639
|
};
|
|
1585
1640
|
const lazyRouteFunctionCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -1621,7 +1676,9 @@ function loadLazyRoute(route, type, manifest, mapRouteProperties, lazyRoutePrope
|
|
|
1621
1676
|
});
|
|
1622
1677
|
})();
|
|
1623
1678
|
lazyRouteFunctionCache.set(routeToUpdate, lazyRoutePromise);
|
|
1624
|
-
lazyRoutePromise.catch(() => {
|
|
1679
|
+
lazyRoutePromise.catch(() => {
|
|
1680
|
+
if (lazyRouteFunctionCache.get(routeToUpdate) === lazyRoutePromise) lazyRouteFunctionCache.delete(routeToUpdate);
|
|
1681
|
+
});
|
|
1625
1682
|
return {
|
|
1626
1683
|
lazyRoutePromise,
|
|
1627
1684
|
lazyHandlerPromise: lazyRoutePromise
|
|
@@ -1909,10 +1966,13 @@ async function callLoaderOrAction({ request, path, pattern, match, lazyHandlerPr
|
|
|
1909
1966
|
pathname,
|
|
1910
1967
|
routeId: match.route.id
|
|
1911
1968
|
});
|
|
1912
|
-
} else
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1969
|
+
} else {
|
|
1970
|
+
await lazyRoutePromise;
|
|
1971
|
+
return {
|
|
1972
|
+
type: "data",
|
|
1973
|
+
result: void 0
|
|
1974
|
+
};
|
|
1975
|
+
}
|
|
1916
1976
|
}
|
|
1917
1977
|
else if (!handler) {
|
|
1918
1978
|
let url = new URL(request.url);
|
|
@@ -1993,6 +2053,7 @@ async function convertDataStrategyResultToDataResult(dataStrategyResult) {
|
|
|
1993
2053
|
function normalizeRelativeRoutingRedirectResponse(response, request, routeId, matches, basename) {
|
|
1994
2054
|
let location = response.headers.get("Location");
|
|
1995
2055
|
invariant$1(location, "Redirects returned/thrown from loaders/actions must have a Location header");
|
|
2056
|
+
validateNavigationTarget(location, location, new URL(request.url), "allow-explicit");
|
|
1996
2057
|
if (!isAbsoluteUrl(location)) {
|
|
1997
2058
|
let trimmedMatches = matches.slice(0, matches.findIndex((m) => m.route.id === routeId) + 1);
|
|
1998
2059
|
location = normalizeTo(new URL(request.url), trimmedMatches, basename, location);
|
|
@@ -2242,6 +2303,12 @@ function getPathsWithAncestors(paths) {
|
|
|
2242
2303
|
});
|
|
2243
2304
|
return Array.from(result);
|
|
2244
2305
|
}
|
|
2306
|
+
const MANIFEST_MATCH_COST_LIMIT = 5e4;
|
|
2307
|
+
const MANIFEST_MATCH_COST_SEGMENT_THRESHOLD = 10;
|
|
2308
|
+
function shouldUseInitialRouteDiscovery(pathname, matchCost = 0) {
|
|
2309
|
+
let segmentCount = pathname.split("/").length - 1;
|
|
2310
|
+
return Math.max(segmentCount - 1, 0) * pathname.length + 1 > 7680 || segmentCount > MANIFEST_MATCH_COST_SEGMENT_THRESHOLD && segmentCount * matchCost > MANIFEST_MATCH_COST_LIMIT;
|
|
2311
|
+
}
|
|
2245
2312
|
//#endregion
|
|
2246
2313
|
//#region lib/actions.ts
|
|
2247
2314
|
function throwIfPotentialCSRFAttack(request, allowedActionOrigins) {
|
|
@@ -2337,12 +2404,15 @@ const replace = (...args) => {
|
|
|
2337
2404
|
const cachedResolvePromise = React.cache(async (resolve) => {
|
|
2338
2405
|
return Promise.allSettled([resolve]).then((r) => r[0]);
|
|
2339
2406
|
});
|
|
2340
|
-
const Await = (async (
|
|
2341
|
-
|
|
2407
|
+
const Await = (async (props) => {
|
|
2408
|
+
invariant$1("resolve" in props !== "unstable_promise" in props, "<Await> requires exactly one of `resolve` or `unstable_promise`");
|
|
2409
|
+
let { children, errorElement } = props;
|
|
2410
|
+
let usePromise = "unstable_promise" in props;
|
|
2411
|
+
let resolved = await cachedResolvePromise(usePromise ? props.unstable_promise : props.resolve);
|
|
2342
2412
|
if (resolved.status === "rejected" && !errorElement) throw resolved.reason;
|
|
2343
2413
|
if (resolved.status === "rejected") return React.createElement(UNSAFE_AwaitContextProvider, {
|
|
2344
2414
|
children: React.createElement(React.Fragment, null, errorElement),
|
|
2345
|
-
value: {
|
|
2415
|
+
value: usePromise ? resolved : {
|
|
2346
2416
|
_tracked: true,
|
|
2347
2417
|
_error: resolved.reason
|
|
2348
2418
|
}
|
|
@@ -2350,7 +2420,7 @@ const Await = (async ({ children, resolve, errorElement }) => {
|
|
|
2350
2420
|
const toRender = typeof children === "function" ? children(resolved.value) : children;
|
|
2351
2421
|
return React.createElement(UNSAFE_AwaitContextProvider, {
|
|
2352
2422
|
children: toRender,
|
|
2353
|
-
value: {
|
|
2423
|
+
value: usePromise ? resolved : {
|
|
2354
2424
|
_tracked: true,
|
|
2355
2425
|
_data: resolved.value
|
|
2356
2426
|
}
|
|
@@ -2466,9 +2536,20 @@ async function matchRSCServerRequest({ allowedActionOrigins, createTemporaryRefe
|
|
|
2466
2536
|
let matches = staticHandler.match(url.pathname);
|
|
2467
2537
|
if (matches) await Promise.all(matches.map((m) => explodeLazyRoute(m.route)));
|
|
2468
2538
|
const leafMatch = matches?.[matches.length - 1];
|
|
2469
|
-
if (!isDataRequest && leafMatch && !leafMatch.route.Component && !leafMatch.route.ErrorBoundary) return generateResourceResponse(routerRequest, staticHandler, leafMatch.route.id, requestContext, onError);
|
|
2539
|
+
if (!isDataRequest && leafMatch && !leafMatch.route.Component && !leafMatch.route.ErrorBoundary) return normalizeResourceResponse(request, await generateResourceResponse(routerRequest, staticHandler, leafMatch.route.id, requestContext, onError));
|
|
2470
2540
|
let response = await generateRenderResponse(routerRequest, staticHandler, basename, isDataRequest, decodeReply, requestContext, loadServerAction, decodeAction, decodeFormState, onError, generateResponse, temporaryReferences, allowedActionOrigins, routeDiscovery, clientVersion);
|
|
2471
2541
|
response.headers.set("X-Remix-Response", "yes");
|
|
2542
|
+
return normalizeResourceResponse(request, response);
|
|
2543
|
+
}
|
|
2544
|
+
function normalizeResourceResponse(request, response) {
|
|
2545
|
+
if (request.method === "HEAD" && response.headers.get("React-Router-Resource") === "true") {
|
|
2546
|
+
response.body?.cancel().catch(() => {});
|
|
2547
|
+
return new Response(null, {
|
|
2548
|
+
status: response.status,
|
|
2549
|
+
statusText: response.statusText,
|
|
2550
|
+
headers: response.headers
|
|
2551
|
+
});
|
|
2552
|
+
}
|
|
2472
2553
|
return response;
|
|
2473
2554
|
}
|
|
2474
2555
|
async function generateManifestResponse(routes, basename, request, generateResponse, temporaryReferences, routeDiscovery, clientVersion) {
|
|
@@ -2667,7 +2748,7 @@ async function generateRenderResponse(request, staticHandler, basename, isDataRe
|
|
|
2667
2748
|
let result = await processServerAction(request, basename, decodeReply, loadServerAction, decodeAction, decodeFormState, onError, temporaryReferences).finally(() => {
|
|
2668
2749
|
ctx.runningAction = false;
|
|
2669
2750
|
});
|
|
2670
|
-
if (isResponse(result)) return generateRedirectResponse(result, actionResult, basename, isDataRequest, generateResponse, temporaryReferences, ctx.redirect?.headers);
|
|
2751
|
+
if (isResponse(result)) return generateRedirectResponse(result, actionResult, basename, isDataRequest, generateResponse, temporaryReferences, result === ctx.redirect ? void 0 : ctx.redirect?.headers);
|
|
2671
2752
|
skipRevalidation = result?.skipRevalidation ?? false;
|
|
2672
2753
|
actionResult = result?.actionResult;
|
|
2673
2754
|
formState = result?.formState;
|
|
@@ -2701,7 +2782,7 @@ function generateRedirectResponse(response, actionResult, basename, isDataReques
|
|
|
2701
2782
|
status: response.status,
|
|
2702
2783
|
actionResult
|
|
2703
2784
|
};
|
|
2704
|
-
let headers = new Headers(sideEffectRedirectHeaders);
|
|
2785
|
+
let headers = new Headers(sideEffectRedirectHeaders === response.headers ? void 0 : sideEffectRedirectHeaders);
|
|
2705
2786
|
for (const [key, value] of response.headers.entries()) headers.append(key, value);
|
|
2706
2787
|
headers.delete("Location");
|
|
2707
2788
|
headers.delete("X-Remix-Reload-Document");
|
|
@@ -2779,11 +2860,14 @@ async function getRenderPayload(baseRenderPayload, staticHandler, basename, rout
|
|
|
2779
2860
|
parentId
|
|
2780
2861
|
});
|
|
2781
2862
|
}));
|
|
2863
|
+
let pathname = staticContext.location.pathname;
|
|
2864
|
+
let useInitialRouteDiscovery = routeDiscovery?.mode === "initial" && !isDataRequest || shouldUseInitialRouteDiscovery(pathname, staticHandler._matchCost);
|
|
2782
2865
|
let patches;
|
|
2783
|
-
if (
|
|
2784
|
-
else patches = getAdditionalRoutePatches(getPathsWithAncestors([
|
|
2866
|
+
if (useInitialRouteDiscovery) patches = getAllRoutePatches(routes, basename).then((patches) => patches.filter((patch) => !staticContext.matches.some((m) => m.route.id === patch.id)));
|
|
2867
|
+
else patches = getAdditionalRoutePatches(getPathsWithAncestors([pathname]).slice(0, -1), staticHandler, staticContext.matches.map((m) => m.route.id));
|
|
2785
2868
|
return {
|
|
2786
2869
|
...baseRenderPayload,
|
|
2870
|
+
routeDiscovery: useInitialRouteDiscovery ? { mode: "initial" } : baseRenderPayload.routeDiscovery,
|
|
2787
2871
|
matches: await matchesPromise,
|
|
2788
2872
|
patches
|
|
2789
2873
|
};
|
|
@@ -2823,6 +2907,7 @@ async function getRSCRouteMatch({ staticContext, match, isBelowErrorBoundary, ro
|
|
|
2823
2907
|
return {
|
|
2824
2908
|
clientAction: route.clientAction,
|
|
2825
2909
|
clientLoader: route.clientLoader,
|
|
2910
|
+
clientMiddleware: route.clientMiddleware,
|
|
2826
2911
|
element,
|
|
2827
2912
|
errorElement,
|
|
2828
2913
|
handle: route.handle,
|
|
@@ -2850,6 +2935,7 @@ async function getManifestRoute(route) {
|
|
|
2850
2935
|
return {
|
|
2851
2936
|
clientAction: route.clientAction,
|
|
2852
2937
|
clientLoader: route.clientLoader,
|
|
2938
|
+
clientMiddleware: route.clientMiddleware,
|
|
2853
2939
|
handle: route.handle,
|
|
2854
2940
|
hasAction: !!route.action,
|
|
2855
2941
|
hasComponent: !!route.Component,
|
|
@@ -2959,14 +3045,14 @@ function href(path, ...args) {
|
|
|
2959
3045
|
let result = trimTrailingSplat(path).replace(/\/:([\w-]+)(\?)?/g, (_, param, questionMark) => {
|
|
2960
3046
|
const isRequired = questionMark === void 0;
|
|
2961
3047
|
const value = params?.[param];
|
|
2962
|
-
if (isRequired && value
|
|
3048
|
+
if (isRequired && value == null) throw new Error(`Path '${path}' requires param '${param}' but it was not provided`);
|
|
2963
3049
|
return value == null ? "" : "/" + encodePathParam(stringify(value));
|
|
2964
3050
|
});
|
|
2965
3051
|
if (path.endsWith("*")) {
|
|
2966
3052
|
const value = params?.["*"];
|
|
2967
|
-
if (value
|
|
3053
|
+
if (value != null) result += "/" + stringify(value).split("/").map(encodePathParam).join("/");
|
|
2968
3054
|
}
|
|
2969
|
-
return result || "/";
|
|
3055
|
+
return result.replace(/^\/{2,}/, "/") || "/";
|
|
2970
3056
|
}
|
|
2971
3057
|
/**
|
|
2972
3058
|
* Removes a trailing splat and any number of slashes from the end of the path.
|
|
@@ -3242,7 +3328,7 @@ function createSessionStorage({ cookie: cookieArg, createData, readData, updateD
|
|
|
3242
3328
|
return cookie.serialize(id, options);
|
|
3243
3329
|
},
|
|
3244
3330
|
async destroySession(session, options) {
|
|
3245
|
-
await deleteData(session.id);
|
|
3331
|
+
if (session.id) await deleteData(session.id);
|
|
3246
3332
|
return cookie.serialize("", {
|
|
3247
3333
|
...options,
|
|
3248
3334
|
maxAge: void 0,
|
package/dist/production/index.js
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-router v8.5.2
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
//#region lib/await.ts
|
|
12
|
+
function getPromiseResult(promise) {
|
|
13
|
+
if (promise.status === "fulfilled") return {
|
|
14
|
+
status: "fulfilled",
|
|
15
|
+
value: promise.value
|
|
16
|
+
};
|
|
17
|
+
if (promise.status === "rejected") return {
|
|
18
|
+
status: "rejected",
|
|
19
|
+
reason: promise.reason
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
let awaitPromises = /* @__PURE__ */ new WeakMap();
|
|
23
|
+
function getAwaitPromise(source, onError) {
|
|
24
|
+
let cached = awaitPromises.get(source);
|
|
25
|
+
if (!cached) {
|
|
26
|
+
cached = {
|
|
27
|
+
promise: Promise.resolve(source).then((value) => settle({
|
|
28
|
+
status: "fulfilled",
|
|
29
|
+
value
|
|
30
|
+
}), (reason) => {
|
|
31
|
+
let result = settle({
|
|
32
|
+
status: "rejected",
|
|
33
|
+
reason
|
|
34
|
+
});
|
|
35
|
+
try {
|
|
36
|
+
cached.onError(reason);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error("<Await> caught the following error in onError", error);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}),
|
|
42
|
+
onError
|
|
43
|
+
};
|
|
44
|
+
awaitPromises.set(source, cached);
|
|
45
|
+
} else cached.onError = onError;
|
|
46
|
+
function settle(result) {
|
|
47
|
+
Object.assign(cached.promise, {
|
|
48
|
+
status: "fulfilled",
|
|
49
|
+
value: result
|
|
50
|
+
});
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
let result = getPromiseResult(source);
|
|
54
|
+
if (result) settle(result);
|
|
55
|
+
return cached.promise;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { getAwaitPromise, getPromiseResult };
|
|
@@ -447,7 +447,7 @@ interface PathRouteProps {
|
|
|
447
447
|
action?: NonIndexRouteObject["action"];
|
|
448
448
|
/**
|
|
449
449
|
* The route shouldRevalidate function.
|
|
450
|
-
* See [`shouldRevalidate`](../../start/data/route-object#
|
|
450
|
+
* See [`shouldRevalidate`](../../start/data/route-object#shouldrevalidate).
|
|
451
451
|
*/
|
|
452
452
|
shouldRevalidate?: NonIndexRouteObject["shouldRevalidate"];
|
|
453
453
|
/**
|
|
@@ -537,7 +537,7 @@ interface IndexRouteProps {
|
|
|
537
537
|
action?: IndexRouteObject["action"];
|
|
538
538
|
/**
|
|
539
539
|
* The route shouldRevalidate function.
|
|
540
|
-
* See [`shouldRevalidate`](../../start/data/route-object#
|
|
540
|
+
* See [`shouldRevalidate`](../../start/data/route-object#shouldrevalidate).
|
|
541
541
|
*/
|
|
542
542
|
shouldRevalidate?: IndexRouteObject["shouldRevalidate"];
|
|
543
543
|
/**
|
|
@@ -768,6 +768,26 @@ interface AwaitResolveRenderFunction<Resolve = any> {
|
|
|
768
768
|
* @category Types
|
|
769
769
|
*/
|
|
770
770
|
interface AwaitProps<Resolve> {
|
|
771
|
+
/**
|
|
772
|
+
* <docs-warning>This prop is currently unstable and may change in minor or patch
|
|
773
|
+
* releases.</docs-warning>
|
|
774
|
+
*
|
|
775
|
+
* A promise (or value) to read using React's `use()` API in client components.
|
|
776
|
+
* Unlike `resolve` in client components, this accepts promise-like values
|
|
777
|
+
* without modifying the source promise.
|
|
778
|
+
*
|
|
779
|
+
* `errorElement` handles promise rejections. Errors thrown while rendering
|
|
780
|
+
* children bubble to the nearest React error boundary.
|
|
781
|
+
*
|
|
782
|
+
* ```tsx
|
|
783
|
+
* <Await unstable_promise={reviewsPromise} errorElement={<ReviewsError />}>
|
|
784
|
+
* {(reviews) => <Reviews items={reviews} />}
|
|
785
|
+
* </Await>
|
|
786
|
+
* ```
|
|
787
|
+
*
|
|
788
|
+
* Pass exactly one of `unstable_promise` or `resolve`.
|
|
789
|
+
*/
|
|
790
|
+
unstable_promise?: never;
|
|
771
791
|
/**
|
|
772
792
|
* When using a function, the resolved value is provided as the parameter.
|
|
773
793
|
*
|
|
@@ -793,14 +813,17 @@ interface AwaitProps<Resolve> {
|
|
|
793
813
|
*/
|
|
794
814
|
children: React$1.ReactNode | AwaitResolveRenderFunction<Resolve>;
|
|
795
815
|
/**
|
|
796
|
-
* The error element renders instead of the `children` when the
|
|
816
|
+
* The error element renders instead of the `children` when the
|
|
817
|
+
* [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
|
797
818
|
* rejects.
|
|
798
819
|
*
|
|
820
|
+
* When using `resolve`, errors thrown while rendering `children` are
|
|
821
|
+
* also caught and rendered here. With `unstable_promise`, only promise
|
|
822
|
+
* rejections are handled; errors thrown while rendering `children` bubble to
|
|
823
|
+
* the nearest React error boundary.
|
|
824
|
+
*
|
|
799
825
|
* ```tsx
|
|
800
|
-
* <Await
|
|
801
|
-
* errorElement={<div>Oops</div>}
|
|
802
|
-
* resolve={reviewsPromise}
|
|
803
|
-
* >
|
|
826
|
+
* <Await resolve={reviewsPromise} errorElement={<div>Oops</div>}>
|
|
804
827
|
* <Reviews />
|
|
805
828
|
* </Await>
|
|
806
829
|
* ```
|
|
@@ -809,10 +832,7 @@ interface AwaitProps<Resolve> {
|
|
|
809
832
|
* child component
|
|
810
833
|
*
|
|
811
834
|
* ```tsx
|
|
812
|
-
* <Await
|
|
813
|
-
* errorElement={<ReviewsError />}
|
|
814
|
-
* resolve={reviewsPromise}
|
|
815
|
-
* >
|
|
835
|
+
* <Await resolve={reviewsPromise} errorElement={<ReviewsError />}>
|
|
816
836
|
* <Reviews />
|
|
817
837
|
* </Await>
|
|
818
838
|
*
|
|
@@ -869,11 +889,40 @@ interface AwaitProps<Resolve> {
|
|
|
869
889
|
*/
|
|
870
890
|
resolve: Resolve;
|
|
871
891
|
}
|
|
892
|
+
interface AwaitPromiseProps<Resolve> extends Omit<AwaitProps<Resolve>, "resolve" | "unstable_promise"> {
|
|
893
|
+
resolve?: never;
|
|
894
|
+
/**
|
|
895
|
+
* A promise (or value) to read using React's `use()` API in client components.
|
|
896
|
+
* Unlike `resolve` in client components, this accepts promise-like values
|
|
897
|
+
* without modifying the source promise.
|
|
898
|
+
*
|
|
899
|
+
* `errorElement` handles promise rejections. Errors thrown while rendering
|
|
900
|
+
* children bubble to the nearest React error boundary.
|
|
901
|
+
*
|
|
902
|
+
* ```tsx
|
|
903
|
+
* <Await unstable_promise={reviewsPromise} errorElement={<ReviewsError />}>
|
|
904
|
+
* {(reviews) => <Reviews items={reviews} />}
|
|
905
|
+
* </Await>
|
|
906
|
+
* ```
|
|
907
|
+
*
|
|
908
|
+
* Pass exactly one of `unstable_promise` or `resolve`.
|
|
909
|
+
*/
|
|
910
|
+
unstable_promise: Resolve;
|
|
911
|
+
}
|
|
872
912
|
/**
|
|
873
913
|
* Used to render promise values with automatic error handling.
|
|
874
914
|
*
|
|
915
|
+
* Passing the promise through `resolve` uses React Router-specific promise tracking, while passing
|
|
916
|
+
* it through `unstable_promise` uses React's built-in [`use()`](https://react.dev/reference/react/use)
|
|
917
|
+
* API.
|
|
918
|
+
*
|
|
875
919
|
* **Note:** `<Await>` expects to be rendered inside a [`<React.Suspense>`](https://react.dev/reference/react/Suspense)
|
|
876
920
|
*
|
|
921
|
+
* <docs-warning>The `unstable_promise` prop is currently unstable and may change in minor or patch
|
|
922
|
+
* releases. We plan to stabilize `<Await promise>` and deprecate `<Await resolve>` during v8, and then
|
|
923
|
+
* drop `<Await resolve>` in v9. Please see below for minor `errorElement` behavioral differences
|
|
924
|
+
* between the two approaches.</docs-warning>
|
|
925
|
+
*
|
|
877
926
|
* @example
|
|
878
927
|
* import { Await, useLoaderData } from "react-router";
|
|
879
928
|
*
|
|
@@ -892,15 +941,9 @@ interface AwaitProps<Resolve> {
|
|
|
892
941
|
* <h1>{book.title}</h1>
|
|
893
942
|
* <p>{book.description}</p>
|
|
894
943
|
* <React.Suspense fallback={<ReviewsSkeleton />}>
|
|
895
|
-
* <Await
|
|
896
|
-
*
|
|
897
|
-
*
|
|
898
|
-
* <div>Could not load reviews 😬</div>
|
|
899
|
-
* }
|
|
900
|
-
* children={(resolvedReviews) => (
|
|
901
|
-
* <Reviews items={resolvedReviews} />
|
|
902
|
-
* )}
|
|
903
|
-
* />
|
|
944
|
+
* <Await resolve={reviews} errorElement={<BookError />}>
|
|
945
|
+
* {(resolvedReviews) => <Reviews items={resolvedReviews} />}
|
|
946
|
+
* </Await>
|
|
904
947
|
* </React.Suspense>
|
|
905
948
|
* </div>
|
|
906
949
|
* );
|
|
@@ -914,13 +957,10 @@ interface AwaitProps<Resolve> {
|
|
|
914
957
|
* @param {AwaitProps.children} props.children n/a
|
|
915
958
|
* @param {AwaitProps.errorElement} props.errorElement n/a
|
|
916
959
|
* @param {AwaitProps.resolve} props.resolve n/a
|
|
960
|
+
* @param {AwaitProps.unstable_promise} props.unstable_promise n/a
|
|
917
961
|
* @returns React element for the rendered awaited value
|
|
918
962
|
*/
|
|
919
|
-
declare function Await<Resolve>(
|
|
920
|
-
children,
|
|
921
|
-
errorElement,
|
|
922
|
-
resolve
|
|
923
|
-
}: AwaitProps<Resolve>): React$1.JSX.Element;
|
|
963
|
+
declare function Await<Resolve>(props: AwaitProps<Resolve> | AwaitPromiseProps<Resolve>): React$1.JSX.Element;
|
|
924
964
|
/**
|
|
925
965
|
* Creates a route config from a React "children" object, which is usually
|
|
926
966
|
* either a `<Route>` element or an array of them. Used internally by
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* react-router v8.5.
|
|
2
|
+
* react-router v8.5.2
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
11
|
import { createMemoryHistory, createPath, invariant, parsePath, warning } from "./router/history.js";
|
|
12
|
-
import { defaultMapRouteProperties, getResolveToMatches, getRoutePattern, resolveTo, stripBasename } from "./router/utils.js";
|
|
13
12
|
import { getNavigatorCurrentUrl, validateNavigationTarget } from "./router/navigation.js";
|
|
13
|
+
import { defaultMapRouteProperties, getResolveToMatches, getRoutePattern, resolveTo, stripBasename } from "./router/utils.js";
|
|
14
14
|
import { clientErrors, createRouter } from "./router/router.js";
|
|
15
15
|
import { AwaitContext, DataRouterContext, DataRouterDataContext, DataRouterNavigationContext, DataRouterStateContext, FetchersContext, LocationContext, NavigationContext, RouteContext, ViewTransitionContext, useIsRSCRouterContext } from "./context.js";
|
|
16
|
+
import { getAwaitPromise, getPromiseResult } from "./await.js";
|
|
16
17
|
import { _renderMatches, useActionData, useAsyncValue, useInRouterContext, useLoaderData, useLocation, useMatches, useNavigate, useOutlet, useParams, useRouteError, useRoutes, useRoutesImpl } from "./hooks.js";
|
|
17
18
|
import { warnOnce } from "./server-runtime/warnings.js";
|
|
18
19
|
import { FrameworkContext } from "./dom/ssr/components.js";
|
|
@@ -85,6 +86,11 @@ var Deferred = class {
|
|
|
85
86
|
});
|
|
86
87
|
}
|
|
87
88
|
};
|
|
89
|
+
function handleSkippedViewTransition(transition, document) {
|
|
90
|
+
transition.ready.catch((reason) => {
|
|
91
|
+
if (!(document.visibilityState === "hidden" || typeof reason === "object" && reason !== null && "name" in reason && reason.name === "AbortError")) throw reason;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
88
94
|
/**
|
|
89
95
|
* Render the UI for the given {@link DataRouter}. This component should
|
|
90
96
|
* typically be at the top of an app's element tree. The router prop should
|
|
@@ -172,6 +178,7 @@ function RouterProvider({ router, flushSync: reactDomFlushSyncImpl, onError, use
|
|
|
172
178
|
let t = router.window.document.startViewTransition(() => {
|
|
173
179
|
reactDomFlushSyncImpl(() => setStateImpl(newState));
|
|
174
180
|
});
|
|
181
|
+
handleSkippedViewTransition(t, router.window.document);
|
|
175
182
|
t.finished.finally(() => {
|
|
176
183
|
reactDomFlushSyncImpl(() => {
|
|
177
184
|
setRenderDfd(void 0);
|
|
@@ -234,6 +241,7 @@ function RouterProvider({ router, flushSync: reactDomFlushSyncImpl, onError, use
|
|
|
234
241
|
});
|
|
235
242
|
await renderPromise;
|
|
236
243
|
});
|
|
244
|
+
handleSkippedViewTransition(transition, router.window.document);
|
|
237
245
|
transition.finished.finally(() => {
|
|
238
246
|
setRenderDfd(void 0);
|
|
239
247
|
setTransition(void 0);
|
|
@@ -641,8 +649,17 @@ function Routes({ children, location }) {
|
|
|
641
649
|
/**
|
|
642
650
|
* Used to render promise values with automatic error handling.
|
|
643
651
|
*
|
|
652
|
+
* Passing the promise through `resolve` uses React Router-specific promise tracking, while passing
|
|
653
|
+
* it through `unstable_promise` uses React's built-in [`use()`](https://react.dev/reference/react/use)
|
|
654
|
+
* API.
|
|
655
|
+
*
|
|
644
656
|
* **Note:** `<Await>` expects to be rendered inside a [`<React.Suspense>`](https://react.dev/reference/react/Suspense)
|
|
645
657
|
*
|
|
658
|
+
* <docs-warning>The `unstable_promise` prop is currently unstable and may change in minor or patch
|
|
659
|
+
* releases. We plan to stabilize `<Await promise>` and deprecate `<Await resolve>` during v8, and then
|
|
660
|
+
* drop `<Await resolve>` in v9. Please see below for minor `errorElement` behavioral differences
|
|
661
|
+
* between the two approaches.</docs-warning>
|
|
662
|
+
*
|
|
646
663
|
* @example
|
|
647
664
|
* import { Await, useLoaderData } from "react-router";
|
|
648
665
|
*
|
|
@@ -661,15 +678,9 @@ function Routes({ children, location }) {
|
|
|
661
678
|
* <h1>{book.title}</h1>
|
|
662
679
|
* <p>{book.description}</p>
|
|
663
680
|
* <React.Suspense fallback={<ReviewsSkeleton />}>
|
|
664
|
-
* <Await
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
* <div>Could not load reviews 😬</div>
|
|
668
|
-
* }
|
|
669
|
-
* children={(resolvedReviews) => (
|
|
670
|
-
* <Reviews items={resolvedReviews} />
|
|
671
|
-
* )}
|
|
672
|
-
* />
|
|
681
|
+
* <Await resolve={reviews} errorElement={<BookError />}>
|
|
682
|
+
* {(resolvedReviews) => <Reviews items={resolvedReviews} />}
|
|
683
|
+
* </Await>
|
|
673
684
|
* </React.Suspense>
|
|
674
685
|
* </div>
|
|
675
686
|
* );
|
|
@@ -683,9 +694,12 @@ function Routes({ children, location }) {
|
|
|
683
694
|
* @param {AwaitProps.children} props.children n/a
|
|
684
695
|
* @param {AwaitProps.errorElement} props.errorElement n/a
|
|
685
696
|
* @param {AwaitProps.resolve} props.resolve n/a
|
|
697
|
+
* @param {AwaitProps.unstable_promise} props.unstable_promise n/a
|
|
686
698
|
* @returns React element for the rendered awaited value
|
|
687
699
|
*/
|
|
688
|
-
function Await(
|
|
700
|
+
function Await(props) {
|
|
701
|
+
invariant("resolve" in props !== "unstable_promise" in props, "<Await> requires exactly one of `resolve` or `unstable_promise`");
|
|
702
|
+
let { children, errorElement } = props;
|
|
689
703
|
let dataRouterContext = React$1.useContext(DataRouterContext);
|
|
690
704
|
let dataRouterStateContext = React$1.useContext(DataRouterStateContext);
|
|
691
705
|
let onError = React$1.useCallback((error, errorInfo) => {
|
|
@@ -696,20 +710,43 @@ function Await({ children, errorElement, resolve }) {
|
|
|
696
710
|
errorInfo
|
|
697
711
|
});
|
|
698
712
|
}, [dataRouterContext, dataRouterStateContext]);
|
|
713
|
+
if ("unstable_promise" in props) return /* @__PURE__ */ React$1.createElement(AwaitPromise, {
|
|
714
|
+
value: props.unstable_promise,
|
|
715
|
+
errorElement,
|
|
716
|
+
onError
|
|
717
|
+
}, children);
|
|
699
718
|
return /* @__PURE__ */ React$1.createElement(AwaitErrorBoundary, {
|
|
700
|
-
resolve,
|
|
719
|
+
resolve: props.resolve,
|
|
701
720
|
errorElement,
|
|
702
721
|
onError
|
|
703
722
|
}, /* @__PURE__ */ React$1.createElement(ResolveAwait, null, children));
|
|
704
723
|
}
|
|
724
|
+
function AwaitPromise({ value, children, errorElement, onError }) {
|
|
725
|
+
let result = value != null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function" ? React$1.use(getAwaitPromise(value, onError)) : {
|
|
726
|
+
status: "fulfilled",
|
|
727
|
+
value
|
|
728
|
+
};
|
|
729
|
+
if (result.status === "rejected" && !errorElement) throw result.reason;
|
|
730
|
+
return /* @__PURE__ */ React$1.createElement(AwaitContext.Provider, { value: result }, result.status === "rejected" ? errorElement : /* @__PURE__ */ React$1.createElement(ResolveAwait, null, children));
|
|
731
|
+
}
|
|
705
732
|
var AwaitErrorBoundary = class extends React$1.Component {
|
|
706
733
|
constructor(props) {
|
|
707
734
|
super(props);
|
|
708
|
-
this.state = {
|
|
735
|
+
this.state = {
|
|
736
|
+
error: null,
|
|
737
|
+
resolve: props.resolve
|
|
738
|
+
};
|
|
709
739
|
}
|
|
710
740
|
static getDerivedStateFromError(error) {
|
|
711
741
|
return { error };
|
|
712
742
|
}
|
|
743
|
+
static getDerivedStateFromProps(props, state) {
|
|
744
|
+
if (props.resolve !== state.resolve) return {
|
|
745
|
+
error: null,
|
|
746
|
+
resolve: props.resolve
|
|
747
|
+
};
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
713
750
|
componentDidCatch(error, errorInfo) {
|
|
714
751
|
if (this.props.onError) this.props.onError(error, errorInfo);
|
|
715
752
|
else console.error("<Await> caught the following error during render", error, errorInfo);
|
|
@@ -735,7 +772,24 @@ var AwaitErrorBoundary = class extends React$1.Component {
|
|
|
735
772
|
} else {
|
|
736
773
|
status = 0;
|
|
737
774
|
Object.defineProperty(resolve, "_tracked", { get: () => true });
|
|
738
|
-
|
|
775
|
+
let result = getPromiseResult(resolve);
|
|
776
|
+
if (result) {
|
|
777
|
+
promise = resolve;
|
|
778
|
+
if (result.status === "fulfilled") {
|
|
779
|
+
status = 1;
|
|
780
|
+
Object.defineProperty(resolve, "_data", { get: () => result.value });
|
|
781
|
+
} else {
|
|
782
|
+
status = 2;
|
|
783
|
+
Object.defineProperty(resolve, "_error", { get: () => result.reason });
|
|
784
|
+
Promise.resolve(resolve).catch((reason) => {
|
|
785
|
+
try {
|
|
786
|
+
this.props.onError?.(reason);
|
|
787
|
+
} catch (error) {
|
|
788
|
+
console.error("<Await> caught the following error in onError", error);
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
} else promise = Promise.resolve(resolve).then((data) => Object.defineProperty(resolve, "_data", { get: () => data }), (error) => {
|
|
739
793
|
this.props.onError?.(error);
|
|
740
794
|
Object.defineProperty(resolve, "_error", { get: () => error });
|
|
741
795
|
});
|