source: imaps-frontend/node_modules/@remix-run/router/dist/router.cjs.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 200.1 KB
RevLine 
[d565449]1/**
[0c6b92a]2 * @remix-run/router v1.21.0
[d565449]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'use strict';
12
13Object.defineProperty(exports, '__esModule', { value: true });
14
15function _extends() {
16 _extends = Object.assign ? Object.assign.bind() : function (target) {
17 for (var i = 1; i < arguments.length; i++) {
18 var source = arguments[i];
19 for (var key in source) {
20 if (Object.prototype.hasOwnProperty.call(source, key)) {
21 target[key] = source[key];
22 }
23 }
24 }
25 return target;
26 };
27 return _extends.apply(this, arguments);
28}
29
30////////////////////////////////////////////////////////////////////////////////
31//#region Types and Constants
32////////////////////////////////////////////////////////////////////////////////
33
34/**
35 * Actions represent the type of change to a location value.
36 */
37let Action = /*#__PURE__*/function (Action) {
38 Action["Pop"] = "POP";
39 Action["Push"] = "PUSH";
40 Action["Replace"] = "REPLACE";
41 return Action;
42}({});
43
44/**
45 * The pathname, search, and hash values of a URL.
46 */
47
48// TODO: (v7) Change the Location generic default from `any` to `unknown` and
49// remove Remix `useLocation` wrapper.
50/**
51 * An entry in a history stack. A location contains information about the
52 * URL path, as well as possibly some arbitrary state and a key.
53 */
54/**
55 * A change to the current location.
56 */
57/**
58 * A function that receives notifications about location changes.
59 */
60/**
61 * Describes a location that is the destination of some navigation, either via
62 * `history.push` or `history.replace`. This may be either a URL or the pieces
63 * of a URL path.
64 */
65/**
66 * A history is an interface to the navigation stack. The history serves as the
67 * source of truth for the current location, as well as provides a set of
68 * methods that may be used to change it.
69 *
70 * It is similar to the DOM's `window.history` object, but with a smaller, more
71 * focused API.
72 */
73const PopStateEventType = "popstate";
74//#endregion
75
76////////////////////////////////////////////////////////////////////////////////
77//#region Memory History
78////////////////////////////////////////////////////////////////////////////////
79
80/**
81 * A user-supplied object that describes a location. Used when providing
82 * entries to `createMemoryHistory` via its `initialEntries` option.
83 */
84/**
85 * A memory history stores locations in memory. This is useful in stateful
86 * environments where there is no web browser, such as node tests or React
87 * Native.
88 */
89/**
90 * Memory history stores the current location in memory. It is designed for use
91 * in stateful non-browser environments like tests and React Native.
92 */
93function createMemoryHistory(options) {
94 if (options === void 0) {
95 options = {};
96 }
97 let {
98 initialEntries = ["/"],
99 initialIndex,
100 v5Compat = false
101 } = options;
102 let entries; // Declare so we can access from createMemoryLocation
103 entries = initialEntries.map((entry, index) => createMemoryLocation(entry, typeof entry === "string" ? null : entry.state, index === 0 ? "default" : undefined));
104 let index = clampIndex(initialIndex == null ? entries.length - 1 : initialIndex);
105 let action = Action.Pop;
106 let listener = null;
107 function clampIndex(n) {
108 return Math.min(Math.max(n, 0), entries.length - 1);
109 }
110 function getCurrentLocation() {
111 return entries[index];
112 }
113 function createMemoryLocation(to, state, key) {
114 if (state === void 0) {
115 state = null;
116 }
117 let location = createLocation(entries ? getCurrentLocation().pathname : "/", to, state, key);
118 warning(location.pathname.charAt(0) === "/", "relative pathnames are not supported in memory history: " + JSON.stringify(to));
119 return location;
120 }
121 function createHref(to) {
122 return typeof to === "string" ? to : createPath(to);
123 }
124 let history = {
125 get index() {
126 return index;
127 },
128 get action() {
129 return action;
130 },
131 get location() {
132 return getCurrentLocation();
133 },
134 createHref,
135 createURL(to) {
136 return new URL(createHref(to), "http://localhost");
137 },
138 encodeLocation(to) {
139 let path = typeof to === "string" ? parsePath(to) : to;
140 return {
141 pathname: path.pathname || "",
142 search: path.search || "",
143 hash: path.hash || ""
144 };
145 },
146 push(to, state) {
147 action = Action.Push;
148 let nextLocation = createMemoryLocation(to, state);
149 index += 1;
150 entries.splice(index, entries.length, nextLocation);
151 if (v5Compat && listener) {
152 listener({
153 action,
154 location: nextLocation,
155 delta: 1
156 });
157 }
158 },
159 replace(to, state) {
160 action = Action.Replace;
161 let nextLocation = createMemoryLocation(to, state);
162 entries[index] = nextLocation;
163 if (v5Compat && listener) {
164 listener({
165 action,
166 location: nextLocation,
167 delta: 0
168 });
169 }
170 },
171 go(delta) {
172 action = Action.Pop;
173 let nextIndex = clampIndex(index + delta);
174 let nextLocation = entries[nextIndex];
175 index = nextIndex;
176 if (listener) {
177 listener({
178 action,
179 location: nextLocation,
180 delta
181 });
182 }
183 },
184 listen(fn) {
185 listener = fn;
186 return () => {
187 listener = null;
188 };
189 }
190 };
191 return history;
192}
193//#endregion
194
195////////////////////////////////////////////////////////////////////////////////
196//#region Browser History
197////////////////////////////////////////////////////////////////////////////////
198
199/**
200 * A browser history stores the current location in regular URLs in a web
201 * browser environment. This is the standard for most web apps and provides the
202 * cleanest URLs the browser's address bar.
203 *
204 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
205 */
206/**
207 * Browser history stores the location in regular URLs. This is the standard for
208 * most web apps, but it requires some configuration on the server to ensure you
209 * serve the same app at multiple URLs.
210 *
211 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
212 */
213function createBrowserHistory(options) {
214 if (options === void 0) {
215 options = {};
216 }
217 function createBrowserLocation(window, globalHistory) {
218 let {
219 pathname,
220 search,
221 hash
222 } = window.location;
223 return createLocation("", {
224 pathname,
225 search,
226 hash
227 },
228 // state defaults to `null` because `window.history.state` does
229 globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
230 }
231 function createBrowserHref(window, to) {
232 return typeof to === "string" ? to : createPath(to);
233 }
234 return getUrlBasedHistory(createBrowserLocation, createBrowserHref, null, options);
235}
236//#endregion
237
238////////////////////////////////////////////////////////////////////////////////
239//#region Hash History
240////////////////////////////////////////////////////////////////////////////////
241
242/**
243 * A hash history stores the current location in the fragment identifier portion
244 * of the URL in a web browser environment.
245 *
246 * This is ideal for apps that do not control the server for some reason
247 * (because the fragment identifier is never sent to the server), including some
248 * shared hosting environments that do not provide fine-grained controls over
249 * which pages are served at which URLs.
250 *
251 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
252 */
253/**
254 * Hash history stores the location in window.location.hash. This makes it ideal
255 * for situations where you don't want to send the location to the server for
256 * some reason, either because you do cannot configure it or the URL space is
257 * reserved for something else.
258 *
259 * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
260 */
261function createHashHistory(options) {
262 if (options === void 0) {
263 options = {};
264 }
265 function createHashLocation(window, globalHistory) {
266 let {
267 pathname = "/",
268 search = "",
269 hash = ""
270 } = parsePath(window.location.hash.substr(1));
271
272 // Hash URL should always have a leading / just like window.location.pathname
273 // does, so if an app ends up at a route like /#something then we add a
274 // leading slash so all of our path-matching behaves the same as if it would
275 // in a browser router. This is particularly important when there exists a
276 // root splat route (<Route path="*">) since that matches internally against
277 // "/*" and we'd expect /#something to 404 in a hash router app.
278 if (!pathname.startsWith("/") && !pathname.startsWith(".")) {
279 pathname = "/" + pathname;
280 }
281 return createLocation("", {
282 pathname,
283 search,
284 hash
285 },
286 // state defaults to `null` because `window.history.state` does
287 globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
288 }
289 function createHashHref(window, to) {
290 let base = window.document.querySelector("base");
291 let href = "";
292 if (base && base.getAttribute("href")) {
293 let url = window.location.href;
294 let hashIndex = url.indexOf("#");
295 href = hashIndex === -1 ? url : url.slice(0, hashIndex);
296 }
297 return href + "#" + (typeof to === "string" ? to : createPath(to));
298 }
299 function validateHashLocation(location, to) {
300 warning(location.pathname.charAt(0) === "/", "relative pathnames are not supported in hash history.push(" + JSON.stringify(to) + ")");
301 }
302 return getUrlBasedHistory(createHashLocation, createHashHref, validateHashLocation, options);
303}
304//#endregion
305
306////////////////////////////////////////////////////////////////////////////////
307//#region UTILS
308////////////////////////////////////////////////////////////////////////////////
309
310/**
311 * @private
312 */
313function invariant(value, message) {
314 if (value === false || value === null || typeof value === "undefined") {
315 throw new Error(message);
316 }
317}
318function warning(cond, message) {
319 if (!cond) {
320 // eslint-disable-next-line no-console
321 if (typeof console !== "undefined") console.warn(message);
322 try {
323 // Welcome to debugging history!
324 //
325 // This error is thrown as a convenience, so you can more easily
326 // find the source for a warning that appears in the console by
327 // enabling "pause on exceptions" in your JavaScript debugger.
328 throw new Error(message);
329 // eslint-disable-next-line no-empty
330 } catch (e) {}
331 }
332}
333function createKey() {
334 return Math.random().toString(36).substr(2, 8);
335}
336
337/**
338 * For browser-based histories, we combine the state and key into an object
339 */
340function getHistoryState(location, index) {
341 return {
342 usr: location.state,
343 key: location.key,
344 idx: index
345 };
346}
347
348/**
349 * Creates a Location object with a unique key from the given Path
350 */
351function createLocation(current, to, state, key) {
352 if (state === void 0) {
353 state = null;
354 }
355 let location = _extends({
356 pathname: typeof current === "string" ? current : current.pathname,
357 search: "",
358 hash: ""
359 }, typeof to === "string" ? parsePath(to) : to, {
360 state,
361 // TODO: This could be cleaned up. push/replace should probably just take
362 // full Locations now and avoid the need to run through this flow at all
363 // But that's a pretty big refactor to the current test suite so going to
364 // keep as is for the time being and just let any incoming keys take precedence
365 key: to && to.key || key || createKey()
366 });
367 return location;
368}
369
370/**
371 * Creates a string URL path from the given pathname, search, and hash components.
372 */
373function createPath(_ref) {
374 let {
375 pathname = "/",
376 search = "",
377 hash = ""
378 } = _ref;
379 if (search && search !== "?") pathname += search.charAt(0) === "?" ? search : "?" + search;
380 if (hash && hash !== "#") pathname += hash.charAt(0) === "#" ? hash : "#" + hash;
381 return pathname;
382}
383
384/**
385 * Parses a string URL path into its separate pathname, search, and hash components.
386 */
387function parsePath(path) {
388 let parsedPath = {};
389 if (path) {
390 let hashIndex = path.indexOf("#");
391 if (hashIndex >= 0) {
392 parsedPath.hash = path.substr(hashIndex);
393 path = path.substr(0, hashIndex);
394 }
395 let searchIndex = path.indexOf("?");
396 if (searchIndex >= 0) {
397 parsedPath.search = path.substr(searchIndex);
398 path = path.substr(0, searchIndex);
399 }
400 if (path) {
401 parsedPath.pathname = path;
402 }
403 }
404 return parsedPath;
405}
406function getUrlBasedHistory(getLocation, createHref, validateLocation, options) {
407 if (options === void 0) {
408 options = {};
409 }
410 let {
411 window = document.defaultView,
412 v5Compat = false
413 } = options;
414 let globalHistory = window.history;
415 let action = Action.Pop;
416 let listener = null;
417 let index = getIndex();
418 // Index should only be null when we initialize. If not, it's because the
419 // user called history.pushState or history.replaceState directly, in which
420 // case we should log a warning as it will result in bugs.
421 if (index == null) {
422 index = 0;
423 globalHistory.replaceState(_extends({}, globalHistory.state, {
424 idx: index
425 }), "");
426 }
427 function getIndex() {
428 let state = globalHistory.state || {
429 idx: null
430 };
431 return state.idx;
432 }
433 function handlePop() {
434 action = Action.Pop;
435 let nextIndex = getIndex();
436 let delta = nextIndex == null ? null : nextIndex - index;
437 index = nextIndex;
438 if (listener) {
439 listener({
440 action,
441 location: history.location,
442 delta
443 });
444 }
445 }
446 function push(to, state) {
447 action = Action.Push;
448 let location = createLocation(history.location, to, state);
449 if (validateLocation) validateLocation(location, to);
450 index = getIndex() + 1;
451 let historyState = getHistoryState(location, index);
452 let url = history.createHref(location);
453
454 // try...catch because iOS limits us to 100 pushState calls :/
455 try {
456 globalHistory.pushState(historyState, "", url);
457 } catch (error) {
458 // If the exception is because `state` can't be serialized, let that throw
459 // outwards just like a replace call would so the dev knows the cause
460 // https://html.spec.whatwg.org/multipage/nav-history-apis.html#shared-history-push/replace-state-steps
461 // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal
462 if (error instanceof DOMException && error.name === "DataCloneError") {
463 throw error;
464 }
465 // They are going to lose state here, but there is no real
466 // way to warn them about it since the page will refresh...
467 window.location.assign(url);
468 }
469 if (v5Compat && listener) {
470 listener({
471 action,
472 location: history.location,
473 delta: 1
474 });
475 }
476 }
477 function replace(to, state) {
478 action = Action.Replace;
479 let location = createLocation(history.location, to, state);
480 if (validateLocation) validateLocation(location, to);
481 index = getIndex();
482 let historyState = getHistoryState(location, index);
483 let url = history.createHref(location);
484 globalHistory.replaceState(historyState, "", url);
485 if (v5Compat && listener) {
486 listener({
487 action,
488 location: history.location,
489 delta: 0
490 });
491 }
492 }
493 function createURL(to) {
494 // window.location.origin is "null" (the literal string value) in Firefox
495 // under certain conditions, notably when serving from a local HTML file
496 // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
497 let base = window.location.origin !== "null" ? window.location.origin : window.location.href;
498 let href = typeof to === "string" ? to : createPath(to);
499 // Treating this as a full URL will strip any trailing spaces so we need to
500 // pre-encode them since they might be part of a matching splat param from
501 // an ancestor route
502 href = href.replace(/ $/, "%20");
503 invariant(base, "No window.location.(origin|href) available to create URL for href: " + href);
504 return new URL(href, base);
505 }
506 let history = {
507 get action() {
508 return action;
509 },
510 get location() {
511 return getLocation(window, globalHistory);
512 },
513 listen(fn) {
514 if (listener) {
515 throw new Error("A history only accepts one active listener");
516 }
517 window.addEventListener(PopStateEventType, handlePop);
518 listener = fn;
519 return () => {
520 window.removeEventListener(PopStateEventType, handlePop);
521 listener = null;
522 };
523 },
524 createHref(to) {
525 return createHref(window, to);
526 },
527 createURL,
528 encodeLocation(to) {
529 // Encode a Location the same way window.location would
530 let url = createURL(to);
531 return {
532 pathname: url.pathname,
533 search: url.search,
534 hash: url.hash
535 };
536 },
537 push,
538 replace,
539 go(n) {
540 return globalHistory.go(n);
541 }
542 };
543 return history;
544}
545
546//#endregion
547
548/**
549 * Map of routeId -> data returned from a loader/action/error
550 */
551
552let ResultType = /*#__PURE__*/function (ResultType) {
553 ResultType["data"] = "data";
554 ResultType["deferred"] = "deferred";
555 ResultType["redirect"] = "redirect";
556 ResultType["error"] = "error";
557 return ResultType;
558}({});
559
560/**
561 * Successful result from a loader or action
562 */
563
564/**
565 * Successful defer() result from a loader or action
566 */
567
568/**
569 * Redirect result from a loader or action
570 */
571
572/**
573 * Unsuccessful result from a loader or action
574 */
575
576/**
577 * Result from a loader or action - potentially successful or unsuccessful
578 */
579
580/**
581 * Users can specify either lowercase or uppercase form methods on `<Form>`,
582 * useSubmit(), `<fetcher.Form>`, etc.
583 */
584
585/**
586 * Active navigation/fetcher form methods are exposed in lowercase on the
587 * RouterState
588 */
589
590/**
591 * In v7, active navigation/fetcher form methods are exposed in uppercase on the
592 * RouterState. This is to align with the normalization done via fetch().
593 */
594
595// Thanks https://github.com/sindresorhus/type-fest!
596
597/**
598 * @private
599 * Internal interface to pass around for action submissions, not intended for
600 * external consumption
601 */
602
603/**
604 * @private
605 * Arguments passed to route loader/action functions. Same for now but we keep
606 * this as a private implementation detail in case they diverge in the future.
607 */
608
609// TODO: (v7) Change the defaults from any to unknown in and remove Remix wrappers:
610// ActionFunction, ActionFunctionArgs, LoaderFunction, LoaderFunctionArgs
611// Also, make them a type alias instead of an interface
612/**
613 * Arguments passed to loader functions
614 */
615/**
616 * Arguments passed to action functions
617 */
618/**
619 * Loaders and actions can return anything except `undefined` (`null` is a
620 * valid return value if there is no data to return). Responses are preferred
621 * and will ease any future migration to Remix
622 */
623/**
624 * Route loader function signature
625 */
626/**
627 * Route action function signature
628 */
629/**
630 * Arguments passed to shouldRevalidate function
631 */
632/**
633 * Route shouldRevalidate function signature. This runs after any submission
634 * (navigation or fetcher), so we flatten the navigation/fetcher submission
635 * onto the arguments. It shouldn't matter whether it came from a navigation
636 * or a fetcher, what really matters is the URLs and the formData since loaders
637 * have to re-run based on the data models that were potentially mutated.
638 */
639/**
640 * Function provided by the framework-aware layers to set `hasErrorBoundary`
641 * from the framework-aware `errorElement` prop
642 *
643 * @deprecated Use `mapRouteProperties` instead
644 */
[0c6b92a]645/**
646 * Result from a loader or action called via dataStrategy
647 */
[d565449]648/**
649 * Function provided by the framework-aware layers to set any framework-specific
650 * properties from framework-agnostic properties
651 */
652/**
653 * Keys we cannot change from within a lazy() function. We spread all other keys
654 * onto the route. Either they're meaningful to the router, or they'll get
655 * ignored.
656 */
657const immutableRouteKeys = new Set(["lazy", "caseSensitive", "path", "id", "index", "children"]);
658
659/**
660 * lazy() function to load a route definition, which can add non-matching
661 * related properties to a route
662 */
663
664/**
665 * Base RouteObject with common props shared by all types of routes
666 */
667
668/**
669 * Index routes must not have children
670 */
671
672/**
673 * Non-index routes may have children, but cannot have index
674 */
675
676/**
677 * A route object represents a logical route, with (optionally) its child
678 * routes organized in a tree-like structure.
679 */
680
681/**
682 * A data route object, which is just a RouteObject with a required unique ID
683 */
684
685// Recursive helper for finding path parameters in the absence of wildcards
686
687/**
688 * Examples:
689 * "/a/b/*" -> "*"
690 * ":a" -> "a"
691 * "/a/:b" -> "b"
692 * "/a/blahblahblah:b" -> "b"
693 * "/:a/:b" -> "a" | "b"
694 * "/:a/b/:c/*" -> "a" | "c" | "*"
695 */
696
697// Attempt to parse the given string segment. If it fails, then just return the
698// plain string type as a default fallback. Otherwise, return the union of the
699// parsed string literals that were referenced as dynamic segments in the route.
700/**
701 * The parameters that were parsed from the URL path.
702 */
703/**
704 * A RouteMatch contains info about how a route matched a URL.
705 */
706function isIndexRoute(route) {
707 return route.index === true;
708}
709
710// Walk the route tree generating unique IDs where necessary, so we are working
711// solely with AgnosticDataRouteObject's within the Router
712function convertRoutesToDataRoutes(routes, mapRouteProperties, parentPath, manifest) {
713 if (parentPath === void 0) {
714 parentPath = [];
715 }
716 if (manifest === void 0) {
717 manifest = {};
718 }
719 return routes.map((route, index) => {
720 let treePath = [...parentPath, String(index)];
721 let id = typeof route.id === "string" ? route.id : treePath.join("-");
722 invariant(route.index !== true || !route.children, "Cannot specify children on an index route");
723 invariant(!manifest[id], "Found a route id collision on id \"" + id + "\". Route " + "id's must be globally unique within Data Router usages");
724 if (isIndexRoute(route)) {
725 let indexRoute = _extends({}, route, mapRouteProperties(route), {
726 id
727 });
728 manifest[id] = indexRoute;
729 return indexRoute;
730 } else {
731 let pathOrLayoutRoute = _extends({}, route, mapRouteProperties(route), {
732 id,
733 children: undefined
734 });
735 manifest[id] = pathOrLayoutRoute;
736 if (route.children) {
737 pathOrLayoutRoute.children = convertRoutesToDataRoutes(route.children, mapRouteProperties, treePath, manifest);
738 }
739 return pathOrLayoutRoute;
740 }
741 });
742}
743
744/**
745 * Matches the given routes to a location and returns the match data.
746 *
[0c6b92a]747 * @see https://reactrouter.com/v6/utils/match-routes
[d565449]748 */
749function matchRoutes(routes, locationArg, basename) {
750 if (basename === void 0) {
751 basename = "/";
752 }
753 return matchRoutesImpl(routes, locationArg, basename, false);
754}
755function matchRoutesImpl(routes, locationArg, basename, allowPartial) {
756 let location = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
757 let pathname = stripBasename(location.pathname || "/", basename);
758 if (pathname == null) {
759 return null;
760 }
761 let branches = flattenRoutes(routes);
762 rankRouteBranches(branches);
763 let matches = null;
764 for (let i = 0; matches == null && i < branches.length; ++i) {
765 // Incoming pathnames are generally encoded from either window.location
766 // or from router.navigate, but we want to match against the unencoded
767 // paths in the route definitions. Memory router locations won't be
768 // encoded here but there also shouldn't be anything to decode so this
769 // should be a safe operation. This avoids needing matchRoutes to be
770 // history-aware.
771 let decoded = decodePath(pathname);
772 matches = matchRouteBranch(branches[i], decoded, allowPartial);
773 }
774 return matches;
775}
776function convertRouteMatchToUiMatch(match, loaderData) {
777 let {
778 route,
779 pathname,
780 params
781 } = match;
782 return {
783 id: route.id,
784 pathname,
785 params,
786 data: loaderData[route.id],
787 handle: route.handle
788 };
789}
790function flattenRoutes(routes, branches, parentsMeta, parentPath) {
791 if (branches === void 0) {
792 branches = [];
793 }
794 if (parentsMeta === void 0) {
795 parentsMeta = [];
796 }
797 if (parentPath === void 0) {
798 parentPath = "";
799 }
800 let flattenRoute = (route, index, relativePath) => {
801 let meta = {
802 relativePath: relativePath === undefined ? route.path || "" : relativePath,
803 caseSensitive: route.caseSensitive === true,
804 childrenIndex: index,
805 route
806 };
807 if (meta.relativePath.startsWith("/")) {
808 invariant(meta.relativePath.startsWith(parentPath), "Absolute route path \"" + meta.relativePath + "\" nested under path " + ("\"" + parentPath + "\" is not valid. An absolute child route path ") + "must start with the combined path of all its parent routes.");
809 meta.relativePath = meta.relativePath.slice(parentPath.length);
810 }
811 let path = joinPaths([parentPath, meta.relativePath]);
812 let routesMeta = parentsMeta.concat(meta);
813
814 // Add the children before adding this route to the array, so we traverse the
815 // route tree depth-first and child routes appear before their parents in
816 // the "flattened" version.
817 if (route.children && route.children.length > 0) {
818 invariant(
819 // Our types know better, but runtime JS may not!
820 // @ts-expect-error
821 route.index !== true, "Index routes must not have child routes. Please remove " + ("all child routes from route path \"" + path + "\"."));
822 flattenRoutes(route.children, branches, routesMeta, path);
823 }
824
825 // Routes without a path shouldn't ever match by themselves unless they are
826 // index routes, so don't add them to the list of possible branches.
827 if (route.path == null && !route.index) {
828 return;
829 }
830 branches.push({
831 path,
832 score: computeScore(path, route.index),
833 routesMeta
834 });
835 };
836 routes.forEach((route, index) => {
837 var _route$path;
838 // coarse-grain check for optional params
839 if (route.path === "" || !((_route$path = route.path) != null && _route$path.includes("?"))) {
840 flattenRoute(route, index);
841 } else {
842 for (let exploded of explodeOptionalSegments(route.path)) {
843 flattenRoute(route, index, exploded);
844 }
845 }
846 });
847 return branches;
848}
849
850/**
851 * Computes all combinations of optional path segments for a given path,
852 * excluding combinations that are ambiguous and of lower priority.
853 *
854 * For example, `/one/:two?/three/:four?/:five?` explodes to:
855 * - `/one/three`
856 * - `/one/:two/three`
857 * - `/one/three/:four`
858 * - `/one/three/:five`
859 * - `/one/:two/three/:four`
860 * - `/one/:two/three/:five`
861 * - `/one/three/:four/:five`
862 * - `/one/:two/three/:four/:five`
863 */
864function explodeOptionalSegments(path) {
865 let segments = path.split("/");
866 if (segments.length === 0) return [];
867 let [first, ...rest] = segments;
868
869 // Optional path segments are denoted by a trailing `?`
870 let isOptional = first.endsWith("?");
871 // Compute the corresponding required segment: `foo?` -> `foo`
872 let required = first.replace(/\?$/, "");
873 if (rest.length === 0) {
874 // Intepret empty string as omitting an optional segment
875 // `["one", "", "three"]` corresponds to omitting `:two` from `/one/:two?/three` -> `/one/three`
876 return isOptional ? [required, ""] : [required];
877 }
878 let restExploded = explodeOptionalSegments(rest.join("/"));
879 let result = [];
880
881 // All child paths with the prefix. Do this for all children before the
882 // optional version for all children, so we get consistent ordering where the
883 // parent optional aspect is preferred as required. Otherwise, we can get
884 // child sections interspersed where deeper optional segments are higher than
885 // parent optional segments, where for example, /:two would explode _earlier_
886 // then /:one. By always including the parent as required _for all children_
887 // first, we avoid this issue
888 result.push(...restExploded.map(subpath => subpath === "" ? required : [required, subpath].join("/")));
889
890 // Then, if this is an optional value, add all child versions without
891 if (isOptional) {
892 result.push(...restExploded);
893 }
894
895 // for absolute paths, ensure `/` instead of empty segment
896 return result.map(exploded => path.startsWith("/") && exploded === "" ? "/" : exploded);
897}
898function rankRouteBranches(branches) {
899 branches.sort((a, b) => a.score !== b.score ? b.score - a.score // Higher score first
900 : compareIndexes(a.routesMeta.map(meta => meta.childrenIndex), b.routesMeta.map(meta => meta.childrenIndex)));
901}
902const paramRe = /^:[\w-]+$/;
903const dynamicSegmentValue = 3;
904const indexRouteValue = 2;
905const emptySegmentValue = 1;
906const staticSegmentValue = 10;
907const splatPenalty = -2;
908const isSplat = s => s === "*";
909function computeScore(path, index) {
910 let segments = path.split("/");
911 let initialScore = segments.length;
912 if (segments.some(isSplat)) {
913 initialScore += splatPenalty;
914 }
915 if (index) {
916 initialScore += indexRouteValue;
917 }
918 return segments.filter(s => !isSplat(s)).reduce((score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), initialScore);
919}
920function compareIndexes(a, b) {
921 let siblings = a.length === b.length && a.slice(0, -1).every((n, i) => n === b[i]);
922 return siblings ?
923 // If two routes are siblings, we should try to match the earlier sibling
924 // first. This allows people to have fine-grained control over the matching
925 // behavior by simply putting routes with identical paths in the order they
926 // want them tried.
927 a[a.length - 1] - b[b.length - 1] :
928 // Otherwise, it doesn't really make sense to rank non-siblings by index,
929 // so they sort equally.
930 0;
931}
932function matchRouteBranch(branch, pathname, allowPartial) {
933 if (allowPartial === void 0) {
934 allowPartial = false;
935 }
936 let {
937 routesMeta
938 } = branch;
939 let matchedParams = {};
940 let matchedPathname = "/";
941 let matches = [];
942 for (let i = 0; i < routesMeta.length; ++i) {
943 let meta = routesMeta[i];
944 let end = i === routesMeta.length - 1;
945 let remainingPathname = matchedPathname === "/" ? pathname : pathname.slice(matchedPathname.length) || "/";
946 let match = matchPath({
947 path: meta.relativePath,
948 caseSensitive: meta.caseSensitive,
949 end
950 }, remainingPathname);
951 let route = meta.route;
952 if (!match && end && allowPartial && !routesMeta[routesMeta.length - 1].route.index) {
953 match = matchPath({
954 path: meta.relativePath,
955 caseSensitive: meta.caseSensitive,
956 end: false
957 }, remainingPathname);
958 }
959 if (!match) {
960 return null;
961 }
962 Object.assign(matchedParams, match.params);
963 matches.push({
964 // TODO: Can this as be avoided?
965 params: matchedParams,
966 pathname: joinPaths([matchedPathname, match.pathname]),
967 pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])),
968 route
969 });
970 if (match.pathnameBase !== "/") {
971 matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
972 }
973 }
974 return matches;
975}
976
977/**
978 * Returns a path with params interpolated.
979 *
[0c6b92a]980 * @see https://reactrouter.com/v6/utils/generate-path
[d565449]981 */
982function generatePath(originalPath, params) {
983 if (params === void 0) {
984 params = {};
985 }
986 let path = originalPath;
987 if (path.endsWith("*") && path !== "*" && !path.endsWith("/*")) {
988 warning(false, "Route path \"" + path + "\" will be treated as if it were " + ("\"" + path.replace(/\*$/, "/*") + "\" because the `*` character must ") + "always follow a `/` in the pattern. To get rid of this warning, " + ("please change the route path to \"" + path.replace(/\*$/, "/*") + "\"."));
989 path = path.replace(/\*$/, "/*");
990 }
991
992 // ensure `/` is added at the beginning if the path is absolute
993 const prefix = path.startsWith("/") ? "/" : "";
994 const stringify = p => p == null ? "" : typeof p === "string" ? p : String(p);
995 const segments = path.split(/\/+/).map((segment, index, array) => {
996 const isLastSegment = index === array.length - 1;
997
998 // only apply the splat if it's the last segment
999 if (isLastSegment && segment === "*") {
1000 const star = "*";
1001 // Apply the splat
1002 return stringify(params[star]);
1003 }
1004 const keyMatch = segment.match(/^:([\w-]+)(\??)$/);
1005 if (keyMatch) {
1006 const [, key, optional] = keyMatch;
1007 let param = params[key];
1008 invariant(optional === "?" || param != null, "Missing \":" + key + "\" param");
1009 return stringify(param);
1010 }
1011
1012 // Remove any optional markers from optional static segments
1013 return segment.replace(/\?$/g, "");
1014 })
1015 // Remove empty segments
1016 .filter(segment => !!segment);
1017 return prefix + segments.join("/");
1018}
1019
1020/**
1021 * A PathPattern is used to match on some portion of a URL pathname.
1022 */
1023
1024/**
1025 * A PathMatch contains info about how a PathPattern matched on a URL pathname.
1026 */
1027
1028/**
1029 * Performs pattern matching on a URL pathname and returns information about
1030 * the match.
1031 *
[0c6b92a]1032 * @see https://reactrouter.com/v6/utils/match-path
[d565449]1033 */
1034function matchPath(pattern, pathname) {
1035 if (typeof pattern === "string") {
1036 pattern = {
1037 path: pattern,
1038 caseSensitive: false,
1039 end: true
1040 };
1041 }
1042 let [matcher, compiledParams] = compilePath(pattern.path, pattern.caseSensitive, pattern.end);
1043 let match = pathname.match(matcher);
1044 if (!match) return null;
1045 let matchedPathname = match[0];
1046 let pathnameBase = matchedPathname.replace(/(.)\/+$/, "$1");
1047 let captureGroups = match.slice(1);
1048 let params = compiledParams.reduce((memo, _ref, index) => {
1049 let {
1050 paramName,
1051 isOptional
1052 } = _ref;
1053 // We need to compute the pathnameBase here using the raw splat value
1054 // instead of using params["*"] later because it will be decoded then
1055 if (paramName === "*") {
1056 let splatValue = captureGroups[index] || "";
1057 pathnameBase = matchedPathname.slice(0, matchedPathname.length - splatValue.length).replace(/(.)\/+$/, "$1");
1058 }
1059 const value = captureGroups[index];
1060 if (isOptional && !value) {
1061 memo[paramName] = undefined;
1062 } else {
1063 memo[paramName] = (value || "").replace(/%2F/g, "/");
1064 }
1065 return memo;
1066 }, {});
1067 return {
1068 params,
1069 pathname: matchedPathname,
1070 pathnameBase,
1071 pattern
1072 };
1073}
1074function compilePath(path, caseSensitive, end) {
1075 if (caseSensitive === void 0) {
1076 caseSensitive = false;
1077 }
1078 if (end === void 0) {
1079 end = true;
1080 }
1081 warning(path === "*" || !path.endsWith("*") || path.endsWith("/*"), "Route path \"" + path + "\" will be treated as if it were " + ("\"" + path.replace(/\*$/, "/*") + "\" because the `*` character must ") + "always follow a `/` in the pattern. To get rid of this warning, " + ("please change the route path to \"" + path.replace(/\*$/, "/*") + "\"."));
1082 let params = [];
1083 let regexpSource = "^" + path.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
1084 .replace(/^\/*/, "/") // Make sure it has a leading /
1085 .replace(/[\\.*+^${}|()[\]]/g, "\\$&") // Escape special regex chars
1086 .replace(/\/:([\w-]+)(\?)?/g, (_, paramName, isOptional) => {
1087 params.push({
1088 paramName,
1089 isOptional: isOptional != null
1090 });
1091 return isOptional ? "/?([^\\/]+)?" : "/([^\\/]+)";
1092 });
1093 if (path.endsWith("*")) {
1094 params.push({
1095 paramName: "*"
1096 });
1097 regexpSource += path === "*" || path === "/*" ? "(.*)$" // Already matched the initial /, just match the rest
1098 : "(?:\\/(.+)|\\/*)$"; // Don't include the / in params["*"]
1099 } else if (end) {
1100 // When matching to the end, ignore trailing slashes
1101 regexpSource += "\\/*$";
1102 } else if (path !== "" && path !== "/") {
1103 // If our path is non-empty and contains anything beyond an initial slash,
1104 // then we have _some_ form of path in our regex, so we should expect to
1105 // match only if we find the end of this path segment. Look for an optional
1106 // non-captured trailing slash (to match a portion of the URL) or the end
1107 // of the path (if we've matched to the end). We used to do this with a
1108 // word boundary but that gives false positives on routes like
1109 // /user-preferences since `-` counts as a word boundary.
1110 regexpSource += "(?:(?=\\/|$))";
1111 } else ;
1112 let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
1113 return [matcher, params];
1114}
1115function decodePath(value) {
1116 try {
1117 return value.split("/").map(v => decodeURIComponent(v).replace(/\//g, "%2F")).join("/");
1118 } catch (error) {
1119 warning(false, "The URL path \"" + value + "\" could not be decoded because it is is a " + "malformed URL segment. This is probably due to a bad percent " + ("encoding (" + error + ")."));
1120 return value;
1121 }
1122}
1123
1124/**
1125 * @private
1126 */
1127function stripBasename(pathname, basename) {
1128 if (basename === "/") return pathname;
1129 if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
1130 return null;
1131 }
1132
1133 // We want to leave trailing slash behavior in the user's control, so if they
1134 // specify a basename with a trailing slash, we should support it
1135 let startIndex = basename.endsWith("/") ? basename.length - 1 : basename.length;
1136 let nextChar = pathname.charAt(startIndex);
1137 if (nextChar && nextChar !== "/") {
1138 // pathname does not start with basename/
1139 return null;
1140 }
1141 return pathname.slice(startIndex) || "/";
1142}
1143
1144/**
1145 * Returns a resolved path object relative to the given pathname.
1146 *
[0c6b92a]1147 * @see https://reactrouter.com/v6/utils/resolve-path
[d565449]1148 */
1149function resolvePath(to, fromPathname) {
1150 if (fromPathname === void 0) {
1151 fromPathname = "/";
1152 }
1153 let {
1154 pathname: toPathname,
1155 search = "",
1156 hash = ""
1157 } = typeof to === "string" ? parsePath(to) : to;
1158 let pathname = toPathname ? toPathname.startsWith("/") ? toPathname : resolvePathname(toPathname, fromPathname) : fromPathname;
1159 return {
1160 pathname,
1161 search: normalizeSearch(search),
1162 hash: normalizeHash(hash)
1163 };
1164}
1165function resolvePathname(relativePath, fromPathname) {
1166 let segments = fromPathname.replace(/\/+$/, "").split("/");
1167 let relativeSegments = relativePath.split("/");
1168 relativeSegments.forEach(segment => {
1169 if (segment === "..") {
1170 // Keep the root "" segment so the pathname starts at /
1171 if (segments.length > 1) segments.pop();
1172 } else if (segment !== ".") {
1173 segments.push(segment);
1174 }
1175 });
1176 return segments.length > 1 ? segments.join("/") : "/";
1177}
1178function getInvalidPathError(char, field, dest, path) {
1179 return "Cannot include a '" + char + "' character in a manually specified " + ("`to." + field + "` field [" + JSON.stringify(path) + "]. Please separate it out to the ") + ("`to." + dest + "` field. Alternatively you may provide the full path as ") + "a string in <Link to=\"...\"> and the router will parse it for you.";
1180}
1181
1182/**
1183 * @private
1184 *
1185 * When processing relative navigation we want to ignore ancestor routes that
1186 * do not contribute to the path, such that index/pathless layout routes don't
1187 * interfere.
1188 *
1189 * For example, when moving a route element into an index route and/or a
1190 * pathless layout route, relative link behavior contained within should stay
1191 * the same. Both of the following examples should link back to the root:
1192 *
1193 * <Route path="/">
1194 * <Route path="accounts" element={<Link to=".."}>
1195 * </Route>
1196 *
1197 * <Route path="/">
1198 * <Route path="accounts">
1199 * <Route element={<AccountsLayout />}> // <-- Does not contribute
1200 * <Route index element={<Link to=".."} /> // <-- Does not contribute
1201 * </Route
1202 * </Route>
1203 * </Route>
1204 */
1205function getPathContributingMatches(matches) {
1206 return matches.filter((match, index) => index === 0 || match.route.path && match.route.path.length > 0);
1207}
1208
1209// Return the array of pathnames for the current route matches - used to
1210// generate the routePathnames input for resolveTo()
1211function getResolveToMatches(matches, v7_relativeSplatPath) {
1212 let pathMatches = getPathContributingMatches(matches);
1213
1214 // When v7_relativeSplatPath is enabled, use the full pathname for the leaf
1215 // match so we include splat values for "." links. See:
1216 // https://github.com/remix-run/react-router/issues/11052#issuecomment-1836589329
1217 if (v7_relativeSplatPath) {
1218 return pathMatches.map((match, idx) => idx === pathMatches.length - 1 ? match.pathname : match.pathnameBase);
1219 }
1220 return pathMatches.map(match => match.pathnameBase);
1221}
1222
1223/**
1224 * @private
1225 */
1226function resolveTo(toArg, routePathnames, locationPathname, isPathRelative) {
1227 if (isPathRelative === void 0) {
1228 isPathRelative = false;
1229 }
1230 let to;
1231 if (typeof toArg === "string") {
1232 to = parsePath(toArg);
1233 } else {
1234 to = _extends({}, toArg);
1235 invariant(!to.pathname || !to.pathname.includes("?"), getInvalidPathError("?", "pathname", "search", to));
1236 invariant(!to.pathname || !to.pathname.includes("#"), getInvalidPathError("#", "pathname", "hash", to));
1237 invariant(!to.search || !to.search.includes("#"), getInvalidPathError("#", "search", "hash", to));
1238 }
1239 let isEmptyPath = toArg === "" || to.pathname === "";
1240 let toPathname = isEmptyPath ? "/" : to.pathname;
1241 let from;
1242
1243 // Routing is relative to the current pathname if explicitly requested.
1244 //
1245 // If a pathname is explicitly provided in `to`, it should be relative to the
1246 // route context. This is explained in `Note on `<Link to>` values` in our
1247 // migration guide from v5 as a means of disambiguation between `to` values
1248 // that begin with `/` and those that do not. However, this is problematic for
1249 // `to` values that do not provide a pathname. `to` can simply be a search or
1250 // hash string, in which case we should assume that the navigation is relative
1251 // to the current location's pathname and *not* the route pathname.
1252 if (toPathname == null) {
1253 from = locationPathname;
1254 } else {
1255 let routePathnameIndex = routePathnames.length - 1;
1256
1257 // With relative="route" (the default), each leading .. segment means
1258 // "go up one route" instead of "go up one URL segment". This is a key
1259 // difference from how <a href> works and a major reason we call this a
1260 // "to" value instead of a "href".
1261 if (!isPathRelative && toPathname.startsWith("..")) {
1262 let toSegments = toPathname.split("/");
1263 while (toSegments[0] === "..") {
1264 toSegments.shift();
1265 routePathnameIndex -= 1;
1266 }
1267 to.pathname = toSegments.join("/");
1268 }
1269 from = routePathnameIndex >= 0 ? routePathnames[routePathnameIndex] : "/";
1270 }
1271 let path = resolvePath(to, from);
1272
1273 // Ensure the pathname has a trailing slash if the original "to" had one
1274 let hasExplicitTrailingSlash = toPathname && toPathname !== "/" && toPathname.endsWith("/");
1275 // Or if this was a link to the current path which has a trailing slash
1276 let hasCurrentTrailingSlash = (isEmptyPath || toPathname === ".") && locationPathname.endsWith("/");
1277 if (!path.pathname.endsWith("/") && (hasExplicitTrailingSlash || hasCurrentTrailingSlash)) {
1278 path.pathname += "/";
1279 }
1280 return path;
1281}
1282
1283/**
1284 * @private
1285 */
1286function getToPathname(to) {
1287 // Empty strings should be treated the same as / paths
1288 return to === "" || to.pathname === "" ? "/" : typeof to === "string" ? parsePath(to).pathname : to.pathname;
1289}
1290
1291/**
1292 * @private
1293 */
1294const joinPaths = paths => paths.join("/").replace(/\/\/+/g, "/");
1295
1296/**
1297 * @private
1298 */
1299const normalizePathname = pathname => pathname.replace(/\/+$/, "").replace(/^\/*/, "/");
1300
1301/**
1302 * @private
1303 */
1304const normalizeSearch = search => !search || search === "?" ? "" : search.startsWith("?") ? search : "?" + search;
1305
1306/**
1307 * @private
1308 */
1309const normalizeHash = hash => !hash || hash === "#" ? "" : hash.startsWith("#") ? hash : "#" + hash;
1310/**
1311 * This is a shortcut for creating `application/json` responses. Converts `data`
1312 * to JSON and sets the `Content-Type` header.
[0c6b92a]1313 *
1314 * @deprecated The `json` method is deprecated in favor of returning raw objects.
1315 * This method will be removed in v7.
[d565449]1316 */
1317const json = function json(data, init) {
1318 if (init === void 0) {
1319 init = {};
1320 }
1321 let responseInit = typeof init === "number" ? {
1322 status: init
1323 } : init;
1324 let headers = new Headers(responseInit.headers);
1325 if (!headers.has("Content-Type")) {
1326 headers.set("Content-Type", "application/json; charset=utf-8");
1327 }
1328 return new Response(JSON.stringify(data), _extends({}, responseInit, {
1329 headers
1330 }));
1331};
1332class DataWithResponseInit {
1333 constructor(data, init) {
1334 this.type = "DataWithResponseInit";
1335 this.data = data;
1336 this.init = init || null;
1337 }
1338}
1339
1340/**
1341 * Create "responses" that contain `status`/`headers` without forcing
1342 * serialization into an actual `Response` - used by Remix single fetch
1343 */
1344function data(data, init) {
1345 return new DataWithResponseInit(data, typeof init === "number" ? {
1346 status: init
1347 } : init);
1348}
1349class AbortedDeferredError extends Error {}
1350class DeferredData {
1351 constructor(data, responseInit) {
1352 this.pendingKeysSet = new Set();
1353 this.subscribers = new Set();
1354 this.deferredKeys = [];
1355 invariant(data && typeof data === "object" && !Array.isArray(data), "defer() only accepts plain objects");
1356
1357 // Set up an AbortController + Promise we can race against to exit early
1358 // cancellation
1359 let reject;
1360 this.abortPromise = new Promise((_, r) => reject = r);
1361 this.controller = new AbortController();
1362 let onAbort = () => reject(new AbortedDeferredError("Deferred data aborted"));
1363 this.unlistenAbortSignal = () => this.controller.signal.removeEventListener("abort", onAbort);
1364 this.controller.signal.addEventListener("abort", onAbort);
1365 this.data = Object.entries(data).reduce((acc, _ref2) => {
1366 let [key, value] = _ref2;
1367 return Object.assign(acc, {
1368 [key]: this.trackPromise(key, value)
1369 });
1370 }, {});
1371 if (this.done) {
1372 // All incoming values were resolved
1373 this.unlistenAbortSignal();
1374 }
1375 this.init = responseInit;
1376 }
1377 trackPromise(key, value) {
1378 if (!(value instanceof Promise)) {
1379 return value;
1380 }
1381 this.deferredKeys.push(key);
1382 this.pendingKeysSet.add(key);
1383
1384 // We store a little wrapper promise that will be extended with
1385 // _data/_error props upon resolve/reject
1386 let promise = Promise.race([value, this.abortPromise]).then(data => this.onSettle(promise, key, undefined, data), error => this.onSettle(promise, key, error));
1387
1388 // Register rejection listeners to avoid uncaught promise rejections on
1389 // errors or aborted deferred values
1390 promise.catch(() => {});
1391 Object.defineProperty(promise, "_tracked", {
1392 get: () => true
1393 });
1394 return promise;
1395 }
1396 onSettle(promise, key, error, data) {
1397 if (this.controller.signal.aborted && error instanceof AbortedDeferredError) {
1398 this.unlistenAbortSignal();
1399 Object.defineProperty(promise, "_error", {
1400 get: () => error
1401 });
1402 return Promise.reject(error);
1403 }
1404 this.pendingKeysSet.delete(key);
1405 if (this.done) {
1406 // Nothing left to abort!
1407 this.unlistenAbortSignal();
1408 }
1409
1410 // If the promise was resolved/rejected with undefined, we'll throw an error as you
1411 // should always resolve with a value or null
1412 if (error === undefined && data === undefined) {
1413 let undefinedError = new Error("Deferred data for key \"" + key + "\" resolved/rejected with `undefined`, " + "you must resolve/reject with a value or `null`.");
1414 Object.defineProperty(promise, "_error", {
1415 get: () => undefinedError
1416 });
1417 this.emit(false, key);
1418 return Promise.reject(undefinedError);
1419 }
1420 if (data === undefined) {
1421 Object.defineProperty(promise, "_error", {
1422 get: () => error
1423 });
1424 this.emit(false, key);
1425 return Promise.reject(error);
1426 }
1427 Object.defineProperty(promise, "_data", {
1428 get: () => data
1429 });
1430 this.emit(false, key);
1431 return data;
1432 }
1433 emit(aborted, settledKey) {
1434 this.subscribers.forEach(subscriber => subscriber(aborted, settledKey));
1435 }
1436 subscribe(fn) {
1437 this.subscribers.add(fn);
1438 return () => this.subscribers.delete(fn);
1439 }
1440 cancel() {
1441 this.controller.abort();
1442 this.pendingKeysSet.forEach((v, k) => this.pendingKeysSet.delete(k));
1443 this.emit(true);
1444 }
1445 async resolveData(signal) {
1446 let aborted = false;
1447 if (!this.done) {
1448 let onAbort = () => this.cancel();
1449 signal.addEventListener("abort", onAbort);
1450 aborted = await new Promise(resolve => {
1451 this.subscribe(aborted => {
1452 signal.removeEventListener("abort", onAbort);
1453 if (aborted || this.done) {
1454 resolve(aborted);
1455 }
1456 });
1457 });
1458 }
1459 return aborted;
1460 }
1461 get done() {
1462 return this.pendingKeysSet.size === 0;
1463 }
1464 get unwrappedData() {
1465 invariant(this.data !== null && this.done, "Can only unwrap data on initialized and settled deferreds");
1466 return Object.entries(this.data).reduce((acc, _ref3) => {
1467 let [key, value] = _ref3;
1468 return Object.assign(acc, {
1469 [key]: unwrapTrackedPromise(value)
1470 });
1471 }, {});
1472 }
1473 get pendingKeys() {
1474 return Array.from(this.pendingKeysSet);
1475 }
1476}
1477function isTrackedPromise(value) {
1478 return value instanceof Promise && value._tracked === true;
1479}
1480function unwrapTrackedPromise(value) {
1481 if (!isTrackedPromise(value)) {
1482 return value;
1483 }
1484 if (value._error) {
1485 throw value._error;
1486 }
1487 return value._data;
1488}
[0c6b92a]1489/**
1490 * @deprecated The `defer` method is deprecated in favor of returning raw
1491 * objects. This method will be removed in v7.
1492 */
[d565449]1493const defer = function defer(data, init) {
1494 if (init === void 0) {
1495 init = {};
1496 }
1497 let responseInit = typeof init === "number" ? {
1498 status: init
1499 } : init;
1500 return new DeferredData(data, responseInit);
1501};
1502/**
1503 * A redirect response. Sets the status code and the `Location` header.
1504 * Defaults to "302 Found".
1505 */
1506const redirect = function redirect(url, init) {
1507 if (init === void 0) {
1508 init = 302;
1509 }
1510 let responseInit = init;
1511 if (typeof responseInit === "number") {
1512 responseInit = {
1513 status: responseInit
1514 };
1515 } else if (typeof responseInit.status === "undefined") {
1516 responseInit.status = 302;
1517 }
1518 let headers = new Headers(responseInit.headers);
1519 headers.set("Location", url);
1520 return new Response(null, _extends({}, responseInit, {
1521 headers
1522 }));
1523};
1524
1525/**
1526 * A redirect response that will force a document reload to the new location.
1527 * Sets the status code and the `Location` header.
1528 * Defaults to "302 Found".
1529 */
1530const redirectDocument = (url, init) => {
1531 let response = redirect(url, init);
1532 response.headers.set("X-Remix-Reload-Document", "true");
1533 return response;
1534};
1535
1536/**
1537 * A redirect response that will perform a `history.replaceState` instead of a
1538 * `history.pushState` for client-side navigation redirects.
1539 * Sets the status code and the `Location` header.
1540 * Defaults to "302 Found".
1541 */
1542const replace = (url, init) => {
1543 let response = redirect(url, init);
1544 response.headers.set("X-Remix-Replace", "true");
1545 return response;
1546};
1547/**
1548 * @private
1549 * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
1550 *
1551 * We don't export the class for public use since it's an implementation
1552 * detail, but we export the interface above so folks can build their own
1553 * abstractions around instances via isRouteErrorResponse()
1554 */
1555class ErrorResponseImpl {
1556 constructor(status, statusText, data, internal) {
1557 if (internal === void 0) {
1558 internal = false;
1559 }
1560 this.status = status;
1561 this.statusText = statusText || "";
1562 this.internal = internal;
1563 if (data instanceof Error) {
1564 this.data = data.toString();
1565 this.error = data;
1566 } else {
1567 this.data = data;
1568 }
1569 }
1570}
1571
1572/**
1573 * Check if the given error is an ErrorResponse generated from a 4xx/5xx
1574 * Response thrown from an action/loader
1575 */
1576function isRouteErrorResponse(error) {
1577 return error != null && typeof error.status === "number" && typeof error.statusText === "string" && typeof error.internal === "boolean" && "data" in error;
1578}
1579
1580////////////////////////////////////////////////////////////////////////////////
1581//#region Types and Constants
1582////////////////////////////////////////////////////////////////////////////////
1583
1584/**
1585 * A Router instance manages all navigation and data loading/mutations
1586 */
1587/**
1588 * State maintained internally by the router. During a navigation, all states
1589 * reflect the the "old" location unless otherwise noted.
1590 */
1591/**
1592 * Data that can be passed into hydrate a Router from SSR
1593 */
1594/**
1595 * Future flags to toggle new feature behavior
1596 */
1597/**
1598 * Initialization options for createRouter
1599 */
1600/**
1601 * State returned from a server-side query() call
1602 */
1603/**
1604 * A StaticHandler instance manages a singular SSR navigation/fetch event
1605 */
1606/**
1607 * Subscriber function signature for changes to router state
1608 */
1609/**
1610 * Function signature for determining the key to be used in scroll restoration
1611 * for a given location
1612 */
1613/**
1614 * Function signature for determining the current scroll position
1615 */
1616// Allowed for any navigation or fetch
1617// Only allowed for navigations
1618// Only allowed for submission navigations
1619/**
1620 * Options for a navigate() call for a normal (non-submission) navigation
1621 */
1622/**
1623 * Options for a navigate() call for a submission navigation
1624 */
1625/**
1626 * Options to pass to navigate() for a navigation
1627 */
1628/**
1629 * Options for a fetch() load
1630 */
1631/**
1632 * Options for a fetch() submission
1633 */
1634/**
1635 * Options to pass to fetch()
1636 */
1637/**
1638 * Potential states for state.navigation
1639 */
1640/**
1641 * Potential states for fetchers
1642 */
1643/**
1644 * Cached info for active fetcher.load() instances so they can participate
1645 * in revalidation
1646 */
1647/**
1648 * Identified fetcher.load() calls that need to be revalidated
1649 */
1650const validMutationMethodsArr = ["post", "put", "patch", "delete"];
1651const validMutationMethods = new Set(validMutationMethodsArr);
1652const validRequestMethodsArr = ["get", ...validMutationMethodsArr];
1653const validRequestMethods = new Set(validRequestMethodsArr);
1654const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
1655const redirectPreserveMethodStatusCodes = new Set([307, 308]);
1656const IDLE_NAVIGATION = {
1657 state: "idle",
1658 location: undefined,
1659 formMethod: undefined,
1660 formAction: undefined,
1661 formEncType: undefined,
1662 formData: undefined,
1663 json: undefined,
1664 text: undefined
1665};
1666const IDLE_FETCHER = {
1667 state: "idle",
1668 data: undefined,
1669 formMethod: undefined,
1670 formAction: undefined,
1671 formEncType: undefined,
1672 formData: undefined,
1673 json: undefined,
1674 text: undefined
1675};
1676const IDLE_BLOCKER = {
1677 state: "unblocked",
1678 proceed: undefined,
1679 reset: undefined,
1680 location: undefined
1681};
1682const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
1683const defaultMapRouteProperties = route => ({
1684 hasErrorBoundary: Boolean(route.hasErrorBoundary)
1685});
1686const TRANSITIONS_STORAGE_KEY = "remix-router-transitions";
1687
1688//#endregion
1689
1690////////////////////////////////////////////////////////////////////////////////
1691//#region createRouter
1692////////////////////////////////////////////////////////////////////////////////
1693
1694/**
1695 * Create a router and listen to history POP navigations
1696 */
1697function createRouter(init) {
1698 const routerWindow = init.window ? init.window : typeof window !== "undefined" ? window : undefined;
1699 const isBrowser = typeof routerWindow !== "undefined" && typeof routerWindow.document !== "undefined" && typeof routerWindow.document.createElement !== "undefined";
1700 const isServer = !isBrowser;
1701 invariant(init.routes.length > 0, "You must provide a non-empty routes array to createRouter");
1702 let mapRouteProperties;
1703 if (init.mapRouteProperties) {
1704 mapRouteProperties = init.mapRouteProperties;
1705 } else if (init.detectErrorBoundary) {
1706 // If they are still using the deprecated version, wrap it with the new API
1707 let detectErrorBoundary = init.detectErrorBoundary;
1708 mapRouteProperties = route => ({
1709 hasErrorBoundary: detectErrorBoundary(route)
1710 });
1711 } else {
1712 mapRouteProperties = defaultMapRouteProperties;
1713 }
1714
1715 // Routes keyed by ID
1716 let manifest = {};
1717 // Routes in tree format for matching
1718 let dataRoutes = convertRoutesToDataRoutes(init.routes, mapRouteProperties, undefined, manifest);
1719 let inFlightDataRoutes;
1720 let basename = init.basename || "/";
[0c6b92a]1721 let dataStrategyImpl = init.dataStrategy || defaultDataStrategy;
1722 let patchRoutesOnNavigationImpl = init.patchRoutesOnNavigation;
[d565449]1723
1724 // Config driven behavior flags
1725 let future = _extends({
1726 v7_fetcherPersist: false,
1727 v7_normalizeFormMethod: false,
1728 v7_partialHydration: false,
1729 v7_prependBasename: false,
1730 v7_relativeSplatPath: false,
1731 v7_skipActionErrorRevalidation: false
1732 }, init.future);
1733 // Cleanup function for history
1734 let unlistenHistory = null;
1735 // Externally-provided functions to call on all state changes
1736 let subscribers = new Set();
1737 // Externally-provided object to hold scroll restoration locations during routing
1738 let savedScrollPositions = null;
1739 // Externally-provided function to get scroll restoration keys
1740 let getScrollRestorationKey = null;
1741 // Externally-provided function to get current scroll position
1742 let getScrollPosition = null;
1743 // One-time flag to control the initial hydration scroll restoration. Because
1744 // we don't get the saved positions from <ScrollRestoration /> until _after_
1745 // the initial render, we need to manually trigger a separate updateState to
1746 // send along the restoreScrollPosition
1747 // Set to true if we have `hydrationData` since we assume we were SSR'd and that
1748 // SSR did the initial scroll restoration.
1749 let initialScrollRestored = init.hydrationData != null;
1750 let initialMatches = matchRoutes(dataRoutes, init.history.location, basename);
1751 let initialErrors = null;
[0c6b92a]1752 if (initialMatches == null && !patchRoutesOnNavigationImpl) {
[d565449]1753 // If we do not match a user-provided-route, fall back to the root
1754 // to allow the error boundary to take over
1755 let error = getInternalRouterError(404, {
1756 pathname: init.history.location.pathname
1757 });
1758 let {
1759 matches,
1760 route
1761 } = getShortCircuitMatches(dataRoutes);
1762 initialMatches = matches;
1763 initialErrors = {
1764 [route.id]: error
1765 };
1766 }
1767
[0c6b92a]1768 // In SPA apps, if the user provided a patchRoutesOnNavigation implementation and
[d565449]1769 // our initial match is a splat route, clear them out so we run through lazy
1770 // discovery on hydration in case there's a more accurate lazy route match.
1771 // In SSR apps (with `hydrationData`), we expect that the server will send
1772 // up the proper matched routes so we don't want to run lazy discovery on
1773 // initial hydration and want to hydrate into the splat route.
1774 if (initialMatches && !init.hydrationData) {
1775 let fogOfWar = checkFogOfWar(initialMatches, dataRoutes, init.history.location.pathname);
1776 if (fogOfWar.active) {
1777 initialMatches = null;
1778 }
1779 }
1780 let initialized;
1781 if (!initialMatches) {
1782 initialized = false;
1783 initialMatches = [];
1784
1785 // If partial hydration and fog of war is enabled, we will be running
[0c6b92a]1786 // `patchRoutesOnNavigation` during hydration so include any partial matches as
[d565449]1787 // the initial matches so we can properly render `HydrateFallback`'s
1788 if (future.v7_partialHydration) {
1789 let fogOfWar = checkFogOfWar(null, dataRoutes, init.history.location.pathname);
1790 if (fogOfWar.active && fogOfWar.matches) {
1791 initialMatches = fogOfWar.matches;
1792 }
1793 }
1794 } else if (initialMatches.some(m => m.route.lazy)) {
1795 // All initialMatches need to be loaded before we're ready. If we have lazy
1796 // functions around still then we'll need to run them in initialize()
1797 initialized = false;
1798 } else if (!initialMatches.some(m => m.route.loader)) {
1799 // If we've got no loaders to run, then we're good to go
1800 initialized = true;
1801 } else if (future.v7_partialHydration) {
1802 // If partial hydration is enabled, we're initialized so long as we were
1803 // provided with hydrationData for every route with a loader, and no loaders
1804 // were marked for explicit hydration
1805 let loaderData = init.hydrationData ? init.hydrationData.loaderData : null;
1806 let errors = init.hydrationData ? init.hydrationData.errors : null;
1807 // If errors exist, don't consider routes below the boundary
1808 if (errors) {
1809 let idx = initialMatches.findIndex(m => errors[m.route.id] !== undefined);
[0c6b92a]1810 initialized = initialMatches.slice(0, idx + 1).every(m => !shouldLoadRouteOnHydration(m.route, loaderData, errors));
[d565449]1811 } else {
[0c6b92a]1812 initialized = initialMatches.every(m => !shouldLoadRouteOnHydration(m.route, loaderData, errors));
[d565449]1813 }
1814 } else {
1815 // Without partial hydration - we're initialized if we were provided any
1816 // hydrationData - which is expected to be complete
1817 initialized = init.hydrationData != null;
1818 }
1819 let router;
1820 let state = {
1821 historyAction: init.history.action,
1822 location: init.history.location,
1823 matches: initialMatches,
1824 initialized,
1825 navigation: IDLE_NAVIGATION,
1826 // Don't restore on initial updateState() if we were SSR'd
1827 restoreScrollPosition: init.hydrationData != null ? false : null,
1828 preventScrollReset: false,
1829 revalidation: "idle",
1830 loaderData: init.hydrationData && init.hydrationData.loaderData || {},
1831 actionData: init.hydrationData && init.hydrationData.actionData || null,
1832 errors: init.hydrationData && init.hydrationData.errors || initialErrors,
1833 fetchers: new Map(),
1834 blockers: new Map()
1835 };
1836
1837 // -- Stateful internal variables to manage navigations --
1838 // Current navigation in progress (to be committed in completeNavigation)
1839 let pendingAction = Action.Pop;
1840
1841 // Should the current navigation prevent the scroll reset if scroll cannot
1842 // be restored?
1843 let pendingPreventScrollReset = false;
1844
1845 // AbortController for the active navigation
1846 let pendingNavigationController;
1847
1848 // Should the current navigation enable document.startViewTransition?
1849 let pendingViewTransitionEnabled = false;
1850
1851 // Store applied view transitions so we can apply them on POP
1852 let appliedViewTransitions = new Map();
1853
1854 // Cleanup function for persisting applied transitions to sessionStorage
1855 let removePageHideEventListener = null;
1856
1857 // We use this to avoid touching history in completeNavigation if a
1858 // revalidation is entirely uninterrupted
1859 let isUninterruptedRevalidation = false;
1860
1861 // Use this internal flag to force revalidation of all loaders:
1862 // - submissions (completed or interrupted)
1863 // - useRevalidator()
1864 // - X-Remix-Revalidate (from redirect)
1865 let isRevalidationRequired = false;
1866
1867 // Use this internal array to capture routes that require revalidation due
1868 // to a cancelled deferred on action submission
1869 let cancelledDeferredRoutes = [];
1870
1871 // Use this internal array to capture fetcher loads that were cancelled by an
1872 // action navigation and require revalidation
1873 let cancelledFetcherLoads = new Set();
1874
1875 // AbortControllers for any in-flight fetchers
1876 let fetchControllers = new Map();
1877
1878 // Track loads based on the order in which they started
1879 let incrementingLoadId = 0;
1880
1881 // Track the outstanding pending navigation data load to be compared against
1882 // the globally incrementing load when a fetcher load lands after a completed
1883 // navigation
1884 let pendingNavigationLoadId = -1;
1885
1886 // Fetchers that triggered data reloads as a result of their actions
1887 let fetchReloadIds = new Map();
1888
1889 // Fetchers that triggered redirect navigations
1890 let fetchRedirectIds = new Set();
1891
1892 // Most recent href/match for fetcher.load calls for fetchers
1893 let fetchLoadMatches = new Map();
1894
1895 // Ref-count mounted fetchers so we know when it's ok to clean them up
1896 let activeFetchers = new Map();
1897
1898 // Fetchers that have requested a delete when using v7_fetcherPersist,
1899 // they'll be officially removed after they return to idle
1900 let deletedFetchers = new Set();
1901
1902 // Store DeferredData instances for active route matches. When a
1903 // route loader returns defer() we stick one in here. Then, when a nested
1904 // promise resolves we update loaderData. If a new navigation starts we
1905 // cancel active deferreds for eliminated routes.
1906 let activeDeferreds = new Map();
1907
1908 // Store blocker functions in a separate Map outside of router state since
1909 // we don't need to update UI state if they change
1910 let blockerFunctions = new Map();
1911
1912 // Flag to ignore the next history update, so we can revert the URL change on
1913 // a POP navigation that was blocked by the user without touching router state
[0c6b92a]1914 let unblockBlockerHistoryUpdate = undefined;
[d565449]1915
1916 // Initialize the router, all side effects should be kicked off from here.
1917 // Implemented as a Fluent API for ease of:
1918 // let router = createRouter(init).initialize();
1919 function initialize() {
1920 // If history informs us of a POP navigation, start the navigation but do not update
1921 // state. We'll update our own state once the navigation completes
1922 unlistenHistory = init.history.listen(_ref => {
1923 let {
1924 action: historyAction,
1925 location,
1926 delta
1927 } = _ref;
1928 // Ignore this event if it was just us resetting the URL from a
1929 // blocked POP navigation
[0c6b92a]1930 if (unblockBlockerHistoryUpdate) {
1931 unblockBlockerHistoryUpdate();
1932 unblockBlockerHistoryUpdate = undefined;
[d565449]1933 return;
1934 }
1935 warning(blockerFunctions.size === 0 || delta != null, "You are trying to use a blocker on a POP navigation to a location " + "that was not created by @remix-run/router. This will fail silently in " + "production. This can happen if you are navigating outside the router " + "via `window.history.pushState`/`window.location.hash` instead of using " + "router navigation APIs. This can also happen if you are using " + "createHashRouter and the user manually changes the URL.");
1936 let blockerKey = shouldBlockNavigation({
1937 currentLocation: state.location,
1938 nextLocation: location,
1939 historyAction
1940 });
1941 if (blockerKey && delta != null) {
1942 // Restore the URL to match the current UI, but don't update router state
[0c6b92a]1943 let nextHistoryUpdatePromise = new Promise(resolve => {
1944 unblockBlockerHistoryUpdate = resolve;
1945 });
[d565449]1946 init.history.go(delta * -1);
1947
1948 // Put the blocker into a blocked state
1949 updateBlocker(blockerKey, {
1950 state: "blocked",
1951 location,
1952 proceed() {
1953 updateBlocker(blockerKey, {
1954 state: "proceeding",
1955 proceed: undefined,
1956 reset: undefined,
1957 location
1958 });
[0c6b92a]1959 // Re-do the same POP navigation we just blocked, after the url
1960 // restoration is also complete. See:
1961 // https://github.com/remix-run/react-router/issues/11613
1962 nextHistoryUpdatePromise.then(() => init.history.go(delta));
[d565449]1963 },
1964 reset() {
1965 let blockers = new Map(state.blockers);
1966 blockers.set(blockerKey, IDLE_BLOCKER);
1967 updateState({
1968 blockers
1969 });
1970 }
1971 });
1972 return;
1973 }
1974 return startNavigation(historyAction, location);
1975 });
1976 if (isBrowser) {
1977 // FIXME: This feels gross. How can we cleanup the lines between
1978 // scrollRestoration/appliedTransitions persistance?
1979 restoreAppliedTransitions(routerWindow, appliedViewTransitions);
1980 let _saveAppliedTransitions = () => persistAppliedTransitions(routerWindow, appliedViewTransitions);
1981 routerWindow.addEventListener("pagehide", _saveAppliedTransitions);
1982 removePageHideEventListener = () => routerWindow.removeEventListener("pagehide", _saveAppliedTransitions);
1983 }
1984
1985 // Kick off initial data load if needed. Use Pop to avoid modifying history
1986 // Note we don't do any handling of lazy here. For SPA's it'll get handled
1987 // in the normal navigation flow. For SSR it's expected that lazy modules are
1988 // resolved prior to router creation since we can't go into a fallbackElement
1989 // UI for SSR'd apps
1990 if (!state.initialized) {
1991 startNavigation(Action.Pop, state.location, {
1992 initialHydration: true
1993 });
1994 }
1995 return router;
1996 }
1997
1998 // Clean up a router and it's side effects
1999 function dispose() {
2000 if (unlistenHistory) {
2001 unlistenHistory();
2002 }
2003 if (removePageHideEventListener) {
2004 removePageHideEventListener();
2005 }
2006 subscribers.clear();
2007 pendingNavigationController && pendingNavigationController.abort();
2008 state.fetchers.forEach((_, key) => deleteFetcher(key));
2009 state.blockers.forEach((_, key) => deleteBlocker(key));
2010 }
2011
2012 // Subscribe to state updates for the router
2013 function subscribe(fn) {
2014 subscribers.add(fn);
2015 return () => subscribers.delete(fn);
2016 }
2017
2018 // Update our state and notify the calling context of the change
2019 function updateState(newState, opts) {
2020 if (opts === void 0) {
2021 opts = {};
2022 }
2023 state = _extends({}, state, newState);
2024
2025 // Prep fetcher cleanup so we can tell the UI which fetcher data entries
2026 // can be removed
2027 let completedFetchers = [];
2028 let deletedFetchersKeys = [];
2029 if (future.v7_fetcherPersist) {
2030 state.fetchers.forEach((fetcher, key) => {
2031 if (fetcher.state === "idle") {
2032 if (deletedFetchers.has(key)) {
2033 // Unmounted from the UI and can be totally removed
2034 deletedFetchersKeys.push(key);
2035 } else {
2036 // Returned to idle but still mounted in the UI, so semi-remains for
2037 // revalidations and such
2038 completedFetchers.push(key);
2039 }
2040 }
2041 });
2042 }
2043
2044 // Iterate over a local copy so that if flushSync is used and we end up
2045 // removing and adding a new subscriber due to the useCallback dependencies,
2046 // we don't get ourselves into a loop calling the new subscriber immediately
2047 [...subscribers].forEach(subscriber => subscriber(state, {
2048 deletedFetchers: deletedFetchersKeys,
[0c6b92a]2049 viewTransitionOpts: opts.viewTransitionOpts,
2050 flushSync: opts.flushSync === true
[d565449]2051 }));
2052
2053 // Remove idle fetchers from state since we only care about in-flight fetchers.
2054 if (future.v7_fetcherPersist) {
2055 completedFetchers.forEach(key => state.fetchers.delete(key));
2056 deletedFetchersKeys.forEach(key => deleteFetcher(key));
2057 }
2058 }
2059
2060 // Complete a navigation returning the state.navigation back to the IDLE_NAVIGATION
2061 // and setting state.[historyAction/location/matches] to the new route.
2062 // - Location is a required param
2063 // - Navigation will always be set to IDLE_NAVIGATION
2064 // - Can pass any other state in newState
2065 function completeNavigation(location, newState, _temp) {
2066 var _location$state, _location$state2;
2067 let {
2068 flushSync
2069 } = _temp === void 0 ? {} : _temp;
2070 // Deduce if we're in a loading/actionReload state:
2071 // - We have committed actionData in the store
2072 // - The current navigation was a mutation submission
2073 // - We're past the submitting state and into the loading state
2074 // - The location being loaded is not the result of a redirect
2075 let isActionReload = state.actionData != null && state.navigation.formMethod != null && isMutationMethod(state.navigation.formMethod) && state.navigation.state === "loading" && ((_location$state = location.state) == null ? void 0 : _location$state._isRedirect) !== true;
2076 let actionData;
2077 if (newState.actionData) {
2078 if (Object.keys(newState.actionData).length > 0) {
2079 actionData = newState.actionData;
2080 } else {
2081 // Empty actionData -> clear prior actionData due to an action error
2082 actionData = null;
2083 }
2084 } else if (isActionReload) {
2085 // Keep the current data if we're wrapping up the action reload
2086 actionData = state.actionData;
2087 } else {
2088 // Clear actionData on any other completed navigations
2089 actionData = null;
2090 }
2091
2092 // Always preserve any existing loaderData from re-used routes
2093 let loaderData = newState.loaderData ? mergeLoaderData(state.loaderData, newState.loaderData, newState.matches || [], newState.errors) : state.loaderData;
2094
2095 // On a successful navigation we can assume we got through all blockers
2096 // so we can start fresh
2097 let blockers = state.blockers;
2098 if (blockers.size > 0) {
2099 blockers = new Map(blockers);
2100 blockers.forEach((_, k) => blockers.set(k, IDLE_BLOCKER));
2101 }
2102
2103 // Always respect the user flag. Otherwise don't reset on mutation
2104 // submission navigations unless they redirect
2105 let preventScrollReset = pendingPreventScrollReset === true || state.navigation.formMethod != null && isMutationMethod(state.navigation.formMethod) && ((_location$state2 = location.state) == null ? void 0 : _location$state2._isRedirect) !== true;
2106
2107 // Commit any in-flight routes at the end of the HMR revalidation "navigation"
2108 if (inFlightDataRoutes) {
2109 dataRoutes = inFlightDataRoutes;
2110 inFlightDataRoutes = undefined;
2111 }
2112 if (isUninterruptedRevalidation) ; else if (pendingAction === Action.Pop) ; else if (pendingAction === Action.Push) {
2113 init.history.push(location, location.state);
2114 } else if (pendingAction === Action.Replace) {
2115 init.history.replace(location, location.state);
2116 }
2117 let viewTransitionOpts;
2118
2119 // On POP, enable transitions if they were enabled on the original navigation
2120 if (pendingAction === Action.Pop) {
2121 // Forward takes precedence so they behave like the original navigation
2122 let priorPaths = appliedViewTransitions.get(state.location.pathname);
2123 if (priorPaths && priorPaths.has(location.pathname)) {
2124 viewTransitionOpts = {
2125 currentLocation: state.location,
2126 nextLocation: location
2127 };
2128 } else if (appliedViewTransitions.has(location.pathname)) {
2129 // If we don't have a previous forward nav, assume we're popping back to
2130 // the new location and enable if that location previously enabled
2131 viewTransitionOpts = {
2132 currentLocation: location,
2133 nextLocation: state.location
2134 };
2135 }
2136 } else if (pendingViewTransitionEnabled) {
2137 // Store the applied transition on PUSH/REPLACE
2138 let toPaths = appliedViewTransitions.get(state.location.pathname);
2139 if (toPaths) {
2140 toPaths.add(location.pathname);
2141 } else {
2142 toPaths = new Set([location.pathname]);
2143 appliedViewTransitions.set(state.location.pathname, toPaths);
2144 }
2145 viewTransitionOpts = {
2146 currentLocation: state.location,
2147 nextLocation: location
2148 };
2149 }
2150 updateState(_extends({}, newState, {
2151 // matches, errors, fetchers go through as-is
2152 actionData,
2153 loaderData,
2154 historyAction: pendingAction,
2155 location,
2156 initialized: true,
2157 navigation: IDLE_NAVIGATION,
2158 revalidation: "idle",
2159 restoreScrollPosition: getSavedScrollPosition(location, newState.matches || state.matches),
2160 preventScrollReset,
2161 blockers
2162 }), {
2163 viewTransitionOpts,
2164 flushSync: flushSync === true
2165 });
2166
2167 // Reset stateful navigation vars
2168 pendingAction = Action.Pop;
2169 pendingPreventScrollReset = false;
2170 pendingViewTransitionEnabled = false;
2171 isUninterruptedRevalidation = false;
2172 isRevalidationRequired = false;
2173 cancelledDeferredRoutes = [];
2174 }
2175
2176 // Trigger a navigation event, which can either be a numerical POP or a PUSH
2177 // replace with an optional submission
2178 async function navigate(to, opts) {
2179 if (typeof to === "number") {
2180 init.history.go(to);
2181 return;
2182 }
2183 let normalizedPath = normalizeTo(state.location, state.matches, basename, future.v7_prependBasename, to, future.v7_relativeSplatPath, opts == null ? void 0 : opts.fromRouteId, opts == null ? void 0 : opts.relative);
2184 let {
2185 path,
2186 submission,
2187 error
2188 } = normalizeNavigateOptions(future.v7_normalizeFormMethod, false, normalizedPath, opts);
2189 let currentLocation = state.location;
2190 let nextLocation = createLocation(state.location, path, opts && opts.state);
2191
2192 // When using navigate as a PUSH/REPLACE we aren't reading an already-encoded
2193 // URL from window.location, so we need to encode it here so the behavior
2194 // remains the same as POP and non-data-router usages. new URL() does all
2195 // the same encoding we'd get from a history.pushState/window.location read
2196 // without having to touch history
2197 nextLocation = _extends({}, nextLocation, init.history.encodeLocation(nextLocation));
2198 let userReplace = opts && opts.replace != null ? opts.replace : undefined;
2199 let historyAction = Action.Push;
2200 if (userReplace === true) {
2201 historyAction = Action.Replace;
2202 } else if (userReplace === false) ; else if (submission != null && isMutationMethod(submission.formMethod) && submission.formAction === state.location.pathname + state.location.search) {
2203 // By default on submissions to the current location we REPLACE so that
2204 // users don't have to double-click the back button to get to the prior
2205 // location. If the user redirects to a different location from the
2206 // action/loader this will be ignored and the redirect will be a PUSH
2207 historyAction = Action.Replace;
2208 }
2209 let preventScrollReset = opts && "preventScrollReset" in opts ? opts.preventScrollReset === true : undefined;
[0c6b92a]2210 let flushSync = (opts && opts.flushSync) === true;
[d565449]2211 let blockerKey = shouldBlockNavigation({
2212 currentLocation,
2213 nextLocation,
2214 historyAction
2215 });
2216 if (blockerKey) {
2217 // Put the blocker into a blocked state
2218 updateBlocker(blockerKey, {
2219 state: "blocked",
2220 location: nextLocation,
2221 proceed() {
2222 updateBlocker(blockerKey, {
2223 state: "proceeding",
2224 proceed: undefined,
2225 reset: undefined,
2226 location: nextLocation
2227 });
2228 // Send the same navigation through
2229 navigate(to, opts);
2230 },
2231 reset() {
2232 let blockers = new Map(state.blockers);
2233 blockers.set(blockerKey, IDLE_BLOCKER);
2234 updateState({
2235 blockers
2236 });
2237 }
2238 });
2239 return;
2240 }
2241 return await startNavigation(historyAction, nextLocation, {
2242 submission,
2243 // Send through the formData serialization error if we have one so we can
2244 // render at the right error boundary after we match routes
2245 pendingError: error,
2246 preventScrollReset,
2247 replace: opts && opts.replace,
[0c6b92a]2248 enableViewTransition: opts && opts.viewTransition,
[d565449]2249 flushSync
2250 });
2251 }
2252
2253 // Revalidate all current loaders. If a navigation is in progress or if this
2254 // is interrupted by a navigation, allow this to "succeed" by calling all
2255 // loaders during the next loader round
2256 function revalidate() {
2257 interruptActiveLoads();
2258 updateState({
2259 revalidation: "loading"
2260 });
2261
2262 // If we're currently submitting an action, we don't need to start a new
2263 // navigation, we'll just let the follow up loader execution call all loaders
2264 if (state.navigation.state === "submitting") {
2265 return;
2266 }
2267
2268 // If we're currently in an idle state, start a new navigation for the current
2269 // action/location and mark it as uninterrupted, which will skip the history
2270 // update in completeNavigation
2271 if (state.navigation.state === "idle") {
2272 startNavigation(state.historyAction, state.location, {
2273 startUninterruptedRevalidation: true
2274 });
2275 return;
2276 }
2277
2278 // Otherwise, if we're currently in a loading state, just start a new
2279 // navigation to the navigation.location but do not trigger an uninterrupted
2280 // revalidation so that history correctly updates once the navigation completes
2281 startNavigation(pendingAction || state.historyAction, state.navigation.location, {
[0c6b92a]2282 overrideNavigation: state.navigation,
2283 // Proxy through any rending view transition
2284 enableViewTransition: pendingViewTransitionEnabled === true
[d565449]2285 });
2286 }
2287
2288 // Start a navigation to the given action/location. Can optionally provide a
2289 // overrideNavigation which will override the normalLoad in the case of a redirect
2290 // navigation
2291 async function startNavigation(historyAction, location, opts) {
2292 // Abort any in-progress navigations and start a new one. Unset any ongoing
2293 // uninterrupted revalidations unless told otherwise, since we want this
2294 // new navigation to update history normally
2295 pendingNavigationController && pendingNavigationController.abort();
2296 pendingNavigationController = null;
2297 pendingAction = historyAction;
2298 isUninterruptedRevalidation = (opts && opts.startUninterruptedRevalidation) === true;
2299
2300 // Save the current scroll position every time we start a new navigation,
2301 // and track whether we should reset scroll on completion
2302 saveScrollPosition(state.location, state.matches);
2303 pendingPreventScrollReset = (opts && opts.preventScrollReset) === true;
2304 pendingViewTransitionEnabled = (opts && opts.enableViewTransition) === true;
2305 let routesToUse = inFlightDataRoutes || dataRoutes;
2306 let loadingNavigation = opts && opts.overrideNavigation;
2307 let matches = matchRoutes(routesToUse, location, basename);
2308 let flushSync = (opts && opts.flushSync) === true;
2309 let fogOfWar = checkFogOfWar(matches, routesToUse, location.pathname);
2310 if (fogOfWar.active && fogOfWar.matches) {
2311 matches = fogOfWar.matches;
2312 }
2313
2314 // Short circuit with a 404 on the root error boundary if we match nothing
2315 if (!matches) {
2316 let {
2317 error,
2318 notFoundMatches,
2319 route
2320 } = handleNavigational404(location.pathname);
2321 completeNavigation(location, {
2322 matches: notFoundMatches,
2323 loaderData: {},
2324 errors: {
2325 [route.id]: error
2326 }
2327 }, {
2328 flushSync
2329 });
2330 return;
2331 }
2332
2333 // Short circuit if it's only a hash change and not a revalidation or
2334 // mutation submission.
2335 //
[0c6b92a]2336 // Ignore on initial page loads because since the initial hydration will always
[d565449]2337 // be "same hash". For example, on /page#hash and submit a <Form method="post">
2338 // which will default to a navigation to /page
2339 if (state.initialized && !isRevalidationRequired && isHashChangeOnly(state.location, location) && !(opts && opts.submission && isMutationMethod(opts.submission.formMethod))) {
2340 completeNavigation(location, {
2341 matches
2342 }, {
2343 flushSync
2344 });
2345 return;
2346 }
2347
2348 // Create a controller/Request for this navigation
2349 pendingNavigationController = new AbortController();
2350 let request = createClientSideRequest(init.history, location, pendingNavigationController.signal, opts && opts.submission);
2351 let pendingActionResult;
2352 if (opts && opts.pendingError) {
2353 // If we have a pendingError, it means the user attempted a GET submission
2354 // with binary FormData so assign here and skip to handleLoaders. That
2355 // way we handle calling loaders above the boundary etc. It's not really
2356 // different from an actionError in that sense.
2357 pendingActionResult = [findNearestBoundary(matches).route.id, {
2358 type: ResultType.error,
2359 error: opts.pendingError
2360 }];
2361 } else if (opts && opts.submission && isMutationMethod(opts.submission.formMethod)) {
2362 // Call action if we received an action submission
2363 let actionResult = await handleAction(request, location, opts.submission, matches, fogOfWar.active, {
2364 replace: opts.replace,
2365 flushSync
2366 });
2367 if (actionResult.shortCircuited) {
2368 return;
2369 }
2370
2371 // If we received a 404 from handleAction, it's because we couldn't lazily
2372 // discover the destination route so we don't want to call loaders
2373 if (actionResult.pendingActionResult) {
2374 let [routeId, result] = actionResult.pendingActionResult;
2375 if (isErrorResult(result) && isRouteErrorResponse(result.error) && result.error.status === 404) {
2376 pendingNavigationController = null;
2377 completeNavigation(location, {
2378 matches: actionResult.matches,
2379 loaderData: {},
2380 errors: {
2381 [routeId]: result.error
2382 }
2383 });
2384 return;
2385 }
2386 }
2387 matches = actionResult.matches || matches;
2388 pendingActionResult = actionResult.pendingActionResult;
2389 loadingNavigation = getLoadingNavigation(location, opts.submission);
2390 flushSync = false;
2391 // No need to do fog of war matching again on loader execution
2392 fogOfWar.active = false;
2393
2394 // Create a GET request for the loaders
2395 request = createClientSideRequest(init.history, request.url, request.signal);
2396 }
2397
2398 // Call loaders
2399 let {
2400 shortCircuited,
2401 matches: updatedMatches,
2402 loaderData,
2403 errors
2404 } = await handleLoaders(request, location, matches, fogOfWar.active, loadingNavigation, opts && opts.submission, opts && opts.fetcherSubmission, opts && opts.replace, opts && opts.initialHydration === true, flushSync, pendingActionResult);
2405 if (shortCircuited) {
2406 return;
2407 }
2408
2409 // Clean up now that the action/loaders have completed. Don't clean up if
2410 // we short circuited because pendingNavigationController will have already
2411 // been assigned to a new controller for the next navigation
2412 pendingNavigationController = null;
2413 completeNavigation(location, _extends({
2414 matches: updatedMatches || matches
2415 }, getActionDataForCommit(pendingActionResult), {
2416 loaderData,
2417 errors
2418 }));
2419 }
2420
2421 // Call the action matched by the leaf route for this navigation and handle
2422 // redirects/errors
2423 async function handleAction(request, location, submission, matches, isFogOfWar, opts) {
2424 if (opts === void 0) {
2425 opts = {};
2426 }
2427 interruptActiveLoads();
2428
2429 // Put us in a submitting state
2430 let navigation = getSubmittingNavigation(location, submission);
2431 updateState({
2432 navigation
2433 }, {
2434 flushSync: opts.flushSync === true
2435 });
2436 if (isFogOfWar) {
2437 let discoverResult = await discoverRoutes(matches, location.pathname, request.signal);
2438 if (discoverResult.type === "aborted") {
2439 return {
2440 shortCircuited: true
2441 };
2442 } else if (discoverResult.type === "error") {
[0c6b92a]2443 let boundaryId = findNearestBoundary(discoverResult.partialMatches).route.id;
[d565449]2444 return {
2445 matches: discoverResult.partialMatches,
2446 pendingActionResult: [boundaryId, {
2447 type: ResultType.error,
[0c6b92a]2448 error: discoverResult.error
[d565449]2449 }]
2450 };
2451 } else if (!discoverResult.matches) {
2452 let {
2453 notFoundMatches,
2454 error,
2455 route
2456 } = handleNavigational404(location.pathname);
2457 return {
2458 matches: notFoundMatches,
2459 pendingActionResult: [route.id, {
2460 type: ResultType.error,
2461 error
2462 }]
2463 };
2464 } else {
2465 matches = discoverResult.matches;
2466 }
2467 }
2468
2469 // Call our action and get the result
2470 let result;
2471 let actionMatch = getTargetMatch(matches, location);
2472 if (!actionMatch.route.action && !actionMatch.route.lazy) {
2473 result = {
2474 type: ResultType.error,
2475 error: getInternalRouterError(405, {
2476 method: request.method,
2477 pathname: location.pathname,
2478 routeId: actionMatch.route.id
2479 })
2480 };
2481 } else {
[0c6b92a]2482 let results = await callDataStrategy("action", state, request, [actionMatch], matches, null);
2483 result = results[actionMatch.route.id];
[d565449]2484 if (request.signal.aborted) {
2485 return {
2486 shortCircuited: true
2487 };
2488 }
2489 }
2490 if (isRedirectResult(result)) {
2491 let replace;
2492 if (opts && opts.replace != null) {
2493 replace = opts.replace;
2494 } else {
2495 // If the user didn't explicity indicate replace behavior, replace if
2496 // we redirected to the exact same location we're currently at to avoid
2497 // double back-buttons
2498 let location = normalizeRedirectLocation(result.response.headers.get("Location"), new URL(request.url), basename);
2499 replace = location === state.location.pathname + state.location.search;
2500 }
[0c6b92a]2501 await startRedirectNavigation(request, result, true, {
[d565449]2502 submission,
2503 replace
2504 });
2505 return {
2506 shortCircuited: true
2507 };
2508 }
2509 if (isDeferredResult(result)) {
2510 throw getInternalRouterError(400, {
2511 type: "defer-action"
2512 });
2513 }
2514 if (isErrorResult(result)) {
2515 // Store off the pending error - we use it to determine which loaders
2516 // to call and will commit it when we complete the navigation
2517 let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
2518
2519 // By default, all submissions to the current location are REPLACE
2520 // navigations, but if the action threw an error that'll be rendered in
2521 // an errorElement, we fall back to PUSH so that the user can use the
2522 // back button to get back to the pre-submission form location to try
2523 // again
2524 if ((opts && opts.replace) !== true) {
2525 pendingAction = Action.Push;
2526 }
2527 return {
2528 matches,
2529 pendingActionResult: [boundaryMatch.route.id, result]
2530 };
2531 }
2532 return {
2533 matches,
2534 pendingActionResult: [actionMatch.route.id, result]
2535 };
2536 }
2537
2538 // Call all applicable loaders for the given matches, handling redirects,
2539 // errors, etc.
2540 async function handleLoaders(request, location, matches, isFogOfWar, overrideNavigation, submission, fetcherSubmission, replace, initialHydration, flushSync, pendingActionResult) {
2541 // Figure out the right navigation we want to use for data loading
2542 let loadingNavigation = overrideNavigation || getLoadingNavigation(location, submission);
2543
2544 // If this was a redirect from an action we don't have a "submission" but
2545 // we have it on the loading navigation so use that if available
2546 let activeSubmission = submission || fetcherSubmission || getSubmissionFromNavigation(loadingNavigation);
2547
2548 // If this is an uninterrupted revalidation, we remain in our current idle
2549 // state. If not, we need to switch to our loading state and load data,
2550 // preserving any new action data or existing action data (in the case of
2551 // a revalidation interrupting an actionReload)
2552 // If we have partialHydration enabled, then don't update the state for the
2553 // initial data load since it's not a "navigation"
2554 let shouldUpdateNavigationState = !isUninterruptedRevalidation && (!future.v7_partialHydration || !initialHydration);
2555
2556 // When fog of war is enabled, we enter our `loading` state earlier so we
2557 // can discover new routes during the `loading` state. We skip this if
2558 // we've already run actions since we would have done our matching already.
2559 // If the children() function threw then, we want to proceed with the
2560 // partial matches it discovered.
2561 if (isFogOfWar) {
2562 if (shouldUpdateNavigationState) {
2563 let actionData = getUpdatedActionData(pendingActionResult);
2564 updateState(_extends({
2565 navigation: loadingNavigation
2566 }, actionData !== undefined ? {
2567 actionData
2568 } : {}), {
2569 flushSync
2570 });
2571 }
2572 let discoverResult = await discoverRoutes(matches, location.pathname, request.signal);
2573 if (discoverResult.type === "aborted") {
2574 return {
2575 shortCircuited: true
2576 };
2577 } else if (discoverResult.type === "error") {
[0c6b92a]2578 let boundaryId = findNearestBoundary(discoverResult.partialMatches).route.id;
[d565449]2579 return {
2580 matches: discoverResult.partialMatches,
2581 loaderData: {},
2582 errors: {
[0c6b92a]2583 [boundaryId]: discoverResult.error
[d565449]2584 }
2585 };
2586 } else if (!discoverResult.matches) {
2587 let {
2588 error,
2589 notFoundMatches,
2590 route
2591 } = handleNavigational404(location.pathname);
2592 return {
2593 matches: notFoundMatches,
2594 loaderData: {},
2595 errors: {
2596 [route.id]: error
2597 }
2598 };
2599 } else {
2600 matches = discoverResult.matches;
2601 }
2602 }
2603 let routesToUse = inFlightDataRoutes || dataRoutes;
2604 let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(init.history, state, matches, activeSubmission, location, future.v7_partialHydration && initialHydration === true, future.v7_skipActionErrorRevalidation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, deletedFetchers, fetchLoadMatches, fetchRedirectIds, routesToUse, basename, pendingActionResult);
2605
2606 // Cancel pending deferreds for no-longer-matched routes or routes we're
2607 // about to reload. Note that if this is an action reload we would have
2608 // already cancelled all pending deferreds so this would be a no-op
2609 cancelActiveDeferreds(routeId => !(matches && matches.some(m => m.route.id === routeId)) || matchesToLoad && matchesToLoad.some(m => m.route.id === routeId));
2610 pendingNavigationLoadId = ++incrementingLoadId;
2611
2612 // Short circuit if we have no loaders to run
2613 if (matchesToLoad.length === 0 && revalidatingFetchers.length === 0) {
2614 let updatedFetchers = markFetchRedirectsDone();
2615 completeNavigation(location, _extends({
2616 matches,
2617 loaderData: {},
2618 // Commit pending error if we're short circuiting
2619 errors: pendingActionResult && isErrorResult(pendingActionResult[1]) ? {
2620 [pendingActionResult[0]]: pendingActionResult[1].error
2621 } : null
2622 }, getActionDataForCommit(pendingActionResult), updatedFetchers ? {
2623 fetchers: new Map(state.fetchers)
2624 } : {}), {
2625 flushSync
2626 });
2627 return {
2628 shortCircuited: true
2629 };
2630 }
2631 if (shouldUpdateNavigationState) {
2632 let updates = {};
2633 if (!isFogOfWar) {
2634 // Only update navigation/actionNData if we didn't already do it above
2635 updates.navigation = loadingNavigation;
2636 let actionData = getUpdatedActionData(pendingActionResult);
2637 if (actionData !== undefined) {
2638 updates.actionData = actionData;
2639 }
2640 }
2641 if (revalidatingFetchers.length > 0) {
2642 updates.fetchers = getUpdatedRevalidatingFetchers(revalidatingFetchers);
2643 }
2644 updateState(updates, {
2645 flushSync
2646 });
2647 }
2648 revalidatingFetchers.forEach(rf => {
[0c6b92a]2649 abortFetcher(rf.key);
[d565449]2650 if (rf.controller) {
2651 // Fetchers use an independent AbortController so that aborting a fetcher
2652 // (via deleteFetcher) does not abort the triggering navigation that
2653 // triggered the revalidation
2654 fetchControllers.set(rf.key, rf.controller);
2655 }
2656 });
2657
2658 // Proxy navigation abort through to revalidation fetchers
2659 let abortPendingFetchRevalidations = () => revalidatingFetchers.forEach(f => abortFetcher(f.key));
2660 if (pendingNavigationController) {
2661 pendingNavigationController.signal.addEventListener("abort", abortPendingFetchRevalidations);
2662 }
2663 let {
2664 loaderResults,
2665 fetcherResults
[0c6b92a]2666 } = await callLoadersAndMaybeResolveData(state, matches, matchesToLoad, revalidatingFetchers, request);
[d565449]2667 if (request.signal.aborted) {
2668 return {
2669 shortCircuited: true
2670 };
2671 }
2672
2673 // Clean up _after_ loaders have completed. Don't clean up if we short
2674 // circuited because fetchControllers would have been aborted and
2675 // reassigned to new controllers for the next navigation
2676 if (pendingNavigationController) {
2677 pendingNavigationController.signal.removeEventListener("abort", abortPendingFetchRevalidations);
2678 }
2679 revalidatingFetchers.forEach(rf => fetchControllers.delete(rf.key));
2680
2681 // If any loaders returned a redirect Response, start a new REPLACE navigation
[0c6b92a]2682 let redirect = findRedirect(loaderResults);
[d565449]2683 if (redirect) {
[0c6b92a]2684 await startRedirectNavigation(request, redirect.result, true, {
2685 replace
2686 });
2687 return {
2688 shortCircuited: true
2689 };
2690 }
2691 redirect = findRedirect(fetcherResults);
2692 if (redirect) {
2693 // If this redirect came from a fetcher make sure we mark it in
2694 // fetchRedirectIds so it doesn't get revalidated on the next set of
2695 // loader executions
2696 fetchRedirectIds.add(redirect.key);
2697 await startRedirectNavigation(request, redirect.result, true, {
[d565449]2698 replace
2699 });
2700 return {
2701 shortCircuited: true
2702 };
2703 }
2704
2705 // Process and commit output from loaders
2706 let {
2707 loaderData,
2708 errors
[0c6b92a]2709 } = processLoaderData(state, matches, loaderResults, pendingActionResult, revalidatingFetchers, fetcherResults, activeDeferreds);
[d565449]2710
2711 // Wire up subscribers to update loaderData as promises settle
2712 activeDeferreds.forEach((deferredData, routeId) => {
2713 deferredData.subscribe(aborted => {
2714 // Note: No need to updateState here since the TrackedPromise on
2715 // loaderData is stable across resolve/reject
2716 // Remove this instance if we were aborted or if promises have settled
2717 if (aborted || deferredData.done) {
2718 activeDeferreds.delete(routeId);
2719 }
2720 });
2721 });
2722
[0c6b92a]2723 // Preserve SSR errors during partial hydration
[d565449]2724 if (future.v7_partialHydration && initialHydration && state.errors) {
[0c6b92a]2725 errors = _extends({}, state.errors, errors);
[d565449]2726 }
2727 let updatedFetchers = markFetchRedirectsDone();
2728 let didAbortFetchLoads = abortStaleFetchLoads(pendingNavigationLoadId);
2729 let shouldUpdateFetchers = updatedFetchers || didAbortFetchLoads || revalidatingFetchers.length > 0;
2730 return _extends({
2731 matches,
2732 loaderData,
2733 errors
2734 }, shouldUpdateFetchers ? {
2735 fetchers: new Map(state.fetchers)
2736 } : {});
2737 }
2738 function getUpdatedActionData(pendingActionResult) {
2739 if (pendingActionResult && !isErrorResult(pendingActionResult[1])) {
2740 // This is cast to `any` currently because `RouteData`uses any and it
2741 // would be a breaking change to use any.
2742 // TODO: v7 - change `RouteData` to use `unknown` instead of `any`
2743 return {
2744 [pendingActionResult[0]]: pendingActionResult[1].data
2745 };
2746 } else if (state.actionData) {
2747 if (Object.keys(state.actionData).length === 0) {
2748 return null;
2749 } else {
2750 return state.actionData;
2751 }
2752 }
2753 }
2754 function getUpdatedRevalidatingFetchers(revalidatingFetchers) {
2755 revalidatingFetchers.forEach(rf => {
2756 let fetcher = state.fetchers.get(rf.key);
2757 let revalidatingFetcher = getLoadingFetcher(undefined, fetcher ? fetcher.data : undefined);
2758 state.fetchers.set(rf.key, revalidatingFetcher);
2759 });
2760 return new Map(state.fetchers);
2761 }
2762
2763 // Trigger a fetcher load/submit for the given fetcher key
2764 function fetch(key, routeId, href, opts) {
2765 if (isServer) {
2766 throw new Error("router.fetch() was called during the server render, but it shouldn't be. " + "You are likely calling a useFetcher() method in the body of your component. " + "Try moving it to a useEffect or a callback.");
2767 }
[0c6b92a]2768 abortFetcher(key);
2769 let flushSync = (opts && opts.flushSync) === true;
[d565449]2770 let routesToUse = inFlightDataRoutes || dataRoutes;
2771 let normalizedPath = normalizeTo(state.location, state.matches, basename, future.v7_prependBasename, href, future.v7_relativeSplatPath, routeId, opts == null ? void 0 : opts.relative);
2772 let matches = matchRoutes(routesToUse, normalizedPath, basename);
2773 let fogOfWar = checkFogOfWar(matches, routesToUse, normalizedPath);
2774 if (fogOfWar.active && fogOfWar.matches) {
2775 matches = fogOfWar.matches;
2776 }
2777 if (!matches) {
2778 setFetcherError(key, routeId, getInternalRouterError(404, {
2779 pathname: normalizedPath
2780 }), {
2781 flushSync
2782 });
2783 return;
2784 }
2785 let {
2786 path,
2787 submission,
2788 error
2789 } = normalizeNavigateOptions(future.v7_normalizeFormMethod, true, normalizedPath, opts);
2790 if (error) {
2791 setFetcherError(key, routeId, error, {
2792 flushSync
2793 });
2794 return;
2795 }
2796 let match = getTargetMatch(matches, path);
[0c6b92a]2797 let preventScrollReset = (opts && opts.preventScrollReset) === true;
[d565449]2798 if (submission && isMutationMethod(submission.formMethod)) {
[0c6b92a]2799 handleFetcherAction(key, routeId, path, match, matches, fogOfWar.active, flushSync, preventScrollReset, submission);
[d565449]2800 return;
2801 }
2802
2803 // Store off the match so we can call it's shouldRevalidate on subsequent
2804 // revalidations
2805 fetchLoadMatches.set(key, {
2806 routeId,
2807 path
2808 });
[0c6b92a]2809 handleFetcherLoader(key, routeId, path, match, matches, fogOfWar.active, flushSync, preventScrollReset, submission);
[d565449]2810 }
2811
2812 // Call the action for the matched fetcher.submit(), and then handle redirects,
2813 // errors, and revalidation
[0c6b92a]2814 async function handleFetcherAction(key, routeId, path, match, requestMatches, isFogOfWar, flushSync, preventScrollReset, submission) {
[d565449]2815 interruptActiveLoads();
2816 fetchLoadMatches.delete(key);
2817 function detectAndHandle405Error(m) {
2818 if (!m.route.action && !m.route.lazy) {
2819 let error = getInternalRouterError(405, {
2820 method: submission.formMethod,
2821 pathname: path,
2822 routeId: routeId
2823 });
2824 setFetcherError(key, routeId, error, {
2825 flushSync
2826 });
2827 return true;
2828 }
2829 return false;
2830 }
2831 if (!isFogOfWar && detectAndHandle405Error(match)) {
2832 return;
2833 }
2834
2835 // Put this fetcher into it's submitting state
2836 let existingFetcher = state.fetchers.get(key);
2837 updateFetcherState(key, getSubmittingFetcher(submission, existingFetcher), {
2838 flushSync
2839 });
2840 let abortController = new AbortController();
2841 let fetchRequest = createClientSideRequest(init.history, path, abortController.signal, submission);
2842 if (isFogOfWar) {
2843 let discoverResult = await discoverRoutes(requestMatches, path, fetchRequest.signal);
2844 if (discoverResult.type === "aborted") {
2845 return;
2846 } else if (discoverResult.type === "error") {
[0c6b92a]2847 setFetcherError(key, routeId, discoverResult.error, {
[d565449]2848 flushSync
2849 });
2850 return;
2851 } else if (!discoverResult.matches) {
2852 setFetcherError(key, routeId, getInternalRouterError(404, {
2853 pathname: path
2854 }), {
2855 flushSync
2856 });
2857 return;
2858 } else {
2859 requestMatches = discoverResult.matches;
2860 match = getTargetMatch(requestMatches, path);
2861 if (detectAndHandle405Error(match)) {
2862 return;
2863 }
2864 }
2865 }
2866
2867 // Call the action for the fetcher
2868 fetchControllers.set(key, abortController);
2869 let originatingLoadId = incrementingLoadId;
[0c6b92a]2870 let actionResults = await callDataStrategy("action", state, fetchRequest, [match], requestMatches, key);
2871 let actionResult = actionResults[match.route.id];
[d565449]2872 if (fetchRequest.signal.aborted) {
2873 // We can delete this so long as we weren't aborted by our own fetcher
2874 // re-submit which would have put _new_ controller is in fetchControllers
2875 if (fetchControllers.get(key) === abortController) {
2876 fetchControllers.delete(key);
2877 }
2878 return;
2879 }
2880
2881 // When using v7_fetcherPersist, we don't want errors bubbling up to the UI
2882 // or redirects processed for unmounted fetchers so we just revert them to
2883 // idle
2884 if (future.v7_fetcherPersist && deletedFetchers.has(key)) {
2885 if (isRedirectResult(actionResult) || isErrorResult(actionResult)) {
2886 updateFetcherState(key, getDoneFetcher(undefined));
2887 return;
2888 }
2889 // Let SuccessResult's fall through for revalidation
2890 } else {
2891 if (isRedirectResult(actionResult)) {
2892 fetchControllers.delete(key);
2893 if (pendingNavigationLoadId > originatingLoadId) {
2894 // A new navigation was kicked off after our action started, so that
2895 // should take precedence over this redirect navigation. We already
2896 // set isRevalidationRequired so all loaders for the new route should
2897 // fire unless opted out via shouldRevalidate
2898 updateFetcherState(key, getDoneFetcher(undefined));
2899 return;
2900 } else {
2901 fetchRedirectIds.add(key);
2902 updateFetcherState(key, getLoadingFetcher(submission));
[0c6b92a]2903 return startRedirectNavigation(fetchRequest, actionResult, false, {
2904 fetcherSubmission: submission,
2905 preventScrollReset
[d565449]2906 });
2907 }
2908 }
2909
2910 // Process any non-redirect errors thrown
2911 if (isErrorResult(actionResult)) {
2912 setFetcherError(key, routeId, actionResult.error);
2913 return;
2914 }
2915 }
2916 if (isDeferredResult(actionResult)) {
2917 throw getInternalRouterError(400, {
2918 type: "defer-action"
2919 });
2920 }
2921
2922 // Start the data load for current matches, or the next location if we're
2923 // in the middle of a navigation
2924 let nextLocation = state.navigation.location || state.location;
2925 let revalidationRequest = createClientSideRequest(init.history, nextLocation, abortController.signal);
2926 let routesToUse = inFlightDataRoutes || dataRoutes;
2927 let matches = state.navigation.state !== "idle" ? matchRoutes(routesToUse, state.navigation.location, basename) : state.matches;
2928 invariant(matches, "Didn't find any matches after fetcher action");
2929 let loadId = ++incrementingLoadId;
2930 fetchReloadIds.set(key, loadId);
2931 let loadFetcher = getLoadingFetcher(submission, actionResult.data);
2932 state.fetchers.set(key, loadFetcher);
2933 let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(init.history, state, matches, submission, nextLocation, false, future.v7_skipActionErrorRevalidation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, deletedFetchers, fetchLoadMatches, fetchRedirectIds, routesToUse, basename, [match.route.id, actionResult]);
2934
2935 // Put all revalidating fetchers into the loading state, except for the
2936 // current fetcher which we want to keep in it's current loading state which
2937 // contains it's action submission info + action data
2938 revalidatingFetchers.filter(rf => rf.key !== key).forEach(rf => {
2939 let staleKey = rf.key;
2940 let existingFetcher = state.fetchers.get(staleKey);
2941 let revalidatingFetcher = getLoadingFetcher(undefined, existingFetcher ? existingFetcher.data : undefined);
2942 state.fetchers.set(staleKey, revalidatingFetcher);
[0c6b92a]2943 abortFetcher(staleKey);
[d565449]2944 if (rf.controller) {
2945 fetchControllers.set(staleKey, rf.controller);
2946 }
2947 });
2948 updateState({
2949 fetchers: new Map(state.fetchers)
2950 });
2951 let abortPendingFetchRevalidations = () => revalidatingFetchers.forEach(rf => abortFetcher(rf.key));
2952 abortController.signal.addEventListener("abort", abortPendingFetchRevalidations);
2953 let {
2954 loaderResults,
2955 fetcherResults
[0c6b92a]2956 } = await callLoadersAndMaybeResolveData(state, matches, matchesToLoad, revalidatingFetchers, revalidationRequest);
[d565449]2957 if (abortController.signal.aborted) {
2958 return;
2959 }
2960 abortController.signal.removeEventListener("abort", abortPendingFetchRevalidations);
2961 fetchReloadIds.delete(key);
2962 fetchControllers.delete(key);
2963 revalidatingFetchers.forEach(r => fetchControllers.delete(r.key));
[0c6b92a]2964 let redirect = findRedirect(loaderResults);
[d565449]2965 if (redirect) {
[0c6b92a]2966 return startRedirectNavigation(revalidationRequest, redirect.result, false, {
2967 preventScrollReset
2968 });
2969 }
2970 redirect = findRedirect(fetcherResults);
2971 if (redirect) {
2972 // If this redirect came from a fetcher make sure we mark it in
2973 // fetchRedirectIds so it doesn't get revalidated on the next set of
2974 // loader executions
2975 fetchRedirectIds.add(redirect.key);
2976 return startRedirectNavigation(revalidationRequest, redirect.result, false, {
2977 preventScrollReset
2978 });
[d565449]2979 }
2980
2981 // Process and commit output from loaders
2982 let {
2983 loaderData,
2984 errors
[0c6b92a]2985 } = processLoaderData(state, matches, loaderResults, undefined, revalidatingFetchers, fetcherResults, activeDeferreds);
[d565449]2986
2987 // Since we let revalidations complete even if the submitting fetcher was
2988 // deleted, only put it back to idle if it hasn't been deleted
2989 if (state.fetchers.has(key)) {
2990 let doneFetcher = getDoneFetcher(actionResult.data);
2991 state.fetchers.set(key, doneFetcher);
2992 }
2993 abortStaleFetchLoads(loadId);
2994
2995 // If we are currently in a navigation loading state and this fetcher is
2996 // more recent than the navigation, we want the newer data so abort the
2997 // navigation and complete it with the fetcher data
2998 if (state.navigation.state === "loading" && loadId > pendingNavigationLoadId) {
2999 invariant(pendingAction, "Expected pending action");
3000 pendingNavigationController && pendingNavigationController.abort();
3001 completeNavigation(state.navigation.location, {
3002 matches,
3003 loaderData,
3004 errors,
3005 fetchers: new Map(state.fetchers)
3006 });
3007 } else {
3008 // otherwise just update with the fetcher data, preserving any existing
3009 // loaderData for loaders that did not need to reload. We have to
3010 // manually merge here since we aren't going through completeNavigation
3011 updateState({
3012 errors,
3013 loaderData: mergeLoaderData(state.loaderData, loaderData, matches, errors),
3014 fetchers: new Map(state.fetchers)
3015 });
3016 isRevalidationRequired = false;
3017 }
3018 }
3019
3020 // Call the matched loader for fetcher.load(), handling redirects, errors, etc.
[0c6b92a]3021 async function handleFetcherLoader(key, routeId, path, match, matches, isFogOfWar, flushSync, preventScrollReset, submission) {
[d565449]3022 let existingFetcher = state.fetchers.get(key);
3023 updateFetcherState(key, getLoadingFetcher(submission, existingFetcher ? existingFetcher.data : undefined), {
3024 flushSync
3025 });
3026 let abortController = new AbortController();
3027 let fetchRequest = createClientSideRequest(init.history, path, abortController.signal);
3028 if (isFogOfWar) {
3029 let discoverResult = await discoverRoutes(matches, path, fetchRequest.signal);
3030 if (discoverResult.type === "aborted") {
3031 return;
3032 } else if (discoverResult.type === "error") {
[0c6b92a]3033 setFetcherError(key, routeId, discoverResult.error, {
[d565449]3034 flushSync
3035 });
3036 return;
3037 } else if (!discoverResult.matches) {
3038 setFetcherError(key, routeId, getInternalRouterError(404, {
3039 pathname: path
3040 }), {
3041 flushSync
3042 });
3043 return;
3044 } else {
3045 matches = discoverResult.matches;
3046 match = getTargetMatch(matches, path);
3047 }
3048 }
3049
3050 // Call the loader for this fetcher route match
3051 fetchControllers.set(key, abortController);
3052 let originatingLoadId = incrementingLoadId;
[0c6b92a]3053 let results = await callDataStrategy("loader", state, fetchRequest, [match], matches, key);
3054 let result = results[match.route.id];
[d565449]3055
3056 // Deferred isn't supported for fetcher loads, await everything and treat it
3057 // as a normal load. resolveDeferredData will return undefined if this
3058 // fetcher gets aborted, so we just leave result untouched and short circuit
3059 // below if that happens
3060 if (isDeferredResult(result)) {
3061 result = (await resolveDeferredData(result, fetchRequest.signal, true)) || result;
3062 }
3063
3064 // We can delete this so long as we weren't aborted by our our own fetcher
3065 // re-load which would have put _new_ controller is in fetchControllers
3066 if (fetchControllers.get(key) === abortController) {
3067 fetchControllers.delete(key);
3068 }
3069 if (fetchRequest.signal.aborted) {
3070 return;
3071 }
3072
3073 // We don't want errors bubbling up or redirects followed for unmounted
3074 // fetchers, so short circuit here if it was removed from the UI
3075 if (deletedFetchers.has(key)) {
3076 updateFetcherState(key, getDoneFetcher(undefined));
3077 return;
3078 }
3079
3080 // If the loader threw a redirect Response, start a new REPLACE navigation
3081 if (isRedirectResult(result)) {
3082 if (pendingNavigationLoadId > originatingLoadId) {
3083 // A new navigation was kicked off after our loader started, so that
3084 // should take precedence over this redirect navigation
3085 updateFetcherState(key, getDoneFetcher(undefined));
3086 return;
3087 } else {
3088 fetchRedirectIds.add(key);
[0c6b92a]3089 await startRedirectNavigation(fetchRequest, result, false, {
3090 preventScrollReset
3091 });
[d565449]3092 return;
3093 }
3094 }
3095
3096 // Process any non-redirect errors thrown
3097 if (isErrorResult(result)) {
3098 setFetcherError(key, routeId, result.error);
3099 return;
3100 }
3101 invariant(!isDeferredResult(result), "Unhandled fetcher deferred data");
3102
3103 // Put the fetcher back into an idle state
3104 updateFetcherState(key, getDoneFetcher(result.data));
3105 }
3106
3107 /**
3108 * Utility function to handle redirects returned from an action or loader.
3109 * Normally, a redirect "replaces" the navigation that triggered it. So, for
3110 * example:
3111 *
3112 * - user is on /a
3113 * - user clicks a link to /b
3114 * - loader for /b redirects to /c
3115 *
3116 * In a non-JS app the browser would track the in-flight navigation to /b and
3117 * then replace it with /c when it encountered the redirect response. In
3118 * the end it would only ever update the URL bar with /c.
3119 *
3120 * In client-side routing using pushState/replaceState, we aim to emulate
3121 * this behavior and we also do not update history until the end of the
3122 * navigation (including processed redirects). This means that we never
3123 * actually touch history until we've processed redirects, so we just use
3124 * the history action from the original navigation (PUSH or REPLACE).
3125 */
[0c6b92a]3126 async function startRedirectNavigation(request, redirect, isNavigation, _temp2) {
[d565449]3127 let {
3128 submission,
3129 fetcherSubmission,
[0c6b92a]3130 preventScrollReset,
[d565449]3131 replace
3132 } = _temp2 === void 0 ? {} : _temp2;
3133 if (redirect.response.headers.has("X-Remix-Revalidate")) {
3134 isRevalidationRequired = true;
3135 }
3136 let location = redirect.response.headers.get("Location");
3137 invariant(location, "Expected a Location header on the redirect Response");
3138 location = normalizeRedirectLocation(location, new URL(request.url), basename);
3139 let redirectLocation = createLocation(state.location, location, {
3140 _isRedirect: true
3141 });
3142 if (isBrowser) {
3143 let isDocumentReload = false;
3144 if (redirect.response.headers.has("X-Remix-Reload-Document")) {
3145 // Hard reload if the response contained X-Remix-Reload-Document
3146 isDocumentReload = true;
3147 } else if (ABSOLUTE_URL_REGEX.test(location)) {
3148 const url = init.history.createURL(location);
3149 isDocumentReload =
3150 // Hard reload if it's an absolute URL to a new origin
3151 url.origin !== routerWindow.location.origin ||
3152 // Hard reload if it's an absolute URL that does not match our basename
3153 stripBasename(url.pathname, basename) == null;
3154 }
3155 if (isDocumentReload) {
3156 if (replace) {
3157 routerWindow.location.replace(location);
3158 } else {
3159 routerWindow.location.assign(location);
3160 }
3161 return;
3162 }
3163 }
3164
3165 // There's no need to abort on redirects, since we don't detect the
3166 // redirect until the action/loaders have settled
3167 pendingNavigationController = null;
3168 let redirectHistoryAction = replace === true || redirect.response.headers.has("X-Remix-Replace") ? Action.Replace : Action.Push;
3169
3170 // Use the incoming submission if provided, fallback on the active one in
3171 // state.navigation
3172 let {
3173 formMethod,
3174 formAction,
3175 formEncType
3176 } = state.navigation;
3177 if (!submission && !fetcherSubmission && formMethod && formAction && formEncType) {
3178 submission = getSubmissionFromNavigation(state.navigation);
3179 }
3180
3181 // If this was a 307/308 submission we want to preserve the HTTP method and
3182 // re-submit the GET/POST/PUT/PATCH/DELETE as a submission navigation to the
3183 // redirected location
3184 let activeSubmission = submission || fetcherSubmission;
3185 if (redirectPreserveMethodStatusCodes.has(redirect.response.status) && activeSubmission && isMutationMethod(activeSubmission.formMethod)) {
3186 await startNavigation(redirectHistoryAction, redirectLocation, {
3187 submission: _extends({}, activeSubmission, {
3188 formAction: location
3189 }),
[0c6b92a]3190 // Preserve these flags across redirects
3191 preventScrollReset: preventScrollReset || pendingPreventScrollReset,
3192 enableViewTransition: isNavigation ? pendingViewTransitionEnabled : undefined
[d565449]3193 });
3194 } else {
3195 // If we have a navigation submission, we will preserve it through the
3196 // redirect navigation
3197 let overrideNavigation = getLoadingNavigation(redirectLocation, submission);
3198 await startNavigation(redirectHistoryAction, redirectLocation, {
3199 overrideNavigation,
3200 // Send fetcher submissions through for shouldRevalidate
3201 fetcherSubmission,
[0c6b92a]3202 // Preserve these flags across redirects
3203 preventScrollReset: preventScrollReset || pendingPreventScrollReset,
3204 enableViewTransition: isNavigation ? pendingViewTransitionEnabled : undefined
[d565449]3205 });
3206 }
3207 }
3208
3209 // Utility wrapper for calling dataStrategy client-side without having to
3210 // pass around the manifest, mapRouteProperties, etc.
[0c6b92a]3211 async function callDataStrategy(type, state, request, matchesToLoad, matches, fetcherKey) {
3212 let results;
3213 let dataResults = {};
[d565449]3214 try {
[0c6b92a]3215 results = await callDataStrategyImpl(dataStrategyImpl, type, state, request, matchesToLoad, matches, fetcherKey, manifest, mapRouteProperties);
[d565449]3216 } catch (e) {
3217 // If the outer dataStrategy method throws, just return the error for all
3218 // matches - and it'll naturally bubble to the root
[0c6b92a]3219 matchesToLoad.forEach(m => {
3220 dataResults[m.route.id] = {
3221 type: ResultType.error,
3222 error: e
3223 };
3224 });
3225 return dataResults;
3226 }
3227 for (let [routeId, result] of Object.entries(results)) {
3228 if (isRedirectDataStrategyResultResult(result)) {
3229 let response = result.result;
3230 dataResults[routeId] = {
3231 type: ResultType.redirect,
3232 response: normalizeRelativeRoutingRedirectResponse(response, request, routeId, matches, basename, future.v7_relativeSplatPath)
3233 };
3234 } else {
3235 dataResults[routeId] = await convertDataStrategyResultToDataResult(result);
3236 }
[d565449]3237 }
[0c6b92a]3238 return dataResults;
[d565449]3239 }
[0c6b92a]3240 async function callLoadersAndMaybeResolveData(state, matches, matchesToLoad, fetchersToLoad, request) {
3241 let currentMatches = state.matches;
3242
3243 // Kick off loaders and fetchers in parallel
3244 let loaderResultsPromise = callDataStrategy("loader", state, request, matchesToLoad, matches, null);
3245 let fetcherResultsPromise = Promise.all(fetchersToLoad.map(async f => {
[d565449]3246 if (f.matches && f.match && f.controller) {
[0c6b92a]3247 let results = await callDataStrategy("loader", state, createClientSideRequest(init.history, f.path, f.controller.signal), [f.match], f.matches, f.key);
3248 let result = results[f.match.route.id];
3249 // Fetcher results are keyed by fetcher key from here on out, not routeId
3250 return {
3251 [f.key]: result
3252 };
[d565449]3253 } else {
3254 return Promise.resolve({
[0c6b92a]3255 [f.key]: {
3256 type: ResultType.error,
3257 error: getInternalRouterError(404, {
3258 pathname: f.path
3259 })
3260 }
[d565449]3261 });
3262 }
[0c6b92a]3263 }));
3264 let loaderResults = await loaderResultsPromise;
3265 let fetcherResults = (await fetcherResultsPromise).reduce((acc, r) => Object.assign(acc, r), {});
3266 await Promise.all([resolveNavigationDeferredResults(matches, loaderResults, request.signal, currentMatches, state.loaderData), resolveFetcherDeferredResults(matches, fetcherResults, fetchersToLoad)]);
[d565449]3267 return {
3268 loaderResults,
3269 fetcherResults
3270 };
3271 }
3272 function interruptActiveLoads() {
3273 // Every interruption triggers a revalidation
3274 isRevalidationRequired = true;
3275
3276 // Cancel pending route-level deferreds and mark cancelled routes for
3277 // revalidation
3278 cancelledDeferredRoutes.push(...cancelActiveDeferreds());
3279
3280 // Abort in-flight fetcher loads
3281 fetchLoadMatches.forEach((_, key) => {
3282 if (fetchControllers.has(key)) {
3283 cancelledFetcherLoads.add(key);
3284 }
[0c6b92a]3285 abortFetcher(key);
[d565449]3286 });
3287 }
3288 function updateFetcherState(key, fetcher, opts) {
3289 if (opts === void 0) {
3290 opts = {};
3291 }
3292 state.fetchers.set(key, fetcher);
3293 updateState({
3294 fetchers: new Map(state.fetchers)
3295 }, {
3296 flushSync: (opts && opts.flushSync) === true
3297 });
3298 }
3299 function setFetcherError(key, routeId, error, opts) {
3300 if (opts === void 0) {
3301 opts = {};
3302 }
3303 let boundaryMatch = findNearestBoundary(state.matches, routeId);
3304 deleteFetcher(key);
3305 updateState({
3306 errors: {
3307 [boundaryMatch.route.id]: error
3308 },
3309 fetchers: new Map(state.fetchers)
3310 }, {
3311 flushSync: (opts && opts.flushSync) === true
3312 });
3313 }
3314 function getFetcher(key) {
3315 if (future.v7_fetcherPersist) {
3316 activeFetchers.set(key, (activeFetchers.get(key) || 0) + 1);
3317 // If this fetcher was previously marked for deletion, unmark it since we
3318 // have a new instance
3319 if (deletedFetchers.has(key)) {
3320 deletedFetchers.delete(key);
3321 }
3322 }
3323 return state.fetchers.get(key) || IDLE_FETCHER;
3324 }
3325 function deleteFetcher(key) {
3326 let fetcher = state.fetchers.get(key);
3327 // Don't abort the controller if this is a deletion of a fetcher.submit()
3328 // in it's loading phase since - we don't want to abort the corresponding
3329 // revalidation and want them to complete and land
3330 if (fetchControllers.has(key) && !(fetcher && fetcher.state === "loading" && fetchReloadIds.has(key))) {
3331 abortFetcher(key);
3332 }
3333 fetchLoadMatches.delete(key);
3334 fetchReloadIds.delete(key);
3335 fetchRedirectIds.delete(key);
3336 deletedFetchers.delete(key);
3337 cancelledFetcherLoads.delete(key);
3338 state.fetchers.delete(key);
3339 }
3340 function deleteFetcherAndUpdateState(key) {
3341 if (future.v7_fetcherPersist) {
3342 let count = (activeFetchers.get(key) || 0) - 1;
3343 if (count <= 0) {
3344 activeFetchers.delete(key);
3345 deletedFetchers.add(key);
3346 } else {
3347 activeFetchers.set(key, count);
3348 }
3349 } else {
3350 deleteFetcher(key);
3351 }
3352 updateState({
3353 fetchers: new Map(state.fetchers)
3354 });
3355 }
3356 function abortFetcher(key) {
3357 let controller = fetchControllers.get(key);
[0c6b92a]3358 if (controller) {
3359 controller.abort();
3360 fetchControllers.delete(key);
3361 }
[d565449]3362 }
3363 function markFetchersDone(keys) {
3364 for (let key of keys) {
3365 let fetcher = getFetcher(key);
3366 let doneFetcher = getDoneFetcher(fetcher.data);
3367 state.fetchers.set(key, doneFetcher);
3368 }
3369 }
3370 function markFetchRedirectsDone() {
3371 let doneKeys = [];
3372 let updatedFetchers = false;
3373 for (let key of fetchRedirectIds) {
3374 let fetcher = state.fetchers.get(key);
3375 invariant(fetcher, "Expected fetcher: " + key);
3376 if (fetcher.state === "loading") {
3377 fetchRedirectIds.delete(key);
3378 doneKeys.push(key);
3379 updatedFetchers = true;
3380 }
3381 }
3382 markFetchersDone(doneKeys);
3383 return updatedFetchers;
3384 }
3385 function abortStaleFetchLoads(landedId) {
3386 let yeetedKeys = [];
3387 for (let [key, id] of fetchReloadIds) {
3388 if (id < landedId) {
3389 let fetcher = state.fetchers.get(key);
3390 invariant(fetcher, "Expected fetcher: " + key);
3391 if (fetcher.state === "loading") {
3392 abortFetcher(key);
3393 fetchReloadIds.delete(key);
3394 yeetedKeys.push(key);
3395 }
3396 }
3397 }
3398 markFetchersDone(yeetedKeys);
3399 return yeetedKeys.length > 0;
3400 }
3401 function getBlocker(key, fn) {
3402 let blocker = state.blockers.get(key) || IDLE_BLOCKER;
3403 if (blockerFunctions.get(key) !== fn) {
3404 blockerFunctions.set(key, fn);
3405 }
3406 return blocker;
3407 }
3408 function deleteBlocker(key) {
3409 state.blockers.delete(key);
3410 blockerFunctions.delete(key);
3411 }
3412
3413 // Utility function to update blockers, ensuring valid state transitions
3414 function updateBlocker(key, newBlocker) {
3415 let blocker = state.blockers.get(key) || IDLE_BLOCKER;
3416
3417 // Poor mans state machine :)
3418 // https://mermaid.live/edit#pako:eNqVkc9OwzAMxl8l8nnjAYrEtDIOHEBIgwvKJTReGy3_lDpIqO27k6awMG0XcrLlnz87nwdonESogKXXBuE79rq75XZO3-yHds0RJVuv70YrPlUrCEe2HfrORS3rubqZfuhtpg5C9wk5tZ4VKcRUq88q9Z8RS0-48cE1iHJkL0ugbHuFLus9L6spZy8nX9MP2CNdomVaposqu3fGayT8T8-jJQwhepo_UtpgBQaDEUom04dZhAN1aJBDlUKJBxE1ceB2Smj0Mln-IBW5AFU2dwUiktt_2Qaq2dBfaKdEup85UV7Yd-dKjlnkabl2Pvr0DTkTreM
3419 invariant(blocker.state === "unblocked" && newBlocker.state === "blocked" || blocker.state === "blocked" && newBlocker.state === "blocked" || blocker.state === "blocked" && newBlocker.state === "proceeding" || blocker.state === "blocked" && newBlocker.state === "unblocked" || blocker.state === "proceeding" && newBlocker.state === "unblocked", "Invalid blocker state transition: " + blocker.state + " -> " + newBlocker.state);
3420 let blockers = new Map(state.blockers);
3421 blockers.set(key, newBlocker);
3422 updateState({
3423 blockers
3424 });
3425 }
[0c6b92a]3426 function shouldBlockNavigation(_ref2) {
[d565449]3427 let {
3428 currentLocation,
3429 nextLocation,
3430 historyAction
[0c6b92a]3431 } = _ref2;
[d565449]3432 if (blockerFunctions.size === 0) {
3433 return;
3434 }
3435
3436 // We ony support a single active blocker at the moment since we don't have
3437 // any compelling use cases for multi-blocker yet
3438 if (blockerFunctions.size > 1) {
3439 warning(false, "A router only supports one blocker at a time");
3440 }
3441 let entries = Array.from(blockerFunctions.entries());
3442 let [blockerKey, blockerFunction] = entries[entries.length - 1];
3443 let blocker = state.blockers.get(blockerKey);
3444 if (blocker && blocker.state === "proceeding") {
3445 // If the blocker is currently proceeding, we don't need to re-check
3446 // it and can let this navigation continue
3447 return;
3448 }
3449
3450 // At this point, we know we're unblocked/blocked so we need to check the
3451 // user-provided blocker function
3452 if (blockerFunction({
3453 currentLocation,
3454 nextLocation,
3455 historyAction
3456 })) {
3457 return blockerKey;
3458 }
3459 }
3460 function handleNavigational404(pathname) {
3461 let error = getInternalRouterError(404, {
3462 pathname
3463 });
3464 let routesToUse = inFlightDataRoutes || dataRoutes;
3465 let {
3466 matches,
3467 route
3468 } = getShortCircuitMatches(routesToUse);
3469
3470 // Cancel all pending deferred on 404s since we don't keep any routes
3471 cancelActiveDeferreds();
3472 return {
3473 notFoundMatches: matches,
3474 route,
3475 error
3476 };
3477 }
3478 function cancelActiveDeferreds(predicate) {
3479 let cancelledRouteIds = [];
3480 activeDeferreds.forEach((dfd, routeId) => {
3481 if (!predicate || predicate(routeId)) {
3482 // Cancel the deferred - but do not remove from activeDeferreds here -
3483 // we rely on the subscribers to do that so our tests can assert proper
3484 // cleanup via _internalActiveDeferreds
3485 dfd.cancel();
3486 cancelledRouteIds.push(routeId);
3487 activeDeferreds.delete(routeId);
3488 }
3489 });
3490 return cancelledRouteIds;
3491 }
3492
3493 // Opt in to capturing and reporting scroll positions during navigations,
3494 // used by the <ScrollRestoration> component
3495 function enableScrollRestoration(positions, getPosition, getKey) {
3496 savedScrollPositions = positions;
3497 getScrollPosition = getPosition;
3498 getScrollRestorationKey = getKey || null;
3499
3500 // Perform initial hydration scroll restoration, since we miss the boat on
3501 // the initial updateState() because we've not yet rendered <ScrollRestoration/>
3502 // and therefore have no savedScrollPositions available
3503 if (!initialScrollRestored && state.navigation === IDLE_NAVIGATION) {
3504 initialScrollRestored = true;
3505 let y = getSavedScrollPosition(state.location, state.matches);
3506 if (y != null) {
3507 updateState({
3508 restoreScrollPosition: y
3509 });
3510 }
3511 }
3512 return () => {
3513 savedScrollPositions = null;
3514 getScrollPosition = null;
3515 getScrollRestorationKey = null;
3516 };
3517 }
3518 function getScrollKey(location, matches) {
3519 if (getScrollRestorationKey) {
3520 let key = getScrollRestorationKey(location, matches.map(m => convertRouteMatchToUiMatch(m, state.loaderData)));
3521 return key || location.key;
3522 }
3523 return location.key;
3524 }
3525 function saveScrollPosition(location, matches) {
3526 if (savedScrollPositions && getScrollPosition) {
3527 let key = getScrollKey(location, matches);
3528 savedScrollPositions[key] = getScrollPosition();
3529 }
3530 }
3531 function getSavedScrollPosition(location, matches) {
3532 if (savedScrollPositions) {
3533 let key = getScrollKey(location, matches);
3534 let y = savedScrollPositions[key];
3535 if (typeof y === "number") {
3536 return y;
3537 }
3538 }
3539 return null;
3540 }
3541 function checkFogOfWar(matches, routesToUse, pathname) {
[0c6b92a]3542 if (patchRoutesOnNavigationImpl) {
[d565449]3543 if (!matches) {
3544 let fogMatches = matchRoutesImpl(routesToUse, pathname, basename, true);
3545 return {
3546 active: true,
3547 matches: fogMatches || []
3548 };
3549 } else {
[0c6b92a]3550 if (Object.keys(matches[0].params).length > 0) {
3551 // If we matched a dynamic param or a splat, it might only be because
3552 // we haven't yet discovered other routes that would match with a
3553 // higher score. Call patchRoutesOnNavigation just to be sure
[d565449]3554 let partialMatches = matchRoutesImpl(routesToUse, pathname, basename, true);
3555 return {
3556 active: true,
3557 matches: partialMatches
3558 };
3559 }
3560 }
3561 }
3562 return {
3563 active: false,
3564 matches: null
3565 };
3566 }
3567 async function discoverRoutes(matches, pathname, signal) {
[0c6b92a]3568 if (!patchRoutesOnNavigationImpl) {
3569 return {
3570 type: "success",
3571 matches
3572 };
3573 }
[d565449]3574 let partialMatches = matches;
3575 while (true) {
3576 let isNonHMR = inFlightDataRoutes == null;
3577 let routesToUse = inFlightDataRoutes || dataRoutes;
[0c6b92a]3578 let localManifest = manifest;
[d565449]3579 try {
[0c6b92a]3580 await patchRoutesOnNavigationImpl({
3581 path: pathname,
3582 matches: partialMatches,
3583 patch: (routeId, children) => {
3584 if (signal.aborted) return;
3585 patchRoutesImpl(routeId, children, routesToUse, localManifest, mapRouteProperties);
3586 }
3587 });
[d565449]3588 } catch (e) {
3589 return {
3590 type: "error",
3591 error: e,
3592 partialMatches
3593 };
3594 } finally {
3595 // If we are not in the middle of an HMR revalidation and we changed the
3596 // routes, provide a new identity so when we `updateState` at the end of
3597 // this navigation/fetch `router.routes` will be a new identity and
3598 // trigger a re-run of memoized `router.routes` dependencies.
3599 // HMR will already update the identity and reflow when it lands
3600 // `inFlightDataRoutes` in `completeNavigation`
[0c6b92a]3601 if (isNonHMR && !signal.aborted) {
[d565449]3602 dataRoutes = [...dataRoutes];
3603 }
3604 }
3605 if (signal.aborted) {
3606 return {
3607 type: "aborted"
3608 };
3609 }
3610 let newMatches = matchRoutes(routesToUse, pathname, basename);
3611 if (newMatches) {
3612 return {
3613 type: "success",
[0c6b92a]3614 matches: newMatches
[d565449]3615 };
3616 }
[0c6b92a]3617 let newPartialMatches = matchRoutesImpl(routesToUse, pathname, basename, true);
3618
3619 // Avoid loops if the second pass results in the same partial matches
3620 if (!newPartialMatches || partialMatches.length === newPartialMatches.length && partialMatches.every((m, i) => m.route.id === newPartialMatches[i].route.id)) {
[d565449]3621 return {
3622 type: "success",
[0c6b92a]3623 matches: null
[d565449]3624 };
3625 }
[0c6b92a]3626 partialMatches = newPartialMatches;
[d565449]3627 }
3628 }
3629 function _internalSetRoutes(newRoutes) {
3630 manifest = {};
3631 inFlightDataRoutes = convertRoutesToDataRoutes(newRoutes, mapRouteProperties, undefined, manifest);
3632 }
3633 function patchRoutes(routeId, children) {
3634 let isNonHMR = inFlightDataRoutes == null;
3635 let routesToUse = inFlightDataRoutes || dataRoutes;
3636 patchRoutesImpl(routeId, children, routesToUse, manifest, mapRouteProperties);
3637
3638 // If we are not in the middle of an HMR revalidation and we changed the
3639 // routes, provide a new identity and trigger a reflow via `updateState`
3640 // to re-run memoized `router.routes` dependencies.
3641 // HMR will already update the identity and reflow when it lands
3642 // `inFlightDataRoutes` in `completeNavigation`
3643 if (isNonHMR) {
3644 dataRoutes = [...dataRoutes];
3645 updateState({});
3646 }
3647 }
3648 router = {
3649 get basename() {
3650 return basename;
3651 },
3652 get future() {
3653 return future;
3654 },
3655 get state() {
3656 return state;
3657 },
3658 get routes() {
3659 return dataRoutes;
3660 },
3661 get window() {
3662 return routerWindow;
3663 },
3664 initialize,
3665 subscribe,
3666 enableScrollRestoration,
3667 navigate,
3668 fetch,
3669 revalidate,
3670 // Passthrough to history-aware createHref used by useHref so we get proper
3671 // hash-aware URLs in DOM paths
3672 createHref: to => init.history.createHref(to),
3673 encodeLocation: to => init.history.encodeLocation(to),
3674 getFetcher,
3675 deleteFetcher: deleteFetcherAndUpdateState,
3676 dispose,
3677 getBlocker,
3678 deleteBlocker,
3679 patchRoutes,
3680 _internalFetchControllers: fetchControllers,
3681 _internalActiveDeferreds: activeDeferreds,
3682 // TODO: Remove setRoutes, it's temporary to avoid dealing with
3683 // updating the tree while validating the update algorithm.
3684 _internalSetRoutes
3685 };
3686 return router;
3687}
3688//#endregion
3689
3690////////////////////////////////////////////////////////////////////////////////
3691//#region createStaticHandler
3692////////////////////////////////////////////////////////////////////////////////
3693
3694const UNSAFE_DEFERRED_SYMBOL = Symbol("deferred");
3695
3696/**
3697 * Future flags to toggle new feature behavior
3698 */
3699
3700function createStaticHandler(routes, opts) {
3701 invariant(routes.length > 0, "You must provide a non-empty routes array to createStaticHandler");
3702 let manifest = {};
3703 let basename = (opts ? opts.basename : null) || "/";
3704 let mapRouteProperties;
3705 if (opts != null && opts.mapRouteProperties) {
3706 mapRouteProperties = opts.mapRouteProperties;
3707 } else if (opts != null && opts.detectErrorBoundary) {
3708 // If they are still using the deprecated version, wrap it with the new API
3709 let detectErrorBoundary = opts.detectErrorBoundary;
3710 mapRouteProperties = route => ({
3711 hasErrorBoundary: detectErrorBoundary(route)
3712 });
3713 } else {
3714 mapRouteProperties = defaultMapRouteProperties;
3715 }
3716 // Config driven behavior flags
3717 let future = _extends({
3718 v7_relativeSplatPath: false,
3719 v7_throwAbortReason: false
3720 }, opts ? opts.future : null);
3721 let dataRoutes = convertRoutesToDataRoutes(routes, mapRouteProperties, undefined, manifest);
3722
3723 /**
3724 * The query() method is intended for document requests, in which we want to
3725 * call an optional action and potentially multiple loaders for all nested
3726 * routes. It returns a StaticHandlerContext object, which is very similar
3727 * to the router state (location, loaderData, actionData, errors, etc.) and
3728 * also adds SSR-specific information such as the statusCode and headers
3729 * from action/loaders Responses.
3730 *
3731 * It _should_ never throw and should report all errors through the
3732 * returned context.errors object, properly associating errors to their error
3733 * boundary. Additionally, it tracks _deepestRenderedBoundaryId which can be
3734 * used to emulate React error boundaries during SSr by performing a second
3735 * pass only down to the boundaryId.
3736 *
3737 * The one exception where we do not return a StaticHandlerContext is when a
3738 * redirect response is returned or thrown from any action/loader. We
3739 * propagate that out and return the raw Response so the HTTP server can
3740 * return it directly.
3741 *
3742 * - `opts.requestContext` is an optional server context that will be passed
3743 * to actions/loaders in the `context` parameter
3744 * - `opts.skipLoaderErrorBubbling` is an optional parameter that will prevent
3745 * the bubbling of errors which allows single-fetch-type implementations
3746 * where the client will handle the bubbling and we may need to return data
3747 * for the handling route
3748 */
3749 async function query(request, _temp3) {
3750 let {
3751 requestContext,
3752 skipLoaderErrorBubbling,
[0c6b92a]3753 dataStrategy
[d565449]3754 } = _temp3 === void 0 ? {} : _temp3;
3755 let url = new URL(request.url);
3756 let method = request.method;
3757 let location = createLocation("", createPath(url), null, "default");
3758 let matches = matchRoutes(dataRoutes, location, basename);
3759
3760 // SSR supports HEAD requests while SPA doesn't
3761 if (!isValidMethod(method) && method !== "HEAD") {
3762 let error = getInternalRouterError(405, {
3763 method
3764 });
3765 let {
3766 matches: methodNotAllowedMatches,
3767 route
3768 } = getShortCircuitMatches(dataRoutes);
3769 return {
3770 basename,
3771 location,
3772 matches: methodNotAllowedMatches,
3773 loaderData: {},
3774 actionData: null,
3775 errors: {
3776 [route.id]: error
3777 },
3778 statusCode: error.status,
3779 loaderHeaders: {},
3780 actionHeaders: {},
3781 activeDeferreds: null
3782 };
3783 } else if (!matches) {
3784 let error = getInternalRouterError(404, {
3785 pathname: location.pathname
3786 });
3787 let {
3788 matches: notFoundMatches,
3789 route
3790 } = getShortCircuitMatches(dataRoutes);
3791 return {
3792 basename,
3793 location,
3794 matches: notFoundMatches,
3795 loaderData: {},
3796 actionData: null,
3797 errors: {
3798 [route.id]: error
3799 },
3800 statusCode: error.status,
3801 loaderHeaders: {},
3802 actionHeaders: {},
3803 activeDeferreds: null
3804 };
3805 }
[0c6b92a]3806 let result = await queryImpl(request, location, matches, requestContext, dataStrategy || null, skipLoaderErrorBubbling === true, null);
[d565449]3807 if (isResponse(result)) {
3808 return result;
3809 }
3810
3811 // When returning StaticHandlerContext, we patch back in the location here
3812 // since we need it for React Context. But this helps keep our submit and
3813 // loadRouteData operating on a Request instead of a Location
3814 return _extends({
3815 location,
3816 basename
3817 }, result);
3818 }
3819
3820 /**
3821 * The queryRoute() method is intended for targeted route requests, either
3822 * for fetch ?_data requests or resource route requests. In this case, we
3823 * are only ever calling a single action or loader, and we are returning the
3824 * returned value directly. In most cases, this will be a Response returned
3825 * from the action/loader, but it may be a primitive or other value as well -
3826 * and in such cases the calling context should handle that accordingly.
3827 *
3828 * We do respect the throw/return differentiation, so if an action/loader
3829 * throws, then this method will throw the value. This is important so we
3830 * can do proper boundary identification in Remix where a thrown Response
3831 * must go to the Catch Boundary but a returned Response is happy-path.
3832 *
3833 * One thing to note is that any Router-initiated Errors that make sense
3834 * to associate with a status code will be thrown as an ErrorResponse
3835 * instance which include the raw Error, such that the calling context can
3836 * serialize the error as they see fit while including the proper response
3837 * code. Examples here are 404 and 405 errors that occur prior to reaching
3838 * any user-defined loaders.
3839 *
3840 * - `opts.routeId` allows you to specify the specific route handler to call.
3841 * If not provided the handler will determine the proper route by matching
3842 * against `request.url`
3843 * - `opts.requestContext` is an optional server context that will be passed
3844 * to actions/loaders in the `context` parameter
3845 */
3846 async function queryRoute(request, _temp4) {
3847 let {
3848 routeId,
3849 requestContext,
[0c6b92a]3850 dataStrategy
[d565449]3851 } = _temp4 === void 0 ? {} : _temp4;
3852 let url = new URL(request.url);
3853 let method = request.method;
3854 let location = createLocation("", createPath(url), null, "default");
3855 let matches = matchRoutes(dataRoutes, location, basename);
3856
3857 // SSR supports HEAD requests while SPA doesn't
3858 if (!isValidMethod(method) && method !== "HEAD" && method !== "OPTIONS") {
3859 throw getInternalRouterError(405, {
3860 method
3861 });
3862 } else if (!matches) {
3863 throw getInternalRouterError(404, {
3864 pathname: location.pathname
3865 });
3866 }
3867 let match = routeId ? matches.find(m => m.route.id === routeId) : getTargetMatch(matches, location);
3868 if (routeId && !match) {
3869 throw getInternalRouterError(403, {
3870 pathname: location.pathname,
3871 routeId
3872 });
3873 } else if (!match) {
3874 // This should never hit I don't think?
3875 throw getInternalRouterError(404, {
3876 pathname: location.pathname
3877 });
3878 }
[0c6b92a]3879 let result = await queryImpl(request, location, matches, requestContext, dataStrategy || null, false, match);
[d565449]3880 if (isResponse(result)) {
3881 return result;
3882 }
3883 let error = result.errors ? Object.values(result.errors)[0] : undefined;
3884 if (error !== undefined) {
3885 // If we got back result.errors, that means the loader/action threw
3886 // _something_ that wasn't a Response, but it's not guaranteed/required
3887 // to be an `instanceof Error` either, so we have to use throw here to
3888 // preserve the "error" state outside of queryImpl.
3889 throw error;
3890 }
3891
3892 // Pick off the right state value to return
3893 if (result.actionData) {
3894 return Object.values(result.actionData)[0];
3895 }
3896 if (result.loaderData) {
3897 var _result$activeDeferre;
3898 let data = Object.values(result.loaderData)[0];
3899 if ((_result$activeDeferre = result.activeDeferreds) != null && _result$activeDeferre[match.route.id]) {
3900 data[UNSAFE_DEFERRED_SYMBOL] = result.activeDeferreds[match.route.id];
3901 }
3902 return data;
3903 }
3904 return undefined;
3905 }
[0c6b92a]3906 async function queryImpl(request, location, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch) {
[d565449]3907 invariant(request.signal, "query()/queryRoute() requests must contain an AbortController signal");
3908 try {
3909 if (isMutationMethod(request.method.toLowerCase())) {
[0c6b92a]3910 let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch != null);
[d565449]3911 return result;
3912 }
[0c6b92a]3913 let result = await loadRouteData(request, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch);
[d565449]3914 return isResponse(result) ? result : _extends({}, result, {
3915 actionData: null,
3916 actionHeaders: {}
3917 });
3918 } catch (e) {
3919 // If the user threw/returned a Response in callLoaderOrAction for a
[0c6b92a]3920 // `queryRoute` call, we throw the `DataStrategyResult` to bail out early
[d565449]3921 // and then return or throw the raw Response here accordingly
[0c6b92a]3922 if (isDataStrategyResult(e) && isResponse(e.result)) {
[d565449]3923 if (e.type === ResultType.error) {
3924 throw e.result;
3925 }
3926 return e.result;
3927 }
3928 // Redirects are always returned since they don't propagate to catch
3929 // boundaries
3930 if (isRedirectResponse(e)) {
3931 return e;
3932 }
3933 throw e;
3934 }
3935 }
[0c6b92a]3936 async function submit(request, matches, actionMatch, requestContext, dataStrategy, skipLoaderErrorBubbling, isRouteRequest) {
[d565449]3937 let result;
3938 if (!actionMatch.route.action && !actionMatch.route.lazy) {
3939 let error = getInternalRouterError(405, {
3940 method: request.method,
3941 pathname: new URL(request.url).pathname,
3942 routeId: actionMatch.route.id
3943 });
3944 if (isRouteRequest) {
3945 throw error;
3946 }
3947 result = {
3948 type: ResultType.error,
3949 error
3950 };
3951 } else {
[0c6b92a]3952 let results = await callDataStrategy("action", request, [actionMatch], matches, isRouteRequest, requestContext, dataStrategy);
3953 result = results[actionMatch.route.id];
[d565449]3954 if (request.signal.aborted) {
3955 throwStaticHandlerAbortedError(request, isRouteRequest, future);
3956 }
3957 }
3958 if (isRedirectResult(result)) {
3959 // Uhhhh - this should never happen, we should always throw these from
3960 // callLoaderOrAction, but the type narrowing here keeps TS happy and we
3961 // can get back on the "throw all redirect responses" train here should
3962 // this ever happen :/
3963 throw new Response(null, {
3964 status: result.response.status,
3965 headers: {
3966 Location: result.response.headers.get("Location")
3967 }
3968 });
3969 }
3970 if (isDeferredResult(result)) {
3971 let error = getInternalRouterError(400, {
3972 type: "defer-action"
3973 });
3974 if (isRouteRequest) {
3975 throw error;
3976 }
3977 result = {
3978 type: ResultType.error,
3979 error
3980 };
3981 }
3982 if (isRouteRequest) {
3983 // Note: This should only be non-Response values if we get here, since
3984 // isRouteRequest should throw any Response received in callLoaderOrAction
3985 if (isErrorResult(result)) {
3986 throw result.error;
3987 }
3988 return {
3989 matches: [actionMatch],
3990 loaderData: {},
3991 actionData: {
3992 [actionMatch.route.id]: result.data
3993 },
3994 errors: null,
3995 // Note: statusCode + headers are unused here since queryRoute will
3996 // return the raw Response or value
3997 statusCode: 200,
3998 loaderHeaders: {},
3999 actionHeaders: {},
4000 activeDeferreds: null
4001 };
4002 }
4003
4004 // Create a GET request for the loaders
4005 let loaderRequest = new Request(request.url, {
4006 headers: request.headers,
4007 redirect: request.redirect,
4008 signal: request.signal
4009 });
4010 if (isErrorResult(result)) {
4011 // Store off the pending error - we use it to determine which loaders
4012 // to call and will commit it when we complete the navigation
4013 let boundaryMatch = skipLoaderErrorBubbling ? actionMatch : findNearestBoundary(matches, actionMatch.route.id);
[0c6b92a]4014 let context = await loadRouteData(loaderRequest, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, null, [boundaryMatch.route.id, result]);
[d565449]4015
4016 // action status codes take precedence over loader status codes
4017 return _extends({}, context, {
4018 statusCode: isRouteErrorResponse(result.error) ? result.error.status : result.statusCode != null ? result.statusCode : 500,
4019 actionData: null,
4020 actionHeaders: _extends({}, result.headers ? {
4021 [actionMatch.route.id]: result.headers
4022 } : {})
4023 });
4024 }
[0c6b92a]4025 let context = await loadRouteData(loaderRequest, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, null);
[d565449]4026 return _extends({}, context, {
4027 actionData: {
4028 [actionMatch.route.id]: result.data
4029 }
4030 }, result.statusCode ? {
4031 statusCode: result.statusCode
4032 } : {}, {
4033 actionHeaders: result.headers ? {
4034 [actionMatch.route.id]: result.headers
4035 } : {}
4036 });
4037 }
[0c6b92a]4038 async function loadRouteData(request, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch, pendingActionResult) {
[d565449]4039 let isRouteRequest = routeMatch != null;
4040
4041 // Short circuit if we have no loaders to run (queryRoute())
4042 if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader) && !(routeMatch != null && routeMatch.route.lazy)) {
4043 throw getInternalRouterError(400, {
4044 method: request.method,
4045 pathname: new URL(request.url).pathname,
4046 routeId: routeMatch == null ? void 0 : routeMatch.route.id
4047 });
4048 }
4049 let requestMatches = routeMatch ? [routeMatch] : pendingActionResult && isErrorResult(pendingActionResult[1]) ? getLoaderMatchesUntilBoundary(matches, pendingActionResult[0]) : matches;
4050 let matchesToLoad = requestMatches.filter(m => m.route.loader || m.route.lazy);
4051
4052 // Short circuit if we have no loaders to run (query())
4053 if (matchesToLoad.length === 0) {
4054 return {
4055 matches,
4056 // Add a null for all matched routes for proper revalidation on the client
4057 loaderData: matches.reduce((acc, m) => Object.assign(acc, {
4058 [m.route.id]: null
4059 }), {}),
4060 errors: pendingActionResult && isErrorResult(pendingActionResult[1]) ? {
4061 [pendingActionResult[0]]: pendingActionResult[1].error
4062 } : null,
4063 statusCode: 200,
4064 loaderHeaders: {},
4065 activeDeferreds: null
4066 };
4067 }
[0c6b92a]4068 let results = await callDataStrategy("loader", request, matchesToLoad, matches, isRouteRequest, requestContext, dataStrategy);
[d565449]4069 if (request.signal.aborted) {
4070 throwStaticHandlerAbortedError(request, isRouteRequest, future);
4071 }
4072
4073 // Process and commit output from loaders
4074 let activeDeferreds = new Map();
[0c6b92a]4075 let context = processRouteLoaderData(matches, results, pendingActionResult, activeDeferreds, skipLoaderErrorBubbling);
[d565449]4076
4077 // Add a null for any non-loader matches for proper revalidation on the client
4078 let executedLoaders = new Set(matchesToLoad.map(match => match.route.id));
4079 matches.forEach(match => {
4080 if (!executedLoaders.has(match.route.id)) {
4081 context.loaderData[match.route.id] = null;
4082 }
4083 });
4084 return _extends({}, context, {
4085 matches,
4086 activeDeferreds: activeDeferreds.size > 0 ? Object.fromEntries(activeDeferreds.entries()) : null
4087 });
4088 }
4089
4090 // Utility wrapper for calling dataStrategy server-side without having to
4091 // pass around the manifest, mapRouteProperties, etc.
[0c6b92a]4092 async function callDataStrategy(type, request, matchesToLoad, matches, isRouteRequest, requestContext, dataStrategy) {
4093 let results = await callDataStrategyImpl(dataStrategy || defaultDataStrategy, type, null, request, matchesToLoad, matches, null, manifest, mapRouteProperties, requestContext);
4094 let dataResults = {};
4095 await Promise.all(matches.map(async match => {
4096 if (!(match.route.id in results)) {
4097 return;
4098 }
4099 let result = results[match.route.id];
4100 if (isRedirectDataStrategyResultResult(result)) {
[d565449]4101 let response = result.result;
4102 // Throw redirects and let the server handle them with an HTTP redirect
[0c6b92a]4103 throw normalizeRelativeRoutingRedirectResponse(response, request, match.route.id, matches, basename, future.v7_relativeSplatPath);
[d565449]4104 }
4105 if (isResponse(result.result) && isRouteRequest) {
4106 // For SSR single-route requests, we want to hand Responses back
4107 // directly without unwrapping
4108 throw result;
4109 }
[0c6b92a]4110 dataResults[match.route.id] = await convertDataStrategyResultToDataResult(result);
[d565449]4111 }));
[0c6b92a]4112 return dataResults;
[d565449]4113 }
4114 return {
4115 dataRoutes,
4116 query,
4117 queryRoute
4118 };
4119}
4120
4121//#endregion
4122
4123////////////////////////////////////////////////////////////////////////////////
4124//#region Helpers
4125////////////////////////////////////////////////////////////////////////////////
4126
4127/**
4128 * Given an existing StaticHandlerContext and an error thrown at render time,
4129 * provide an updated StaticHandlerContext suitable for a second SSR render
4130 */
4131function getStaticContextFromError(routes, context, error) {
4132 let newContext = _extends({}, context, {
4133 statusCode: isRouteErrorResponse(error) ? error.status : 500,
4134 errors: {
4135 [context._deepestRenderedBoundaryId || routes[0].id]: error
4136 }
4137 });
4138 return newContext;
4139}
4140function throwStaticHandlerAbortedError(request, isRouteRequest, future) {
4141 if (future.v7_throwAbortReason && request.signal.reason !== undefined) {
4142 throw request.signal.reason;
4143 }
4144 let method = isRouteRequest ? "queryRoute" : "query";
4145 throw new Error(method + "() call aborted: " + request.method + " " + request.url);
4146}
4147function isSubmissionNavigation(opts) {
4148 return opts != null && ("formData" in opts && opts.formData != null || "body" in opts && opts.body !== undefined);
4149}
4150function normalizeTo(location, matches, basename, prependBasename, to, v7_relativeSplatPath, fromRouteId, relative) {
4151 let contextualMatches;
4152 let activeRouteMatch;
4153 if (fromRouteId) {
4154 // Grab matches up to the calling route so our route-relative logic is
4155 // relative to the correct source route
4156 contextualMatches = [];
4157 for (let match of matches) {
4158 contextualMatches.push(match);
4159 if (match.route.id === fromRouteId) {
4160 activeRouteMatch = match;
4161 break;
4162 }
4163 }
4164 } else {
4165 contextualMatches = matches;
4166 activeRouteMatch = matches[matches.length - 1];
4167 }
4168
4169 // Resolve the relative path
4170 let path = resolveTo(to ? to : ".", getResolveToMatches(contextualMatches, v7_relativeSplatPath), stripBasename(location.pathname, basename) || location.pathname, relative === "path");
4171
4172 // When `to` is not specified we inherit search/hash from the current
4173 // location, unlike when to="." and we just inherit the path.
4174 // See https://github.com/remix-run/remix/issues/927
4175 if (to == null) {
4176 path.search = location.search;
4177 path.hash = location.hash;
4178 }
4179
[0c6b92a]4180 // Account for `?index` params when routing to the current location
4181 if ((to == null || to === "" || to === ".") && activeRouteMatch) {
4182 let nakedIndex = hasNakedIndexQuery(path.search);
4183 if (activeRouteMatch.route.index && !nakedIndex) {
4184 // Add one when we're targeting an index route
4185 path.search = path.search ? path.search.replace(/^\?/, "?index&") : "?index";
4186 } else if (!activeRouteMatch.route.index && nakedIndex) {
4187 // Remove existing ones when we're not
4188 let params = new URLSearchParams(path.search);
4189 let indexValues = params.getAll("index");
4190 params.delete("index");
4191 indexValues.filter(v => v).forEach(v => params.append("index", v));
4192 let qs = params.toString();
4193 path.search = qs ? "?" + qs : "";
4194 }
[d565449]4195 }
4196
4197 // If we're operating within a basename, prepend it to the pathname. If
4198 // this is a root navigation, then just use the raw basename which allows
4199 // the basename to have full control over the presence of a trailing slash
4200 // on root actions
4201 if (prependBasename && basename !== "/") {
4202 path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
4203 }
4204 return createPath(path);
4205}
4206
4207// Normalize navigation options by converting formMethod=GET formData objects to
4208// URLSearchParams so they behave identically to links with query params
4209function normalizeNavigateOptions(normalizeFormMethod, isFetcher, path, opts) {
4210 // Return location verbatim on non-submission navigations
4211 if (!opts || !isSubmissionNavigation(opts)) {
4212 return {
4213 path
4214 };
4215 }
4216 if (opts.formMethod && !isValidMethod(opts.formMethod)) {
4217 return {
4218 path,
4219 error: getInternalRouterError(405, {
4220 method: opts.formMethod
4221 })
4222 };
4223 }
4224 let getInvalidBodyError = () => ({
4225 path,
4226 error: getInternalRouterError(400, {
4227 type: "invalid-body"
4228 })
4229 });
4230
4231 // Create a Submission on non-GET navigations
4232 let rawFormMethod = opts.formMethod || "get";
4233 let formMethod = normalizeFormMethod ? rawFormMethod.toUpperCase() : rawFormMethod.toLowerCase();
4234 let formAction = stripHashFromPath(path);
4235 if (opts.body !== undefined) {
4236 if (opts.formEncType === "text/plain") {
4237 // text only support POST/PUT/PATCH/DELETE submissions
4238 if (!isMutationMethod(formMethod)) {
4239 return getInvalidBodyError();
4240 }
4241 let text = typeof opts.body === "string" ? opts.body : opts.body instanceof FormData || opts.body instanceof URLSearchParams ?
4242 // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#plain-text-form-data
[0c6b92a]4243 Array.from(opts.body.entries()).reduce((acc, _ref3) => {
4244 let [name, value] = _ref3;
[d565449]4245 return "" + acc + name + "=" + value + "\n";
4246 }, "") : String(opts.body);
4247 return {
4248 path,
4249 submission: {
4250 formMethod,
4251 formAction,
4252 formEncType: opts.formEncType,
4253 formData: undefined,
4254 json: undefined,
4255 text
4256 }
4257 };
4258 } else if (opts.formEncType === "application/json") {
4259 // json only supports POST/PUT/PATCH/DELETE submissions
4260 if (!isMutationMethod(formMethod)) {
4261 return getInvalidBodyError();
4262 }
4263 try {
4264 let json = typeof opts.body === "string" ? JSON.parse(opts.body) : opts.body;
4265 return {
4266 path,
4267 submission: {
4268 formMethod,
4269 formAction,
4270 formEncType: opts.formEncType,
4271 formData: undefined,
4272 json,
4273 text: undefined
4274 }
4275 };
4276 } catch (e) {
4277 return getInvalidBodyError();
4278 }
4279 }
4280 }
4281 invariant(typeof FormData === "function", "FormData is not available in this environment");
4282 let searchParams;
4283 let formData;
4284 if (opts.formData) {
4285 searchParams = convertFormDataToSearchParams(opts.formData);
4286 formData = opts.formData;
4287 } else if (opts.body instanceof FormData) {
4288 searchParams = convertFormDataToSearchParams(opts.body);
4289 formData = opts.body;
4290 } else if (opts.body instanceof URLSearchParams) {
4291 searchParams = opts.body;
4292 formData = convertSearchParamsToFormData(searchParams);
4293 } else if (opts.body == null) {
4294 searchParams = new URLSearchParams();
4295 formData = new FormData();
4296 } else {
4297 try {
4298 searchParams = new URLSearchParams(opts.body);
4299 formData = convertSearchParamsToFormData(searchParams);
4300 } catch (e) {
4301 return getInvalidBodyError();
4302 }
4303 }
4304 let submission = {
4305 formMethod,
4306 formAction,
4307 formEncType: opts && opts.formEncType || "application/x-www-form-urlencoded",
4308 formData,
4309 json: undefined,
4310 text: undefined
4311 };
4312 if (isMutationMethod(submission.formMethod)) {
4313 return {
4314 path,
4315 submission
4316 };
4317 }
4318
4319 // Flatten submission onto URLSearchParams for GET submissions
4320 let parsedPath = parsePath(path);
4321 // On GET navigation submissions we can drop the ?index param from the
4322 // resulting location since all loaders will run. But fetcher GET submissions
4323 // only run a single loader so we need to preserve any incoming ?index params
4324 if (isFetcher && parsedPath.search && hasNakedIndexQuery(parsedPath.search)) {
4325 searchParams.append("index", "");
4326 }
4327 parsedPath.search = "?" + searchParams;
4328 return {
4329 path: createPath(parsedPath),
4330 submission
4331 };
4332}
4333
[0c6b92a]4334// Filter out all routes at/below any caught error as they aren't going to
[d565449]4335// render so we don't need to load them
[0c6b92a]4336function getLoaderMatchesUntilBoundary(matches, boundaryId, includeBoundary) {
4337 if (includeBoundary === void 0) {
4338 includeBoundary = false;
4339 }
4340 let index = matches.findIndex(m => m.route.id === boundaryId);
4341 if (index >= 0) {
4342 return matches.slice(0, includeBoundary ? index + 1 : index);
[d565449]4343 }
[0c6b92a]4344 return matches;
[d565449]4345}
[0c6b92a]4346function getMatchesToLoad(history, state, matches, submission, location, initialHydration, skipActionErrorRevalidation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, deletedFetchers, fetchLoadMatches, fetchRedirectIds, routesToUse, basename, pendingActionResult) {
[d565449]4347 let actionResult = pendingActionResult ? isErrorResult(pendingActionResult[1]) ? pendingActionResult[1].error : pendingActionResult[1].data : undefined;
4348 let currentUrl = history.createURL(state.location);
4349 let nextUrl = history.createURL(location);
4350
4351 // Pick navigation matches that are net-new or qualify for revalidation
[0c6b92a]4352 let boundaryMatches = matches;
4353 if (initialHydration && state.errors) {
4354 // On initial hydration, only consider matches up to _and including_ the boundary.
4355 // This is inclusive to handle cases where a server loader ran successfully,
4356 // a child server loader bubbled up to this route, but this route has
4357 // `clientLoader.hydrate` so we want to still run the `clientLoader` so that
4358 // we have a complete version of `loaderData`
4359 boundaryMatches = getLoaderMatchesUntilBoundary(matches, Object.keys(state.errors)[0], true);
4360 } else if (pendingActionResult && isErrorResult(pendingActionResult[1])) {
4361 // If an action threw an error, we call loaders up to, but not including the
4362 // boundary
4363 boundaryMatches = getLoaderMatchesUntilBoundary(matches, pendingActionResult[0]);
4364 }
[d565449]4365
4366 // Don't revalidate loaders by default after action 4xx/5xx responses
4367 // when the flag is enabled. They can still opt-into revalidation via
4368 // `shouldRevalidate` via `actionResult`
4369 let actionStatus = pendingActionResult ? pendingActionResult[1].statusCode : undefined;
4370 let shouldSkipRevalidation = skipActionErrorRevalidation && actionStatus && actionStatus >= 400;
4371 let navigationMatches = boundaryMatches.filter((match, index) => {
4372 let {
4373 route
4374 } = match;
4375 if (route.lazy) {
4376 // We haven't loaded this route yet so we don't know if it's got a loader!
4377 return true;
4378 }
4379 if (route.loader == null) {
4380 return false;
4381 }
[0c6b92a]4382 if (initialHydration) {
4383 return shouldLoadRouteOnHydration(route, state.loaderData, state.errors);
[d565449]4384 }
4385
4386 // Always call the loader on new route instances and pending defer cancellations
4387 if (isNewLoader(state.loaderData, state.matches[index], match) || cancelledDeferredRoutes.some(id => id === match.route.id)) {
4388 return true;
4389 }
4390
4391 // This is the default implementation for when we revalidate. If the route
4392 // provides it's own implementation, then we give them full control but
4393 // provide this value so they can leverage it if needed after they check
4394 // their own specific use cases
4395 let currentRouteMatch = state.matches[index];
4396 let nextRouteMatch = match;
4397 return shouldRevalidateLoader(match, _extends({
4398 currentUrl,
4399 currentParams: currentRouteMatch.params,
4400 nextUrl,
4401 nextParams: nextRouteMatch.params
4402 }, submission, {
4403 actionResult,
4404 actionStatus,
4405 defaultShouldRevalidate: shouldSkipRevalidation ? false :
4406 // Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate
4407 isRevalidationRequired || currentUrl.pathname + currentUrl.search === nextUrl.pathname + nextUrl.search ||
4408 // Search params affect all loaders
4409 currentUrl.search !== nextUrl.search || isNewRouteInstance(currentRouteMatch, nextRouteMatch)
4410 }));
4411 });
4412
4413 // Pick fetcher.loads that need to be revalidated
4414 let revalidatingFetchers = [];
4415 fetchLoadMatches.forEach((f, key) => {
4416 // Don't revalidate:
[0c6b92a]4417 // - on initial hydration (shouldn't be any fetchers then anyway)
[d565449]4418 // - if fetcher won't be present in the subsequent render
4419 // - no longer matches the URL (v7_fetcherPersist=false)
4420 // - was unmounted but persisted due to v7_fetcherPersist=true
[0c6b92a]4421 if (initialHydration || !matches.some(m => m.route.id === f.routeId) || deletedFetchers.has(key)) {
[d565449]4422 return;
4423 }
4424 let fetcherMatches = matchRoutes(routesToUse, f.path, basename);
4425
4426 // If the fetcher path no longer matches, push it in with null matches so
4427 // we can trigger a 404 in callLoadersAndMaybeResolveData. Note this is
4428 // currently only a use-case for Remix HMR where the route tree can change
4429 // at runtime and remove a route previously loaded via a fetcher
4430 if (!fetcherMatches) {
4431 revalidatingFetchers.push({
4432 key,
4433 routeId: f.routeId,
4434 path: f.path,
4435 matches: null,
4436 match: null,
4437 controller: null
4438 });
4439 return;
4440 }
4441
4442 // Revalidating fetchers are decoupled from the route matches since they
4443 // load from a static href. They revalidate based on explicit revalidation
4444 // (submission, useRevalidator, or X-Remix-Revalidate)
4445 let fetcher = state.fetchers.get(key);
4446 let fetcherMatch = getTargetMatch(fetcherMatches, f.path);
4447 let shouldRevalidate = false;
4448 if (fetchRedirectIds.has(key)) {
4449 // Never trigger a revalidation of an actively redirecting fetcher
4450 shouldRevalidate = false;
4451 } else if (cancelledFetcherLoads.has(key)) {
4452 // Always mark for revalidation if the fetcher was cancelled
4453 cancelledFetcherLoads.delete(key);
4454 shouldRevalidate = true;
4455 } else if (fetcher && fetcher.state !== "idle" && fetcher.data === undefined) {
4456 // If the fetcher hasn't ever completed loading yet, then this isn't a
4457 // revalidation, it would just be a brand new load if an explicit
4458 // revalidation is required
4459 shouldRevalidate = isRevalidationRequired;
4460 } else {
4461 // Otherwise fall back on any user-defined shouldRevalidate, defaulting
4462 // to explicit revalidations only
4463 shouldRevalidate = shouldRevalidateLoader(fetcherMatch, _extends({
4464 currentUrl,
4465 currentParams: state.matches[state.matches.length - 1].params,
4466 nextUrl,
4467 nextParams: matches[matches.length - 1].params
4468 }, submission, {
4469 actionResult,
4470 actionStatus,
4471 defaultShouldRevalidate: shouldSkipRevalidation ? false : isRevalidationRequired
4472 }));
4473 }
4474 if (shouldRevalidate) {
4475 revalidatingFetchers.push({
4476 key,
4477 routeId: f.routeId,
4478 path: f.path,
4479 matches: fetcherMatches,
4480 match: fetcherMatch,
4481 controller: new AbortController()
4482 });
4483 }
4484 });
4485 return [navigationMatches, revalidatingFetchers];
4486}
[0c6b92a]4487function shouldLoadRouteOnHydration(route, loaderData, errors) {
4488 // We dunno if we have a loader - gotta find out!
4489 if (route.lazy) {
4490 return true;
4491 }
4492
4493 // No loader, nothing to initialize
4494 if (!route.loader) {
4495 return false;
4496 }
4497 let hasData = loaderData != null && loaderData[route.id] !== undefined;
4498 let hasError = errors != null && errors[route.id] !== undefined;
4499
4500 // Don't run if we error'd during SSR
4501 if (!hasData && hasError) {
4502 return false;
4503 }
4504
4505 // Explicitly opting-in to running on hydration
4506 if (typeof route.loader === "function" && route.loader.hydrate === true) {
4507 return true;
4508 }
4509
4510 // Otherwise, run if we're not yet initialized with anything
4511 return !hasData && !hasError;
4512}
[d565449]4513function isNewLoader(currentLoaderData, currentMatch, match) {
4514 let isNew =
4515 // [a] -> [a, b]
4516 !currentMatch ||
4517 // [a, b] -> [a, c]
4518 match.route.id !== currentMatch.route.id;
4519
4520 // Handle the case that we don't have data for a re-used route, potentially
4521 // from a prior error or from a cancelled pending deferred
4522 let isMissingData = currentLoaderData[match.route.id] === undefined;
4523
4524 // Always load if this is a net-new route or we don't yet have data
4525 return isNew || isMissingData;
4526}
4527function isNewRouteInstance(currentMatch, match) {
4528 let currentPath = currentMatch.route.path;
4529 return (
4530 // param change for this match, /users/123 -> /users/456
4531 currentMatch.pathname !== match.pathname ||
4532 // splat param changed, which is not present in match.path
4533 // e.g. /files/images/avatar.jpg -> files/finances.xls
4534 currentPath != null && currentPath.endsWith("*") && currentMatch.params["*"] !== match.params["*"]
4535 );
4536}
4537function shouldRevalidateLoader(loaderMatch, arg) {
4538 if (loaderMatch.route.shouldRevalidate) {
4539 let routeChoice = loaderMatch.route.shouldRevalidate(arg);
4540 if (typeof routeChoice === "boolean") {
4541 return routeChoice;
4542 }
4543 }
4544 return arg.defaultShouldRevalidate;
4545}
4546function patchRoutesImpl(routeId, children, routesToUse, manifest, mapRouteProperties) {
[0c6b92a]4547 var _childrenToPatch;
4548 let childrenToPatch;
[d565449]4549 if (routeId) {
4550 let route = manifest[routeId];
4551 invariant(route, "No route found to patch children into: routeId = " + routeId);
[0c6b92a]4552 if (!route.children) {
4553 route.children = [];
[d565449]4554 }
[0c6b92a]4555 childrenToPatch = route.children;
[d565449]4556 } else {
[0c6b92a]4557 childrenToPatch = routesToUse;
[d565449]4558 }
[0c6b92a]4559
4560 // Don't patch in routes we already know about so that `patch` is idempotent
4561 // to simplify user-land code. This is useful because we re-call the
4562 // `patchRoutesOnNavigation` function for matched routes with params.
4563 let uniqueChildren = children.filter(newRoute => !childrenToPatch.some(existingRoute => isSameRoute(newRoute, existingRoute)));
4564 let newRoutes = convertRoutesToDataRoutes(uniqueChildren, mapRouteProperties, [routeId || "_", "patch", String(((_childrenToPatch = childrenToPatch) == null ? void 0 : _childrenToPatch.length) || "0")], manifest);
4565 childrenToPatch.push(...newRoutes);
4566}
4567function isSameRoute(newRoute, existingRoute) {
4568 // Most optimal check is by id
4569 if ("id" in newRoute && "id" in existingRoute && newRoute.id === existingRoute.id) {
4570 return true;
4571 }
4572
4573 // Second is by pathing differences
4574 if (!(newRoute.index === existingRoute.index && newRoute.path === existingRoute.path && newRoute.caseSensitive === existingRoute.caseSensitive)) {
4575 return false;
4576 }
4577
4578 // Pathless layout routes are trickier since we need to check children.
4579 // If they have no children then they're the same as far as we can tell
4580 if ((!newRoute.children || newRoute.children.length === 0) && (!existingRoute.children || existingRoute.children.length === 0)) {
4581 return true;
4582 }
4583
4584 // Otherwise, we look to see if every child in the new route is already
4585 // represented in the existing route's children
4586 return newRoute.children.every((aChild, i) => {
4587 var _existingRoute$childr;
4588 return (_existingRoute$childr = existingRoute.children) == null ? void 0 : _existingRoute$childr.some(bChild => isSameRoute(aChild, bChild));
4589 });
[d565449]4590}
4591
4592/**
4593 * Execute route.lazy() methods to lazily load route modules (loader, action,
4594 * shouldRevalidate) and update the routeManifest in place which shares objects
4595 * with dataRoutes so those get updated as well.
4596 */
4597async function loadLazyRouteModule(route, mapRouteProperties, manifest) {
4598 if (!route.lazy) {
4599 return;
4600 }
4601 let lazyRoute = await route.lazy();
4602
4603 // If the lazy route function was executed and removed by another parallel
4604 // call then we can return - first lazy() to finish wins because the return
4605 // value of lazy is expected to be static
4606 if (!route.lazy) {
4607 return;
4608 }
4609 let routeToUpdate = manifest[route.id];
4610 invariant(routeToUpdate, "No route found in manifest");
4611
4612 // Update the route in place. This should be safe because there's no way
4613 // we could yet be sitting on this route as we can't get there without
4614 // resolving lazy() first.
4615 //
4616 // This is different than the HMR "update" use-case where we may actively be
4617 // on the route being updated. The main concern boils down to "does this
4618 // mutation affect any ongoing navigations or any current state.matches
4619 // values?". If not, it should be safe to update in place.
4620 let routeUpdates = {};
4621 for (let lazyRouteProperty in lazyRoute) {
4622 let staticRouteValue = routeToUpdate[lazyRouteProperty];
4623 let isPropertyStaticallyDefined = staticRouteValue !== undefined &&
4624 // This property isn't static since it should always be updated based
4625 // on the route updates
4626 lazyRouteProperty !== "hasErrorBoundary";
4627 warning(!isPropertyStaticallyDefined, "Route \"" + routeToUpdate.id + "\" has a static property \"" + lazyRouteProperty + "\" " + "defined but its lazy function is also returning a value for this property. " + ("The lazy route property \"" + lazyRouteProperty + "\" will be ignored."));
4628 if (!isPropertyStaticallyDefined && !immutableRouteKeys.has(lazyRouteProperty)) {
4629 routeUpdates[lazyRouteProperty] = lazyRoute[lazyRouteProperty];
4630 }
4631 }
4632
4633 // Mutate the route with the provided updates. Do this first so we pass
4634 // the updated version to mapRouteProperties
4635 Object.assign(routeToUpdate, routeUpdates);
4636
4637 // Mutate the `hasErrorBoundary` property on the route based on the route
4638 // updates and remove the `lazy` function so we don't resolve the lazy
4639 // route again.
4640 Object.assign(routeToUpdate, _extends({}, mapRouteProperties(routeToUpdate), {
4641 lazy: undefined
4642 }));
4643}
4644
4645// Default implementation of `dataStrategy` which fetches all loaders in parallel
[0c6b92a]4646async function defaultDataStrategy(_ref4) {
4647 let {
4648 matches
4649 } = _ref4;
4650 let matchesToLoad = matches.filter(m => m.shouldLoad);
4651 let results = await Promise.all(matchesToLoad.map(m => m.resolve()));
4652 return results.reduce((acc, result, i) => Object.assign(acc, {
4653 [matchesToLoad[i].route.id]: result
4654 }), {});
[d565449]4655}
[0c6b92a]4656async function callDataStrategyImpl(dataStrategyImpl, type, state, request, matchesToLoad, matches, fetcherKey, manifest, mapRouteProperties, requestContext) {
4657 let loadRouteDefinitionsPromises = matches.map(m => m.route.lazy ? loadLazyRouteModule(m.route, mapRouteProperties, manifest) : undefined);
4658 let dsMatches = matches.map((match, i) => {
4659 let loadRoutePromise = loadRouteDefinitionsPromises[i];
4660 let shouldLoad = matchesToLoad.some(m => m.route.id === match.route.id);
4661 // `resolve` encapsulates route.lazy(), executing the loader/action,
4662 // and mapping return values/thrown errors to a `DataStrategyResult`. Users
4663 // can pass a callback to take fine-grained control over the execution
4664 // of the loader/action
4665 let resolve = async handlerOverride => {
4666 if (handlerOverride && request.method === "GET" && (match.route.lazy || match.route.loader)) {
4667 shouldLoad = true;
4668 }
4669 return shouldLoad ? callLoaderOrAction(type, request, match, loadRoutePromise, handlerOverride, requestContext) : Promise.resolve({
4670 type: ResultType.data,
4671 result: undefined
4672 });
4673 };
4674 return _extends({}, match, {
4675 shouldLoad,
4676 resolve
4677 });
4678 });
[d565449]4679
4680 // Send all matches here to allow for a middleware-type implementation.
4681 // handler will be a no-op for unneeded routes and we filter those results
4682 // back out below.
4683 let results = await dataStrategyImpl({
[0c6b92a]4684 matches: dsMatches,
[d565449]4685 request,
4686 params: matches[0].params,
[0c6b92a]4687 fetcherKey,
[d565449]4688 context: requestContext
4689 });
4690
[0c6b92a]4691 // Wait for all routes to load here but 'swallow the error since we want
4692 // it to bubble up from the `await loadRoutePromise` in `callLoaderOrAction` -
4693 // called from `match.resolve()`
4694 try {
4695 await Promise.all(loadRouteDefinitionsPromises);
4696 } catch (e) {
4697 // No-op
4698 }
4699 return results;
[d565449]4700}
4701
4702// Default logic for calling a loader/action is the user has no specified a dataStrategy
[0c6b92a]4703async function callLoaderOrAction(type, request, match, loadRoutePromise, handlerOverride, staticContext) {
[d565449]4704 let result;
4705 let onReject;
4706 let runHandler = handler => {
4707 // Setup a promise we can race against so that abort signals short circuit
4708 let reject;
[0c6b92a]4709 // This will never resolve so safe to type it as Promise<DataStrategyResult> to
[d565449]4710 // satisfy the function return value
4711 let abortPromise = new Promise((_, r) => reject = r);
4712 onReject = () => reject();
4713 request.signal.addEventListener("abort", onReject);
4714 let actualHandler = ctx => {
4715 if (typeof handler !== "function") {
4716 return Promise.reject(new Error("You cannot call the handler for a route which defines a boolean " + ("\"" + type + "\" [routeId: " + match.route.id + "]")));
4717 }
4718 return handler({
4719 request,
4720 params: match.params,
4721 context: staticContext
4722 }, ...(ctx !== undefined ? [ctx] : []));
4723 };
[0c6b92a]4724 let handlerPromise = (async () => {
4725 try {
4726 let val = await (handlerOverride ? handlerOverride(ctx => actualHandler(ctx)) : actualHandler());
4727 return {
4728 type: "data",
4729 result: val
4730 };
4731 } catch (e) {
4732 return {
4733 type: "error",
4734 result: e
4735 };
4736 }
4737 })();
[d565449]4738 return Promise.race([handlerPromise, abortPromise]);
4739 };
4740 try {
4741 let handler = match.route[type];
[0c6b92a]4742
4743 // If we have a route.lazy promise, await that first
4744 if (loadRoutePromise) {
[d565449]4745 if (handler) {
4746 // Run statically defined handler in parallel with lazy()
4747 let handlerError;
4748 let [value] = await Promise.all([
4749 // If the handler throws, don't let it immediately bubble out,
4750 // since we need to let the lazy() execution finish so we know if this
4751 // route has a boundary that can handle the error
4752 runHandler(handler).catch(e => {
4753 handlerError = e;
[0c6b92a]4754 }), loadRoutePromise]);
[d565449]4755 if (handlerError !== undefined) {
4756 throw handlerError;
4757 }
4758 result = value;
4759 } else {
4760 // Load lazy route module, then run any returned handler
[0c6b92a]4761 await loadRoutePromise;
[d565449]4762 handler = match.route[type];
4763 if (handler) {
4764 // Handler still runs even if we got interrupted to maintain consistency
4765 // with un-abortable behavior of handler execution on non-lazy or
4766 // previously-lazy-loaded routes
4767 result = await runHandler(handler);
4768 } else if (type === "action") {
4769 let url = new URL(request.url);
4770 let pathname = url.pathname + url.search;
4771 throw getInternalRouterError(405, {
4772 method: request.method,
4773 pathname,
4774 routeId: match.route.id
4775 });
4776 } else {
4777 // lazy() route has no loader to run. Short circuit here so we don't
4778 // hit the invariant below that errors on returning undefined.
4779 return {
4780 type: ResultType.data,
4781 result: undefined
4782 };
4783 }
4784 }
4785 } else if (!handler) {
4786 let url = new URL(request.url);
4787 let pathname = url.pathname + url.search;
4788 throw getInternalRouterError(404, {
4789 pathname
4790 });
4791 } else {
4792 result = await runHandler(handler);
4793 }
4794 invariant(result.result !== undefined, "You defined " + (type === "action" ? "an action" : "a loader") + " for route " + ("\"" + match.route.id + "\" but didn't return anything from your `" + type + "` ") + "function. Please return a value or `null`.");
4795 } catch (e) {
4796 // We should already be catching and converting normal handler executions to
[0c6b92a]4797 // DataStrategyResults and returning them, so anything that throws here is an
[d565449]4798 // unexpected error we still need to wrap
4799 return {
4800 type: ResultType.error,
4801 result: e
4802 };
4803 } finally {
4804 if (onReject) {
4805 request.signal.removeEventListener("abort", onReject);
4806 }
4807 }
4808 return result;
4809}
[0c6b92a]4810async function convertDataStrategyResultToDataResult(dataStrategyResult) {
[d565449]4811 let {
4812 result,
4813 type
[0c6b92a]4814 } = dataStrategyResult;
[d565449]4815 if (isResponse(result)) {
4816 let data;
4817 try {
4818 let contentType = result.headers.get("Content-Type");
4819 // Check between word boundaries instead of startsWith() due to the last
4820 // paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
4821 if (contentType && /\bapplication\/json\b/.test(contentType)) {
4822 if (result.body == null) {
4823 data = null;
4824 } else {
4825 data = await result.json();
4826 }
4827 } else {
4828 data = await result.text();
4829 }
4830 } catch (e) {
4831 return {
4832 type: ResultType.error,
4833 error: e
4834 };
4835 }
4836 if (type === ResultType.error) {
4837 return {
4838 type: ResultType.error,
4839 error: new ErrorResponseImpl(result.status, result.statusText, data),
4840 statusCode: result.status,
4841 headers: result.headers
4842 };
4843 }
4844 return {
4845 type: ResultType.data,
4846 data,
4847 statusCode: result.status,
4848 headers: result.headers
4849 };
4850 }
4851 if (type === ResultType.error) {
4852 if (isDataWithResponseInit(result)) {
4853 var _result$init2;
4854 if (result.data instanceof Error) {
4855 var _result$init;
4856 return {
4857 type: ResultType.error,
4858 error: result.data,
4859 statusCode: (_result$init = result.init) == null ? void 0 : _result$init.status
4860 };
4861 }
4862
[0c6b92a]4863 // Convert thrown data() to ErrorResponse instances
[d565449]4864 result = new ErrorResponseImpl(((_result$init2 = result.init) == null ? void 0 : _result$init2.status) || 500, undefined, result.data);
4865 }
4866 return {
4867 type: ResultType.error,
4868 error: result,
4869 statusCode: isRouteErrorResponse(result) ? result.status : undefined
4870 };
4871 }
4872 if (isDeferredData(result)) {
4873 var _result$init3, _result$init4;
4874 return {
4875 type: ResultType.deferred,
4876 deferredData: result,
4877 statusCode: (_result$init3 = result.init) == null ? void 0 : _result$init3.status,
4878 headers: ((_result$init4 = result.init) == null ? void 0 : _result$init4.headers) && new Headers(result.init.headers)
4879 };
4880 }
4881 if (isDataWithResponseInit(result)) {
4882 var _result$init5, _result$init6;
4883 return {
4884 type: ResultType.data,
4885 data: result.data,
4886 statusCode: (_result$init5 = result.init) == null ? void 0 : _result$init5.status,
4887 headers: (_result$init6 = result.init) != null && _result$init6.headers ? new Headers(result.init.headers) : undefined
4888 };
4889 }
4890 return {
4891 type: ResultType.data,
4892 data: result
4893 };
4894}
4895
4896// Support relative routing in internal redirects
4897function normalizeRelativeRoutingRedirectResponse(response, request, routeId, matches, basename, v7_relativeSplatPath) {
4898 let location = response.headers.get("Location");
4899 invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header");
4900 if (!ABSOLUTE_URL_REGEX.test(location)) {
4901 let trimmedMatches = matches.slice(0, matches.findIndex(m => m.route.id === routeId) + 1);
4902 location = normalizeTo(new URL(request.url), trimmedMatches, basename, true, location, v7_relativeSplatPath);
4903 response.headers.set("Location", location);
4904 }
4905 return response;
4906}
4907function normalizeRedirectLocation(location, currentUrl, basename) {
4908 if (ABSOLUTE_URL_REGEX.test(location)) {
4909 // Strip off the protocol+origin for same-origin + same-basename absolute redirects
4910 let normalizedLocation = location;
4911 let url = normalizedLocation.startsWith("//") ? new URL(currentUrl.protocol + normalizedLocation) : new URL(normalizedLocation);
4912 let isSameBasename = stripBasename(url.pathname, basename) != null;
4913 if (url.origin === currentUrl.origin && isSameBasename) {
4914 return url.pathname + url.search + url.hash;
4915 }
4916 }
4917 return location;
4918}
4919
4920// Utility method for creating the Request instances for loaders/actions during
4921// client-side navigations and fetches. During SSR we will always have a
4922// Request instance from the static handler (query/queryRoute)
4923function createClientSideRequest(history, location, signal, submission) {
4924 let url = history.createURL(stripHashFromPath(location)).toString();
4925 let init = {
4926 signal
4927 };
4928 if (submission && isMutationMethod(submission.formMethod)) {
4929 let {
4930 formMethod,
4931 formEncType
4932 } = submission;
4933 // Didn't think we needed this but it turns out unlike other methods, patch
4934 // won't be properly normalized to uppercase and results in a 405 error.
4935 // See: https://fetch.spec.whatwg.org/#concept-method
4936 init.method = formMethod.toUpperCase();
4937 if (formEncType === "application/json") {
4938 init.headers = new Headers({
4939 "Content-Type": formEncType
4940 });
4941 init.body = JSON.stringify(submission.json);
4942 } else if (formEncType === "text/plain") {
4943 // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
4944 init.body = submission.text;
4945 } else if (formEncType === "application/x-www-form-urlencoded" && submission.formData) {
4946 // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
4947 init.body = convertFormDataToSearchParams(submission.formData);
4948 } else {
4949 // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
4950 init.body = submission.formData;
4951 }
4952 }
4953 return new Request(url, init);
4954}
4955function convertFormDataToSearchParams(formData) {
4956 let searchParams = new URLSearchParams();
4957 for (let [key, value] of formData.entries()) {
4958 // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#converting-an-entry-list-to-a-list-of-name-value-pairs
4959 searchParams.append(key, typeof value === "string" ? value : value.name);
4960 }
4961 return searchParams;
4962}
4963function convertSearchParamsToFormData(searchParams) {
4964 let formData = new FormData();
4965 for (let [key, value] of searchParams.entries()) {
4966 formData.append(key, value);
4967 }
4968 return formData;
4969}
[0c6b92a]4970function processRouteLoaderData(matches, results, pendingActionResult, activeDeferreds, skipLoaderErrorBubbling) {
[d565449]4971 // Fill in loaderData/errors from our loaders
4972 let loaderData = {};
4973 let errors = null;
4974 let statusCode;
4975 let foundError = false;
4976 let loaderHeaders = {};
4977 let pendingError = pendingActionResult && isErrorResult(pendingActionResult[1]) ? pendingActionResult[1].error : undefined;
4978
4979 // Process loader results into state.loaderData/state.errors
[0c6b92a]4980 matches.forEach(match => {
4981 if (!(match.route.id in results)) {
4982 return;
4983 }
4984 let id = match.route.id;
4985 let result = results[id];
[d565449]4986 invariant(!isRedirectResult(result), "Cannot handle redirect results in processLoaderData");
4987 if (isErrorResult(result)) {
4988 let error = result.error;
4989 // If we have a pending action error, we report it at the highest-route
4990 // that throws a loader error, and then clear it out to indicate that
4991 // it was consumed
4992 if (pendingError !== undefined) {
4993 error = pendingError;
4994 pendingError = undefined;
4995 }
4996 errors = errors || {};
4997 if (skipLoaderErrorBubbling) {
4998 errors[id] = error;
4999 } else {
5000 // Look upwards from the matched route for the closest ancestor error
5001 // boundary, defaulting to the root match. Prefer higher error values
5002 // if lower errors bubble to the same boundary
5003 let boundaryMatch = findNearestBoundary(matches, id);
5004 if (errors[boundaryMatch.route.id] == null) {
5005 errors[boundaryMatch.route.id] = error;
5006 }
5007 }
5008
5009 // Clear our any prior loaderData for the throwing route
5010 loaderData[id] = undefined;
5011
5012 // Once we find our first (highest) error, we set the status code and
5013 // prevent deeper status codes from overriding
5014 if (!foundError) {
5015 foundError = true;
5016 statusCode = isRouteErrorResponse(result.error) ? result.error.status : 500;
5017 }
5018 if (result.headers) {
5019 loaderHeaders[id] = result.headers;
5020 }
5021 } else {
5022 if (isDeferredResult(result)) {
5023 activeDeferreds.set(id, result.deferredData);
5024 loaderData[id] = result.deferredData.data;
5025 // Error status codes always override success status codes, but if all
5026 // loaders are successful we take the deepest status code.
5027 if (result.statusCode != null && result.statusCode !== 200 && !foundError) {
5028 statusCode = result.statusCode;
5029 }
5030 if (result.headers) {
5031 loaderHeaders[id] = result.headers;
5032 }
5033 } else {
5034 loaderData[id] = result.data;
5035 // Error status codes always override success status codes, but if all
5036 // loaders are successful we take the deepest status code.
5037 if (result.statusCode && result.statusCode !== 200 && !foundError) {
5038 statusCode = result.statusCode;
5039 }
5040 if (result.headers) {
5041 loaderHeaders[id] = result.headers;
5042 }
5043 }
5044 }
5045 });
5046
5047 // If we didn't consume the pending action error (i.e., all loaders
5048 // resolved), then consume it here. Also clear out any loaderData for the
5049 // throwing route
5050 if (pendingError !== undefined && pendingActionResult) {
5051 errors = {
5052 [pendingActionResult[0]]: pendingError
5053 };
5054 loaderData[pendingActionResult[0]] = undefined;
5055 }
5056 return {
5057 loaderData,
5058 errors,
5059 statusCode: statusCode || 200,
5060 loaderHeaders
5061 };
5062}
[0c6b92a]5063function processLoaderData(state, matches, results, pendingActionResult, revalidatingFetchers, fetcherResults, activeDeferreds) {
[d565449]5064 let {
5065 loaderData,
5066 errors
[0c6b92a]5067 } = processRouteLoaderData(matches, results, pendingActionResult, activeDeferreds, false // This method is only called client side so we always want to bubble
[d565449]5068 );
5069
5070 // Process results from our revalidating fetchers
[0c6b92a]5071 revalidatingFetchers.forEach(rf => {
[d565449]5072 let {
5073 key,
5074 match,
5075 controller
[0c6b92a]5076 } = rf;
5077 let result = fetcherResults[key];
5078 invariant(result, "Did not find corresponding fetcher result");
[d565449]5079
5080 // Process fetcher non-redirect errors
5081 if (controller && controller.signal.aborted) {
5082 // Nothing to do for aborted fetchers
[0c6b92a]5083 return;
[d565449]5084 } else if (isErrorResult(result)) {
5085 let boundaryMatch = findNearestBoundary(state.matches, match == null ? void 0 : match.route.id);
5086 if (!(errors && errors[boundaryMatch.route.id])) {
5087 errors = _extends({}, errors, {
5088 [boundaryMatch.route.id]: result.error
5089 });
5090 }
5091 state.fetchers.delete(key);
5092 } else if (isRedirectResult(result)) {
5093 // Should never get here, redirects should get processed above, but we
5094 // keep this to type narrow to a success result in the else
5095 invariant(false, "Unhandled fetcher revalidation redirect");
5096 } else if (isDeferredResult(result)) {
5097 // Should never get here, deferred data should be awaited for fetchers
5098 // in resolveDeferredResults
5099 invariant(false, "Unhandled fetcher deferred data");
5100 } else {
5101 let doneFetcher = getDoneFetcher(result.data);
5102 state.fetchers.set(key, doneFetcher);
5103 }
[0c6b92a]5104 });
[d565449]5105 return {
5106 loaderData,
5107 errors
5108 };
5109}
5110function mergeLoaderData(loaderData, newLoaderData, matches, errors) {
5111 let mergedLoaderData = _extends({}, newLoaderData);
5112 for (let match of matches) {
5113 let id = match.route.id;
5114 if (newLoaderData.hasOwnProperty(id)) {
5115 if (newLoaderData[id] !== undefined) {
5116 mergedLoaderData[id] = newLoaderData[id];
5117 }
5118 } else if (loaderData[id] !== undefined && match.route.loader) {
5119 // Preserve existing keys not included in newLoaderData and where a loader
5120 // wasn't removed by HMR
5121 mergedLoaderData[id] = loaderData[id];
5122 }
5123 if (errors && errors.hasOwnProperty(id)) {
5124 // Don't keep any loader data below the boundary
5125 break;
5126 }
5127 }
5128 return mergedLoaderData;
5129}
5130function getActionDataForCommit(pendingActionResult) {
5131 if (!pendingActionResult) {
5132 return {};
5133 }
5134 return isErrorResult(pendingActionResult[1]) ? {
5135 // Clear out prior actionData on errors
5136 actionData: {}
5137 } : {
5138 actionData: {
5139 [pendingActionResult[0]]: pendingActionResult[1].data
5140 }
5141 };
5142}
5143
5144// Find the nearest error boundary, looking upwards from the leaf route (or the
5145// route specified by routeId) for the closest ancestor error boundary,
5146// defaulting to the root match
5147function findNearestBoundary(matches, routeId) {
5148 let eligibleMatches = routeId ? matches.slice(0, matches.findIndex(m => m.route.id === routeId) + 1) : [...matches];
5149 return eligibleMatches.reverse().find(m => m.route.hasErrorBoundary === true) || matches[0];
5150}
5151function getShortCircuitMatches(routes) {
5152 // Prefer a root layout route if present, otherwise shim in a route object
5153 let route = routes.length === 1 ? routes[0] : routes.find(r => r.index || !r.path || r.path === "/") || {
5154 id: "__shim-error-route__"
5155 };
5156 return {
5157 matches: [{
5158 params: {},
5159 pathname: "",
5160 pathnameBase: "",
5161 route
5162 }],
5163 route
5164 };
5165}
5166function getInternalRouterError(status, _temp5) {
5167 let {
5168 pathname,
5169 routeId,
5170 method,
5171 type,
5172 message
5173 } = _temp5 === void 0 ? {} : _temp5;
5174 let statusText = "Unknown Server Error";
5175 let errorMessage = "Unknown @remix-run/router error";
5176 if (status === 400) {
5177 statusText = "Bad Request";
[0c6b92a]5178 if (method && pathname && routeId) {
[d565449]5179 errorMessage = "You made a " + method + " request to \"" + pathname + "\" but " + ("did not provide a `loader` for route \"" + routeId + "\", ") + "so there is no way to handle the request.";
5180 } else if (type === "defer-action") {
5181 errorMessage = "defer() is not supported in actions";
5182 } else if (type === "invalid-body") {
5183 errorMessage = "Unable to encode submission body";
5184 }
5185 } else if (status === 403) {
5186 statusText = "Forbidden";
5187 errorMessage = "Route \"" + routeId + "\" does not match URL \"" + pathname + "\"";
5188 } else if (status === 404) {
5189 statusText = "Not Found";
5190 errorMessage = "No route matches URL \"" + pathname + "\"";
5191 } else if (status === 405) {
5192 statusText = "Method Not Allowed";
5193 if (method && pathname && routeId) {
5194 errorMessage = "You made a " + method.toUpperCase() + " request to \"" + pathname + "\" but " + ("did not provide an `action` for route \"" + routeId + "\", ") + "so there is no way to handle the request.";
5195 } else if (method) {
5196 errorMessage = "Invalid request method \"" + method.toUpperCase() + "\"";
5197 }
5198 }
5199 return new ErrorResponseImpl(status || 500, statusText, new Error(errorMessage), true);
5200}
5201
5202// Find any returned redirect errors, starting from the lowest match
5203function findRedirect(results) {
[0c6b92a]5204 let entries = Object.entries(results);
5205 for (let i = entries.length - 1; i >= 0; i--) {
5206 let [key, result] = entries[i];
[d565449]5207 if (isRedirectResult(result)) {
5208 return {
[0c6b92a]5209 key,
5210 result
[d565449]5211 };
5212 }
5213 }
5214}
5215function stripHashFromPath(path) {
5216 let parsedPath = typeof path === "string" ? parsePath(path) : path;
5217 return createPath(_extends({}, parsedPath, {
5218 hash: ""
5219 }));
5220}
5221function isHashChangeOnly(a, b) {
5222 if (a.pathname !== b.pathname || a.search !== b.search) {
5223 return false;
5224 }
5225 if (a.hash === "") {
5226 // /page -> /page#hash
5227 return b.hash !== "";
5228 } else if (a.hash === b.hash) {
5229 // /page#hash -> /page#hash
5230 return true;
5231 } else if (b.hash !== "") {
5232 // /page#hash -> /page#other
5233 return true;
5234 }
5235
5236 // If the hash is removed the browser will re-perform a request to the server
5237 // /page#hash -> /page
5238 return false;
5239}
[0c6b92a]5240function isDataStrategyResult(result) {
[d565449]5241 return result != null && typeof result === "object" && "type" in result && "result" in result && (result.type === ResultType.data || result.type === ResultType.error);
5242}
[0c6b92a]5243function isRedirectDataStrategyResultResult(result) {
[d565449]5244 return isResponse(result.result) && redirectStatusCodes.has(result.result.status);
5245}
5246function isDeferredResult(result) {
5247 return result.type === ResultType.deferred;
5248}
5249function isErrorResult(result) {
5250 return result.type === ResultType.error;
5251}
5252function isRedirectResult(result) {
5253 return (result && result.type) === ResultType.redirect;
5254}
5255function isDataWithResponseInit(value) {
5256 return typeof value === "object" && value != null && "type" in value && "data" in value && "init" in value && value.type === "DataWithResponseInit";
5257}
5258function isDeferredData(value) {
5259 let deferred = value;
5260 return deferred && typeof deferred === "object" && typeof deferred.data === "object" && typeof deferred.subscribe === "function" && typeof deferred.cancel === "function" && typeof deferred.resolveData === "function";
5261}
5262function isResponse(value) {
5263 return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
5264}
5265function isRedirectResponse(result) {
5266 if (!isResponse(result)) {
5267 return false;
5268 }
5269 let status = result.status;
5270 let location = result.headers.get("Location");
5271 return status >= 300 && status <= 399 && location != null;
5272}
5273function isValidMethod(method) {
5274 return validRequestMethods.has(method.toLowerCase());
5275}
5276function isMutationMethod(method) {
5277 return validMutationMethods.has(method.toLowerCase());
5278}
[0c6b92a]5279async function resolveNavigationDeferredResults(matches, results, signal, currentMatches, currentLoaderData) {
5280 let entries = Object.entries(results);
5281 for (let index = 0; index < entries.length; index++) {
5282 let [routeId, result] = entries[index];
5283 let match = matches.find(m => (m == null ? void 0 : m.route.id) === routeId);
[d565449]5284 // If we don't have a match, then we can have a deferred result to do
5285 // anything with. This is for revalidating fetchers where the route was
5286 // removed during HMR
5287 if (!match) {
5288 continue;
5289 }
5290 let currentMatch = currentMatches.find(m => m.route.id === match.route.id);
5291 let isRevalidatingLoader = currentMatch != null && !isNewRouteInstance(currentMatch, match) && (currentLoaderData && currentLoaderData[match.route.id]) !== undefined;
[0c6b92a]5292 if (isDeferredResult(result) && isRevalidatingLoader) {
5293 // Note: we do not have to touch activeDeferreds here since we race them
5294 // against the signal in resolveDeferredData and they'll get aborted
5295 // there if needed
5296 await resolveDeferredData(result, signal, false).then(result => {
5297 if (result) {
5298 results[routeId] = result;
5299 }
5300 });
5301 }
5302 }
5303}
5304async function resolveFetcherDeferredResults(matches, results, revalidatingFetchers) {
5305 for (let index = 0; index < revalidatingFetchers.length; index++) {
5306 let {
5307 key,
5308 routeId,
5309 controller
5310 } = revalidatingFetchers[index];
5311 let result = results[key];
5312 let match = matches.find(m => (m == null ? void 0 : m.route.id) === routeId);
5313 // If we don't have a match, then we can have a deferred result to do
5314 // anything with. This is for revalidating fetchers where the route was
5315 // removed during HMR
5316 if (!match) {
5317 continue;
5318 }
5319 if (isDeferredResult(result)) {
[d565449]5320 // Note: we do not have to touch activeDeferreds here since we race them
5321 // against the signal in resolveDeferredData and they'll get aborted
5322 // there if needed
[0c6b92a]5323 invariant(controller, "Expected an AbortController for revalidating fetcher deferred result");
5324 await resolveDeferredData(result, controller.signal, true).then(result => {
[d565449]5325 if (result) {
[0c6b92a]5326 results[key] = result;
[d565449]5327 }
5328 });
5329 }
5330 }
5331}
5332async function resolveDeferredData(result, signal, unwrap) {
5333 if (unwrap === void 0) {
5334 unwrap = false;
5335 }
5336 let aborted = await result.deferredData.resolveData(signal);
5337 if (aborted) {
5338 return;
5339 }
5340 if (unwrap) {
5341 try {
5342 return {
5343 type: ResultType.data,
5344 data: result.deferredData.unwrappedData
5345 };
5346 } catch (e) {
5347 // Handle any TrackedPromise._error values encountered while unwrapping
5348 return {
5349 type: ResultType.error,
5350 error: e
5351 };
5352 }
5353 }
5354 return {
5355 type: ResultType.data,
5356 data: result.deferredData.data
5357 };
5358}
5359function hasNakedIndexQuery(search) {
5360 return new URLSearchParams(search).getAll("index").some(v => v === "");
5361}
5362function getTargetMatch(matches, location) {
5363 let search = typeof location === "string" ? parsePath(location).search : location.search;
5364 if (matches[matches.length - 1].route.index && hasNakedIndexQuery(search || "")) {
5365 // Return the leaf index route when index is present
5366 return matches[matches.length - 1];
5367 }
5368 // Otherwise grab the deepest "path contributing" match (ignoring index and
5369 // pathless layout routes)
5370 let pathMatches = getPathContributingMatches(matches);
5371 return pathMatches[pathMatches.length - 1];
5372}
5373function getSubmissionFromNavigation(navigation) {
5374 let {
5375 formMethod,
5376 formAction,
5377 formEncType,
5378 text,
5379 formData,
5380 json
5381 } = navigation;
5382 if (!formMethod || !formAction || !formEncType) {
5383 return;
5384 }
5385 if (text != null) {
5386 return {
5387 formMethod,
5388 formAction,
5389 formEncType,
5390 formData: undefined,
5391 json: undefined,
5392 text
5393 };
5394 } else if (formData != null) {
5395 return {
5396 formMethod,
5397 formAction,
5398 formEncType,
5399 formData,
5400 json: undefined,
5401 text: undefined
5402 };
5403 } else if (json !== undefined) {
5404 return {
5405 formMethod,
5406 formAction,
5407 formEncType,
5408 formData: undefined,
5409 json,
5410 text: undefined
5411 };
5412 }
5413}
5414function getLoadingNavigation(location, submission) {
5415 if (submission) {
5416 let navigation = {
5417 state: "loading",
5418 location,
5419 formMethod: submission.formMethod,
5420 formAction: submission.formAction,
5421 formEncType: submission.formEncType,
5422 formData: submission.formData,
5423 json: submission.json,
5424 text: submission.text
5425 };
5426 return navigation;
5427 } else {
5428 let navigation = {
5429 state: "loading",
5430 location,
5431 formMethod: undefined,
5432 formAction: undefined,
5433 formEncType: undefined,
5434 formData: undefined,
5435 json: undefined,
5436 text: undefined
5437 };
5438 return navigation;
5439 }
5440}
5441function getSubmittingNavigation(location, submission) {
5442 let navigation = {
5443 state: "submitting",
5444 location,
5445 formMethod: submission.formMethod,
5446 formAction: submission.formAction,
5447 formEncType: submission.formEncType,
5448 formData: submission.formData,
5449 json: submission.json,
5450 text: submission.text
5451 };
5452 return navigation;
5453}
5454function getLoadingFetcher(submission, data) {
5455 if (submission) {
5456 let fetcher = {
5457 state: "loading",
5458 formMethod: submission.formMethod,
5459 formAction: submission.formAction,
5460 formEncType: submission.formEncType,
5461 formData: submission.formData,
5462 json: submission.json,
5463 text: submission.text,
5464 data
5465 };
5466 return fetcher;
5467 } else {
5468 let fetcher = {
5469 state: "loading",
5470 formMethod: undefined,
5471 formAction: undefined,
5472 formEncType: undefined,
5473 formData: undefined,
5474 json: undefined,
5475 text: undefined,
5476 data
5477 };
5478 return fetcher;
5479 }
5480}
5481function getSubmittingFetcher(submission, existingFetcher) {
5482 let fetcher = {
5483 state: "submitting",
5484 formMethod: submission.formMethod,
5485 formAction: submission.formAction,
5486 formEncType: submission.formEncType,
5487 formData: submission.formData,
5488 json: submission.json,
5489 text: submission.text,
5490 data: existingFetcher ? existingFetcher.data : undefined
5491 };
5492 return fetcher;
5493}
5494function getDoneFetcher(data) {
5495 let fetcher = {
5496 state: "idle",
5497 formMethod: undefined,
5498 formAction: undefined,
5499 formEncType: undefined,
5500 formData: undefined,
5501 json: undefined,
5502 text: undefined,
5503 data
5504 };
5505 return fetcher;
5506}
5507function restoreAppliedTransitions(_window, transitions) {
5508 try {
5509 let sessionPositions = _window.sessionStorage.getItem(TRANSITIONS_STORAGE_KEY);
5510 if (sessionPositions) {
5511 let json = JSON.parse(sessionPositions);
5512 for (let [k, v] of Object.entries(json || {})) {
5513 if (v && Array.isArray(v)) {
5514 transitions.set(k, new Set(v || []));
5515 }
5516 }
5517 }
5518 } catch (e) {
5519 // no-op, use default empty object
5520 }
5521}
5522function persistAppliedTransitions(_window, transitions) {
5523 if (transitions.size > 0) {
5524 let json = {};
5525 for (let [k, v] of transitions) {
5526 json[k] = [...v];
5527 }
5528 try {
5529 _window.sessionStorage.setItem(TRANSITIONS_STORAGE_KEY, JSON.stringify(json));
5530 } catch (error) {
5531 warning(false, "Failed to save applied view transitions in sessionStorage (" + error + ").");
5532 }
5533 }
5534}
5535//#endregion
5536
5537exports.AbortedDeferredError = AbortedDeferredError;
5538exports.Action = Action;
5539exports.IDLE_BLOCKER = IDLE_BLOCKER;
5540exports.IDLE_FETCHER = IDLE_FETCHER;
5541exports.IDLE_NAVIGATION = IDLE_NAVIGATION;
5542exports.UNSAFE_DEFERRED_SYMBOL = UNSAFE_DEFERRED_SYMBOL;
5543exports.UNSAFE_DeferredData = DeferredData;
5544exports.UNSAFE_ErrorResponseImpl = ErrorResponseImpl;
5545exports.UNSAFE_convertRouteMatchToUiMatch = convertRouteMatchToUiMatch;
5546exports.UNSAFE_convertRoutesToDataRoutes = convertRoutesToDataRoutes;
5547exports.UNSAFE_decodePath = decodePath;
5548exports.UNSAFE_getResolveToMatches = getResolveToMatches;
5549exports.UNSAFE_invariant = invariant;
5550exports.UNSAFE_warning = warning;
5551exports.createBrowserHistory = createBrowserHistory;
5552exports.createHashHistory = createHashHistory;
5553exports.createMemoryHistory = createMemoryHistory;
5554exports.createPath = createPath;
5555exports.createRouter = createRouter;
5556exports.createStaticHandler = createStaticHandler;
[0c6b92a]5557exports.data = data;
[d565449]5558exports.defer = defer;
5559exports.generatePath = generatePath;
5560exports.getStaticContextFromError = getStaticContextFromError;
5561exports.getToPathname = getToPathname;
5562exports.isDataWithResponseInit = isDataWithResponseInit;
5563exports.isDeferredData = isDeferredData;
5564exports.isRouteErrorResponse = isRouteErrorResponse;
5565exports.joinPaths = joinPaths;
5566exports.json = json;
5567exports.matchPath = matchPath;
5568exports.matchRoutes = matchRoutes;
5569exports.normalizePathname = normalizePathname;
5570exports.parsePath = parsePath;
5571exports.redirect = redirect;
5572exports.redirectDocument = redirectDocument;
5573exports.replace = replace;
5574exports.resolvePath = resolvePath;
5575exports.resolveTo = resolveTo;
5576exports.stripBasename = stripBasename;
5577//# sourceMappingURL=router.cjs.js.map
Note: See TracBrowser for help on using the repository browser.