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
|
|
@@ -730,11 +770,15 @@ function data(data, init) {
|
|
|
730
770
|
* header.
|
|
731
771
|
*/
|
|
732
772
|
const redirect$1 = (url, init = 302) => {
|
|
773
|
+
validateDocumentNavigationTarget(url);
|
|
733
774
|
let responseInit = init;
|
|
734
775
|
if (typeof responseInit === "number") responseInit = { status: responseInit };
|
|
735
|
-
else if (typeof responseInit.status === "undefined") responseInit.status = 302;
|
|
736
776
|
let headers = new Headers(responseInit.headers);
|
|
737
777
|
headers.set("Location", url);
|
|
778
|
+
if (typeof responseInit.status === "undefined") responseInit = {
|
|
779
|
+
...responseInit,
|
|
780
|
+
status: 302
|
|
781
|
+
};
|
|
738
782
|
return new Response(null, {
|
|
739
783
|
...responseInit,
|
|
740
784
|
headers
|
|
@@ -1047,12 +1091,17 @@ var V6RegExMatcher = class {
|
|
|
1047
1091
|
#routes = [];
|
|
1048
1092
|
#branches = [];
|
|
1049
1093
|
#basename;
|
|
1094
|
+
#matchCost = 0;
|
|
1050
1095
|
constructor(basename) {
|
|
1051
1096
|
this.#basename = basename;
|
|
1052
1097
|
}
|
|
1053
1098
|
update(routes) {
|
|
1054
1099
|
this.#routes = routes;
|
|
1055
1100
|
this.#branches = flattenAndRankRoutes(routes);
|
|
1101
|
+
this.#matchCost = this.#branches.reduce((total, branch) => total + branch.routesMeta.length, 0);
|
|
1102
|
+
}
|
|
1103
|
+
get matchCost() {
|
|
1104
|
+
return this.#matchCost;
|
|
1056
1105
|
}
|
|
1057
1106
|
match(locationArg, allowPartial = false) {
|
|
1058
1107
|
return matchRoutesImpl(this.#routes, locationArg, this.#basename, allowPartial, this.#branches);
|
|
@@ -1478,6 +1527,9 @@ function createStaticHandler(routes, opts) {
|
|
|
1478
1527
|
}
|
|
1479
1528
|
return {
|
|
1480
1529
|
dataRoutes,
|
|
1530
|
+
get _matchCost() {
|
|
1531
|
+
return dataRouteMatcher instanceof V6RegExMatcher ? dataRouteMatcher.matchCost : void 0;
|
|
1532
|
+
},
|
|
1481
1533
|
match,
|
|
1482
1534
|
query,
|
|
1483
1535
|
queryRoute
|
|
@@ -1589,6 +1641,9 @@ const loadLazyRouteProperty = ({ key, route, manifest, mapRouteProperties }) =>
|
|
|
1589
1641
|
}
|
|
1590
1642
|
})();
|
|
1591
1643
|
cache[key] = propertyPromise;
|
|
1644
|
+
propertyPromise.catch(() => {
|
|
1645
|
+
if (cache[key] === propertyPromise) delete cache[key];
|
|
1646
|
+
});
|
|
1592
1647
|
return propertyPromise;
|
|
1593
1648
|
};
|
|
1594
1649
|
const lazyRouteFunctionCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -1630,7 +1685,9 @@ function loadLazyRoute(route, type, manifest, mapRouteProperties, lazyRoutePrope
|
|
|
1630
1685
|
});
|
|
1631
1686
|
})();
|
|
1632
1687
|
lazyRouteFunctionCache.set(routeToUpdate, lazyRoutePromise);
|
|
1633
|
-
lazyRoutePromise.catch(() => {
|
|
1688
|
+
lazyRoutePromise.catch(() => {
|
|
1689
|
+
if (lazyRouteFunctionCache.get(routeToUpdate) === lazyRoutePromise) lazyRouteFunctionCache.delete(routeToUpdate);
|
|
1690
|
+
});
|
|
1634
1691
|
return {
|
|
1635
1692
|
lazyRoutePromise,
|
|
1636
1693
|
lazyHandlerPromise: lazyRoutePromise
|
|
@@ -1918,10 +1975,13 @@ async function callLoaderOrAction({ request, path, pattern, match, lazyHandlerPr
|
|
|
1918
1975
|
pathname,
|
|
1919
1976
|
routeId: match.route.id
|
|
1920
1977
|
});
|
|
1921
|
-
} else
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1978
|
+
} else {
|
|
1979
|
+
await lazyRoutePromise;
|
|
1980
|
+
return {
|
|
1981
|
+
type: "data",
|
|
1982
|
+
result: void 0
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1925
1985
|
}
|
|
1926
1986
|
else if (!handler) {
|
|
1927
1987
|
let url = new URL(request.url);
|
|
@@ -2002,6 +2062,7 @@ async function convertDataStrategyResultToDataResult(dataStrategyResult) {
|
|
|
2002
2062
|
function normalizeRelativeRoutingRedirectResponse(response, request, routeId, matches, basename) {
|
|
2003
2063
|
let location = response.headers.get("Location");
|
|
2004
2064
|
invariant$1(location, "Redirects returned/thrown from loaders/actions must have a Location header");
|
|
2065
|
+
validateNavigationTarget(location, location, new URL(request.url), "allow-explicit");
|
|
2005
2066
|
if (!isAbsoluteUrl(location)) {
|
|
2006
2067
|
let trimmedMatches = matches.slice(0, matches.findIndex((m) => m.route.id === routeId) + 1);
|
|
2007
2068
|
location = normalizeTo(new URL(request.url), trimmedMatches, basename, location);
|
|
@@ -2251,6 +2312,12 @@ function getPathsWithAncestors(paths) {
|
|
|
2251
2312
|
});
|
|
2252
2313
|
return Array.from(result);
|
|
2253
2314
|
}
|
|
2315
|
+
const MANIFEST_MATCH_COST_LIMIT = 5e4;
|
|
2316
|
+
const MANIFEST_MATCH_COST_SEGMENT_THRESHOLD = 10;
|
|
2317
|
+
function shouldUseInitialRouteDiscovery(pathname, matchCost = 0) {
|
|
2318
|
+
let segmentCount = pathname.split("/").length - 1;
|
|
2319
|
+
return Math.max(segmentCount - 1, 0) * pathname.length + 1 > 7680 || segmentCount > MANIFEST_MATCH_COST_SEGMENT_THRESHOLD && segmentCount * matchCost > MANIFEST_MATCH_COST_LIMIT;
|
|
2320
|
+
}
|
|
2254
2321
|
//#endregion
|
|
2255
2322
|
//#region lib/actions.ts
|
|
2256
2323
|
function throwIfPotentialCSRFAttack(request, allowedActionOrigins) {
|
|
@@ -2346,12 +2413,15 @@ const replace = (...args) => {
|
|
|
2346
2413
|
const cachedResolvePromise = React.cache(async (resolve) => {
|
|
2347
2414
|
return Promise.allSettled([resolve]).then((r) => r[0]);
|
|
2348
2415
|
});
|
|
2349
|
-
const Await = (async (
|
|
2350
|
-
|
|
2416
|
+
const Await = (async (props) => {
|
|
2417
|
+
invariant$1("resolve" in props !== "unstable_promise" in props, "<Await> requires exactly one of `resolve` or `unstable_promise`");
|
|
2418
|
+
let { children, errorElement } = props;
|
|
2419
|
+
let usePromise = "unstable_promise" in props;
|
|
2420
|
+
let resolved = await cachedResolvePromise(usePromise ? props.unstable_promise : props.resolve);
|
|
2351
2421
|
if (resolved.status === "rejected" && !errorElement) throw resolved.reason;
|
|
2352
2422
|
if (resolved.status === "rejected") return React.createElement(UNSAFE_AwaitContextProvider, {
|
|
2353
2423
|
children: React.createElement(React.Fragment, null, errorElement),
|
|
2354
|
-
value: {
|
|
2424
|
+
value: usePromise ? resolved : {
|
|
2355
2425
|
_tracked: true,
|
|
2356
2426
|
_error: resolved.reason
|
|
2357
2427
|
}
|
|
@@ -2359,7 +2429,7 @@ const Await = (async ({ children, resolve, errorElement }) => {
|
|
|
2359
2429
|
const toRender = typeof children === "function" ? children(resolved.value) : children;
|
|
2360
2430
|
return React.createElement(UNSAFE_AwaitContextProvider, {
|
|
2361
2431
|
children: toRender,
|
|
2362
|
-
value: {
|
|
2432
|
+
value: usePromise ? resolved : {
|
|
2363
2433
|
_tracked: true,
|
|
2364
2434
|
_data: resolved.value
|
|
2365
2435
|
}
|
|
@@ -2475,9 +2545,20 @@ async function matchRSCServerRequest({ allowedActionOrigins, createTemporaryRefe
|
|
|
2475
2545
|
let matches = staticHandler.match(url.pathname);
|
|
2476
2546
|
if (matches) await Promise.all(matches.map((m) => explodeLazyRoute(m.route)));
|
|
2477
2547
|
const leafMatch = matches?.[matches.length - 1];
|
|
2478
|
-
if (!isDataRequest && leafMatch && !leafMatch.route.Component && !leafMatch.route.ErrorBoundary) return generateResourceResponse(routerRequest, staticHandler, leafMatch.route.id, requestContext, onError);
|
|
2548
|
+
if (!isDataRequest && leafMatch && !leafMatch.route.Component && !leafMatch.route.ErrorBoundary) return normalizeResourceResponse(request, await generateResourceResponse(routerRequest, staticHandler, leafMatch.route.id, requestContext, onError));
|
|
2479
2549
|
let response = await generateRenderResponse(routerRequest, staticHandler, basename, isDataRequest, decodeReply, requestContext, loadServerAction, decodeAction, decodeFormState, onError, generateResponse, temporaryReferences, allowedActionOrigins, routeDiscovery, clientVersion);
|
|
2480
2550
|
response.headers.set("X-Remix-Response", "yes");
|
|
2551
|
+
return normalizeResourceResponse(request, response);
|
|
2552
|
+
}
|
|
2553
|
+
function normalizeResourceResponse(request, response) {
|
|
2554
|
+
if (request.method === "HEAD" && response.headers.get("React-Router-Resource") === "true") {
|
|
2555
|
+
response.body?.cancel().catch(() => {});
|
|
2556
|
+
return new Response(null, {
|
|
2557
|
+
status: response.status,
|
|
2558
|
+
statusText: response.statusText,
|
|
2559
|
+
headers: response.headers
|
|
2560
|
+
});
|
|
2561
|
+
}
|
|
2481
2562
|
return response;
|
|
2482
2563
|
}
|
|
2483
2564
|
async function generateManifestResponse(routes, basename, request, generateResponse, temporaryReferences, routeDiscovery, clientVersion) {
|
|
@@ -2676,7 +2757,7 @@ async function generateRenderResponse(request, staticHandler, basename, isDataRe
|
|
|
2676
2757
|
let result = await processServerAction(request, basename, decodeReply, loadServerAction, decodeAction, decodeFormState, onError, temporaryReferences).finally(() => {
|
|
2677
2758
|
ctx.runningAction = false;
|
|
2678
2759
|
});
|
|
2679
|
-
if (isResponse(result)) return generateRedirectResponse(result, actionResult, basename, isDataRequest, generateResponse, temporaryReferences, ctx.redirect?.headers);
|
|
2760
|
+
if (isResponse(result)) return generateRedirectResponse(result, actionResult, basename, isDataRequest, generateResponse, temporaryReferences, result === ctx.redirect ? void 0 : ctx.redirect?.headers);
|
|
2680
2761
|
skipRevalidation = result?.skipRevalidation ?? false;
|
|
2681
2762
|
actionResult = result?.actionResult;
|
|
2682
2763
|
formState = result?.formState;
|
|
@@ -2710,7 +2791,7 @@ function generateRedirectResponse(response, actionResult, basename, isDataReques
|
|
|
2710
2791
|
status: response.status,
|
|
2711
2792
|
actionResult
|
|
2712
2793
|
};
|
|
2713
|
-
let headers = new Headers(sideEffectRedirectHeaders);
|
|
2794
|
+
let headers = new Headers(sideEffectRedirectHeaders === response.headers ? void 0 : sideEffectRedirectHeaders);
|
|
2714
2795
|
for (const [key, value] of response.headers.entries()) headers.append(key, value);
|
|
2715
2796
|
headers.delete("Location");
|
|
2716
2797
|
headers.delete("X-Remix-Reload-Document");
|
|
@@ -2788,11 +2869,14 @@ async function getRenderPayload(baseRenderPayload, staticHandler, basename, rout
|
|
|
2788
2869
|
parentId
|
|
2789
2870
|
});
|
|
2790
2871
|
}));
|
|
2872
|
+
let pathname = staticContext.location.pathname;
|
|
2873
|
+
let useInitialRouteDiscovery = routeDiscovery?.mode === "initial" && !isDataRequest || shouldUseInitialRouteDiscovery(pathname, staticHandler._matchCost);
|
|
2791
2874
|
let patches;
|
|
2792
|
-
if (
|
|
2793
|
-
else patches = getAdditionalRoutePatches(getPathsWithAncestors([
|
|
2875
|
+
if (useInitialRouteDiscovery) patches = getAllRoutePatches(routes, basename).then((patches) => patches.filter((patch) => !staticContext.matches.some((m) => m.route.id === patch.id)));
|
|
2876
|
+
else patches = getAdditionalRoutePatches(getPathsWithAncestors([pathname]).slice(0, -1), staticHandler, staticContext.matches.map((m) => m.route.id));
|
|
2794
2877
|
return {
|
|
2795
2878
|
...baseRenderPayload,
|
|
2879
|
+
routeDiscovery: useInitialRouteDiscovery ? { mode: "initial" } : baseRenderPayload.routeDiscovery,
|
|
2796
2880
|
matches: await matchesPromise,
|
|
2797
2881
|
patches
|
|
2798
2882
|
};
|
|
@@ -2832,6 +2916,7 @@ async function getRSCRouteMatch({ staticContext, match, isBelowErrorBoundary, ro
|
|
|
2832
2916
|
return {
|
|
2833
2917
|
clientAction: route.clientAction,
|
|
2834
2918
|
clientLoader: route.clientLoader,
|
|
2919
|
+
clientMiddleware: route.clientMiddleware,
|
|
2835
2920
|
element,
|
|
2836
2921
|
errorElement,
|
|
2837
2922
|
handle: route.handle,
|
|
@@ -2859,6 +2944,7 @@ async function getManifestRoute(route) {
|
|
|
2859
2944
|
return {
|
|
2860
2945
|
clientAction: route.clientAction,
|
|
2861
2946
|
clientLoader: route.clientLoader,
|
|
2947
|
+
clientMiddleware: route.clientMiddleware,
|
|
2862
2948
|
handle: route.handle,
|
|
2863
2949
|
hasAction: !!route.action,
|
|
2864
2950
|
hasComponent: !!route.Component,
|
|
@@ -2968,14 +3054,14 @@ function href(path, ...args) {
|
|
|
2968
3054
|
let result = trimTrailingSplat(path).replace(/\/:([\w-]+)(\?)?/g, (_, param, questionMark) => {
|
|
2969
3055
|
const isRequired = questionMark === void 0;
|
|
2970
3056
|
const value = params?.[param];
|
|
2971
|
-
if (isRequired && value
|
|
3057
|
+
if (isRequired && value == null) throw new Error(`Path '${path}' requires param '${param}' but it was not provided`);
|
|
2972
3058
|
return value == null ? "" : "/" + encodePathParam(stringify(value));
|
|
2973
3059
|
});
|
|
2974
3060
|
if (path.endsWith("*")) {
|
|
2975
3061
|
const value = params?.["*"];
|
|
2976
|
-
if (value
|
|
3062
|
+
if (value != null) result += "/" + stringify(value).split("/").map(encodePathParam).join("/");
|
|
2977
3063
|
}
|
|
2978
|
-
return result || "/";
|
|
3064
|
+
return result.replace(/^\/{2,}/, "/") || "/";
|
|
2979
3065
|
}
|
|
2980
3066
|
/**
|
|
2981
3067
|
* Removes a trailing splat and any number of slashes from the end of the path.
|
|
@@ -3251,7 +3337,7 @@ function createSessionStorage({ cookie: cookieArg, createData, readData, updateD
|
|
|
3251
3337
|
return cookie.serialize(id, options);
|
|
3252
3338
|
},
|
|
3253
3339
|
async destroySession(session, options) {
|
|
3254
|
-
await deleteData(session.id);
|
|
3340
|
+
if (session.id) await deleteData(session.id);
|
|
3255
3341
|
return cookie.serialize("", {
|
|
3256
3342
|
...options,
|
|
3257
3343
|
maxAge: void 0,
|
|
@@ -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
|
});
|