@remix-run/router 0.0.0-experimental-48058118

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4210 @@
1
+ /**
2
+ * @remix-run/router v0.0.0-experimental-48058118
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
+ (function (global, factory) {
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
13
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.RemixRouter = {}));
15
+ })(this, (function (exports) { 'use strict';
16
+
17
+ function _extends() {
18
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
19
+ for (var i = 1; i < arguments.length; i++) {
20
+ var source = arguments[i];
21
+
22
+ for (var key in source) {
23
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
24
+ target[key] = source[key];
25
+ }
26
+ }
27
+ }
28
+
29
+ return target;
30
+ };
31
+ return _extends.apply(this, arguments);
32
+ }
33
+
34
+ ////////////////////////////////////////////////////////////////////////////////
35
+ //#region Types and Constants
36
+ ////////////////////////////////////////////////////////////////////////////////
37
+
38
+ /**
39
+ * Actions represent the type of change to a location value.
40
+ */
41
+ exports.Action = void 0;
42
+ /**
43
+ * The pathname, search, and hash values of a URL.
44
+ */
45
+
46
+ (function (Action) {
47
+ Action["Pop"] = "POP";
48
+ Action["Push"] = "PUSH";
49
+ Action["Replace"] = "REPLACE";
50
+ })(exports.Action || (exports.Action = {}));
51
+
52
+ const PopStateEventType = "popstate"; //#endregion
53
+ ////////////////////////////////////////////////////////////////////////////////
54
+ //#region Memory History
55
+ ////////////////////////////////////////////////////////////////////////////////
56
+
57
+ /**
58
+ * A user-supplied object that describes a location. Used when providing
59
+ * entries to `createMemoryHistory` via its `initialEntries` option.
60
+ */
61
+
62
+ /**
63
+ * Memory history stores the current location in memory. It is designed for use
64
+ * in stateful non-browser environments like tests and React Native.
65
+ */
66
+ function createMemoryHistory(options) {
67
+ if (options === void 0) {
68
+ options = {};
69
+ }
70
+
71
+ let {
72
+ initialEntries = ["/"],
73
+ initialIndex,
74
+ v5Compat = false
75
+ } = options;
76
+ let entries; // Declare so we can access from createMemoryLocation
77
+
78
+ entries = initialEntries.map((entry, index) => createMemoryLocation(entry, typeof entry === "string" ? null : entry.state, index === 0 ? "default" : undefined));
79
+ let index = clampIndex(initialIndex == null ? entries.length - 1 : initialIndex);
80
+ let action = exports.Action.Pop;
81
+ let listener = null;
82
+
83
+ function clampIndex(n) {
84
+ return Math.min(Math.max(n, 0), entries.length - 1);
85
+ }
86
+
87
+ function getCurrentLocation() {
88
+ return entries[index];
89
+ }
90
+
91
+ function createMemoryLocation(to, state, key) {
92
+ if (state === void 0) {
93
+ state = null;
94
+ }
95
+
96
+ let location = createLocation(entries ? getCurrentLocation().pathname : "/", to, state, key);
97
+ warning$1(location.pathname.charAt(0) === "/", "relative pathnames are not supported in memory history: " + JSON.stringify(to));
98
+ return location;
99
+ }
100
+
101
+ function createHref(to) {
102
+ return typeof to === "string" ? to : createPath(to);
103
+ }
104
+
105
+ let history = {
106
+ get index() {
107
+ return index;
108
+ },
109
+
110
+ get action() {
111
+ return action;
112
+ },
113
+
114
+ get location() {
115
+ return getCurrentLocation();
116
+ },
117
+
118
+ createHref,
119
+
120
+ createURL(to) {
121
+ return new URL(createHref(to), "http://localhost");
122
+ },
123
+
124
+ encodeLocation(to) {
125
+ let path = typeof to === "string" ? parsePath(to) : to;
126
+ return {
127
+ pathname: path.pathname || "",
128
+ search: path.search || "",
129
+ hash: path.hash || ""
130
+ };
131
+ },
132
+
133
+ push(to, state) {
134
+ action = exports.Action.Push;
135
+ let nextLocation = createMemoryLocation(to, state);
136
+ index += 1;
137
+ entries.splice(index, entries.length, nextLocation);
138
+
139
+ if (v5Compat && listener) {
140
+ listener({
141
+ action,
142
+ location: nextLocation,
143
+ delta: 1
144
+ });
145
+ }
146
+ },
147
+
148
+ replace(to, state) {
149
+ action = exports.Action.Replace;
150
+ let nextLocation = createMemoryLocation(to, state);
151
+ entries[index] = nextLocation;
152
+
153
+ if (v5Compat && listener) {
154
+ listener({
155
+ action,
156
+ location: nextLocation,
157
+ delta: 0
158
+ });
159
+ }
160
+ },
161
+
162
+ go(delta) {
163
+ action = exports.Action.Pop;
164
+ let nextIndex = clampIndex(index + delta);
165
+ let nextLocation = entries[nextIndex];
166
+ index = nextIndex;
167
+
168
+ if (listener) {
169
+ listener({
170
+ action,
171
+ location: nextLocation,
172
+ delta
173
+ });
174
+ }
175
+ },
176
+
177
+ listen(fn) {
178
+ listener = fn;
179
+ return () => {
180
+ listener = null;
181
+ };
182
+ }
183
+
184
+ };
185
+ return history;
186
+ } //#endregion
187
+ ////////////////////////////////////////////////////////////////////////////////
188
+ //#region Browser History
189
+ ////////////////////////////////////////////////////////////////////////////////
190
+
191
+ /**
192
+ * A browser history stores the current location in regular URLs in a web
193
+ * browser environment. This is the standard for most web apps and provides the
194
+ * cleanest URLs the browser's address bar.
195
+ *
196
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
197
+ */
198
+
199
+ /**
200
+ * Browser history stores the location in regular URLs. This is the standard for
201
+ * most web apps, but it requires some configuration on the server to ensure you
202
+ * serve the same app at multiple URLs.
203
+ *
204
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
205
+ */
206
+ function createBrowserHistory(options) {
207
+ if (options === void 0) {
208
+ options = {};
209
+ }
210
+
211
+ function createBrowserLocation(window, globalHistory) {
212
+ let {
213
+ pathname,
214
+ search,
215
+ hash
216
+ } = window.location;
217
+ return createLocation("", {
218
+ pathname,
219
+ search,
220
+ hash
221
+ }, // state defaults to `null` because `window.history.state` does
222
+ globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
223
+ }
224
+
225
+ function createBrowserHref(window, to) {
226
+ return typeof to === "string" ? to : createPath(to);
227
+ }
228
+
229
+ return getUrlBasedHistory(createBrowserLocation, createBrowserHref, null, options);
230
+ } //#endregion
231
+ ////////////////////////////////////////////////////////////////////////////////
232
+ //#region Hash History
233
+ ////////////////////////////////////////////////////////////////////////////////
234
+
235
+ /**
236
+ * A hash history stores the current location in the fragment identifier portion
237
+ * of the URL in a web browser environment.
238
+ *
239
+ * This is ideal for apps that do not control the server for some reason
240
+ * (because the fragment identifier is never sent to the server), including some
241
+ * shared hosting environments that do not provide fine-grained controls over
242
+ * which pages are served at which URLs.
243
+ *
244
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
245
+ */
246
+
247
+ /**
248
+ * Hash history stores the location in window.location.hash. This makes it ideal
249
+ * for situations where you don't want to send the location to the server for
250
+ * some reason, either because you do cannot configure it or the URL space is
251
+ * reserved for something else.
252
+ *
253
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
254
+ */
255
+ function createHashHistory(options) {
256
+ if (options === void 0) {
257
+ options = {};
258
+ }
259
+
260
+ function createHashLocation(window, globalHistory) {
261
+ let {
262
+ pathname = "/",
263
+ search = "",
264
+ hash = ""
265
+ } = parsePath(window.location.hash.substr(1));
266
+ return createLocation("", {
267
+ pathname,
268
+ search,
269
+ hash
270
+ }, // state defaults to `null` because `window.history.state` does
271
+ globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
272
+ }
273
+
274
+ function createHashHref(window, to) {
275
+ let base = window.document.querySelector("base");
276
+ let href = "";
277
+
278
+ if (base && base.getAttribute("href")) {
279
+ let url = window.location.href;
280
+ let hashIndex = url.indexOf("#");
281
+ href = hashIndex === -1 ? url : url.slice(0, hashIndex);
282
+ }
283
+
284
+ return href + "#" + (typeof to === "string" ? to : createPath(to));
285
+ }
286
+
287
+ function validateHashLocation(location, to) {
288
+ warning$1(location.pathname.charAt(0) === "/", "relative pathnames are not supported in hash history.push(" + JSON.stringify(to) + ")");
289
+ }
290
+
291
+ return getUrlBasedHistory(createHashLocation, createHashHref, validateHashLocation, options);
292
+ } //#endregion
293
+ ////////////////////////////////////////////////////////////////////////////////
294
+ //#region UTILS
295
+ ////////////////////////////////////////////////////////////////////////////////
296
+
297
+ /**
298
+ * @private
299
+ */
300
+
301
+ function invariant(value, message) {
302
+ if (value === false || value === null || typeof value === "undefined") {
303
+ throw new Error(message);
304
+ }
305
+ }
306
+
307
+ function warning$1(cond, message) {
308
+ if (!cond) {
309
+ // eslint-disable-next-line no-console
310
+ if (typeof console !== "undefined") console.warn(message);
311
+
312
+ try {
313
+ // Welcome to debugging history!
314
+ //
315
+ // This error is thrown as a convenience so you can more easily
316
+ // find the source for a warning that appears in the console by
317
+ // enabling "pause on exceptions" in your JavaScript debugger.
318
+ throw new Error(message); // eslint-disable-next-line no-empty
319
+ } catch (e) {}
320
+ }
321
+ }
322
+
323
+ function createKey() {
324
+ return Math.random().toString(36).substr(2, 8);
325
+ }
326
+ /**
327
+ * For browser-based histories, we combine the state and key into an object
328
+ */
329
+
330
+
331
+ function getHistoryState(location, index) {
332
+ return {
333
+ usr: location.state,
334
+ key: location.key,
335
+ idx: index
336
+ };
337
+ }
338
+ /**
339
+ * Creates a Location object with a unique key from the given Path
340
+ */
341
+
342
+
343
+ function createLocation(current, to, state, key) {
344
+ if (state === void 0) {
345
+ state = null;
346
+ }
347
+
348
+ let location = _extends({
349
+ pathname: typeof current === "string" ? current : current.pathname,
350
+ search: "",
351
+ hash: ""
352
+ }, typeof to === "string" ? parsePath(to) : to, {
353
+ state,
354
+ // TODO: This could be cleaned up. push/replace should probably just take
355
+ // full Locations now and avoid the need to run through this flow at all
356
+ // But that's a pretty big refactor to the current test suite so going to
357
+ // keep as is for the time being and just let any incoming keys take precedence
358
+ key: to && to.key || key || createKey()
359
+ });
360
+
361
+ return location;
362
+ }
363
+ /**
364
+ * Creates a string URL path from the given pathname, search, and hash components.
365
+ */
366
+
367
+ function createPath(_ref) {
368
+ let {
369
+ pathname = "/",
370
+ search = "",
371
+ hash = ""
372
+ } = _ref;
373
+ if (search && search !== "?") pathname += search.charAt(0) === "?" ? search : "?" + search;
374
+ if (hash && hash !== "#") pathname += hash.charAt(0) === "#" ? hash : "#" + hash;
375
+ return pathname;
376
+ }
377
+ /**
378
+ * Parses a string URL path into its separate pathname, search, and hash components.
379
+ */
380
+
381
+ function parsePath(path) {
382
+ let parsedPath = {};
383
+
384
+ if (path) {
385
+ let hashIndex = path.indexOf("#");
386
+
387
+ if (hashIndex >= 0) {
388
+ parsedPath.hash = path.substr(hashIndex);
389
+ path = path.substr(0, hashIndex);
390
+ }
391
+
392
+ let searchIndex = path.indexOf("?");
393
+
394
+ if (searchIndex >= 0) {
395
+ parsedPath.search = path.substr(searchIndex);
396
+ path = path.substr(0, searchIndex);
397
+ }
398
+
399
+ if (path) {
400
+ parsedPath.pathname = path;
401
+ }
402
+ }
403
+
404
+ return parsedPath;
405
+ }
406
+
407
+ function getUrlBasedHistory(getLocation, createHref, validateLocation, options) {
408
+ if (options === void 0) {
409
+ options = {};
410
+ }
411
+
412
+ let {
413
+ window = document.defaultView,
414
+ v5Compat = false
415
+ } = options;
416
+ let globalHistory = window.history;
417
+ let action = exports.Action.Pop;
418
+ let listener = null;
419
+ let index = getIndex(); // Index should only be null when we initialize. If not, it's because the
420
+ // user called history.pushState or history.replaceState directly, in which
421
+ // case we should log a warning as it will result in bugs.
422
+
423
+ if (index == null) {
424
+ index = 0;
425
+ globalHistory.replaceState(_extends({}, globalHistory.state, {
426
+ idx: index
427
+ }), "");
428
+ }
429
+
430
+ function getIndex() {
431
+ let state = globalHistory.state || {
432
+ idx: null
433
+ };
434
+ return state.idx;
435
+ }
436
+
437
+ function handlePop() {
438
+ action = exports.Action.Pop;
439
+ let nextIndex = getIndex();
440
+ let delta = nextIndex == null ? null : nextIndex - index;
441
+ index = nextIndex;
442
+
443
+ if (listener) {
444
+ listener({
445
+ action,
446
+ location: history.location,
447
+ delta
448
+ });
449
+ }
450
+ }
451
+
452
+ function push(to, state) {
453
+ action = exports.Action.Push;
454
+ let location = createLocation(history.location, to, state);
455
+ if (validateLocation) validateLocation(location, to);
456
+ index = getIndex() + 1;
457
+ let historyState = getHistoryState(location, index);
458
+ let url = history.createHref(location); // try...catch because iOS limits us to 100 pushState calls :/
459
+
460
+ try {
461
+ globalHistory.pushState(historyState, "", url);
462
+ } catch (error) {
463
+ // They are going to lose state here, but there is no real
464
+ // way to warn them about it since the page will refresh...
465
+ window.location.assign(url);
466
+ }
467
+
468
+ if (v5Compat && listener) {
469
+ listener({
470
+ action,
471
+ location: history.location,
472
+ delta: 1
473
+ });
474
+ }
475
+ }
476
+
477
+ function replace(to, state) {
478
+ action = exports.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
+
486
+ if (v5Compat && listener) {
487
+ listener({
488
+ action,
489
+ location: history.location,
490
+ delta: 0
491
+ });
492
+ }
493
+ }
494
+
495
+ function createURL(to) {
496
+ // window.location.origin is "null" (the literal string value) in Firefox
497
+ // under certain conditions, notably when serving from a local HTML file
498
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
499
+ let base = window.location.origin !== "null" ? window.location.origin : window.location.href;
500
+ let href = typeof to === "string" ? to : createPath(to);
501
+ invariant(base, "No window.location.(origin|href) available to create URL for href: " + href);
502
+ return new URL(href, base);
503
+ }
504
+
505
+ let history = {
506
+ get action() {
507
+ return action;
508
+ },
509
+
510
+ get location() {
511
+ return getLocation(window, globalHistory);
512
+ },
513
+
514
+ listen(fn) {
515
+ if (listener) {
516
+ throw new Error("A history only accepts one active listener");
517
+ }
518
+
519
+ window.addEventListener(PopStateEventType, handlePop);
520
+ listener = fn;
521
+ return () => {
522
+ window.removeEventListener(PopStateEventType, handlePop);
523
+ listener = null;
524
+ };
525
+ },
526
+
527
+ createHref(to) {
528
+ return createHref(window, to);
529
+ },
530
+
531
+ createURL,
532
+
533
+ encodeLocation(to) {
534
+ // Encode a Location the same way window.location would
535
+ let url = createURL(to);
536
+ return {
537
+ pathname: url.pathname,
538
+ search: url.search,
539
+ hash: url.hash
540
+ };
541
+ },
542
+
543
+ push,
544
+ replace,
545
+
546
+ go(n) {
547
+ return globalHistory.go(n);
548
+ }
549
+
550
+ };
551
+ return history;
552
+ } //#endregion
553
+
554
+ /**
555
+ * Map of routeId -> data returned from a loader/action/error
556
+ */
557
+
558
+ let ResultType;
559
+ /**
560
+ * Successful result from a loader or action
561
+ */
562
+
563
+ (function (ResultType) {
564
+ ResultType["data"] = "data";
565
+ ResultType["deferred"] = "deferred";
566
+ ResultType["redirect"] = "redirect";
567
+ ResultType["error"] = "error";
568
+ })(ResultType || (ResultType = {}));
569
+
570
+ function isIndexRoute(route) {
571
+ return route.index === true;
572
+ } // Walk the route tree generating unique IDs where necessary so we are working
573
+ // solely with AgnosticDataRouteObject's within the Router
574
+
575
+
576
+ function convertRoutesToDataRoutes(routes, parentPath, allIds) {
577
+ if (parentPath === void 0) {
578
+ parentPath = [];
579
+ }
580
+
581
+ if (allIds === void 0) {
582
+ allIds = new Set();
583
+ }
584
+
585
+ return routes.map((route, index) => {
586
+ let treePath = [...parentPath, index];
587
+ let id = typeof route.id === "string" ? route.id : treePath.join("-");
588
+ invariant(route.index !== true || !route.children, "Cannot specify children on an index route");
589
+ invariant(!allIds.has(id), "Found a route id collision on id \"" + id + "\". Route " + "id's must be globally unique within Data Router usages");
590
+ allIds.add(id);
591
+
592
+ if (isIndexRoute(route)) {
593
+ let indexRoute = _extends({}, route, {
594
+ id
595
+ });
596
+
597
+ return indexRoute;
598
+ } else {
599
+ let pathOrLayoutRoute = _extends({}, route, {
600
+ id,
601
+ children: route.children ? convertRoutesToDataRoutes(route.children, treePath, allIds) : undefined
602
+ });
603
+
604
+ return pathOrLayoutRoute;
605
+ }
606
+ });
607
+ }
608
+ /**
609
+ * Matches the given routes to a location and returns the match data.
610
+ *
611
+ * @see https://reactrouter.com/utils/match-routes
612
+ */
613
+
614
+ function matchRoutes(routes, locationArg, basename) {
615
+ if (basename === void 0) {
616
+ basename = "/";
617
+ }
618
+
619
+ let location = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
620
+ let pathname = stripBasename(location.pathname || "/", basename);
621
+
622
+ if (pathname == null) {
623
+ return null;
624
+ }
625
+
626
+ let branches = flattenRoutes(routes);
627
+ rankRouteBranches(branches);
628
+ let matches = null;
629
+
630
+ for (let i = 0; matches == null && i < branches.length; ++i) {
631
+ matches = matchRouteBranch(branches[i], // Incoming pathnames are generally encoded from either window.location
632
+ // or from router.navigate, but we want to match against the unencoded
633
+ // paths in the route definitions. Memory router locations won't be
634
+ // encoded here but there also shouldn't be anything to decode so this
635
+ // should be a safe operation. This avoids needing matchRoutes to be
636
+ // history-aware.
637
+ safelyDecodeURI(pathname));
638
+ }
639
+
640
+ return matches;
641
+ }
642
+
643
+ function flattenRoutes(routes, branches, parentsMeta, parentPath) {
644
+ if (branches === void 0) {
645
+ branches = [];
646
+ }
647
+
648
+ if (parentsMeta === void 0) {
649
+ parentsMeta = [];
650
+ }
651
+
652
+ if (parentPath === void 0) {
653
+ parentPath = "";
654
+ }
655
+
656
+ let flattenRoute = (route, index, relativePath) => {
657
+ let meta = {
658
+ relativePath: relativePath === undefined ? route.path || "" : relativePath,
659
+ caseSensitive: route.caseSensitive === true,
660
+ childrenIndex: index,
661
+ route
662
+ };
663
+
664
+ if (meta.relativePath.startsWith("/")) {
665
+ 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.");
666
+ meta.relativePath = meta.relativePath.slice(parentPath.length);
667
+ }
668
+
669
+ let path = joinPaths([parentPath, meta.relativePath]);
670
+ let routesMeta = parentsMeta.concat(meta); // Add the children before adding this route to the array so we traverse the
671
+ // route tree depth-first and child routes appear before their parents in
672
+ // the "flattened" version.
673
+
674
+ if (route.children && route.children.length > 0) {
675
+ invariant( // Our types know better, but runtime JS may not!
676
+ // @ts-expect-error
677
+ route.index !== true, "Index routes must not have child routes. Please remove " + ("all child routes from route path \"" + path + "\"."));
678
+ flattenRoutes(route.children, branches, routesMeta, path);
679
+ } // Routes without a path shouldn't ever match by themselves unless they are
680
+ // index routes, so don't add them to the list of possible branches.
681
+
682
+
683
+ if (route.path == null && !route.index) {
684
+ return;
685
+ }
686
+
687
+ branches.push({
688
+ path,
689
+ score: computeScore(path, route.index),
690
+ routesMeta
691
+ });
692
+ };
693
+
694
+ routes.forEach((route, index) => {
695
+ var _route$path;
696
+
697
+ // coarse-grain check for optional params
698
+ if (route.path === "" || !((_route$path = route.path) != null && _route$path.includes("?"))) {
699
+ flattenRoute(route, index);
700
+ } else {
701
+ for (let exploded of explodeOptionalSegments(route.path)) {
702
+ flattenRoute(route, index, exploded);
703
+ }
704
+ }
705
+ });
706
+ return branches;
707
+ }
708
+ /**
709
+ * Computes all combinations of optional path segments for a given path,
710
+ * excluding combinations that are ambiguous and of lower priority.
711
+ *
712
+ * For example, `/one/:two?/three/:four?/:five?` explodes to:
713
+ * - `/one/three`
714
+ * - `/one/:two/three`
715
+ * - `/one/three/:four`
716
+ * - `/one/three/:five`
717
+ * - `/one/:two/three/:four`
718
+ * - `/one/:two/three/:five`
719
+ * - `/one/three/:four/:five`
720
+ * - `/one/:two/three/:four/:five`
721
+ */
722
+
723
+
724
+ function explodeOptionalSegments(path) {
725
+ let segments = path.split("/");
726
+ if (segments.length === 0) return [];
727
+ let [first, ...rest] = segments; // Optional path segments are denoted by a trailing `?`
728
+
729
+ let isOptional = first.endsWith("?"); // Compute the corresponding required segment: `foo?` -> `foo`
730
+
731
+ let required = first.replace(/\?$/, "");
732
+
733
+ if (rest.length === 0) {
734
+ // Intepret empty string as omitting an optional segment
735
+ // `["one", "", "three"]` corresponds to omitting `:two` from `/one/:two?/three` -> `/one/three`
736
+ return isOptional ? [required, ""] : [required];
737
+ }
738
+
739
+ let restExploded = explodeOptionalSegments(rest.join("/"));
740
+ let result = []; // All child paths with the prefix. Do this for all children before the
741
+ // optional version for all children so we get consistent ordering where the
742
+ // parent optional aspect is preferred as required. Otherwise, we can get
743
+ // child sections interspersed where deeper optional segments are higher than
744
+ // parent optional segments, where for example, /:two would explodes _earlier_
745
+ // then /:one. By always including the parent as required _for all children_
746
+ // first, we avoid this issue
747
+
748
+ result.push(...restExploded.map(subpath => subpath === "" ? required : [required, subpath].join("/"))); // Then if this is an optional value, add all child versions without
749
+
750
+ if (isOptional) {
751
+ result.push(...restExploded);
752
+ } // for absolute paths, ensure `/` instead of empty segment
753
+
754
+
755
+ return result.map(exploded => path.startsWith("/") && exploded === "" ? "/" : exploded);
756
+ }
757
+
758
+ function rankRouteBranches(branches) {
759
+ branches.sort((a, b) => a.score !== b.score ? b.score - a.score // Higher score first
760
+ : compareIndexes(a.routesMeta.map(meta => meta.childrenIndex), b.routesMeta.map(meta => meta.childrenIndex)));
761
+ }
762
+
763
+ const paramRe = /^:\w+$/;
764
+ const dynamicSegmentValue = 3;
765
+ const indexRouteValue = 2;
766
+ const emptySegmentValue = 1;
767
+ const staticSegmentValue = 10;
768
+ const splatPenalty = -2;
769
+
770
+ const isSplat = s => s === "*";
771
+
772
+ function computeScore(path, index) {
773
+ let segments = path.split("/");
774
+ let initialScore = segments.length;
775
+
776
+ if (segments.some(isSplat)) {
777
+ initialScore += splatPenalty;
778
+ }
779
+
780
+ if (index) {
781
+ initialScore += indexRouteValue;
782
+ }
783
+
784
+ return segments.filter(s => !isSplat(s)).reduce((score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), initialScore);
785
+ }
786
+
787
+ function compareIndexes(a, b) {
788
+ let siblings = a.length === b.length && a.slice(0, -1).every((n, i) => n === b[i]);
789
+ return siblings ? // If two routes are siblings, we should try to match the earlier sibling
790
+ // first. This allows people to have fine-grained control over the matching
791
+ // behavior by simply putting routes with identical paths in the order they
792
+ // want them tried.
793
+ a[a.length - 1] - b[b.length - 1] : // Otherwise, it doesn't really make sense to rank non-siblings by index,
794
+ // so they sort equally.
795
+ 0;
796
+ }
797
+
798
+ function matchRouteBranch(branch, pathname) {
799
+ let {
800
+ routesMeta
801
+ } = branch;
802
+ let matchedParams = {};
803
+ let matchedPathname = "/";
804
+ let matches = [];
805
+
806
+ for (let i = 0; i < routesMeta.length; ++i) {
807
+ let meta = routesMeta[i];
808
+ let end = i === routesMeta.length - 1;
809
+ let remainingPathname = matchedPathname === "/" ? pathname : pathname.slice(matchedPathname.length) || "/";
810
+ let match = matchPath({
811
+ path: meta.relativePath,
812
+ caseSensitive: meta.caseSensitive,
813
+ end
814
+ }, remainingPathname);
815
+ if (!match) return null;
816
+ Object.assign(matchedParams, match.params);
817
+ let route = meta.route;
818
+ matches.push({
819
+ // TODO: Can this as be avoided?
820
+ params: matchedParams,
821
+ pathname: joinPaths([matchedPathname, match.pathname]),
822
+ pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])),
823
+ route
824
+ });
825
+
826
+ if (match.pathnameBase !== "/") {
827
+ matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
828
+ }
829
+ }
830
+
831
+ return matches;
832
+ }
833
+ /**
834
+ * Returns a path with params interpolated.
835
+ *
836
+ * @see https://reactrouter.com/utils/generate-path
837
+ */
838
+
839
+
840
+ function generatePath(originalPath, params) {
841
+ if (params === void 0) {
842
+ params = {};
843
+ }
844
+
845
+ let path = originalPath;
846
+
847
+ if (path.endsWith("*") && path !== "*" && !path.endsWith("/*")) {
848
+ 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(/\*$/, "/*") + "\"."));
849
+ path = path.replace(/\*$/, "/*");
850
+ }
851
+
852
+ return path.replace(/^:(\w+)(\??)/g, (_, key, optional) => {
853
+ let param = params[key];
854
+
855
+ if (optional === "?") {
856
+ return param == null ? "" : param;
857
+ }
858
+
859
+ if (param == null) {
860
+ invariant(false, "Missing \":" + key + "\" param");
861
+ }
862
+
863
+ return param;
864
+ }).replace(/\/:(\w+)(\??)/g, (_, key, optional) => {
865
+ let param = params[key];
866
+
867
+ if (optional === "?") {
868
+ return param == null ? "" : "/" + param;
869
+ }
870
+
871
+ if (param == null) {
872
+ invariant(false, "Missing \":" + key + "\" param");
873
+ }
874
+
875
+ return "/" + param;
876
+ }) // Remove any optional markers from optional static segments
877
+ .replace(/\?/g, "").replace(/(\/?)\*/, (_, prefix, __, str) => {
878
+ const star = "*";
879
+
880
+ if (params[star] == null) {
881
+ // If no splat was provided, trim the trailing slash _unless_ it's
882
+ // the entire path
883
+ return str === "/*" ? "/" : "";
884
+ } // Apply the splat
885
+
886
+
887
+ return "" + prefix + params[star];
888
+ });
889
+ }
890
+ /**
891
+ * A PathPattern is used to match on some portion of a URL pathname.
892
+ */
893
+
894
+ /**
895
+ * Performs pattern matching on a URL pathname and returns information about
896
+ * the match.
897
+ *
898
+ * @see https://reactrouter.com/utils/match-path
899
+ */
900
+ function matchPath(pattern, pathname) {
901
+ if (typeof pattern === "string") {
902
+ pattern = {
903
+ path: pattern,
904
+ caseSensitive: false,
905
+ end: true
906
+ };
907
+ }
908
+
909
+ let [matcher, paramNames] = compilePath(pattern.path, pattern.caseSensitive, pattern.end);
910
+ let match = pathname.match(matcher);
911
+ if (!match) return null;
912
+ let matchedPathname = match[0];
913
+ let pathnameBase = matchedPathname.replace(/(.)\/+$/, "$1");
914
+ let captureGroups = match.slice(1);
915
+ let params = paramNames.reduce((memo, paramName, index) => {
916
+ // We need to compute the pathnameBase here using the raw splat value
917
+ // instead of using params["*"] later because it will be decoded then
918
+ if (paramName === "*") {
919
+ let splatValue = captureGroups[index] || "";
920
+ pathnameBase = matchedPathname.slice(0, matchedPathname.length - splatValue.length).replace(/(.)\/+$/, "$1");
921
+ }
922
+
923
+ memo[paramName] = safelyDecodeURIComponent(captureGroups[index] || "", paramName);
924
+ return memo;
925
+ }, {});
926
+ return {
927
+ params,
928
+ pathname: matchedPathname,
929
+ pathnameBase,
930
+ pattern
931
+ };
932
+ }
933
+
934
+ function compilePath(path, caseSensitive, end) {
935
+ if (caseSensitive === void 0) {
936
+ caseSensitive = false;
937
+ }
938
+
939
+ if (end === void 0) {
940
+ end = true;
941
+ }
942
+
943
+ 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(/\*$/, "/*") + "\"."));
944
+ let paramNames = [];
945
+ let regexpSource = "^" + path.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
946
+ .replace(/^\/*/, "/") // Make sure it has a leading /
947
+ .replace(/[\\.*+^$?{}|()[\]]/g, "\\$&") // Escape special regex chars
948
+ .replace(/\/:(\w+)/g, (_, paramName) => {
949
+ paramNames.push(paramName);
950
+ return "/([^\\/]+)";
951
+ });
952
+
953
+ if (path.endsWith("*")) {
954
+ paramNames.push("*");
955
+ regexpSource += path === "*" || path === "/*" ? "(.*)$" // Already matched the initial /, just match the rest
956
+ : "(?:\\/(.+)|\\/*)$"; // Don't include the / in params["*"]
957
+ } else if (end) {
958
+ // When matching to the end, ignore trailing slashes
959
+ regexpSource += "\\/*$";
960
+ } else if (path !== "" && path !== "/") {
961
+ // If our path is non-empty and contains anything beyond an initial slash,
962
+ // then we have _some_ form of path in our regex so we should expect to
963
+ // match only if we find the end of this path segment. Look for an optional
964
+ // non-captured trailing slash (to match a portion of the URL) or the end
965
+ // of the path (if we've matched to the end). We used to do this with a
966
+ // word boundary but that gives false positives on routes like
967
+ // /user-preferences since `-` counts as a word boundary.
968
+ regexpSource += "(?:(?=\\/|$))";
969
+ } else ;
970
+
971
+ let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
972
+ return [matcher, paramNames];
973
+ }
974
+
975
+ function safelyDecodeURI(value) {
976
+ try {
977
+ return decodeURI(value);
978
+ } catch (error) {
979
+ 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 + ")."));
980
+ return value;
981
+ }
982
+ }
983
+
984
+ function safelyDecodeURIComponent(value, paramName) {
985
+ try {
986
+ return decodeURIComponent(value);
987
+ } catch (error) {
988
+ warning(false, "The value for the URL param \"" + paramName + "\" will not be decoded because" + (" the string \"" + value + "\" is a malformed URL segment. This is probably") + (" due to a bad percent encoding (" + error + ")."));
989
+ return value;
990
+ }
991
+ }
992
+ /**
993
+ * @private
994
+ */
995
+
996
+
997
+ function stripBasename(pathname, basename) {
998
+ if (basename === "/") return pathname;
999
+
1000
+ if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
1001
+ return null;
1002
+ } // We want to leave trailing slash behavior in the user's control, so if they
1003
+ // specify a basename with a trailing slash, we should support it
1004
+
1005
+
1006
+ let startIndex = basename.endsWith("/") ? basename.length - 1 : basename.length;
1007
+ let nextChar = pathname.charAt(startIndex);
1008
+
1009
+ if (nextChar && nextChar !== "/") {
1010
+ // pathname does not start with basename/
1011
+ return null;
1012
+ }
1013
+
1014
+ return pathname.slice(startIndex) || "/";
1015
+ }
1016
+ /**
1017
+ * @private
1018
+ */
1019
+
1020
+ function warning(cond, message) {
1021
+ if (!cond) {
1022
+ // eslint-disable-next-line no-console
1023
+ if (typeof console !== "undefined") console.warn(message);
1024
+
1025
+ try {
1026
+ // Welcome to debugging @remix-run/router!
1027
+ //
1028
+ // This error is thrown as a convenience so you can more easily
1029
+ // find the source for a warning that appears in the console by
1030
+ // enabling "pause on exceptions" in your JavaScript debugger.
1031
+ throw new Error(message); // eslint-disable-next-line no-empty
1032
+ } catch (e) {}
1033
+ }
1034
+ }
1035
+ /**
1036
+ * Returns a resolved path object relative to the given pathname.
1037
+ *
1038
+ * @see https://reactrouter.com/utils/resolve-path
1039
+ */
1040
+
1041
+ function resolvePath(to, fromPathname) {
1042
+ if (fromPathname === void 0) {
1043
+ fromPathname = "/";
1044
+ }
1045
+
1046
+ let {
1047
+ pathname: toPathname,
1048
+ search = "",
1049
+ hash = ""
1050
+ } = typeof to === "string" ? parsePath(to) : to;
1051
+ let pathname = toPathname ? toPathname.startsWith("/") ? toPathname : resolvePathname(toPathname, fromPathname) : fromPathname;
1052
+ return {
1053
+ pathname,
1054
+ search: normalizeSearch(search),
1055
+ hash: normalizeHash(hash)
1056
+ };
1057
+ }
1058
+
1059
+ function resolvePathname(relativePath, fromPathname) {
1060
+ let segments = fromPathname.replace(/\/+$/, "").split("/");
1061
+ let relativeSegments = relativePath.split("/");
1062
+ relativeSegments.forEach(segment => {
1063
+ if (segment === "..") {
1064
+ // Keep the root "" segment so the pathname starts at /
1065
+ if (segments.length > 1) segments.pop();
1066
+ } else if (segment !== ".") {
1067
+ segments.push(segment);
1068
+ }
1069
+ });
1070
+ return segments.length > 1 ? segments.join("/") : "/";
1071
+ }
1072
+
1073
+ function getInvalidPathError(char, field, dest, path) {
1074
+ 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.";
1075
+ }
1076
+ /**
1077
+ * @private
1078
+ *
1079
+ * When processing relative navigation we want to ignore ancestor routes that
1080
+ * do not contribute to the path, such that index/pathless layout routes don't
1081
+ * interfere.
1082
+ *
1083
+ * For example, when moving a route element into an index route and/or a
1084
+ * pathless layout route, relative link behavior contained within should stay
1085
+ * the same. Both of the following examples should link back to the root:
1086
+ *
1087
+ * <Route path="/">
1088
+ * <Route path="accounts" element={<Link to=".."}>
1089
+ * </Route>
1090
+ *
1091
+ * <Route path="/">
1092
+ * <Route path="accounts">
1093
+ * <Route element={<AccountsLayout />}> // <-- Does not contribute
1094
+ * <Route index element={<Link to=".."} /> // <-- Does not contribute
1095
+ * </Route
1096
+ * </Route>
1097
+ * </Route>
1098
+ */
1099
+
1100
+
1101
+ function getPathContributingMatches(matches) {
1102
+ return matches.filter((match, index) => index === 0 || match.route.path && match.route.path.length > 0);
1103
+ }
1104
+ /**
1105
+ * @private
1106
+ */
1107
+
1108
+ function resolveTo(toArg, routePathnames, locationPathname, isPathRelative) {
1109
+ if (isPathRelative === void 0) {
1110
+ isPathRelative = false;
1111
+ }
1112
+
1113
+ let to;
1114
+
1115
+ if (typeof toArg === "string") {
1116
+ to = parsePath(toArg);
1117
+ } else {
1118
+ to = _extends({}, toArg);
1119
+ invariant(!to.pathname || !to.pathname.includes("?"), getInvalidPathError("?", "pathname", "search", to));
1120
+ invariant(!to.pathname || !to.pathname.includes("#"), getInvalidPathError("#", "pathname", "hash", to));
1121
+ invariant(!to.search || !to.search.includes("#"), getInvalidPathError("#", "search", "hash", to));
1122
+ }
1123
+
1124
+ let isEmptyPath = toArg === "" || to.pathname === "";
1125
+ let toPathname = isEmptyPath ? "/" : to.pathname;
1126
+ let from; // Routing is relative to the current pathname if explicitly requested.
1127
+ //
1128
+ // If a pathname is explicitly provided in `to`, it should be relative to the
1129
+ // route context. This is explained in `Note on `<Link to>` values` in our
1130
+ // migration guide from v5 as a means of disambiguation between `to` values
1131
+ // that begin with `/` and those that do not. However, this is problematic for
1132
+ // `to` values that do not provide a pathname. `to` can simply be a search or
1133
+ // hash string, in which case we should assume that the navigation is relative
1134
+ // to the current location's pathname and *not* the route pathname.
1135
+
1136
+ if (isPathRelative || toPathname == null) {
1137
+ from = locationPathname;
1138
+ } else {
1139
+ let routePathnameIndex = routePathnames.length - 1;
1140
+
1141
+ if (toPathname.startsWith("..")) {
1142
+ let toSegments = toPathname.split("/"); // Each leading .. segment means "go up one route" instead of "go up one
1143
+ // URL segment". This is a key difference from how <a href> works and a
1144
+ // major reason we call this a "to" value instead of a "href".
1145
+
1146
+ while (toSegments[0] === "..") {
1147
+ toSegments.shift();
1148
+ routePathnameIndex -= 1;
1149
+ }
1150
+
1151
+ to.pathname = toSegments.join("/");
1152
+ } // If there are more ".." segments than parent routes, resolve relative to
1153
+ // the root / URL.
1154
+
1155
+
1156
+ from = routePathnameIndex >= 0 ? routePathnames[routePathnameIndex] : "/";
1157
+ }
1158
+
1159
+ let path = resolvePath(to, from); // Ensure the pathname has a trailing slash if the original "to" had one
1160
+
1161
+ let hasExplicitTrailingSlash = toPathname && toPathname !== "/" && toPathname.endsWith("/"); // Or if this was a link to the current path which has a trailing slash
1162
+
1163
+ let hasCurrentTrailingSlash = (isEmptyPath || toPathname === ".") && locationPathname.endsWith("/");
1164
+
1165
+ if (!path.pathname.endsWith("/") && (hasExplicitTrailingSlash || hasCurrentTrailingSlash)) {
1166
+ path.pathname += "/";
1167
+ }
1168
+
1169
+ return path;
1170
+ }
1171
+ /**
1172
+ * @private
1173
+ */
1174
+
1175
+ function getToPathname(to) {
1176
+ // Empty strings should be treated the same as / paths
1177
+ return to === "" || to.pathname === "" ? "/" : typeof to === "string" ? parsePath(to).pathname : to.pathname;
1178
+ }
1179
+ /**
1180
+ * @private
1181
+ */
1182
+
1183
+ const joinPaths = paths => paths.join("/").replace(/\/\/+/g, "/");
1184
+ /**
1185
+ * @private
1186
+ */
1187
+
1188
+ const normalizePathname = pathname => pathname.replace(/\/+$/, "").replace(/^\/*/, "/");
1189
+ /**
1190
+ * @private
1191
+ */
1192
+
1193
+ const normalizeSearch = search => !search || search === "?" ? "" : search.startsWith("?") ? search : "?" + search;
1194
+ /**
1195
+ * @private
1196
+ */
1197
+
1198
+ const normalizeHash = hash => !hash || hash === "#" ? "" : hash.startsWith("#") ? hash : "#" + hash;
1199
+
1200
+ /**
1201
+ * This is a shortcut for creating `application/json` responses. Converts `data`
1202
+ * to JSON and sets the `Content-Type` header.
1203
+ */
1204
+ const json = function json(data, init) {
1205
+ if (init === void 0) {
1206
+ init = {};
1207
+ }
1208
+
1209
+ let responseInit = typeof init === "number" ? {
1210
+ status: init
1211
+ } : init;
1212
+ let headers = new Headers(responseInit.headers);
1213
+
1214
+ if (!headers.has("Content-Type")) {
1215
+ headers.set("Content-Type", "application/json; charset=utf-8");
1216
+ }
1217
+
1218
+ return new Response(JSON.stringify(data), _extends({}, responseInit, {
1219
+ headers
1220
+ }));
1221
+ };
1222
+ class AbortedDeferredError extends Error {}
1223
+ class DeferredData {
1224
+ constructor(data, responseInit) {
1225
+ this.pendingKeysSet = new Set();
1226
+ this.subscribers = new Set();
1227
+ this.deferredKeys = [];
1228
+ invariant(data && typeof data === "object" && !Array.isArray(data), "defer() only accepts plain objects"); // Set up an AbortController + Promise we can race against to exit early
1229
+ // cancellation
1230
+
1231
+ let reject;
1232
+ this.abortPromise = new Promise((_, r) => reject = r);
1233
+ this.controller = new AbortController();
1234
+
1235
+ let onAbort = () => reject(new AbortedDeferredError("Deferred data aborted"));
1236
+
1237
+ this.unlistenAbortSignal = () => this.controller.signal.removeEventListener("abort", onAbort);
1238
+
1239
+ this.controller.signal.addEventListener("abort", onAbort);
1240
+ this.data = Object.entries(data).reduce((acc, _ref) => {
1241
+ let [key, value] = _ref;
1242
+ return Object.assign(acc, {
1243
+ [key]: this.trackPromise(key, value)
1244
+ });
1245
+ }, {});
1246
+
1247
+ if (this.done) {
1248
+ // All incoming values were resolved
1249
+ this.unlistenAbortSignal();
1250
+ }
1251
+
1252
+ this.init = responseInit;
1253
+ }
1254
+
1255
+ trackPromise(key, value) {
1256
+ if (!(value instanceof Promise)) {
1257
+ return value;
1258
+ }
1259
+
1260
+ this.deferredKeys.push(key);
1261
+ this.pendingKeysSet.add(key); // We store a little wrapper promise that will be extended with
1262
+ // _data/_error props upon resolve/reject
1263
+
1264
+ let promise = Promise.race([value, this.abortPromise]).then(data => this.onSettle(promise, key, null, data), error => this.onSettle(promise, key, error)); // Register rejection listeners to avoid uncaught promise rejections on
1265
+ // errors or aborted deferred values
1266
+
1267
+ promise.catch(() => {});
1268
+ Object.defineProperty(promise, "_tracked", {
1269
+ get: () => true
1270
+ });
1271
+ return promise;
1272
+ }
1273
+
1274
+ onSettle(promise, key, error, data) {
1275
+ if (this.controller.signal.aborted && error instanceof AbortedDeferredError) {
1276
+ this.unlistenAbortSignal();
1277
+ Object.defineProperty(promise, "_error", {
1278
+ get: () => error
1279
+ });
1280
+ return Promise.reject(error);
1281
+ }
1282
+
1283
+ this.pendingKeysSet.delete(key);
1284
+
1285
+ if (this.done) {
1286
+ // Nothing left to abort!
1287
+ this.unlistenAbortSignal();
1288
+ }
1289
+
1290
+ if (error) {
1291
+ Object.defineProperty(promise, "_error", {
1292
+ get: () => error
1293
+ });
1294
+ this.emit(false, key);
1295
+ return Promise.reject(error);
1296
+ }
1297
+
1298
+ Object.defineProperty(promise, "_data", {
1299
+ get: () => data
1300
+ });
1301
+ this.emit(false, key);
1302
+ return data;
1303
+ }
1304
+
1305
+ emit(aborted, settledKey) {
1306
+ this.subscribers.forEach(subscriber => subscriber(aborted, settledKey));
1307
+ }
1308
+
1309
+ subscribe(fn) {
1310
+ this.subscribers.add(fn);
1311
+ return () => this.subscribers.delete(fn);
1312
+ }
1313
+
1314
+ cancel() {
1315
+ this.controller.abort();
1316
+ this.pendingKeysSet.forEach((v, k) => this.pendingKeysSet.delete(k));
1317
+ this.emit(true);
1318
+ }
1319
+
1320
+ async resolveData(signal) {
1321
+ let aborted = false;
1322
+
1323
+ if (!this.done) {
1324
+ let onAbort = () => this.cancel();
1325
+
1326
+ signal.addEventListener("abort", onAbort);
1327
+ aborted = await new Promise(resolve => {
1328
+ this.subscribe(aborted => {
1329
+ signal.removeEventListener("abort", onAbort);
1330
+
1331
+ if (aborted || this.done) {
1332
+ resolve(aborted);
1333
+ }
1334
+ });
1335
+ });
1336
+ }
1337
+
1338
+ return aborted;
1339
+ }
1340
+
1341
+ get done() {
1342
+ return this.pendingKeysSet.size === 0;
1343
+ }
1344
+
1345
+ get unwrappedData() {
1346
+ invariant(this.data !== null && this.done, "Can only unwrap data on initialized and settled deferreds");
1347
+ return Object.entries(this.data).reduce((acc, _ref2) => {
1348
+ let [key, value] = _ref2;
1349
+ return Object.assign(acc, {
1350
+ [key]: unwrapTrackedPromise(value)
1351
+ });
1352
+ }, {});
1353
+ }
1354
+
1355
+ get pendingKeys() {
1356
+ return Array.from(this.pendingKeysSet);
1357
+ }
1358
+
1359
+ }
1360
+
1361
+ function isTrackedPromise(value) {
1362
+ return value instanceof Promise && value._tracked === true;
1363
+ }
1364
+
1365
+ function unwrapTrackedPromise(value) {
1366
+ if (!isTrackedPromise(value)) {
1367
+ return value;
1368
+ }
1369
+
1370
+ if (value._error) {
1371
+ throw value._error;
1372
+ }
1373
+
1374
+ return value._data;
1375
+ }
1376
+
1377
+ const defer = function defer(data, init) {
1378
+ if (init === void 0) {
1379
+ init = {};
1380
+ }
1381
+
1382
+ let responseInit = typeof init === "number" ? {
1383
+ status: init
1384
+ } : init;
1385
+ return new DeferredData(data, responseInit);
1386
+ };
1387
+
1388
+ /**
1389
+ * A redirect response. Sets the status code and the `Location` header.
1390
+ * Defaults to "302 Found".
1391
+ */
1392
+ const redirect = function redirect(url, init) {
1393
+ if (init === void 0) {
1394
+ init = 302;
1395
+ }
1396
+
1397
+ let responseInit = init;
1398
+
1399
+ if (typeof responseInit === "number") {
1400
+ responseInit = {
1401
+ status: responseInit
1402
+ };
1403
+ } else if (typeof responseInit.status === "undefined") {
1404
+ responseInit.status = 302;
1405
+ }
1406
+
1407
+ let headers = new Headers(responseInit.headers);
1408
+ headers.set("Location", url);
1409
+ return new Response(null, _extends({}, responseInit, {
1410
+ headers
1411
+ }));
1412
+ };
1413
+ /**
1414
+ * @private
1415
+ * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
1416
+ */
1417
+
1418
+ class ErrorResponse {
1419
+ constructor(status, statusText, data, internal) {
1420
+ if (internal === void 0) {
1421
+ internal = false;
1422
+ }
1423
+
1424
+ this.status = status;
1425
+ this.statusText = statusText || "";
1426
+ this.internal = internal;
1427
+
1428
+ if (data instanceof Error) {
1429
+ this.data = data.toString();
1430
+ this.error = data;
1431
+ } else {
1432
+ this.data = data;
1433
+ }
1434
+ }
1435
+
1436
+ }
1437
+ /**
1438
+ * Check if the given error is an ErrorResponse generated from a 4xx/5xx
1439
+ * Response thrown from an action/loader
1440
+ */
1441
+
1442
+ function isRouteErrorResponse(error) {
1443
+ return error != null && typeof error.status === "number" && typeof error.statusText === "string" && typeof error.internal === "boolean" && "data" in error;
1444
+ }
1445
+ /**
1446
+ * Context object passed through middleware functions and into action/loaders.
1447
+ *
1448
+ * Supports only key/value for now, eventually will be enhanced
1449
+ */
1450
+
1451
+ /**
1452
+ * Generic class to "hold" a default middleware value and the generic type so
1453
+ * we can enforce typings on middleware.get/set
1454
+ */
1455
+ class MiddlewareContextInstance {
1456
+ constructor(defaultValue) {
1457
+ if (typeof defaultValue !== "undefined") {
1458
+ this.defaultValue = defaultValue;
1459
+ }
1460
+ }
1461
+
1462
+ getDefaultValue() {
1463
+ if (typeof this.defaultValue === "undefined") {
1464
+ throw new Error("Unable to find a value in the middleware context");
1465
+ }
1466
+
1467
+ return this.defaultValue;
1468
+ }
1469
+
1470
+ }
1471
+ /**
1472
+ * Create a middleware context that can be used as a "key" to set/get middleware
1473
+ * values in a strongly-typed fashion
1474
+ */
1475
+
1476
+ function createMiddlewareContext(defaultValue) {
1477
+ return new MiddlewareContextInstance(defaultValue);
1478
+ }
1479
+ /**
1480
+ * @internal
1481
+ * PRIVATE - DO NOT USE
1482
+ *
1483
+ * Create a middleware "context" to store values and provide a next() hook
1484
+ */
1485
+
1486
+ function createMiddlewareStore(initialMiddlewareContext) {
1487
+ let store = new Map(initialMiddlewareContext == null ? void 0 : initialMiddlewareContext.entries());
1488
+ let middlewareContext = {
1489
+ get(k) {
1490
+ if (store.has(k)) {
1491
+ return store.get(k);
1492
+ }
1493
+
1494
+ return k.getDefaultValue();
1495
+ },
1496
+
1497
+ set(k, v) {
1498
+ if (typeof v === "undefined") {
1499
+ throw new Error("You cannot set an undefined value in the middleware context");
1500
+ }
1501
+
1502
+ store.set(k, v);
1503
+ },
1504
+
1505
+ next: () => {},
1506
+ entries: () => store.entries()
1507
+ };
1508
+ return middlewareContext;
1509
+ }
1510
+
1511
+ //#region Types and Constants
1512
+ ////////////////////////////////////////////////////////////////////////////////
1513
+
1514
+ /**
1515
+ * A Router instance manages all navigation and data loading/mutations
1516
+ */
1517
+
1518
+ const defaultFutureConfig = {
1519
+ unstable_middleware: false
1520
+ };
1521
+ const validMutationMethodsArr = ["post", "put", "patch", "delete"];
1522
+ const validMutationMethods = new Set(validMutationMethodsArr);
1523
+ const validRequestMethodsArr = ["get", ...validMutationMethodsArr];
1524
+ const validRequestMethods = new Set(validRequestMethodsArr);
1525
+ const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
1526
+ const redirectPreserveMethodStatusCodes = new Set([307, 308]);
1527
+ const IDLE_NAVIGATION = {
1528
+ state: "idle",
1529
+ location: undefined,
1530
+ formMethod: undefined,
1531
+ formAction: undefined,
1532
+ formEncType: undefined,
1533
+ formData: undefined
1534
+ };
1535
+ const IDLE_FETCHER = {
1536
+ state: "idle",
1537
+ data: undefined,
1538
+ formMethod: undefined,
1539
+ formAction: undefined,
1540
+ formEncType: undefined,
1541
+ formData: undefined
1542
+ };
1543
+ const IDLE_BLOCKER = {
1544
+ state: "unblocked",
1545
+ proceed: undefined,
1546
+ reset: undefined,
1547
+ location: undefined
1548
+ };
1549
+ const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
1550
+ const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined";
1551
+ const isServer = !isBrowser; //#endregion
1552
+ ////////////////////////////////////////////////////////////////////////////////
1553
+ //#region createRouter
1554
+ ////////////////////////////////////////////////////////////////////////////////
1555
+
1556
+ /**
1557
+ * Create a router and listen to history POP navigations
1558
+ */
1559
+
1560
+ function createRouter(init) {
1561
+ invariant(init.routes.length > 0, "You must provide a non-empty routes array to createRouter");
1562
+ let dataRoutes = convertRoutesToDataRoutes(init.routes);
1563
+
1564
+ let future = _extends({}, defaultFutureConfig, init.future); // Cleanup function for history
1565
+
1566
+
1567
+ let unlistenHistory = null; // Externally-provided functions to call on all state changes
1568
+
1569
+ let subscribers = new Set(); // Externally-provided object to hold scroll restoration locations during routing
1570
+
1571
+ let savedScrollPositions = null; // Externally-provided function to get scroll restoration keys
1572
+
1573
+ let getScrollRestorationKey = null; // Externally-provided function to get current scroll position
1574
+
1575
+ let getScrollPosition = null; // One-time flag to control the initial hydration scroll restoration. Because
1576
+ // we don't get the saved positions from <ScrollRestoration /> until _after_
1577
+ // the initial render, we need to manually trigger a separate updateState to
1578
+ // send along the restoreScrollPosition
1579
+ // Set to true if we have `hydrationData` since we assume we were SSR'd and that
1580
+ // SSR did the initial scroll restoration.
1581
+
1582
+ let initialScrollRestored = init.hydrationData != null;
1583
+ let initialMatches = matchRoutes(dataRoutes, init.history.location, init.basename);
1584
+ let initialErrors = null;
1585
+
1586
+ if (initialMatches == null) {
1587
+ // If we do not match a user-provided-route, fall back to the root
1588
+ // to allow the error boundary to take over
1589
+ let error = getInternalRouterError(404, {
1590
+ pathname: init.history.location.pathname
1591
+ });
1592
+ let {
1593
+ matches,
1594
+ route
1595
+ } = getShortCircuitMatches(dataRoutes);
1596
+ initialMatches = matches;
1597
+ initialErrors = {
1598
+ [route.id]: error
1599
+ };
1600
+ }
1601
+
1602
+ let initialized = !initialMatches.some(m => m.route.loader) || init.hydrationData != null;
1603
+ let router;
1604
+ let state = {
1605
+ historyAction: init.history.action,
1606
+ location: init.history.location,
1607
+ matches: initialMatches,
1608
+ initialized,
1609
+ navigation: IDLE_NAVIGATION,
1610
+ // Don't restore on initial updateState() if we were SSR'd
1611
+ restoreScrollPosition: init.hydrationData != null ? false : null,
1612
+ preventScrollReset: false,
1613
+ revalidation: "idle",
1614
+ loaderData: init.hydrationData && init.hydrationData.loaderData || {},
1615
+ actionData: init.hydrationData && init.hydrationData.actionData || null,
1616
+ errors: init.hydrationData && init.hydrationData.errors || initialErrors,
1617
+ fetchers: new Map(),
1618
+ blockers: new Map()
1619
+ }; // -- Stateful internal variables to manage navigations --
1620
+ // Current navigation in progress (to be committed in completeNavigation)
1621
+
1622
+ let pendingAction = exports.Action.Pop; // Should the current navigation prevent the scroll reset if scroll cannot
1623
+ // be restored?
1624
+
1625
+ let pendingPreventScrollReset = false; // AbortController for the active navigation
1626
+
1627
+ let pendingNavigationController; // We use this to avoid touching history in completeNavigation if a
1628
+ // revalidation is entirely uninterrupted
1629
+
1630
+ let isUninterruptedRevalidation = false; // Use this internal flag to force revalidation of all loaders:
1631
+ // - submissions (completed or interrupted)
1632
+ // - useRevalidate()
1633
+ // - X-Remix-Revalidate (from redirect)
1634
+
1635
+ let isRevalidationRequired = false; // Use this internal array to capture routes that require revalidation due
1636
+ // to a cancelled deferred on action submission
1637
+
1638
+ let cancelledDeferredRoutes = []; // Use this internal array to capture fetcher loads that were cancelled by an
1639
+ // action navigation and require revalidation
1640
+
1641
+ let cancelledFetcherLoads = []; // AbortControllers for any in-flight fetchers
1642
+
1643
+ let fetchControllers = new Map(); // Track loads based on the order in which they started
1644
+
1645
+ let incrementingLoadId = 0; // Track the outstanding pending navigation data load to be compared against
1646
+ // the globally incrementing load when a fetcher load lands after a completed
1647
+ // navigation
1648
+
1649
+ let pendingNavigationLoadId = -1; // Fetchers that triggered data reloads as a result of their actions
1650
+
1651
+ let fetchReloadIds = new Map(); // Fetchers that triggered redirect navigations from their actions
1652
+
1653
+ let fetchRedirectIds = new Set(); // Most recent href/match for fetcher.load calls for fetchers
1654
+
1655
+ let fetchLoadMatches = new Map(); // Store DeferredData instances for active route matches. When a
1656
+ // route loader returns defer() we stick one in here. Then, when a nested
1657
+ // promise resolves we update loaderData. If a new navigation starts we
1658
+ // cancel active deferreds for eliminated routes.
1659
+
1660
+ let activeDeferreds = new Map(); // Store blocker functions in a separate Map outside of router state since
1661
+ // we don't need to update UI state if they change
1662
+
1663
+ let blockerFunctions = new Map(); // Flag to ignore the next history update, so we can revert the URL change on
1664
+ // a POP navigation that was blocked by the user without touching router state
1665
+
1666
+ let ignoreNextHistoryUpdate = false; // Initialize the router, all side effects should be kicked off from here.
1667
+ // Implemented as a Fluent API for ease of:
1668
+ // let router = createRouter(init).initialize();
1669
+
1670
+ function initialize() {
1671
+ // If history informs us of a POP navigation, start the navigation but do not update
1672
+ // state. We'll update our own state once the navigation completes
1673
+ unlistenHistory = init.history.listen(_ref => {
1674
+ let {
1675
+ action: historyAction,
1676
+ location,
1677
+ delta
1678
+ } = _ref;
1679
+
1680
+ // Ignore this event if it was just us resetting the URL from a
1681
+ // blocked POP navigation
1682
+ if (ignoreNextHistoryUpdate) {
1683
+ ignoreNextHistoryUpdate = false;
1684
+ return;
1685
+ }
1686
+
1687
+ 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.");
1688
+ let blockerKey = shouldBlockNavigation({
1689
+ currentLocation: state.location,
1690
+ nextLocation: location,
1691
+ historyAction
1692
+ });
1693
+
1694
+ if (blockerKey && delta != null) {
1695
+ // Restore the URL to match the current UI, but don't update router state
1696
+ ignoreNextHistoryUpdate = true;
1697
+ init.history.go(delta * -1); // Put the blocker into a blocked state
1698
+
1699
+ updateBlocker(blockerKey, {
1700
+ state: "blocked",
1701
+ location,
1702
+
1703
+ proceed() {
1704
+ updateBlocker(blockerKey, {
1705
+ state: "proceeding",
1706
+ proceed: undefined,
1707
+ reset: undefined,
1708
+ location
1709
+ }); // Re-do the same POP navigation we just blocked
1710
+
1711
+ init.history.go(delta);
1712
+ },
1713
+
1714
+ reset() {
1715
+ deleteBlocker(blockerKey);
1716
+ updateState({
1717
+ blockers: new Map(router.state.blockers)
1718
+ });
1719
+ }
1720
+
1721
+ });
1722
+ return;
1723
+ }
1724
+
1725
+ return startNavigation(historyAction, location);
1726
+ }); // Kick off initial data load if needed. Use Pop to avoid modifying history
1727
+
1728
+ if (!state.initialized) {
1729
+ startNavigation(exports.Action.Pop, state.location);
1730
+ }
1731
+
1732
+ return router;
1733
+ } // Clean up a router and it's side effects
1734
+
1735
+
1736
+ function dispose() {
1737
+ if (unlistenHistory) {
1738
+ unlistenHistory();
1739
+ }
1740
+
1741
+ subscribers.clear();
1742
+ pendingNavigationController && pendingNavigationController.abort();
1743
+ state.fetchers.forEach((_, key) => deleteFetcher(key));
1744
+ state.blockers.forEach((_, key) => deleteBlocker(key));
1745
+ } // Subscribe to state updates for the router
1746
+
1747
+
1748
+ function subscribe(fn) {
1749
+ subscribers.add(fn);
1750
+ return () => subscribers.delete(fn);
1751
+ } // Update our state and notify the calling context of the change
1752
+
1753
+
1754
+ function updateState(newState) {
1755
+ state = _extends({}, state, newState);
1756
+ subscribers.forEach(subscriber => subscriber(state));
1757
+ } // Complete a navigation returning the state.navigation back to the IDLE_NAVIGATION
1758
+ // and setting state.[historyAction/location/matches] to the new route.
1759
+ // - Location is a required param
1760
+ // - Navigation will always be set to IDLE_NAVIGATION
1761
+ // - Can pass any other state in newState
1762
+
1763
+
1764
+ function completeNavigation(location, newState) {
1765
+ var _location$state, _location$state2;
1766
+
1767
+ // Deduce if we're in a loading/actionReload state:
1768
+ // - We have committed actionData in the store
1769
+ // - The current navigation was a mutation submission
1770
+ // - We're past the submitting state and into the loading state
1771
+ // - The location being loaded is not the result of a redirect
1772
+ 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;
1773
+ let actionData;
1774
+
1775
+ if (newState.actionData) {
1776
+ if (Object.keys(newState.actionData).length > 0) {
1777
+ actionData = newState.actionData;
1778
+ } else {
1779
+ // Empty actionData -> clear prior actionData due to an action error
1780
+ actionData = null;
1781
+ }
1782
+ } else if (isActionReload) {
1783
+ // Keep the current data if we're wrapping up the action reload
1784
+ actionData = state.actionData;
1785
+ } else {
1786
+ // Clear actionData on any other completed navigations
1787
+ actionData = null;
1788
+ } // Always preserve any existing loaderData from re-used routes
1789
+
1790
+
1791
+ let loaderData = newState.loaderData ? mergeLoaderData(state.loaderData, newState.loaderData, newState.matches || [], newState.errors) : state.loaderData; // On a successful navigation we can assume we got through all blockers
1792
+ // so we can start fresh
1793
+
1794
+ for (let [key] of blockerFunctions) {
1795
+ deleteBlocker(key);
1796
+ } // Always respect the user flag. Otherwise don't reset on mutation
1797
+ // submission navigations unless they redirect
1798
+
1799
+
1800
+ let preventScrollReset = pendingPreventScrollReset === true || state.navigation.formMethod != null && isMutationMethod(state.navigation.formMethod) && ((_location$state2 = location.state) == null ? void 0 : _location$state2._isRedirect) !== true;
1801
+ updateState(_extends({}, newState, {
1802
+ // matches, errors, fetchers go through as-is
1803
+ actionData,
1804
+ loaderData,
1805
+ historyAction: pendingAction,
1806
+ location,
1807
+ initialized: true,
1808
+ navigation: IDLE_NAVIGATION,
1809
+ revalidation: "idle",
1810
+ restoreScrollPosition: getSavedScrollPosition(location, newState.matches || state.matches),
1811
+ preventScrollReset,
1812
+ blockers: new Map(state.blockers)
1813
+ }));
1814
+
1815
+ if (isUninterruptedRevalidation) ; else if (pendingAction === exports.Action.Pop) ; else if (pendingAction === exports.Action.Push) {
1816
+ init.history.push(location, location.state);
1817
+ } else if (pendingAction === exports.Action.Replace) {
1818
+ init.history.replace(location, location.state);
1819
+ } // Reset stateful navigation vars
1820
+
1821
+
1822
+ pendingAction = exports.Action.Pop;
1823
+ pendingPreventScrollReset = false;
1824
+ isUninterruptedRevalidation = false;
1825
+ isRevalidationRequired = false;
1826
+ cancelledDeferredRoutes = [];
1827
+ cancelledFetcherLoads = [];
1828
+ } // Trigger a navigation event, which can either be a numerical POP or a PUSH
1829
+ // replace with an optional submission
1830
+
1831
+
1832
+ async function navigate(to, opts) {
1833
+ if (typeof to === "number") {
1834
+ init.history.go(to);
1835
+ return;
1836
+ }
1837
+
1838
+ let {
1839
+ path,
1840
+ submission,
1841
+ error
1842
+ } = normalizeNavigateOptions(to, opts);
1843
+ let currentLocation = state.location;
1844
+ let nextLocation = createLocation(state.location, path, opts && opts.state); // When using navigate as a PUSH/REPLACE we aren't reading an already-encoded
1845
+ // URL from window.location, so we need to encode it here so the behavior
1846
+ // remains the same as POP and non-data-router usages. new URL() does all
1847
+ // the same encoding we'd get from a history.pushState/window.location read
1848
+ // without having to touch history
1849
+
1850
+ nextLocation = _extends({}, nextLocation, init.history.encodeLocation(nextLocation));
1851
+ let userReplace = opts && opts.replace != null ? opts.replace : undefined;
1852
+ let historyAction = exports.Action.Push;
1853
+
1854
+ if (userReplace === true) {
1855
+ historyAction = exports.Action.Replace;
1856
+ } else if (userReplace === false) ; else if (submission != null && isMutationMethod(submission.formMethod) && submission.formAction === state.location.pathname + state.location.search) {
1857
+ // By default on submissions to the current location we REPLACE so that
1858
+ // users don't have to double-click the back button to get to the prior
1859
+ // location. If the user redirects to a different location from the
1860
+ // action/loader this will be ignored and the redirect will be a PUSH
1861
+ historyAction = exports.Action.Replace;
1862
+ }
1863
+
1864
+ let preventScrollReset = opts && "preventScrollReset" in opts ? opts.preventScrollReset === true : undefined;
1865
+ let blockerKey = shouldBlockNavigation({
1866
+ currentLocation,
1867
+ nextLocation,
1868
+ historyAction
1869
+ });
1870
+
1871
+ if (blockerKey) {
1872
+ // Put the blocker into a blocked state
1873
+ updateBlocker(blockerKey, {
1874
+ state: "blocked",
1875
+ location: nextLocation,
1876
+
1877
+ proceed() {
1878
+ updateBlocker(blockerKey, {
1879
+ state: "proceeding",
1880
+ proceed: undefined,
1881
+ reset: undefined,
1882
+ location: nextLocation
1883
+ }); // Send the same navigation through
1884
+
1885
+ navigate(to, opts);
1886
+ },
1887
+
1888
+ reset() {
1889
+ deleteBlocker(blockerKey);
1890
+ updateState({
1891
+ blockers: new Map(state.blockers)
1892
+ });
1893
+ }
1894
+
1895
+ });
1896
+ return;
1897
+ }
1898
+
1899
+ return await startNavigation(historyAction, nextLocation, {
1900
+ submission,
1901
+ // Send through the formData serialization error if we have one so we can
1902
+ // render at the right error boundary after we match routes
1903
+ pendingError: error,
1904
+ preventScrollReset,
1905
+ replace: opts && opts.replace
1906
+ });
1907
+ } // Revalidate all current loaders. If a navigation is in progress or if this
1908
+ // is interrupted by a navigation, allow this to "succeed" by calling all
1909
+ // loaders during the next loader round
1910
+
1911
+
1912
+ function revalidate() {
1913
+ interruptActiveLoads();
1914
+ updateState({
1915
+ revalidation: "loading"
1916
+ }); // If we're currently submitting an action, we don't need to start a new
1917
+ // navigation, we'll just let the follow up loader execution call all loaders
1918
+
1919
+ if (state.navigation.state === "submitting") {
1920
+ return;
1921
+ } // If we're currently in an idle state, start a new navigation for the current
1922
+ // action/location and mark it as uninterrupted, which will skip the history
1923
+ // update in completeNavigation
1924
+
1925
+
1926
+ if (state.navigation.state === "idle") {
1927
+ startNavigation(state.historyAction, state.location, {
1928
+ startUninterruptedRevalidation: true
1929
+ });
1930
+ return;
1931
+ } // Otherwise, if we're currently in a loading state, just start a new
1932
+ // navigation to the navigation.location but do not trigger an uninterrupted
1933
+ // revalidation so that history correctly updates once the navigation completes
1934
+
1935
+
1936
+ startNavigation(pendingAction || state.historyAction, state.navigation.location, {
1937
+ overrideNavigation: state.navigation
1938
+ });
1939
+ } // Start a navigation to the given action/location. Can optionally provide a
1940
+ // overrideNavigation which will override the normalLoad in the case of a redirect
1941
+ // navigation
1942
+
1943
+
1944
+ async function startNavigation(historyAction, location, opts) {
1945
+ // Abort any in-progress navigations and start a new one. Unset any ongoing
1946
+ // uninterrupted revalidations unless told otherwise, since we want this
1947
+ // new navigation to update history normally
1948
+ pendingNavigationController && pendingNavigationController.abort();
1949
+ pendingNavigationController = null;
1950
+ pendingAction = historyAction;
1951
+ isUninterruptedRevalidation = (opts && opts.startUninterruptedRevalidation) === true; // Save the current scroll position every time we start a new navigation,
1952
+ // and track whether we should reset scroll on completion
1953
+
1954
+ saveScrollPosition(state.location, state.matches);
1955
+ pendingPreventScrollReset = (opts && opts.preventScrollReset) === true;
1956
+ let loadingNavigation = opts && opts.overrideNavigation;
1957
+ let matches = matchRoutes(dataRoutes, location, init.basename); // Short circuit with a 404 on the root error boundary if we match nothing
1958
+
1959
+ if (!matches) {
1960
+ let error = getInternalRouterError(404, {
1961
+ pathname: location.pathname
1962
+ });
1963
+ let {
1964
+ matches: notFoundMatches,
1965
+ route
1966
+ } = getShortCircuitMatches(dataRoutes); // Cancel all pending deferred on 404s since we don't keep any routes
1967
+
1968
+ cancelActiveDeferreds();
1969
+ completeNavigation(location, {
1970
+ matches: notFoundMatches,
1971
+ loaderData: {},
1972
+ errors: {
1973
+ [route.id]: error
1974
+ }
1975
+ });
1976
+ return;
1977
+ } // Short circuit if it's only a hash change and not a mutation submission
1978
+ // For example, on /page#hash and submit a <Form method="post"> which will
1979
+ // default to a navigation to /page
1980
+
1981
+
1982
+ if (isHashChangeOnly(state.location, location) && !(opts && opts.submission && isMutationMethod(opts.submission.formMethod))) {
1983
+ completeNavigation(location, {
1984
+ matches
1985
+ });
1986
+ return;
1987
+ } // Create a controller/Request for this navigation
1988
+
1989
+
1990
+ pendingNavigationController = new AbortController();
1991
+ let request = createClientSideRequest(init.history, location, pendingNavigationController.signal, opts && opts.submission);
1992
+ let pendingActionData;
1993
+ let pendingError;
1994
+
1995
+ if (opts && opts.pendingError) {
1996
+ // If we have a pendingError, it means the user attempted a GET submission
1997
+ // with binary FormData so assign here and skip to handleLoaders. That
1998
+ // way we handle calling loaders above the boundary etc. It's not really
1999
+ // different from an actionError in that sense.
2000
+ pendingError = {
2001
+ [findNearestBoundary(matches).route.id]: opts.pendingError
2002
+ };
2003
+ } else if (opts && opts.submission && isMutationMethod(opts.submission.formMethod)) {
2004
+ // Call action if we received an action submission
2005
+ let actionOutput = await handleAction(request, location, opts.submission, matches, {
2006
+ replace: opts.replace
2007
+ });
2008
+
2009
+ if (actionOutput.shortCircuited) {
2010
+ return;
2011
+ }
2012
+
2013
+ pendingActionData = actionOutput.pendingActionData;
2014
+ pendingError = actionOutput.pendingActionError;
2015
+
2016
+ let navigation = _extends({
2017
+ state: "loading",
2018
+ location
2019
+ }, opts.submission);
2020
+
2021
+ loadingNavigation = navigation; // Create a GET request for the loaders
2022
+
2023
+ request = new Request(request.url, {
2024
+ signal: request.signal
2025
+ });
2026
+ } // Call loaders
2027
+
2028
+
2029
+ let {
2030
+ shortCircuited,
2031
+ loaderData,
2032
+ errors
2033
+ } = await handleLoaders(request, location, matches, loadingNavigation, opts && opts.submission, opts && opts.replace, pendingActionData, pendingError);
2034
+
2035
+ if (shortCircuited) {
2036
+ return;
2037
+ } // Clean up now that the action/loaders have completed. Don't clean up if
2038
+ // we short circuited because pendingNavigationController will have already
2039
+ // been assigned to a new controller for the next navigation
2040
+
2041
+
2042
+ pendingNavigationController = null;
2043
+ completeNavigation(location, _extends({
2044
+ matches
2045
+ }, pendingActionData ? {
2046
+ actionData: pendingActionData
2047
+ } : {}, {
2048
+ loaderData,
2049
+ errors
2050
+ }));
2051
+ } // Call the action matched by the leaf route for this navigation and handle
2052
+ // redirects/errors
2053
+
2054
+
2055
+ async function handleAction(request, location, submission, matches, opts) {
2056
+ interruptActiveLoads(); // Put us in a submitting state
2057
+
2058
+ let navigation = _extends({
2059
+ state: "submitting",
2060
+ location
2061
+ }, submission);
2062
+
2063
+ updateState({
2064
+ navigation
2065
+ }); // Call our action and get the result
2066
+
2067
+ let result;
2068
+ let actionMatch = getTargetMatch(matches, location);
2069
+
2070
+ if (!actionMatch.route.action) {
2071
+ result = {
2072
+ type: ResultType.error,
2073
+ error: getInternalRouterError(405, {
2074
+ method: request.method,
2075
+ pathname: location.pathname,
2076
+ routeId: actionMatch.route.id
2077
+ })
2078
+ };
2079
+ } else {
2080
+ result = await callLoaderOrAction("action", request, actionMatch, matches, router.basename, future.unstable_middleware);
2081
+
2082
+ if (request.signal.aborted) {
2083
+ return {
2084
+ shortCircuited: true
2085
+ };
2086
+ }
2087
+ }
2088
+
2089
+ if (isRedirectResult(result)) {
2090
+ let replace;
2091
+
2092
+ if (opts && opts.replace != null) {
2093
+ replace = opts.replace;
2094
+ } else {
2095
+ // If the user didn't explicity indicate replace behavior, replace if
2096
+ // we redirected to the exact same location we're currently at to avoid
2097
+ // double back-buttons
2098
+ replace = result.location === state.location.pathname + state.location.search;
2099
+ }
2100
+
2101
+ await startRedirectNavigation(state, result, {
2102
+ submission,
2103
+ replace
2104
+ });
2105
+ return {
2106
+ shortCircuited: true
2107
+ };
2108
+ }
2109
+
2110
+ if (isErrorResult(result)) {
2111
+ // Store off the pending error - we use it to determine which loaders
2112
+ // to call and will commit it when we complete the navigation
2113
+ let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id); // By default, all submissions are REPLACE navigations, but if the
2114
+ // action threw an error that'll be rendered in an errorElement, we fall
2115
+ // back to PUSH so that the user can use the back button to get back to
2116
+ // the pre-submission form location to try again
2117
+
2118
+ if ((opts && opts.replace) !== true) {
2119
+ pendingAction = exports.Action.Push;
2120
+ }
2121
+
2122
+ return {
2123
+ // Send back an empty object we can use to clear out any prior actionData
2124
+ pendingActionData: {},
2125
+ pendingActionError: {
2126
+ [boundaryMatch.route.id]: result.error
2127
+ }
2128
+ };
2129
+ }
2130
+
2131
+ if (isDeferredResult(result)) {
2132
+ throw getInternalRouterError(400, {
2133
+ type: "defer-action"
2134
+ });
2135
+ }
2136
+
2137
+ return {
2138
+ pendingActionData: {
2139
+ [actionMatch.route.id]: result.data
2140
+ }
2141
+ };
2142
+ } // Call all applicable loaders for the given matches, handling redirects,
2143
+ // errors, etc.
2144
+
2145
+
2146
+ async function handleLoaders(request, location, matches, overrideNavigation, submission, replace, pendingActionData, pendingError) {
2147
+ // Figure out the right navigation we want to use for data loading
2148
+ let loadingNavigation = overrideNavigation;
2149
+
2150
+ if (!loadingNavigation) {
2151
+ let navigation = _extends({
2152
+ state: "loading",
2153
+ location,
2154
+ formMethod: undefined,
2155
+ formAction: undefined,
2156
+ formEncType: undefined,
2157
+ formData: undefined
2158
+ }, submission);
2159
+
2160
+ loadingNavigation = navigation;
2161
+ } // If this was a redirect from an action we don't have a "submission" but
2162
+ // we have it on the loading navigation so use that if available
2163
+
2164
+
2165
+ let activeSubmission = submission ? submission : loadingNavigation.formMethod && loadingNavigation.formAction && loadingNavigation.formData && loadingNavigation.formEncType ? {
2166
+ formMethod: loadingNavigation.formMethod,
2167
+ formAction: loadingNavigation.formAction,
2168
+ formData: loadingNavigation.formData,
2169
+ formEncType: loadingNavigation.formEncType
2170
+ } : undefined;
2171
+ let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(init.history, state, matches, activeSubmission, location, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, pendingActionData, pendingError, fetchLoadMatches); // Cancel pending deferreds for no-longer-matched routes or routes we're
2172
+ // about to reload. Note that if this is an action reload we would have
2173
+ // already cancelled all pending deferreds so this would be a no-op
2174
+
2175
+ cancelActiveDeferreds(routeId => !(matches && matches.some(m => m.route.id === routeId)) || matchesToLoad && matchesToLoad.some(m => m.route.id === routeId)); // Short circuit if we have no loaders to run
2176
+
2177
+ if (matchesToLoad.length === 0 && revalidatingFetchers.length === 0) {
2178
+ completeNavigation(location, _extends({
2179
+ matches,
2180
+ loaderData: {},
2181
+ // Commit pending error if we're short circuiting
2182
+ errors: pendingError || null
2183
+ }, pendingActionData ? {
2184
+ actionData: pendingActionData
2185
+ } : {}));
2186
+ return {
2187
+ shortCircuited: true
2188
+ };
2189
+ } // If this is an uninterrupted revalidation, we remain in our current idle
2190
+ // state. If not, we need to switch to our loading state and load data,
2191
+ // preserving any new action data or existing action data (in the case of
2192
+ // a revalidation interrupting an actionReload)
2193
+
2194
+
2195
+ if (!isUninterruptedRevalidation) {
2196
+ revalidatingFetchers.forEach(rf => {
2197
+ let fetcher = state.fetchers.get(rf.key);
2198
+ let revalidatingFetcher = {
2199
+ state: "loading",
2200
+ data: fetcher && fetcher.data,
2201
+ formMethod: undefined,
2202
+ formAction: undefined,
2203
+ formEncType: undefined,
2204
+ formData: undefined,
2205
+ " _hasFetcherDoneAnything ": true
2206
+ };
2207
+ state.fetchers.set(rf.key, revalidatingFetcher);
2208
+ });
2209
+ let actionData = pendingActionData || state.actionData;
2210
+ updateState(_extends({
2211
+ navigation: loadingNavigation
2212
+ }, actionData ? Object.keys(actionData).length === 0 ? {
2213
+ actionData: null
2214
+ } : {
2215
+ actionData
2216
+ } : {}, revalidatingFetchers.length > 0 ? {
2217
+ fetchers: new Map(state.fetchers)
2218
+ } : {}));
2219
+ }
2220
+
2221
+ pendingNavigationLoadId = ++incrementingLoadId;
2222
+ revalidatingFetchers.forEach(rf => fetchControllers.set(rf.key, pendingNavigationController));
2223
+ let {
2224
+ results,
2225
+ loaderResults,
2226
+ fetcherResults
2227
+ } = await callLoadersAndMaybeResolveData(state.matches, matches, matchesToLoad, revalidatingFetchers, request);
2228
+
2229
+ if (request.signal.aborted) {
2230
+ return {
2231
+ shortCircuited: true
2232
+ };
2233
+ } // Clean up _after_ loaders have completed. Don't clean up if we short
2234
+ // circuited because fetchControllers would have been aborted and
2235
+ // reassigned to new controllers for the next navigation
2236
+
2237
+
2238
+ revalidatingFetchers.forEach(rf => fetchControllers.delete(rf.key)); // If any loaders returned a redirect Response, start a new REPLACE navigation
2239
+
2240
+ let redirect = findRedirect(results);
2241
+
2242
+ if (redirect) {
2243
+ await startRedirectNavigation(state, redirect, {
2244
+ replace
2245
+ });
2246
+ return {
2247
+ shortCircuited: true
2248
+ };
2249
+ } // Process and commit output from loaders
2250
+
2251
+
2252
+ let {
2253
+ loaderData,
2254
+ errors
2255
+ } = processLoaderData(state, matches, matchesToLoad, loaderResults, pendingError, revalidatingFetchers, fetcherResults, activeDeferreds); // Wire up subscribers to update loaderData as promises settle
2256
+
2257
+ activeDeferreds.forEach((deferredData, routeId) => {
2258
+ deferredData.subscribe(aborted => {
2259
+ // Note: No need to updateState here since the TrackedPromise on
2260
+ // loaderData is stable across resolve/reject
2261
+ // Remove this instance if we were aborted or if promises have settled
2262
+ if (aborted || deferredData.done) {
2263
+ activeDeferreds.delete(routeId);
2264
+ }
2265
+ });
2266
+ });
2267
+ markFetchRedirectsDone();
2268
+ let didAbortFetchLoads = abortStaleFetchLoads(pendingNavigationLoadId);
2269
+ return _extends({
2270
+ loaderData,
2271
+ errors
2272
+ }, didAbortFetchLoads || revalidatingFetchers.length > 0 ? {
2273
+ fetchers: new Map(state.fetchers)
2274
+ } : {});
2275
+ }
2276
+
2277
+ function getFetcher(key) {
2278
+ return state.fetchers.get(key) || IDLE_FETCHER;
2279
+ } // Trigger a fetcher load/submit for the given fetcher key
2280
+
2281
+
2282
+ function fetch(key, routeId, href, opts) {
2283
+ if (isServer) {
2284
+ 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.");
2285
+ }
2286
+
2287
+ if (fetchControllers.has(key)) abortFetcher(key);
2288
+ let matches = matchRoutes(dataRoutes, href, init.basename);
2289
+
2290
+ if (!matches) {
2291
+ setFetcherError(key, routeId, getInternalRouterError(404, {
2292
+ pathname: href
2293
+ }));
2294
+ return;
2295
+ }
2296
+
2297
+ let {
2298
+ path,
2299
+ submission
2300
+ } = normalizeNavigateOptions(href, opts, true);
2301
+ let match = getTargetMatch(matches, path);
2302
+ pendingPreventScrollReset = (opts && opts.preventScrollReset) === true;
2303
+
2304
+ if (submission && isMutationMethod(submission.formMethod)) {
2305
+ return handleFetcherAction(key, routeId, path, match, matches, submission);
2306
+ } // Store off the match so we can call it's shouldRevalidate on subsequent
2307
+ // revalidations
2308
+
2309
+
2310
+ fetchLoadMatches.set(key, {
2311
+ routeId,
2312
+ path,
2313
+ match,
2314
+ matches
2315
+ });
2316
+ return handleFetcherLoader(key, routeId, path, match, matches, submission);
2317
+ } // Call the action for the matched fetcher.submit(), and then handle redirects,
2318
+ // errors, and revalidation
2319
+
2320
+
2321
+ async function handleFetcherAction(key, routeId, path, match, requestMatches, submission) {
2322
+ interruptActiveLoads();
2323
+ fetchLoadMatches.delete(key);
2324
+
2325
+ if (!match.route.action) {
2326
+ let error = getInternalRouterError(405, {
2327
+ method: submission.formMethod,
2328
+ pathname: path,
2329
+ routeId: routeId
2330
+ });
2331
+ setFetcherError(key, routeId, error);
2332
+ return;
2333
+ } // Put this fetcher into it's submitting state
2334
+
2335
+
2336
+ let existingFetcher = state.fetchers.get(key);
2337
+
2338
+ let fetcher = _extends({
2339
+ state: "submitting"
2340
+ }, submission, {
2341
+ data: existingFetcher && existingFetcher.data,
2342
+ " _hasFetcherDoneAnything ": true
2343
+ });
2344
+
2345
+ state.fetchers.set(key, fetcher);
2346
+ updateState({
2347
+ fetchers: new Map(state.fetchers)
2348
+ }); // Call the action for the fetcher
2349
+
2350
+ let abortController = new AbortController();
2351
+ let fetchRequest = createClientSideRequest(init.history, path, abortController.signal, submission);
2352
+ fetchControllers.set(key, abortController);
2353
+ let actionResult = await callLoaderOrAction("action", fetchRequest, match, requestMatches, router.basename, future.unstable_middleware);
2354
+
2355
+ if (fetchRequest.signal.aborted) {
2356
+ // We can delete this so long as we weren't aborted by ou our own fetcher
2357
+ // re-submit which would have put _new_ controller is in fetchControllers
2358
+ if (fetchControllers.get(key) === abortController) {
2359
+ fetchControllers.delete(key);
2360
+ }
2361
+
2362
+ return;
2363
+ }
2364
+
2365
+ if (isRedirectResult(actionResult)) {
2366
+ fetchControllers.delete(key);
2367
+ fetchRedirectIds.add(key);
2368
+
2369
+ let loadingFetcher = _extends({
2370
+ state: "loading"
2371
+ }, submission, {
2372
+ data: undefined,
2373
+ " _hasFetcherDoneAnything ": true
2374
+ });
2375
+
2376
+ state.fetchers.set(key, loadingFetcher);
2377
+ updateState({
2378
+ fetchers: new Map(state.fetchers)
2379
+ });
2380
+ return startRedirectNavigation(state, actionResult, {
2381
+ isFetchActionRedirect: true
2382
+ });
2383
+ } // Process any non-redirect errors thrown
2384
+
2385
+
2386
+ if (isErrorResult(actionResult)) {
2387
+ setFetcherError(key, routeId, actionResult.error);
2388
+ return;
2389
+ }
2390
+
2391
+ if (isDeferredResult(actionResult)) {
2392
+ throw getInternalRouterError(400, {
2393
+ type: "defer-action"
2394
+ });
2395
+ } // Start the data load for current matches, or the next location if we're
2396
+ // in the middle of a navigation
2397
+
2398
+
2399
+ let nextLocation = state.navigation.location || state.location;
2400
+ let revalidationRequest = createClientSideRequest(init.history, nextLocation, abortController.signal);
2401
+ let matches = state.navigation.state !== "idle" ? matchRoutes(dataRoutes, state.navigation.location, init.basename) : state.matches;
2402
+ invariant(matches, "Didn't find any matches after fetcher action");
2403
+ let loadId = ++incrementingLoadId;
2404
+ fetchReloadIds.set(key, loadId);
2405
+
2406
+ let loadFetcher = _extends({
2407
+ state: "loading",
2408
+ data: actionResult.data
2409
+ }, submission, {
2410
+ " _hasFetcherDoneAnything ": true
2411
+ });
2412
+
2413
+ state.fetchers.set(key, loadFetcher);
2414
+ let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(init.history, state, matches, submission, nextLocation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, {
2415
+ [match.route.id]: actionResult.data
2416
+ }, undefined, // No need to send through errors since we short circuit above
2417
+ fetchLoadMatches); // Put all revalidating fetchers into the loading state, except for the
2418
+ // current fetcher which we want to keep in it's current loading state which
2419
+ // contains it's action submission info + action data
2420
+
2421
+ revalidatingFetchers.filter(rf => rf.key !== key).forEach(rf => {
2422
+ let staleKey = rf.key;
2423
+ let existingFetcher = state.fetchers.get(staleKey);
2424
+ let revalidatingFetcher = {
2425
+ state: "loading",
2426
+ data: existingFetcher && existingFetcher.data,
2427
+ formMethod: undefined,
2428
+ formAction: undefined,
2429
+ formEncType: undefined,
2430
+ formData: undefined,
2431
+ " _hasFetcherDoneAnything ": true
2432
+ };
2433
+ state.fetchers.set(staleKey, revalidatingFetcher);
2434
+ fetchControllers.set(staleKey, abortController);
2435
+ });
2436
+ updateState({
2437
+ fetchers: new Map(state.fetchers)
2438
+ });
2439
+ let {
2440
+ results,
2441
+ loaderResults,
2442
+ fetcherResults
2443
+ } = await callLoadersAndMaybeResolveData(state.matches, matches, matchesToLoad, revalidatingFetchers, revalidationRequest);
2444
+
2445
+ if (abortController.signal.aborted) {
2446
+ return;
2447
+ }
2448
+
2449
+ fetchReloadIds.delete(key);
2450
+ fetchControllers.delete(key);
2451
+ revalidatingFetchers.forEach(r => fetchControllers.delete(r.key));
2452
+ let redirect = findRedirect(results);
2453
+
2454
+ if (redirect) {
2455
+ return startRedirectNavigation(state, redirect);
2456
+ } // Process and commit output from loaders
2457
+
2458
+
2459
+ let {
2460
+ loaderData,
2461
+ errors
2462
+ } = processLoaderData(state, state.matches, matchesToLoad, loaderResults, undefined, revalidatingFetchers, fetcherResults, activeDeferreds);
2463
+ let doneFetcher = {
2464
+ state: "idle",
2465
+ data: actionResult.data,
2466
+ formMethod: undefined,
2467
+ formAction: undefined,
2468
+ formEncType: undefined,
2469
+ formData: undefined,
2470
+ " _hasFetcherDoneAnything ": true
2471
+ };
2472
+ state.fetchers.set(key, doneFetcher);
2473
+ let didAbortFetchLoads = abortStaleFetchLoads(loadId); // If we are currently in a navigation loading state and this fetcher is
2474
+ // more recent than the navigation, we want the newer data so abort the
2475
+ // navigation and complete it with the fetcher data
2476
+
2477
+ if (state.navigation.state === "loading" && loadId > pendingNavigationLoadId) {
2478
+ invariant(pendingAction, "Expected pending action");
2479
+ pendingNavigationController && pendingNavigationController.abort();
2480
+ completeNavigation(state.navigation.location, {
2481
+ matches,
2482
+ loaderData,
2483
+ errors,
2484
+ fetchers: new Map(state.fetchers)
2485
+ });
2486
+ } else {
2487
+ // otherwise just update with the fetcher data, preserving any existing
2488
+ // loaderData for loaders that did not need to reload. We have to
2489
+ // manually merge here since we aren't going through completeNavigation
2490
+ updateState(_extends({
2491
+ errors,
2492
+ loaderData: mergeLoaderData(state.loaderData, loaderData, matches, errors)
2493
+ }, didAbortFetchLoads ? {
2494
+ fetchers: new Map(state.fetchers)
2495
+ } : {}));
2496
+ isRevalidationRequired = false;
2497
+ }
2498
+ } // Call the matched loader for fetcher.load(), handling redirects, errors, etc.
2499
+
2500
+
2501
+ async function handleFetcherLoader(key, routeId, path, match, matches, submission) {
2502
+ let existingFetcher = state.fetchers.get(key); // Put this fetcher into it's loading state
2503
+
2504
+ let loadingFetcher = _extends({
2505
+ state: "loading",
2506
+ formMethod: undefined,
2507
+ formAction: undefined,
2508
+ formEncType: undefined,
2509
+ formData: undefined
2510
+ }, submission, {
2511
+ data: existingFetcher && existingFetcher.data,
2512
+ " _hasFetcherDoneAnything ": true
2513
+ });
2514
+
2515
+ state.fetchers.set(key, loadingFetcher);
2516
+ updateState({
2517
+ fetchers: new Map(state.fetchers)
2518
+ }); // Call the loader for this fetcher route match
2519
+
2520
+ let abortController = new AbortController();
2521
+ let fetchRequest = createClientSideRequest(init.history, path, abortController.signal);
2522
+ fetchControllers.set(key, abortController);
2523
+ let result = await callLoaderOrAction("loader", fetchRequest, match, matches, router.basename, future.unstable_middleware); // Deferred isn't supported for fetcher loads, await everything and treat it
2524
+ // as a normal load. resolveDeferredData will return undefined if this
2525
+ // fetcher gets aborted, so we just leave result untouched and short circuit
2526
+ // below if that happens
2527
+
2528
+ if (isDeferredResult(result)) {
2529
+ result = (await resolveDeferredData(result, fetchRequest.signal, true)) || result;
2530
+ } // We can delete this so long as we weren't aborted by ou our own fetcher
2531
+ // re-load which would have put _new_ controller is in fetchControllers
2532
+
2533
+
2534
+ if (fetchControllers.get(key) === abortController) {
2535
+ fetchControllers.delete(key);
2536
+ }
2537
+
2538
+ if (fetchRequest.signal.aborted) {
2539
+ return;
2540
+ } // If the loader threw a redirect Response, start a new REPLACE navigation
2541
+
2542
+
2543
+ if (isRedirectResult(result)) {
2544
+ await startRedirectNavigation(state, result);
2545
+ return;
2546
+ } // Process any non-redirect errors thrown
2547
+
2548
+
2549
+ if (isErrorResult(result)) {
2550
+ let boundaryMatch = findNearestBoundary(state.matches, routeId);
2551
+ state.fetchers.delete(key); // TODO: In remix, this would reset to IDLE_NAVIGATION if it was a catch -
2552
+ // do we need to behave any differently with our non-redirect errors?
2553
+ // What if it was a non-redirect Response?
2554
+
2555
+ updateState({
2556
+ fetchers: new Map(state.fetchers),
2557
+ errors: {
2558
+ [boundaryMatch.route.id]: result.error
2559
+ }
2560
+ });
2561
+ return;
2562
+ }
2563
+
2564
+ invariant(!isDeferredResult(result), "Unhandled fetcher deferred data"); // Put the fetcher back into an idle state
2565
+
2566
+ let doneFetcher = {
2567
+ state: "idle",
2568
+ data: result.data,
2569
+ formMethod: undefined,
2570
+ formAction: undefined,
2571
+ formEncType: undefined,
2572
+ formData: undefined,
2573
+ " _hasFetcherDoneAnything ": true
2574
+ };
2575
+ state.fetchers.set(key, doneFetcher);
2576
+ updateState({
2577
+ fetchers: new Map(state.fetchers)
2578
+ });
2579
+ }
2580
+ /**
2581
+ * Utility function to handle redirects returned from an action or loader.
2582
+ * Normally, a redirect "replaces" the navigation that triggered it. So, for
2583
+ * example:
2584
+ *
2585
+ * - user is on /a
2586
+ * - user clicks a link to /b
2587
+ * - loader for /b redirects to /c
2588
+ *
2589
+ * In a non-JS app the browser would track the in-flight navigation to /b and
2590
+ * then replace it with /c when it encountered the redirect response. In
2591
+ * the end it would only ever update the URL bar with /c.
2592
+ *
2593
+ * In client-side routing using pushState/replaceState, we aim to emulate
2594
+ * this behavior and we also do not update history until the end of the
2595
+ * navigation (including processed redirects). This means that we never
2596
+ * actually touch history until we've processed redirects, so we just use
2597
+ * the history action from the original navigation (PUSH or REPLACE).
2598
+ */
2599
+
2600
+
2601
+ async function startRedirectNavigation(state, redirect, _temp) {
2602
+ var _window;
2603
+
2604
+ let {
2605
+ submission,
2606
+ replace,
2607
+ isFetchActionRedirect
2608
+ } = _temp === void 0 ? {} : _temp;
2609
+
2610
+ if (redirect.revalidate) {
2611
+ isRevalidationRequired = true;
2612
+ }
2613
+
2614
+ let redirectLocation = createLocation(state.location, redirect.location, // TODO: This can be removed once we get rid of useTransition in Remix v2
2615
+ _extends({
2616
+ _isRedirect: true
2617
+ }, isFetchActionRedirect ? {
2618
+ _isFetchActionRedirect: true
2619
+ } : {}));
2620
+ invariant(redirectLocation, "Expected a location on the redirect navigation"); // Check if this an absolute external redirect that goes to a new origin
2621
+
2622
+ if (ABSOLUTE_URL_REGEX.test(redirect.location) && isBrowser && typeof ((_window = window) == null ? void 0 : _window.location) !== "undefined") {
2623
+ let newOrigin = init.history.createURL(redirect.location).origin;
2624
+
2625
+ if (window.location.origin !== newOrigin) {
2626
+ if (replace) {
2627
+ window.location.replace(redirect.location);
2628
+ } else {
2629
+ window.location.assign(redirect.location);
2630
+ }
2631
+
2632
+ return;
2633
+ }
2634
+ } // There's no need to abort on redirects, since we don't detect the
2635
+ // redirect until the action/loaders have settled
2636
+
2637
+
2638
+ pendingNavigationController = null;
2639
+ let redirectHistoryAction = replace === true ? exports.Action.Replace : exports.Action.Push; // Use the incoming submission if provided, fallback on the active one in
2640
+ // state.navigation
2641
+
2642
+ let {
2643
+ formMethod,
2644
+ formAction,
2645
+ formEncType,
2646
+ formData
2647
+ } = state.navigation;
2648
+
2649
+ if (!submission && formMethod && formAction && formData && formEncType) {
2650
+ submission = {
2651
+ formMethod,
2652
+ formAction,
2653
+ formEncType,
2654
+ formData
2655
+ };
2656
+ } // If this was a 307/308 submission we want to preserve the HTTP method and
2657
+ // re-submit the GET/POST/PUT/PATCH/DELETE as a submission navigation to the
2658
+ // redirected location
2659
+
2660
+
2661
+ if (redirectPreserveMethodStatusCodes.has(redirect.status) && submission && isMutationMethod(submission.formMethod)) {
2662
+ await startNavigation(redirectHistoryAction, redirectLocation, {
2663
+ submission: _extends({}, submission, {
2664
+ formAction: redirect.location
2665
+ }),
2666
+ // Preserve this flag across redirects
2667
+ preventScrollReset: pendingPreventScrollReset
2668
+ });
2669
+ } else {
2670
+ // Otherwise, we kick off a new loading navigation, preserving the
2671
+ // submission info for the duration of this navigation
2672
+ await startNavigation(redirectHistoryAction, redirectLocation, {
2673
+ overrideNavigation: {
2674
+ state: "loading",
2675
+ location: redirectLocation,
2676
+ formMethod: submission ? submission.formMethod : undefined,
2677
+ formAction: submission ? submission.formAction : undefined,
2678
+ formEncType: submission ? submission.formEncType : undefined,
2679
+ formData: submission ? submission.formData : undefined
2680
+ },
2681
+ // Preserve this flag across redirects
2682
+ preventScrollReset: pendingPreventScrollReset
2683
+ });
2684
+ }
2685
+ }
2686
+
2687
+ async function callLoadersAndMaybeResolveData(currentMatches, matches, matchesToLoad, fetchersToLoad, request) {
2688
+ // Call all navigation loaders and revalidating fetcher loaders in parallel,
2689
+ // then slice off the results into separate arrays so we can handle them
2690
+ // accordingly
2691
+ let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, router.basename, future.unstable_middleware)), ...fetchersToLoad.map(f => callLoaderOrAction("loader", createClientSideRequest(init.history, f.path, request.signal), f.match, f.matches, router.basename, future.unstable_middleware))]);
2692
+ let loaderResults = results.slice(0, matchesToLoad.length);
2693
+ let fetcherResults = results.slice(matchesToLoad.length);
2694
+ await Promise.all([resolveDeferredResults(currentMatches, matchesToLoad, loaderResults, request.signal, false, state.loaderData), resolveDeferredResults(currentMatches, fetchersToLoad.map(f => f.match), fetcherResults, request.signal, true)]);
2695
+ return {
2696
+ results,
2697
+ loaderResults,
2698
+ fetcherResults
2699
+ };
2700
+ }
2701
+
2702
+ function interruptActiveLoads() {
2703
+ // Every interruption triggers a revalidation
2704
+ isRevalidationRequired = true; // Cancel pending route-level deferreds and mark cancelled routes for
2705
+ // revalidation
2706
+
2707
+ cancelledDeferredRoutes.push(...cancelActiveDeferreds()); // Abort in-flight fetcher loads
2708
+
2709
+ fetchLoadMatches.forEach((_, key) => {
2710
+ if (fetchControllers.has(key)) {
2711
+ cancelledFetcherLoads.push(key);
2712
+ abortFetcher(key);
2713
+ }
2714
+ });
2715
+ }
2716
+
2717
+ function setFetcherError(key, routeId, error) {
2718
+ let boundaryMatch = findNearestBoundary(state.matches, routeId);
2719
+ deleteFetcher(key);
2720
+ updateState({
2721
+ errors: {
2722
+ [boundaryMatch.route.id]: error
2723
+ },
2724
+ fetchers: new Map(state.fetchers)
2725
+ });
2726
+ }
2727
+
2728
+ function deleteFetcher(key) {
2729
+ if (fetchControllers.has(key)) abortFetcher(key);
2730
+ fetchLoadMatches.delete(key);
2731
+ fetchReloadIds.delete(key);
2732
+ fetchRedirectIds.delete(key);
2733
+ state.fetchers.delete(key);
2734
+ }
2735
+
2736
+ function abortFetcher(key) {
2737
+ let controller = fetchControllers.get(key);
2738
+ invariant(controller, "Expected fetch controller: " + key);
2739
+ controller.abort();
2740
+ fetchControllers.delete(key);
2741
+ }
2742
+
2743
+ function markFetchersDone(keys) {
2744
+ for (let key of keys) {
2745
+ let fetcher = getFetcher(key);
2746
+ let doneFetcher = {
2747
+ state: "idle",
2748
+ data: fetcher.data,
2749
+ formMethod: undefined,
2750
+ formAction: undefined,
2751
+ formEncType: undefined,
2752
+ formData: undefined,
2753
+ " _hasFetcherDoneAnything ": true
2754
+ };
2755
+ state.fetchers.set(key, doneFetcher);
2756
+ }
2757
+ }
2758
+
2759
+ function markFetchRedirectsDone() {
2760
+ let doneKeys = [];
2761
+
2762
+ for (let key of fetchRedirectIds) {
2763
+ let fetcher = state.fetchers.get(key);
2764
+ invariant(fetcher, "Expected fetcher: " + key);
2765
+
2766
+ if (fetcher.state === "loading") {
2767
+ fetchRedirectIds.delete(key);
2768
+ doneKeys.push(key);
2769
+ }
2770
+ }
2771
+
2772
+ markFetchersDone(doneKeys);
2773
+ }
2774
+
2775
+ function abortStaleFetchLoads(landedId) {
2776
+ let yeetedKeys = [];
2777
+
2778
+ for (let [key, id] of fetchReloadIds) {
2779
+ if (id < landedId) {
2780
+ let fetcher = state.fetchers.get(key);
2781
+ invariant(fetcher, "Expected fetcher: " + key);
2782
+
2783
+ if (fetcher.state === "loading") {
2784
+ abortFetcher(key);
2785
+ fetchReloadIds.delete(key);
2786
+ yeetedKeys.push(key);
2787
+ }
2788
+ }
2789
+ }
2790
+
2791
+ markFetchersDone(yeetedKeys);
2792
+ return yeetedKeys.length > 0;
2793
+ }
2794
+
2795
+ function getBlocker(key, fn) {
2796
+ let blocker = state.blockers.get(key) || IDLE_BLOCKER;
2797
+
2798
+ if (blockerFunctions.get(key) !== fn) {
2799
+ blockerFunctions.set(key, fn);
2800
+ }
2801
+
2802
+ return blocker;
2803
+ }
2804
+
2805
+ function deleteBlocker(key) {
2806
+ state.blockers.delete(key);
2807
+ blockerFunctions.delete(key);
2808
+ } // Utility function to update blockers, ensuring valid state transitions
2809
+
2810
+
2811
+ function updateBlocker(key, newBlocker) {
2812
+ let blocker = state.blockers.get(key) || IDLE_BLOCKER; // Poor mans state machine :)
2813
+ // https://mermaid.live/edit#pako:eNqVkc9OwzAMxl8l8nnjAYrEtDIOHEBIgwvKJTReGy3_lDpIqO27k6awMG0XcrLlnz87nwdonESogKXXBuE79rq75XZO3-yHds0RJVuv70YrPlUrCEe2HfrORS3rubqZfuhtpg5C9wk5tZ4VKcRUq88q9Z8RS0-48cE1iHJkL0ugbHuFLus9L6spZy8nX9MP2CNdomVaposqu3fGayT8T8-jJQwhepo_UtpgBQaDEUom04dZhAN1aJBDlUKJBxE1ceB2Smj0Mln-IBW5AFU2dwUiktt_2Qaq2dBfaKdEup85UV7Yd-dKjlnkabl2Pvr0DTkTreM
2814
+
2815
+ 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);
2816
+ state.blockers.set(key, newBlocker);
2817
+ updateState({
2818
+ blockers: new Map(state.blockers)
2819
+ });
2820
+ }
2821
+
2822
+ function shouldBlockNavigation(_ref2) {
2823
+ let {
2824
+ currentLocation,
2825
+ nextLocation,
2826
+ historyAction
2827
+ } = _ref2;
2828
+
2829
+ if (blockerFunctions.size === 0) {
2830
+ return;
2831
+ } // We ony support a single active blocker at the moment since we don't have
2832
+ // any compelling use cases for multi-blocker yet
2833
+
2834
+
2835
+ if (blockerFunctions.size > 1) {
2836
+ warning(false, "A router only supports one blocker at a time");
2837
+ }
2838
+
2839
+ let entries = Array.from(blockerFunctions.entries());
2840
+ let [blockerKey, blockerFunction] = entries[entries.length - 1];
2841
+ let blocker = state.blockers.get(blockerKey);
2842
+
2843
+ if (blocker && blocker.state === "proceeding") {
2844
+ // If the blocker is currently proceeding, we don't need to re-check
2845
+ // it and can let this navigation continue
2846
+ return;
2847
+ } // At this point, we know we're unblocked/blocked so we need to check the
2848
+ // user-provided blocker function
2849
+
2850
+
2851
+ if (blockerFunction({
2852
+ currentLocation,
2853
+ nextLocation,
2854
+ historyAction
2855
+ })) {
2856
+ return blockerKey;
2857
+ }
2858
+ }
2859
+
2860
+ function cancelActiveDeferreds(predicate) {
2861
+ let cancelledRouteIds = [];
2862
+ activeDeferreds.forEach((dfd, routeId) => {
2863
+ if (!predicate || predicate(routeId)) {
2864
+ // Cancel the deferred - but do not remove from activeDeferreds here -
2865
+ // we rely on the subscribers to do that so our tests can assert proper
2866
+ // cleanup via _internalActiveDeferreds
2867
+ dfd.cancel();
2868
+ cancelledRouteIds.push(routeId);
2869
+ activeDeferreds.delete(routeId);
2870
+ }
2871
+ });
2872
+ return cancelledRouteIds;
2873
+ } // Opt in to capturing and reporting scroll positions during navigations,
2874
+ // used by the <ScrollRestoration> component
2875
+
2876
+
2877
+ function enableScrollRestoration(positions, getPosition, getKey) {
2878
+ savedScrollPositions = positions;
2879
+ getScrollPosition = getPosition;
2880
+
2881
+ getScrollRestorationKey = getKey || (location => location.key); // Perform initial hydration scroll restoration, since we miss the boat on
2882
+ // the initial updateState() because we've not yet rendered <ScrollRestoration/>
2883
+ // and therefore have no savedScrollPositions available
2884
+
2885
+
2886
+ if (!initialScrollRestored && state.navigation === IDLE_NAVIGATION) {
2887
+ initialScrollRestored = true;
2888
+ let y = getSavedScrollPosition(state.location, state.matches);
2889
+
2890
+ if (y != null) {
2891
+ updateState({
2892
+ restoreScrollPosition: y
2893
+ });
2894
+ }
2895
+ }
2896
+
2897
+ return () => {
2898
+ savedScrollPositions = null;
2899
+ getScrollPosition = null;
2900
+ getScrollRestorationKey = null;
2901
+ };
2902
+ }
2903
+
2904
+ function saveScrollPosition(location, matches) {
2905
+ if (savedScrollPositions && getScrollRestorationKey && getScrollPosition) {
2906
+ let userMatches = matches.map(m => createUseMatchesMatch(m, state.loaderData));
2907
+ let key = getScrollRestorationKey(location, userMatches) || location.key;
2908
+ savedScrollPositions[key] = getScrollPosition();
2909
+ }
2910
+ }
2911
+
2912
+ function getSavedScrollPosition(location, matches) {
2913
+ if (savedScrollPositions && getScrollRestorationKey && getScrollPosition) {
2914
+ let userMatches = matches.map(m => createUseMatchesMatch(m, state.loaderData));
2915
+ let key = getScrollRestorationKey(location, userMatches) || location.key;
2916
+ let y = savedScrollPositions[key];
2917
+
2918
+ if (typeof y === "number") {
2919
+ return y;
2920
+ }
2921
+ }
2922
+
2923
+ return null;
2924
+ }
2925
+
2926
+ router = {
2927
+ get basename() {
2928
+ return init.basename;
2929
+ },
2930
+
2931
+ get state() {
2932
+ return state;
2933
+ },
2934
+
2935
+ get routes() {
2936
+ return dataRoutes;
2937
+ },
2938
+
2939
+ initialize,
2940
+ subscribe,
2941
+ enableScrollRestoration,
2942
+ navigate,
2943
+ fetch,
2944
+ revalidate,
2945
+ // Passthrough to history-aware createHref used by useHref so we get proper
2946
+ // hash-aware URLs in DOM paths
2947
+ createHref: to => init.history.createHref(to),
2948
+ encodeLocation: to => init.history.encodeLocation(to),
2949
+ getFetcher,
2950
+ deleteFetcher,
2951
+ dispose,
2952
+ getBlocker,
2953
+ deleteBlocker,
2954
+ _internalFetchControllers: fetchControllers,
2955
+ _internalActiveDeferreds: activeDeferreds
2956
+ };
2957
+ return router;
2958
+ } //#endregion
2959
+ ////////////////////////////////////////////////////////////////////////////////
2960
+ //#region createStaticHandler
2961
+ ////////////////////////////////////////////////////////////////////////////////
2962
+
2963
+ const UNSAFE_DEFERRED_SYMBOL = Symbol("deferred");
2964
+ function createStaticHandler(routes, init) {
2965
+ invariant(routes.length > 0, "You must provide a non-empty routes array to createStaticHandler");
2966
+ let dataRoutes = convertRoutesToDataRoutes(routes);
2967
+ let basename = (init ? init.basename : null) || "/";
2968
+
2969
+ let future = _extends({}, defaultFutureConfig, init && init.future ? init.future : null);
2970
+ /**
2971
+ * The query() method is intended for document requests, in which we want to
2972
+ * call an optional action and potentially multiple loaders for all nested
2973
+ * routes. It returns a StaticHandlerContext object, which is very similar
2974
+ * to the router state (location, loaderData, actionData, errors, etc.) and
2975
+ * also adds SSR-specific information such as the statusCode and headers
2976
+ * from action/loaders Responses.
2977
+ *
2978
+ * It _should_ never throw and should report all errors through the
2979
+ * returned context.errors object, properly associating errors to their error
2980
+ * boundary. Additionally, it tracks _deepestRenderedBoundaryId which can be
2981
+ * used to emulate React error boundaries during SSr by performing a second
2982
+ * pass only down to the boundaryId.
2983
+ *
2984
+ * The one exception where we do not return a StaticHandlerContext is when a
2985
+ * redirect response is returned or thrown from any action/loader. We
2986
+ * propagate that out and return the raw Response so the HTTP server can
2987
+ * return it directly.
2988
+ */
2989
+
2990
+
2991
+ async function query(request, _temp2) {
2992
+ let {
2993
+ requestContext,
2994
+ middlewareContext
2995
+ } = _temp2 === void 0 ? {} : _temp2;
2996
+ let url = new URL(request.url);
2997
+ let method = request.method.toLowerCase();
2998
+ let location = createLocation("", createPath(url), null, "default");
2999
+ let matches = matchRoutes(dataRoutes, location, basename); // SSR supports HEAD requests while SPA doesn't
3000
+
3001
+ if (!isValidMethod(method) && method !== "head") {
3002
+ let error = getInternalRouterError(405, {
3003
+ method
3004
+ });
3005
+ let {
3006
+ matches: methodNotAllowedMatches,
3007
+ route
3008
+ } = getShortCircuitMatches(dataRoutes);
3009
+ return {
3010
+ basename,
3011
+ location,
3012
+ matches: methodNotAllowedMatches,
3013
+ loaderData: {},
3014
+ actionData: null,
3015
+ errors: {
3016
+ [route.id]: error
3017
+ },
3018
+ statusCode: error.status,
3019
+ loaderHeaders: {},
3020
+ actionHeaders: {},
3021
+ activeDeferreds: null
3022
+ };
3023
+ } else if (!matches) {
3024
+ let error = getInternalRouterError(404, {
3025
+ pathname: location.pathname
3026
+ });
3027
+ let {
3028
+ matches: notFoundMatches,
3029
+ route
3030
+ } = getShortCircuitMatches(dataRoutes);
3031
+ return {
3032
+ basename,
3033
+ location,
3034
+ matches: notFoundMatches,
3035
+ loaderData: {},
3036
+ actionData: null,
3037
+ errors: {
3038
+ [route.id]: error
3039
+ },
3040
+ statusCode: error.status,
3041
+ loaderHeaders: {},
3042
+ actionHeaders: {},
3043
+ activeDeferreds: null
3044
+ };
3045
+ }
3046
+
3047
+ let result = await queryImpl(request, location, matches, requestContext, middlewareContext);
3048
+
3049
+ if (isResponse(result)) {
3050
+ return result;
3051
+ } // When returning StaticHandlerContext, we patch back in the location here
3052
+ // since we need it for React Context. But this helps keep our submit and
3053
+ // loadRouteData operating on a Request instead of a Location
3054
+
3055
+
3056
+ return _extends({
3057
+ location,
3058
+ basename
3059
+ }, result);
3060
+ }
3061
+ /**
3062
+ * The queryRoute() method is intended for targeted route requests, either
3063
+ * for fetch ?_data requests or resource route requests. In this case, we
3064
+ * are only ever calling a single action or loader, and we are returning the
3065
+ * returned value directly. In most cases, this will be a Response returned
3066
+ * from the action/loader, but it may be a primitive or other value as well -
3067
+ * and in such cases the calling context should handle that accordingly.
3068
+ *
3069
+ * We do respect the throw/return differentiation, so if an action/loader
3070
+ * throws, then this method will throw the value. This is important so we
3071
+ * can do proper boundary identification in Remix where a thrown Response
3072
+ * must go to the Catch Boundary but a returned Response is happy-path.
3073
+ *
3074
+ * One thing to note is that any Router-initiated Errors that make sense
3075
+ * to associate with a status code will be thrown as an ErrorResponse
3076
+ * instance which include the raw Error, such that the calling context can
3077
+ * serialize the error as they see fit while including the proper response
3078
+ * code. Examples here are 404 and 405 errors that occur prior to reaching
3079
+ * any user-defined loaders.
3080
+ */
3081
+
3082
+
3083
+ async function queryRoute(request, _temp3) {
3084
+ let {
3085
+ routeId,
3086
+ requestContext,
3087
+ middlewareContext
3088
+ } = _temp3 === void 0 ? {} : _temp3;
3089
+ let url = new URL(request.url);
3090
+ let method = request.method.toLowerCase();
3091
+ let location = createLocation("", createPath(url), null, "default");
3092
+ let matches = matchRoutes(dataRoutes, location, basename); // SSR supports HEAD requests while SPA doesn't
3093
+
3094
+ if (!isValidMethod(method) && method !== "head" && method !== "options") {
3095
+ throw getInternalRouterError(405, {
3096
+ method
3097
+ });
3098
+ } else if (!matches) {
3099
+ throw getInternalRouterError(404, {
3100
+ pathname: location.pathname
3101
+ });
3102
+ }
3103
+
3104
+ let match = routeId ? matches.find(m => m.route.id === routeId) : getTargetMatch(matches, location);
3105
+
3106
+ if (routeId && !match) {
3107
+ throw getInternalRouterError(403, {
3108
+ pathname: location.pathname,
3109
+ routeId
3110
+ });
3111
+ } else if (!match) {
3112
+ // This should never hit I don't think?
3113
+ throw getInternalRouterError(404, {
3114
+ pathname: location.pathname
3115
+ });
3116
+ }
3117
+
3118
+ let result = await queryImpl(request, location, matches, requestContext, middlewareContext, match);
3119
+
3120
+ if (isResponse(result)) {
3121
+ return result;
3122
+ }
3123
+
3124
+ let error = result.errors ? Object.values(result.errors)[0] : undefined;
3125
+
3126
+ if (error !== undefined) {
3127
+ // If we got back result.errors, that means the loader/action threw
3128
+ // _something_ that wasn't a Response, but it's not guaranteed/required
3129
+ // to be an `instanceof Error` either, so we have to use throw here to
3130
+ // preserve the "error" state outside of queryImpl.
3131
+ throw error;
3132
+ } // Pick off the right state value to return
3133
+
3134
+
3135
+ if (result.actionData) {
3136
+ return Object.values(result.actionData)[0];
3137
+ }
3138
+
3139
+ if (result.loaderData) {
3140
+ var _result$activeDeferre;
3141
+
3142
+ let data = Object.values(result.loaderData)[0];
3143
+
3144
+ if ((_result$activeDeferre = result.activeDeferreds) != null && _result$activeDeferre[match.route.id]) {
3145
+ data[UNSAFE_DEFERRED_SYMBOL] = result.activeDeferreds[match.route.id];
3146
+ }
3147
+
3148
+ return data;
3149
+ }
3150
+
3151
+ return undefined;
3152
+ }
3153
+
3154
+ async function queryImpl(request, location, matches, requestContext, middlewareContext, routeMatch) {
3155
+ invariant(request.signal, "query()/queryRoute() requests must contain an AbortController signal");
3156
+
3157
+ try {
3158
+ if (isMutationMethod(request.method.toLowerCase())) {
3159
+ let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), requestContext, middlewareContext, routeMatch != null);
3160
+ return result;
3161
+ }
3162
+
3163
+ let result = await loadRouteData(request, matches, requestContext, middlewareContext, routeMatch);
3164
+ return isResponse(result) ? result : _extends({}, result, {
3165
+ actionData: null,
3166
+ actionHeaders: {}
3167
+ });
3168
+ } catch (e) {
3169
+ // If the user threw/returned a Response in callLoaderOrAction, we throw
3170
+ // it to bail out and then return or throw here based on whether the user
3171
+ // returned or threw
3172
+ if (isQueryRouteResponse(e)) {
3173
+ if (e.type === ResultType.error && !isRedirectResponse(e.response)) {
3174
+ throw e.response;
3175
+ }
3176
+
3177
+ return e.response;
3178
+ } // Redirects are always returned since they don't propagate to catch
3179
+ // boundaries
3180
+
3181
+
3182
+ if (isRedirectResponse(e)) {
3183
+ return e;
3184
+ }
3185
+
3186
+ throw e;
3187
+ }
3188
+ }
3189
+
3190
+ async function submit(request, matches, actionMatch, requestContext, middlewareContext, isRouteRequest) {
3191
+ let result;
3192
+
3193
+ if (!actionMatch.route.action) {
3194
+ let error = getInternalRouterError(405, {
3195
+ method: request.method,
3196
+ pathname: new URL(request.url).pathname,
3197
+ routeId: actionMatch.route.id
3198
+ });
3199
+
3200
+ if (isRouteRequest) {
3201
+ throw error;
3202
+ }
3203
+
3204
+ result = {
3205
+ type: ResultType.error,
3206
+ error
3207
+ };
3208
+ } else {
3209
+ result = await callLoaderOrAction("action", request, actionMatch, matches, basename, future.unstable_middleware, true, isRouteRequest, requestContext, middlewareContext);
3210
+
3211
+ if (request.signal.aborted) {
3212
+ let method = isRouteRequest ? "queryRoute" : "query";
3213
+ throw new Error(method + "() call aborted");
3214
+ }
3215
+ }
3216
+
3217
+ if (isRedirectResult(result)) {
3218
+ // Uhhhh - this should never happen, we should always throw these from
3219
+ // callLoaderOrAction, but the type narrowing here keeps TS happy and we
3220
+ // can get back on the "throw all redirect responses" train here should
3221
+ // this ever happen :/
3222
+ throw new Response(null, {
3223
+ status: result.status,
3224
+ headers: {
3225
+ Location: result.location
3226
+ }
3227
+ });
3228
+ }
3229
+
3230
+ if (isDeferredResult(result)) {
3231
+ let error = getInternalRouterError(400, {
3232
+ type: "defer-action"
3233
+ });
3234
+
3235
+ if (isRouteRequest) {
3236
+ throw error;
3237
+ }
3238
+
3239
+ result = {
3240
+ type: ResultType.error,
3241
+ error
3242
+ };
3243
+ }
3244
+
3245
+ if (isRouteRequest) {
3246
+ // Note: This should only be non-Response values if we get here, since
3247
+ // isRouteRequest should throw any Response received in callLoaderOrAction
3248
+ if (isErrorResult(result)) {
3249
+ throw result.error;
3250
+ }
3251
+
3252
+ return {
3253
+ matches: [actionMatch],
3254
+ loaderData: {},
3255
+ actionData: {
3256
+ [actionMatch.route.id]: result.data
3257
+ },
3258
+ errors: null,
3259
+ // Note: statusCode + headers are unused here since queryRoute will
3260
+ // return the raw Response or value
3261
+ statusCode: 200,
3262
+ loaderHeaders: {},
3263
+ actionHeaders: {},
3264
+ activeDeferreds: null
3265
+ };
3266
+ }
3267
+
3268
+ if (isErrorResult(result)) {
3269
+ // Store off the pending error - we use it to determine which loaders
3270
+ // to call and will commit it when we complete the navigation
3271
+ let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
3272
+ let context = await loadRouteData(request, matches, requestContext, middlewareContext, undefined, {
3273
+ [boundaryMatch.route.id]: result.error
3274
+ }); // action status codes take precedence over loader status codes
3275
+
3276
+ return _extends({}, context, {
3277
+ statusCode: isRouteErrorResponse(result.error) ? result.error.status : 500,
3278
+ actionData: null,
3279
+ actionHeaders: _extends({}, result.headers ? {
3280
+ [actionMatch.route.id]: result.headers
3281
+ } : {})
3282
+ });
3283
+ } // Create a GET request for the loaders
3284
+
3285
+
3286
+ let loaderRequest = new Request(request.url, {
3287
+ headers: request.headers,
3288
+ redirect: request.redirect,
3289
+ signal: request.signal
3290
+ });
3291
+ let context = await loadRouteData(loaderRequest, matches, requestContext, middlewareContext);
3292
+ return _extends({}, context, result.statusCode ? {
3293
+ statusCode: result.statusCode
3294
+ } : {}, {
3295
+ actionData: {
3296
+ [actionMatch.route.id]: result.data
3297
+ },
3298
+ actionHeaders: _extends({}, result.headers ? {
3299
+ [actionMatch.route.id]: result.headers
3300
+ } : {})
3301
+ });
3302
+ }
3303
+
3304
+ async function loadRouteData(request, matches, requestContext, middlewareContext, routeMatch, pendingActionError) {
3305
+ let isRouteRequest = routeMatch != null; // Short circuit if we have no loaders to run (queryRoute())
3306
+
3307
+ if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
3308
+ throw getInternalRouterError(400, {
3309
+ method: request.method,
3310
+ pathname: new URL(request.url).pathname,
3311
+ routeId: routeMatch == null ? void 0 : routeMatch.route.id
3312
+ });
3313
+ }
3314
+
3315
+ let requestMatches = routeMatch ? [routeMatch] : getLoaderMatchesUntilBoundary(matches, Object.keys(pendingActionError || {})[0]);
3316
+ let matchesToLoad = requestMatches.filter(m => m.route.loader); // Short circuit if we have no loaders to run (query())
3317
+
3318
+ if (matchesToLoad.length === 0) {
3319
+ return {
3320
+ matches,
3321
+ // Add a null for all matched routes for proper revalidation on the client
3322
+ loaderData: matches.reduce((acc, m) => Object.assign(acc, {
3323
+ [m.route.id]: null
3324
+ }), {}),
3325
+ errors: pendingActionError || null,
3326
+ statusCode: 200,
3327
+ loaderHeaders: {},
3328
+ activeDeferreds: null
3329
+ };
3330
+ }
3331
+
3332
+ let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, basename, future.unstable_middleware, true, isRouteRequest, requestContext, middlewareContext))]);
3333
+
3334
+ if (request.signal.aborted) {
3335
+ let method = isRouteRequest ? "queryRoute" : "query";
3336
+ throw new Error(method + "() call aborted");
3337
+ } // Process and commit output from loaders
3338
+
3339
+
3340
+ let activeDeferreds = new Map();
3341
+ let context = processRouteLoaderData(matches, matchesToLoad, results, pendingActionError, activeDeferreds); // Add a null for any non-loader matches for proper revalidation on the client
3342
+
3343
+ let executedLoaders = new Set(matchesToLoad.map(match => match.route.id));
3344
+ matches.forEach(match => {
3345
+ if (!executedLoaders.has(match.route.id)) {
3346
+ context.loaderData[match.route.id] = null;
3347
+ }
3348
+ });
3349
+ return _extends({}, context, {
3350
+ matches,
3351
+ activeDeferreds: activeDeferreds.size > 0 ? Object.fromEntries(activeDeferreds.entries()) : null
3352
+ });
3353
+ }
3354
+
3355
+ return {
3356
+ dataRoutes,
3357
+ query,
3358
+ queryRoute
3359
+ };
3360
+ } //#endregion
3361
+ ////////////////////////////////////////////////////////////////////////////////
3362
+ //#region Helpers
3363
+ ////////////////////////////////////////////////////////////////////////////////
3364
+
3365
+ /**
3366
+ * Given an existing StaticHandlerContext and an error thrown at render time,
3367
+ * provide an updated StaticHandlerContext suitable for a second SSR render
3368
+ */
3369
+
3370
+ function getStaticContextFromError(routes, context, error) {
3371
+ let newContext = _extends({}, context, {
3372
+ statusCode: 500,
3373
+ errors: {
3374
+ [context._deepestRenderedBoundaryId || routes[0].id]: error
3375
+ }
3376
+ });
3377
+
3378
+ return newContext;
3379
+ }
3380
+
3381
+ function isSubmissionNavigation(opts) {
3382
+ return opts != null && "formData" in opts;
3383
+ } // Normalize navigation options by converting formMethod=GET formData objects to
3384
+ // URLSearchParams so they behave identically to links with query params
3385
+
3386
+
3387
+ function normalizeNavigateOptions(to, opts, isFetcher) {
3388
+ if (isFetcher === void 0) {
3389
+ isFetcher = false;
3390
+ }
3391
+
3392
+ let path = typeof to === "string" ? to : createPath(to); // Return location verbatim on non-submission navigations
3393
+
3394
+ if (!opts || !isSubmissionNavigation(opts)) {
3395
+ return {
3396
+ path
3397
+ };
3398
+ }
3399
+
3400
+ if (opts.formMethod && !isValidMethod(opts.formMethod)) {
3401
+ return {
3402
+ path,
3403
+ error: getInternalRouterError(405, {
3404
+ method: opts.formMethod
3405
+ })
3406
+ };
3407
+ } // Create a Submission on non-GET navigations
3408
+
3409
+
3410
+ let submission;
3411
+
3412
+ if (opts.formData) {
3413
+ submission = {
3414
+ formMethod: opts.formMethod || "get",
3415
+ formAction: stripHashFromPath(path),
3416
+ formEncType: opts && opts.formEncType || "application/x-www-form-urlencoded",
3417
+ formData: opts.formData
3418
+ };
3419
+
3420
+ if (isMutationMethod(submission.formMethod)) {
3421
+ return {
3422
+ path,
3423
+ submission
3424
+ };
3425
+ }
3426
+ } // Flatten submission onto URLSearchParams for GET submissions
3427
+
3428
+
3429
+ let parsedPath = parsePath(path);
3430
+ let searchParams = convertFormDataToSearchParams(opts.formData); // Since fetcher GET submissions only run a single loader (as opposed to
3431
+ // navigation GET submissions which run all loaders), we need to preserve
3432
+ // any incoming ?index params
3433
+
3434
+ if (isFetcher && parsedPath.search && hasNakedIndexQuery(parsedPath.search)) {
3435
+ searchParams.append("index", "");
3436
+ }
3437
+
3438
+ parsedPath.search = "?" + searchParams;
3439
+ return {
3440
+ path: createPath(parsedPath),
3441
+ submission
3442
+ };
3443
+ } // Filter out all routes below any caught error as they aren't going to
3444
+ // render so we don't need to load them
3445
+
3446
+
3447
+ function getLoaderMatchesUntilBoundary(matches, boundaryId) {
3448
+ let boundaryMatches = matches;
3449
+
3450
+ if (boundaryId) {
3451
+ let index = matches.findIndex(m => m.route.id === boundaryId);
3452
+
3453
+ if (index >= 0) {
3454
+ boundaryMatches = matches.slice(0, index);
3455
+ }
3456
+ }
3457
+
3458
+ return boundaryMatches;
3459
+ }
3460
+
3461
+ function getMatchesToLoad(history, state, matches, submission, location, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, pendingActionData, pendingError, fetchLoadMatches) {
3462
+ let actionResult = pendingError ? Object.values(pendingError)[0] : pendingActionData ? Object.values(pendingActionData)[0] : undefined;
3463
+ let currentUrl = history.createURL(state.location);
3464
+ let nextUrl = history.createURL(location);
3465
+ let defaultShouldRevalidate = // Forced revalidation due to submission, useRevalidate, or X-Remix-Revalidate
3466
+ isRevalidationRequired || // Clicked the same link, resubmitted a GET form
3467
+ currentUrl.toString() === nextUrl.toString() || // Search params affect all loaders
3468
+ currentUrl.search !== nextUrl.search; // Pick navigation matches that are net-new or qualify for revalidation
3469
+
3470
+ let boundaryId = pendingError ? Object.keys(pendingError)[0] : undefined;
3471
+ let boundaryMatches = getLoaderMatchesUntilBoundary(matches, boundaryId);
3472
+ let navigationMatches = boundaryMatches.filter((match, index) => {
3473
+ if (match.route.loader == null) {
3474
+ return false;
3475
+ } // Always call the loader on new route instances and pending defer cancellations
3476
+
3477
+
3478
+ if (isNewLoader(state.loaderData, state.matches[index], match) || cancelledDeferredRoutes.some(id => id === match.route.id)) {
3479
+ return true;
3480
+ } // This is the default implementation for when we revalidate. If the route
3481
+ // provides it's own implementation, then we give them full control but
3482
+ // provide this value so they can leverage it if needed after they check
3483
+ // their own specific use cases
3484
+
3485
+
3486
+ let currentRouteMatch = state.matches[index];
3487
+ let nextRouteMatch = match;
3488
+ return shouldRevalidateLoader(match, _extends({
3489
+ currentUrl,
3490
+ currentParams: currentRouteMatch.params,
3491
+ nextUrl,
3492
+ nextParams: nextRouteMatch.params
3493
+ }, submission, {
3494
+ actionResult,
3495
+ defaultShouldRevalidate: defaultShouldRevalidate || isNewRouteInstance(currentRouteMatch, nextRouteMatch)
3496
+ }));
3497
+ }); // Pick fetcher.loads that need to be revalidated
3498
+
3499
+ let revalidatingFetchers = [];
3500
+ fetchLoadMatches && fetchLoadMatches.forEach((f, key) => {
3501
+ if (!matches.some(m => m.route.id === f.routeId)) {
3502
+ // This fetcher is not going to be present in the subsequent render so
3503
+ // there's no need to revalidate it
3504
+ return;
3505
+ } else if (cancelledFetcherLoads.includes(key)) {
3506
+ // This fetcher was cancelled from a prior action submission - force reload
3507
+ revalidatingFetchers.push(_extends({
3508
+ key
3509
+ }, f));
3510
+ } else {
3511
+ // Revalidating fetchers are decoupled from the route matches since they
3512
+ // hit a static href, so they _always_ check shouldRevalidate and the
3513
+ // default is strictly if a revalidation is explicitly required (action
3514
+ // submissions, useRevalidator, X-Remix-Revalidate).
3515
+ let shouldRevalidate = shouldRevalidateLoader(f.match, _extends({
3516
+ currentUrl,
3517
+ currentParams: state.matches[state.matches.length - 1].params,
3518
+ nextUrl,
3519
+ nextParams: matches[matches.length - 1].params
3520
+ }, submission, {
3521
+ actionResult,
3522
+ defaultShouldRevalidate
3523
+ }));
3524
+
3525
+ if (shouldRevalidate) {
3526
+ revalidatingFetchers.push(_extends({
3527
+ key
3528
+ }, f));
3529
+ }
3530
+ }
3531
+ });
3532
+ return [navigationMatches, revalidatingFetchers];
3533
+ }
3534
+
3535
+ function isNewLoader(currentLoaderData, currentMatch, match) {
3536
+ let isNew = // [a] -> [a, b]
3537
+ !currentMatch || // [a, b] -> [a, c]
3538
+ match.route.id !== currentMatch.route.id; // Handle the case that we don't have data for a re-used route, potentially
3539
+ // from a prior error or from a cancelled pending deferred
3540
+
3541
+ let isMissingData = currentLoaderData[match.route.id] === undefined; // Always load if this is a net-new route or we don't yet have data
3542
+
3543
+ return isNew || isMissingData;
3544
+ }
3545
+
3546
+ function isNewRouteInstance(currentMatch, match) {
3547
+ let currentPath = currentMatch.route.path;
3548
+ return (// param change for this match, /users/123 -> /users/456
3549
+ currentMatch.pathname !== match.pathname || // splat param changed, which is not present in match.path
3550
+ // e.g. /files/images/avatar.jpg -> files/finances.xls
3551
+ currentPath != null && currentPath.endsWith("*") && currentMatch.params["*"] !== match.params["*"]
3552
+ );
3553
+ }
3554
+
3555
+ function shouldRevalidateLoader(loaderMatch, arg) {
3556
+ if (loaderMatch.route.shouldRevalidate) {
3557
+ let routeChoice = loaderMatch.route.shouldRevalidate(arg);
3558
+
3559
+ if (typeof routeChoice === "boolean") {
3560
+ return routeChoice;
3561
+ }
3562
+ }
3563
+
3564
+ return arg.defaultShouldRevalidate;
3565
+ }
3566
+
3567
+ async function callRouteSubPipeline(request, matches, params, middlewareContext, handler) {
3568
+ if (request.signal.aborted) {
3569
+ throw new Error("Request aborted");
3570
+ }
3571
+
3572
+ if (matches.length === 0) {
3573
+ // We reached the end of our middlewares, call the handler
3574
+ middlewareContext.next = () => {
3575
+ throw new Error("You may only call `next()` once per middleware and you may not call " + "it in an action or loader");
3576
+ };
3577
+
3578
+ return handler({
3579
+ request,
3580
+ params,
3581
+ context: middlewareContext
3582
+ });
3583
+ } // We've still got matches, continue on the middleware train. The `next()`
3584
+ // function will "bubble" back up the middlewares after handlers have executed
3585
+
3586
+
3587
+ let nextCalled = false;
3588
+
3589
+ let next = () => {
3590
+ nextCalled = true;
3591
+ return callRouteSubPipeline(request, matches.slice(1), params, middlewareContext, handler);
3592
+ };
3593
+
3594
+ if (!matches[0].route.middleware) {
3595
+ return next();
3596
+ }
3597
+
3598
+ middlewareContext.next = next;
3599
+ let res = await matches[0].route.middleware({
3600
+ request,
3601
+ params,
3602
+ context: middlewareContext
3603
+ });
3604
+
3605
+ if (nextCalled) {
3606
+ return res;
3607
+ } else {
3608
+ return next();
3609
+ }
3610
+ }
3611
+
3612
+ function disabledMiddlewareFn() {
3613
+ throw new Error("Middleware must be enabled via the `future.unstable_middleware` flag)");
3614
+ }
3615
+
3616
+ const disabledMiddlewareContext = {
3617
+ // @ts-expect-error
3618
+ get: disabledMiddlewareFn,
3619
+ set: disabledMiddlewareFn,
3620
+ next: disabledMiddlewareFn
3621
+ };
3622
+
3623
+ async function callLoaderOrAction(type, request, match, matches, basename, enableMiddleware, isStaticRequest, isRouteRequest, requestContext, middlewareContext) {
3624
+ if (basename === void 0) {
3625
+ basename = "/";
3626
+ }
3627
+
3628
+ if (isStaticRequest === void 0) {
3629
+ isStaticRequest = false;
3630
+ }
3631
+
3632
+ if (isRouteRequest === void 0) {
3633
+ isRouteRequest = false;
3634
+ }
3635
+
3636
+ let resultType;
3637
+ let result; // Setup a promise we can race against so that abort signals short circuit
3638
+
3639
+ let reject;
3640
+ let abortPromise = new Promise((_, r) => reject = r);
3641
+
3642
+ let onReject = () => reject();
3643
+
3644
+ request.signal.addEventListener("abort", onReject);
3645
+
3646
+ try {
3647
+ let handler = match.route[type];
3648
+ invariant(handler, "Could not find the " + type + " to run on the \"" + match.route.id + "\" route"); // Only call the pipeline for the matches up to this specific match
3649
+
3650
+ let idx = matches.findIndex(m => m.route.id === match.route.id);
3651
+ let dataPromise;
3652
+
3653
+ if (enableMiddleware) {
3654
+ dataPromise = callRouteSubPipeline(request, matches.slice(0, idx + 1), matches[0].params, createMiddlewareStore(middlewareContext), handler);
3655
+ } else {
3656
+ dataPromise = handler({
3657
+ request,
3658
+ params: match.params,
3659
+ context: requestContext || disabledMiddlewareContext
3660
+ });
3661
+ }
3662
+
3663
+ result = await Promise.race([dataPromise, abortPromise]);
3664
+ invariant(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`.");
3665
+ } catch (e) {
3666
+ resultType = ResultType.error;
3667
+ result = e;
3668
+ } finally {
3669
+ request.signal.removeEventListener("abort", onReject);
3670
+ }
3671
+
3672
+ if (isResponse(result)) {
3673
+ let status = result.status; // Process redirects
3674
+
3675
+ if (redirectStatusCodes.has(status)) {
3676
+ let location = result.headers.get("Location");
3677
+ invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header"); // Support relative routing in internal redirects
3678
+
3679
+ if (!ABSOLUTE_URL_REGEX.test(location)) {
3680
+ let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
3681
+ let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
3682
+ let resolvedLocation = resolveTo(location, routePathnames, new URL(request.url).pathname);
3683
+ invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
3684
+
3685
+ if (basename) {
3686
+ let path = resolvedLocation.pathname;
3687
+ resolvedLocation.pathname = path === "/" ? basename : joinPaths([basename, path]);
3688
+ }
3689
+
3690
+ location = createPath(resolvedLocation);
3691
+ } else if (!isStaticRequest) {
3692
+ // Strip off the protocol+origin for same-origin absolute redirects.
3693
+ // If this is a static reques, we can let it go back to the browser
3694
+ // as-is
3695
+ let currentUrl = new URL(request.url);
3696
+ let url = location.startsWith("//") ? new URL(currentUrl.protocol + location) : new URL(location);
3697
+
3698
+ if (url.origin === currentUrl.origin) {
3699
+ location = url.pathname + url.search + url.hash;
3700
+ }
3701
+ } // Don't process redirects in the router during static requests requests.
3702
+ // Instead, throw the Response and let the server handle it with an HTTP
3703
+ // redirect. We also update the Location header in place in this flow so
3704
+ // basename and relative routing is taken into account
3705
+
3706
+
3707
+ if (isStaticRequest) {
3708
+ result.headers.set("Location", location);
3709
+ throw result;
3710
+ }
3711
+
3712
+ return {
3713
+ type: ResultType.redirect,
3714
+ status,
3715
+ location,
3716
+ revalidate: result.headers.get("X-Remix-Revalidate") !== null
3717
+ };
3718
+ } // For SSR single-route requests, we want to hand Responses back directly
3719
+ // without unwrapping. We do this with the QueryRouteResponse wrapper
3720
+ // interface so we can know whether it was returned or thrown
3721
+
3722
+
3723
+ if (isRouteRequest) {
3724
+ // eslint-disable-next-line no-throw-literal
3725
+ throw {
3726
+ type: resultType || ResultType.data,
3727
+ response: result
3728
+ };
3729
+ }
3730
+
3731
+ let data;
3732
+ let contentType = result.headers.get("Content-Type"); // Check between word boundaries instead of startsWith() due to the last
3733
+ // paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
3734
+
3735
+ if (contentType && /\bapplication\/json\b/.test(contentType)) {
3736
+ data = await result.json();
3737
+ } else {
3738
+ data = await result.text();
3739
+ }
3740
+
3741
+ if (resultType === ResultType.error) {
3742
+ return {
3743
+ type: resultType,
3744
+ error: new ErrorResponse(status, result.statusText, data),
3745
+ headers: result.headers
3746
+ };
3747
+ }
3748
+
3749
+ return {
3750
+ type: ResultType.data,
3751
+ data,
3752
+ statusCode: result.status,
3753
+ headers: result.headers
3754
+ };
3755
+ }
3756
+
3757
+ if (resultType === ResultType.error) {
3758
+ return {
3759
+ type: resultType,
3760
+ error: result
3761
+ };
3762
+ }
3763
+
3764
+ if (result instanceof DeferredData) {
3765
+ return {
3766
+ type: ResultType.deferred,
3767
+ deferredData: result
3768
+ };
3769
+ }
3770
+
3771
+ return {
3772
+ type: ResultType.data,
3773
+ data: result
3774
+ };
3775
+ } // Utility method for creating the Request instances for loaders/actions during
3776
+ // client-side navigations and fetches. During SSR we will always have a
3777
+ // Request instance from the static handler (query/queryRoute)
3778
+
3779
+
3780
+ function createClientSideRequest(history, location, signal, submission) {
3781
+ let url = history.createURL(stripHashFromPath(location)).toString();
3782
+ let init = {
3783
+ signal
3784
+ };
3785
+
3786
+ if (submission && isMutationMethod(submission.formMethod)) {
3787
+ let {
3788
+ formMethod,
3789
+ formEncType,
3790
+ formData
3791
+ } = submission;
3792
+ init.method = formMethod.toUpperCase();
3793
+ init.body = formEncType === "application/x-www-form-urlencoded" ? convertFormDataToSearchParams(formData) : formData;
3794
+ } // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
3795
+
3796
+
3797
+ return new Request(url, init);
3798
+ }
3799
+
3800
+ function convertFormDataToSearchParams(formData) {
3801
+ let searchParams = new URLSearchParams();
3802
+
3803
+ for (let [key, value] of formData.entries()) {
3804
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#converting-an-entry-list-to-a-list-of-name-value-pairs
3805
+ searchParams.append(key, value instanceof File ? value.name : value);
3806
+ }
3807
+
3808
+ return searchParams;
3809
+ }
3810
+
3811
+ function processRouteLoaderData(matches, matchesToLoad, results, pendingError, activeDeferreds) {
3812
+ // Fill in loaderData/errors from our loaders
3813
+ let loaderData = {};
3814
+ let errors = null;
3815
+ let statusCode;
3816
+ let foundError = false;
3817
+ let loaderHeaders = {}; // Process loader results into state.loaderData/state.errors
3818
+
3819
+ results.forEach((result, index) => {
3820
+ let id = matchesToLoad[index].route.id;
3821
+ invariant(!isRedirectResult(result), "Cannot handle redirect results in processLoaderData");
3822
+
3823
+ if (isErrorResult(result)) {
3824
+ // Look upwards from the matched route for the closest ancestor
3825
+ // error boundary, defaulting to the root match
3826
+ let boundaryMatch = findNearestBoundary(matches, id);
3827
+ let error = result.error; // If we have a pending action error, we report it at the highest-route
3828
+ // that throws a loader error, and then clear it out to indicate that
3829
+ // it was consumed
3830
+
3831
+ if (pendingError) {
3832
+ error = Object.values(pendingError)[0];
3833
+ pendingError = undefined;
3834
+ }
3835
+
3836
+ errors = errors || {}; // Prefer higher error values if lower errors bubble to the same boundary
3837
+
3838
+ if (errors[boundaryMatch.route.id] == null) {
3839
+ errors[boundaryMatch.route.id] = error;
3840
+ } // Clear our any prior loaderData for the throwing route
3841
+
3842
+
3843
+ loaderData[id] = undefined; // Once we find our first (highest) error, we set the status code and
3844
+ // prevent deeper status codes from overriding
3845
+
3846
+ if (!foundError) {
3847
+ foundError = true;
3848
+ statusCode = isRouteErrorResponse(result.error) ? result.error.status : 500;
3849
+ }
3850
+
3851
+ if (result.headers) {
3852
+ loaderHeaders[id] = result.headers;
3853
+ }
3854
+ } else {
3855
+ if (isDeferredResult(result)) {
3856
+ activeDeferreds.set(id, result.deferredData);
3857
+ loaderData[id] = result.deferredData.data;
3858
+ } else {
3859
+ loaderData[id] = result.data;
3860
+ } // Error status codes always override success status codes, but if all
3861
+ // loaders are successful we take the deepest status code.
3862
+
3863
+
3864
+ if (result.statusCode != null && result.statusCode !== 200 && !foundError) {
3865
+ statusCode = result.statusCode;
3866
+ }
3867
+
3868
+ if (result.headers) {
3869
+ loaderHeaders[id] = result.headers;
3870
+ }
3871
+ }
3872
+ }); // If we didn't consume the pending action error (i.e., all loaders
3873
+ // resolved), then consume it here. Also clear out any loaderData for the
3874
+ // throwing route
3875
+
3876
+ if (pendingError) {
3877
+ errors = pendingError;
3878
+ loaderData[Object.keys(pendingError)[0]] = undefined;
3879
+ }
3880
+
3881
+ return {
3882
+ loaderData,
3883
+ errors,
3884
+ statusCode: statusCode || 200,
3885
+ loaderHeaders
3886
+ };
3887
+ }
3888
+
3889
+ function processLoaderData(state, matches, matchesToLoad, results, pendingError, revalidatingFetchers, fetcherResults, activeDeferreds) {
3890
+ let {
3891
+ loaderData,
3892
+ errors
3893
+ } = processRouteLoaderData(matches, matchesToLoad, results, pendingError, activeDeferreds); // Process results from our revalidating fetchers
3894
+
3895
+ for (let index = 0; index < revalidatingFetchers.length; index++) {
3896
+ let {
3897
+ key,
3898
+ match
3899
+ } = revalidatingFetchers[index];
3900
+ invariant(fetcherResults !== undefined && fetcherResults[index] !== undefined, "Did not find corresponding fetcher result");
3901
+ let result = fetcherResults[index]; // Process fetcher non-redirect errors
3902
+
3903
+ if (isErrorResult(result)) {
3904
+ let boundaryMatch = findNearestBoundary(state.matches, match.route.id);
3905
+
3906
+ if (!(errors && errors[boundaryMatch.route.id])) {
3907
+ errors = _extends({}, errors, {
3908
+ [boundaryMatch.route.id]: result.error
3909
+ });
3910
+ }
3911
+
3912
+ state.fetchers.delete(key);
3913
+ } else if (isRedirectResult(result)) {
3914
+ // Should never get here, redirects should get processed above, but we
3915
+ // keep this to type narrow to a success result in the else
3916
+ invariant(false, "Unhandled fetcher revalidation redirect");
3917
+ } else if (isDeferredResult(result)) {
3918
+ // Should never get here, deferred data should be awaited for fetchers
3919
+ // in resolveDeferredResults
3920
+ invariant(false, "Unhandled fetcher deferred data");
3921
+ } else {
3922
+ let doneFetcher = {
3923
+ state: "idle",
3924
+ data: result.data,
3925
+ formMethod: undefined,
3926
+ formAction: undefined,
3927
+ formEncType: undefined,
3928
+ formData: undefined,
3929
+ " _hasFetcherDoneAnything ": true
3930
+ };
3931
+ state.fetchers.set(key, doneFetcher);
3932
+ }
3933
+ }
3934
+
3935
+ return {
3936
+ loaderData,
3937
+ errors
3938
+ };
3939
+ }
3940
+
3941
+ function mergeLoaderData(loaderData, newLoaderData, matches, errors) {
3942
+ let mergedLoaderData = _extends({}, newLoaderData);
3943
+
3944
+ for (let match of matches) {
3945
+ let id = match.route.id;
3946
+
3947
+ if (newLoaderData.hasOwnProperty(id)) {
3948
+ if (newLoaderData[id] !== undefined) {
3949
+ mergedLoaderData[id] = newLoaderData[id];
3950
+ }
3951
+ } else if (loaderData[id] !== undefined) {
3952
+ mergedLoaderData[id] = loaderData[id];
3953
+ }
3954
+
3955
+ if (errors && errors.hasOwnProperty(id)) {
3956
+ // Don't keep any loader data below the boundary
3957
+ break;
3958
+ }
3959
+ }
3960
+
3961
+ return mergedLoaderData;
3962
+ } // Find the nearest error boundary, looking upwards from the leaf route (or the
3963
+ // route specified by routeId) for the closest ancestor error boundary,
3964
+ // defaulting to the root match
3965
+
3966
+
3967
+ function findNearestBoundary(matches, routeId) {
3968
+ let eligibleMatches = routeId ? matches.slice(0, matches.findIndex(m => m.route.id === routeId) + 1) : [...matches];
3969
+ return eligibleMatches.reverse().find(m => m.route.hasErrorBoundary === true) || matches[0];
3970
+ }
3971
+
3972
+ function getShortCircuitMatches(routes) {
3973
+ // Prefer a root layout route if present, otherwise shim in a route object
3974
+ let route = routes.find(r => r.index || !r.path || r.path === "/") || {
3975
+ id: "__shim-error-route__"
3976
+ };
3977
+ return {
3978
+ matches: [{
3979
+ params: {},
3980
+ pathname: "",
3981
+ pathnameBase: "",
3982
+ route
3983
+ }],
3984
+ route
3985
+ };
3986
+ }
3987
+
3988
+ function getInternalRouterError(status, _temp4) {
3989
+ let {
3990
+ pathname,
3991
+ routeId,
3992
+ method,
3993
+ type
3994
+ } = _temp4 === void 0 ? {} : _temp4;
3995
+ let statusText = "Unknown Server Error";
3996
+ let errorMessage = "Unknown @remix-run/router error";
3997
+
3998
+ if (status === 400) {
3999
+ statusText = "Bad Request";
4000
+
4001
+ if (method && pathname && routeId) {
4002
+ 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.";
4003
+ } else if (type === "defer-action") {
4004
+ errorMessage = "defer() is not supported in actions";
4005
+ }
4006
+ } else if (status === 403) {
4007
+ statusText = "Forbidden";
4008
+ errorMessage = "Route \"" + routeId + "\" does not match URL \"" + pathname + "\"";
4009
+ } else if (status === 404) {
4010
+ statusText = "Not Found";
4011
+ errorMessage = "No route matches URL \"" + pathname + "\"";
4012
+ } else if (status === 405) {
4013
+ statusText = "Method Not Allowed";
4014
+
4015
+ if (method && pathname && routeId) {
4016
+ 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.";
4017
+ } else if (method) {
4018
+ errorMessage = "Invalid request method \"" + method.toUpperCase() + "\"";
4019
+ }
4020
+ }
4021
+
4022
+ return new ErrorResponse(status || 500, statusText, new Error(errorMessage), true);
4023
+ } // Find any returned redirect errors, starting from the lowest match
4024
+
4025
+
4026
+ function findRedirect(results) {
4027
+ for (let i = results.length - 1; i >= 0; i--) {
4028
+ let result = results[i];
4029
+
4030
+ if (isRedirectResult(result)) {
4031
+ return result;
4032
+ }
4033
+ }
4034
+ }
4035
+
4036
+ function stripHashFromPath(path) {
4037
+ let parsedPath = typeof path === "string" ? parsePath(path) : path;
4038
+ return createPath(_extends({}, parsedPath, {
4039
+ hash: ""
4040
+ }));
4041
+ }
4042
+
4043
+ function isHashChangeOnly(a, b) {
4044
+ return a.pathname === b.pathname && a.search === b.search && a.hash !== b.hash;
4045
+ }
4046
+
4047
+ function isDeferredResult(result) {
4048
+ return result.type === ResultType.deferred;
4049
+ }
4050
+
4051
+ function isErrorResult(result) {
4052
+ return result.type === ResultType.error;
4053
+ }
4054
+
4055
+ function isRedirectResult(result) {
4056
+ return (result && result.type) === ResultType.redirect;
4057
+ }
4058
+
4059
+ function isResponse(value) {
4060
+ return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
4061
+ }
4062
+
4063
+ function isRedirectResponse(result) {
4064
+ if (!isResponse(result)) {
4065
+ return false;
4066
+ }
4067
+
4068
+ let status = result.status;
4069
+ let location = result.headers.get("Location");
4070
+ return status >= 300 && status <= 399 && location != null;
4071
+ }
4072
+
4073
+ function isQueryRouteResponse(obj) {
4074
+ return obj && isResponse(obj.response) && (obj.type === ResultType.data || ResultType.error);
4075
+ }
4076
+
4077
+ function isValidMethod(method) {
4078
+ return validRequestMethods.has(method);
4079
+ }
4080
+
4081
+ function isMutationMethod(method) {
4082
+ return validMutationMethods.has(method);
4083
+ }
4084
+
4085
+ async function resolveDeferredResults(currentMatches, matchesToLoad, results, signal, isFetcher, currentLoaderData) {
4086
+ for (let index = 0; index < results.length; index++) {
4087
+ let result = results[index];
4088
+ let match = matchesToLoad[index];
4089
+ let currentMatch = currentMatches.find(m => m.route.id === match.route.id);
4090
+ let isRevalidatingLoader = currentMatch != null && !isNewRouteInstance(currentMatch, match) && (currentLoaderData && currentLoaderData[match.route.id]) !== undefined;
4091
+
4092
+ if (isDeferredResult(result) && (isFetcher || isRevalidatingLoader)) {
4093
+ // Note: we do not have to touch activeDeferreds here since we race them
4094
+ // against the signal in resolveDeferredData and they'll get aborted
4095
+ // there if needed
4096
+ await resolveDeferredData(result, signal, isFetcher).then(result => {
4097
+ if (result) {
4098
+ results[index] = result || results[index];
4099
+ }
4100
+ });
4101
+ }
4102
+ }
4103
+ }
4104
+
4105
+ async function resolveDeferredData(result, signal, unwrap) {
4106
+ if (unwrap === void 0) {
4107
+ unwrap = false;
4108
+ }
4109
+
4110
+ let aborted = await result.deferredData.resolveData(signal);
4111
+
4112
+ if (aborted) {
4113
+ return;
4114
+ }
4115
+
4116
+ if (unwrap) {
4117
+ try {
4118
+ return {
4119
+ type: ResultType.data,
4120
+ data: result.deferredData.unwrappedData
4121
+ };
4122
+ } catch (e) {
4123
+ // Handle any TrackedPromise._error values encountered while unwrapping
4124
+ return {
4125
+ type: ResultType.error,
4126
+ error: e
4127
+ };
4128
+ }
4129
+ }
4130
+
4131
+ return {
4132
+ type: ResultType.data,
4133
+ data: result.deferredData.data
4134
+ };
4135
+ }
4136
+
4137
+ function hasNakedIndexQuery(search) {
4138
+ return new URLSearchParams(search).getAll("index").some(v => v === "");
4139
+ } // Note: This should match the format exported by useMatches, so if you change
4140
+ // this please also change that :) Eventually we'll DRY this up
4141
+
4142
+
4143
+ function createUseMatchesMatch(match, loaderData) {
4144
+ let {
4145
+ route,
4146
+ pathname,
4147
+ params
4148
+ } = match;
4149
+ return {
4150
+ id: route.id,
4151
+ pathname,
4152
+ params,
4153
+ data: loaderData[route.id],
4154
+ handle: route.handle
4155
+ };
4156
+ }
4157
+
4158
+ function getTargetMatch(matches, location) {
4159
+ let search = typeof location === "string" ? parsePath(location).search : location.search;
4160
+
4161
+ if (matches[matches.length - 1].route.index && hasNakedIndexQuery(search || "")) {
4162
+ // Return the leaf index route when index is present
4163
+ return matches[matches.length - 1];
4164
+ } // Otherwise grab the deepest "path contributing" match (ignoring index and
4165
+ // pathless layout routes)
4166
+
4167
+
4168
+ let pathMatches = getPathContributingMatches(matches);
4169
+ return pathMatches[pathMatches.length - 1];
4170
+ } //#endregion
4171
+
4172
+ exports.AbortedDeferredError = AbortedDeferredError;
4173
+ exports.ErrorResponse = ErrorResponse;
4174
+ exports.IDLE_BLOCKER = IDLE_BLOCKER;
4175
+ exports.IDLE_FETCHER = IDLE_FETCHER;
4176
+ exports.IDLE_NAVIGATION = IDLE_NAVIGATION;
4177
+ exports.UNSAFE_DEFERRED_SYMBOL = UNSAFE_DEFERRED_SYMBOL;
4178
+ exports.UNSAFE_DeferredData = DeferredData;
4179
+ exports.UNSAFE_convertRoutesToDataRoutes = convertRoutesToDataRoutes;
4180
+ exports.UNSAFE_createMiddlewareStore = createMiddlewareStore;
4181
+ exports.UNSAFE_getPathContributingMatches = getPathContributingMatches;
4182
+ exports.createBrowserHistory = createBrowserHistory;
4183
+ exports.createHashHistory = createHashHistory;
4184
+ exports.createMemoryHistory = createMemoryHistory;
4185
+ exports.createMiddlewareContext = createMiddlewareContext;
4186
+ exports.createPath = createPath;
4187
+ exports.createRouter = createRouter;
4188
+ exports.createStaticHandler = createStaticHandler;
4189
+ exports.defer = defer;
4190
+ exports.generatePath = generatePath;
4191
+ exports.getStaticContextFromError = getStaticContextFromError;
4192
+ exports.getToPathname = getToPathname;
4193
+ exports.invariant = invariant;
4194
+ exports.isRouteErrorResponse = isRouteErrorResponse;
4195
+ exports.joinPaths = joinPaths;
4196
+ exports.json = json;
4197
+ exports.matchPath = matchPath;
4198
+ exports.matchRoutes = matchRoutes;
4199
+ exports.normalizePathname = normalizePathname;
4200
+ exports.parsePath = parsePath;
4201
+ exports.redirect = redirect;
4202
+ exports.resolvePath = resolvePath;
4203
+ exports.resolveTo = resolveTo;
4204
+ exports.stripBasename = stripBasename;
4205
+ exports.warning = warning;
4206
+
4207
+ Object.defineProperty(exports, '__esModule', { value: true });
4208
+
4209
+ }));
4210
+ //# sourceMappingURL=router.umd.js.map