@remix-run/router 1.0.2 → 1.0.3-pre.1

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,3425 @@
1
+ /**
2
+ * @remix-run/router v1.0.3-pre.1
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
+ let history = {
102
+ get index() {
103
+ return index;
104
+ },
105
+
106
+ get action() {
107
+ return action;
108
+ },
109
+
110
+ get location() {
111
+ return getCurrentLocation();
112
+ },
113
+
114
+ createHref(to) {
115
+ return typeof to === "string" ? to : createPath(to);
116
+ },
117
+
118
+ encodeLocation(location) {
119
+ return location;
120
+ },
121
+
122
+ push(to, state) {
123
+ action = exports.Action.Push;
124
+ let nextLocation = createMemoryLocation(to, state);
125
+ index += 1;
126
+ entries.splice(index, entries.length, nextLocation);
127
+
128
+ if (v5Compat && listener) {
129
+ listener({
130
+ action,
131
+ location: nextLocation
132
+ });
133
+ }
134
+ },
135
+
136
+ replace(to, state) {
137
+ action = exports.Action.Replace;
138
+ let nextLocation = createMemoryLocation(to, state);
139
+ entries[index] = nextLocation;
140
+
141
+ if (v5Compat && listener) {
142
+ listener({
143
+ action,
144
+ location: nextLocation
145
+ });
146
+ }
147
+ },
148
+
149
+ go(delta) {
150
+ action = exports.Action.Pop;
151
+ index = clampIndex(index + delta);
152
+
153
+ if (listener) {
154
+ listener({
155
+ action,
156
+ location: getCurrentLocation()
157
+ });
158
+ }
159
+ },
160
+
161
+ listen(fn) {
162
+ listener = fn;
163
+ return () => {
164
+ listener = null;
165
+ };
166
+ }
167
+
168
+ };
169
+ return history;
170
+ } //#endregion
171
+ ////////////////////////////////////////////////////////////////////////////////
172
+ //#region Browser History
173
+ ////////////////////////////////////////////////////////////////////////////////
174
+
175
+ /**
176
+ * A browser history stores the current location in regular URLs in a web
177
+ * browser environment. This is the standard for most web apps and provides the
178
+ * cleanest URLs the browser's address bar.
179
+ *
180
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
181
+ */
182
+
183
+ /**
184
+ * Browser history stores the location in regular URLs. This is the standard for
185
+ * most web apps, but it requires some configuration on the server to ensure you
186
+ * serve the same app at multiple URLs.
187
+ *
188
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
189
+ */
190
+ function createBrowserHistory(options) {
191
+ if (options === void 0) {
192
+ options = {};
193
+ }
194
+
195
+ function createBrowserLocation(window, globalHistory) {
196
+ let {
197
+ pathname,
198
+ search,
199
+ hash
200
+ } = window.location;
201
+ return createLocation("", {
202
+ pathname,
203
+ search,
204
+ hash
205
+ }, // state defaults to `null` because `window.history.state` does
206
+ globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
207
+ }
208
+
209
+ function createBrowserHref(window, to) {
210
+ return typeof to === "string" ? to : createPath(to);
211
+ }
212
+
213
+ return getUrlBasedHistory(createBrowserLocation, createBrowserHref, null, options);
214
+ } //#endregion
215
+ ////////////////////////////////////////////////////////////////////////////////
216
+ //#region Hash History
217
+ ////////////////////////////////////////////////////////////////////////////////
218
+
219
+ /**
220
+ * A hash history stores the current location in the fragment identifier portion
221
+ * of the URL in a web browser environment.
222
+ *
223
+ * This is ideal for apps that do not control the server for some reason
224
+ * (because the fragment identifier is never sent to the server), including some
225
+ * shared hosting environments that do not provide fine-grained controls over
226
+ * which pages are served at which URLs.
227
+ *
228
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
229
+ */
230
+
231
+ /**
232
+ * Hash history stores the location in window.location.hash. This makes it ideal
233
+ * for situations where you don't want to send the location to the server for
234
+ * some reason, either because you do cannot configure it or the URL space is
235
+ * reserved for something else.
236
+ *
237
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
238
+ */
239
+ function createHashHistory(options) {
240
+ if (options === void 0) {
241
+ options = {};
242
+ }
243
+
244
+ function createHashLocation(window, globalHistory) {
245
+ let {
246
+ pathname = "/",
247
+ search = "",
248
+ hash = ""
249
+ } = parsePath(window.location.hash.substr(1));
250
+ return createLocation("", {
251
+ pathname,
252
+ search,
253
+ hash
254
+ }, // state defaults to `null` because `window.history.state` does
255
+ globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
256
+ }
257
+
258
+ function createHashHref(window, to) {
259
+ let base = window.document.querySelector("base");
260
+ let href = "";
261
+
262
+ if (base && base.getAttribute("href")) {
263
+ let url = window.location.href;
264
+ let hashIndex = url.indexOf("#");
265
+ href = hashIndex === -1 ? url : url.slice(0, hashIndex);
266
+ }
267
+
268
+ return href + "#" + (typeof to === "string" ? to : createPath(to));
269
+ }
270
+
271
+ function validateHashLocation(location, to) {
272
+ warning$1(location.pathname.charAt(0) === "/", "relative pathnames are not supported in hash history.push(" + JSON.stringify(to) + ")");
273
+ }
274
+
275
+ return getUrlBasedHistory(createHashLocation, createHashHref, validateHashLocation, options);
276
+ } //#endregion
277
+ ////////////////////////////////////////////////////////////////////////////////
278
+ //#region UTILS
279
+ ////////////////////////////////////////////////////////////////////////////////
280
+
281
+ function warning$1(cond, message) {
282
+ if (!cond) {
283
+ // eslint-disable-next-line no-console
284
+ if (typeof console !== "undefined") console.warn(message);
285
+
286
+ try {
287
+ // Welcome to debugging history!
288
+ //
289
+ // This error is thrown as a convenience so you can more easily
290
+ // find the source for a warning that appears in the console by
291
+ // enabling "pause on exceptions" in your JavaScript debugger.
292
+ throw new Error(message); // eslint-disable-next-line no-empty
293
+ } catch (e) {}
294
+ }
295
+ }
296
+
297
+ function createKey() {
298
+ return Math.random().toString(36).substr(2, 8);
299
+ }
300
+ /**
301
+ * For browser-based histories, we combine the state and key into an object
302
+ */
303
+
304
+
305
+ function getHistoryState(location) {
306
+ return {
307
+ usr: location.state,
308
+ key: location.key
309
+ };
310
+ }
311
+ /**
312
+ * Creates a Location object with a unique key from the given Path
313
+ */
314
+
315
+
316
+ function createLocation(current, to, state, key) {
317
+ if (state === void 0) {
318
+ state = null;
319
+ }
320
+
321
+ let location = _extends({
322
+ pathname: typeof current === "string" ? current : current.pathname,
323
+ search: "",
324
+ hash: ""
325
+ }, typeof to === "string" ? parsePath(to) : to, {
326
+ state,
327
+ // TODO: This could be cleaned up. push/replace should probably just take
328
+ // full Locations now and avoid the need to run through this flow at all
329
+ // But that's a pretty big refactor to the current test suite so going to
330
+ // keep as is for the time being and just let any incoming keys take precedence
331
+ key: to && to.key || key || createKey()
332
+ });
333
+
334
+ return location;
335
+ }
336
+ /**
337
+ * Creates a string URL path from the given pathname, search, and hash components.
338
+ */
339
+
340
+ function createPath(_ref) {
341
+ let {
342
+ pathname = "/",
343
+ search = "",
344
+ hash = ""
345
+ } = _ref;
346
+ if (search && search !== "?") pathname += search.charAt(0) === "?" ? search : "?" + search;
347
+ if (hash && hash !== "#") pathname += hash.charAt(0) === "#" ? hash : "#" + hash;
348
+ return pathname;
349
+ }
350
+ /**
351
+ * Parses a string URL path into its separate pathname, search, and hash components.
352
+ */
353
+
354
+ function parsePath(path) {
355
+ let parsedPath = {};
356
+
357
+ if (path) {
358
+ let hashIndex = path.indexOf("#");
359
+
360
+ if (hashIndex >= 0) {
361
+ parsedPath.hash = path.substr(hashIndex);
362
+ path = path.substr(0, hashIndex);
363
+ }
364
+
365
+ let searchIndex = path.indexOf("?");
366
+
367
+ if (searchIndex >= 0) {
368
+ parsedPath.search = path.substr(searchIndex);
369
+ path = path.substr(0, searchIndex);
370
+ }
371
+
372
+ if (path) {
373
+ parsedPath.pathname = path;
374
+ }
375
+ }
376
+
377
+ return parsedPath;
378
+ }
379
+ function createURL(location) {
380
+ // window.location.origin is "null" (the literal string value) in Firefox
381
+ // under certain conditions, notably when serving from a local HTML file
382
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
383
+ let base = typeof window !== "undefined" && typeof window.location !== "undefined" && window.location.origin !== "null" ? window.location.origin : "unknown://unknown";
384
+ let href = typeof location === "string" ? location : createPath(location);
385
+ return new URL(href, base);
386
+ }
387
+
388
+ function getUrlBasedHistory(getLocation, createHref, validateLocation, options) {
389
+ if (options === void 0) {
390
+ options = {};
391
+ }
392
+
393
+ let {
394
+ window = document.defaultView,
395
+ v5Compat = false
396
+ } = options;
397
+ let globalHistory = window.history;
398
+ let action = exports.Action.Pop;
399
+ let listener = null;
400
+
401
+ function handlePop() {
402
+ action = exports.Action.Pop;
403
+
404
+ if (listener) {
405
+ listener({
406
+ action,
407
+ location: history.location
408
+ });
409
+ }
410
+ }
411
+
412
+ function push(to, state) {
413
+ action = exports.Action.Push;
414
+ let location = createLocation(history.location, to, state);
415
+ if (validateLocation) validateLocation(location, to);
416
+ let historyState = getHistoryState(location);
417
+ let url = history.createHref(location); // try...catch because iOS limits us to 100 pushState calls :/
418
+
419
+ try {
420
+ globalHistory.pushState(historyState, "", url);
421
+ } catch (error) {
422
+ // They are going to lose state here, but there is no real
423
+ // way to warn them about it since the page will refresh...
424
+ window.location.assign(url);
425
+ }
426
+
427
+ if (v5Compat && listener) {
428
+ listener({
429
+ action,
430
+ location: history.location
431
+ });
432
+ }
433
+ }
434
+
435
+ function replace(to, state) {
436
+ action = exports.Action.Replace;
437
+ let location = createLocation(history.location, to, state);
438
+ if (validateLocation) validateLocation(location, to);
439
+ let historyState = getHistoryState(location);
440
+ let url = history.createHref(location);
441
+ globalHistory.replaceState(historyState, "", url);
442
+
443
+ if (v5Compat && listener) {
444
+ listener({
445
+ action,
446
+ location: history.location
447
+ });
448
+ }
449
+ }
450
+
451
+ let history = {
452
+ get action() {
453
+ return action;
454
+ },
455
+
456
+ get location() {
457
+ return getLocation(window, globalHistory);
458
+ },
459
+
460
+ listen(fn) {
461
+ if (listener) {
462
+ throw new Error("A history only accepts one active listener");
463
+ }
464
+
465
+ window.addEventListener(PopStateEventType, handlePop);
466
+ listener = fn;
467
+ return () => {
468
+ window.removeEventListener(PopStateEventType, handlePop);
469
+ listener = null;
470
+ };
471
+ },
472
+
473
+ createHref(to) {
474
+ return createHref(window, to);
475
+ },
476
+
477
+ encodeLocation(location) {
478
+ // Encode a Location the same way window.location would
479
+ let url = createURL(createPath(location));
480
+ return _extends({}, location, {
481
+ pathname: url.pathname,
482
+ search: url.search,
483
+ hash: url.hash
484
+ });
485
+ },
486
+
487
+ push,
488
+ replace,
489
+
490
+ go(n) {
491
+ return globalHistory.go(n);
492
+ }
493
+
494
+ };
495
+ return history;
496
+ } //#endregion
497
+
498
+ /**
499
+ * Map of routeId -> data returned from a loader/action/error
500
+ */
501
+
502
+ let ResultType;
503
+ /**
504
+ * Successful result from a loader or action
505
+ */
506
+
507
+ (function (ResultType) {
508
+ ResultType["data"] = "data";
509
+ ResultType["deferred"] = "deferred";
510
+ ResultType["redirect"] = "redirect";
511
+ ResultType["error"] = "error";
512
+ })(ResultType || (ResultType = {}));
513
+
514
+ function isIndexRoute(route) {
515
+ return route.index === true;
516
+ } // Walk the route tree generating unique IDs where necessary so we are working
517
+ // solely with AgnosticDataRouteObject's within the Router
518
+
519
+
520
+ function convertRoutesToDataRoutes(routes, parentPath, allIds) {
521
+ if (parentPath === void 0) {
522
+ parentPath = [];
523
+ }
524
+
525
+ if (allIds === void 0) {
526
+ allIds = new Set();
527
+ }
528
+
529
+ return routes.map((route, index) => {
530
+ let treePath = [...parentPath, index];
531
+ let id = typeof route.id === "string" ? route.id : treePath.join("-");
532
+ invariant(route.index !== true || !route.children, "Cannot specify children on an index route");
533
+ invariant(!allIds.has(id), "Found a route id collision on id \"" + id + "\". Route " + "id's must be globally unique within Data Router usages");
534
+ allIds.add(id);
535
+
536
+ if (isIndexRoute(route)) {
537
+ let indexRoute = _extends({}, route, {
538
+ id
539
+ });
540
+
541
+ return indexRoute;
542
+ } else {
543
+ let pathOrLayoutRoute = _extends({}, route, {
544
+ id,
545
+ children: route.children ? convertRoutesToDataRoutes(route.children, treePath, allIds) : undefined
546
+ });
547
+
548
+ return pathOrLayoutRoute;
549
+ }
550
+ });
551
+ }
552
+ /**
553
+ * Matches the given routes to a location and returns the match data.
554
+ *
555
+ * @see https://reactrouter.com/docs/en/v6/utils/match-routes
556
+ */
557
+
558
+ function matchRoutes(routes, locationArg, basename) {
559
+ if (basename === void 0) {
560
+ basename = "/";
561
+ }
562
+
563
+ let location = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
564
+ let pathname = stripBasename(location.pathname || "/", basename);
565
+
566
+ if (pathname == null) {
567
+ return null;
568
+ }
569
+
570
+ let branches = flattenRoutes(routes);
571
+ rankRouteBranches(branches);
572
+ let matches = null;
573
+
574
+ for (let i = 0; matches == null && i < branches.length; ++i) {
575
+ matches = matchRouteBranch(branches[i], // Incoming pathnames are generally encoded from either window.location
576
+ // or from router.navigate, but we want to match against the unencoded
577
+ // paths in the route definitions. Memory router locations won't be
578
+ // encoded here but there also shouldn't be anything to decode so this
579
+ // should be a safe operation. This avoids needing matchRoutes to be
580
+ // history-aware.
581
+ safelyDecodeURI(pathname));
582
+ }
583
+
584
+ return matches;
585
+ }
586
+
587
+ function flattenRoutes(routes, branches, parentsMeta, parentPath) {
588
+ if (branches === void 0) {
589
+ branches = [];
590
+ }
591
+
592
+ if (parentsMeta === void 0) {
593
+ parentsMeta = [];
594
+ }
595
+
596
+ if (parentPath === void 0) {
597
+ parentPath = "";
598
+ }
599
+
600
+ routes.forEach((route, index) => {
601
+ let meta = {
602
+ relativePath: route.path || "",
603
+ caseSensitive: route.caseSensitive === true,
604
+ childrenIndex: index,
605
+ route
606
+ };
607
+
608
+ if (meta.relativePath.startsWith("/")) {
609
+ 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.");
610
+ meta.relativePath = meta.relativePath.slice(parentPath.length);
611
+ }
612
+
613
+ let path = joinPaths([parentPath, meta.relativePath]);
614
+ let routesMeta = parentsMeta.concat(meta); // Add the children before adding this route to the array so we traverse the
615
+ // route tree depth-first and child routes appear before their parents in
616
+ // the "flattened" version.
617
+
618
+ if (route.children && route.children.length > 0) {
619
+ invariant( // Our types know better, but runtime JS may not!
620
+ // @ts-expect-error
621
+ route.index !== true, "Index routes must not have child routes. Please remove " + ("all child routes from route path \"" + path + "\"."));
622
+ flattenRoutes(route.children, branches, routesMeta, path);
623
+ } // Routes without a path shouldn't ever match by themselves unless they are
624
+ // index routes, so don't add them to the list of possible branches.
625
+
626
+
627
+ if (route.path == null && !route.index) {
628
+ return;
629
+ }
630
+
631
+ branches.push({
632
+ path,
633
+ score: computeScore(path, route.index),
634
+ routesMeta
635
+ });
636
+ });
637
+ return branches;
638
+ }
639
+
640
+ function rankRouteBranches(branches) {
641
+ branches.sort((a, b) => a.score !== b.score ? b.score - a.score // Higher score first
642
+ : compareIndexes(a.routesMeta.map(meta => meta.childrenIndex), b.routesMeta.map(meta => meta.childrenIndex)));
643
+ }
644
+
645
+ const paramRe = /^:\w+$/;
646
+ const dynamicSegmentValue = 3;
647
+ const indexRouteValue = 2;
648
+ const emptySegmentValue = 1;
649
+ const staticSegmentValue = 10;
650
+ const splatPenalty = -2;
651
+
652
+ const isSplat = s => s === "*";
653
+
654
+ function computeScore(path, index) {
655
+ let segments = path.split("/");
656
+ let initialScore = segments.length;
657
+
658
+ if (segments.some(isSplat)) {
659
+ initialScore += splatPenalty;
660
+ }
661
+
662
+ if (index) {
663
+ initialScore += indexRouteValue;
664
+ }
665
+
666
+ return segments.filter(s => !isSplat(s)).reduce((score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), initialScore);
667
+ }
668
+
669
+ function compareIndexes(a, b) {
670
+ let siblings = a.length === b.length && a.slice(0, -1).every((n, i) => n === b[i]);
671
+ return siblings ? // If two routes are siblings, we should try to match the earlier sibling
672
+ // first. This allows people to have fine-grained control over the matching
673
+ // behavior by simply putting routes with identical paths in the order they
674
+ // want them tried.
675
+ a[a.length - 1] - b[b.length - 1] : // Otherwise, it doesn't really make sense to rank non-siblings by index,
676
+ // so they sort equally.
677
+ 0;
678
+ }
679
+
680
+ function matchRouteBranch(branch, pathname) {
681
+ let {
682
+ routesMeta
683
+ } = branch;
684
+ let matchedParams = {};
685
+ let matchedPathname = "/";
686
+ let matches = [];
687
+
688
+ for (let i = 0; i < routesMeta.length; ++i) {
689
+ let meta = routesMeta[i];
690
+ let end = i === routesMeta.length - 1;
691
+ let remainingPathname = matchedPathname === "/" ? pathname : pathname.slice(matchedPathname.length) || "/";
692
+ let match = matchPath({
693
+ path: meta.relativePath,
694
+ caseSensitive: meta.caseSensitive,
695
+ end
696
+ }, remainingPathname);
697
+ if (!match) return null;
698
+ Object.assign(matchedParams, match.params);
699
+ let route = meta.route;
700
+ matches.push({
701
+ // TODO: Can this as be avoided?
702
+ params: matchedParams,
703
+ pathname: joinPaths([matchedPathname, match.pathname]),
704
+ pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])),
705
+ route
706
+ });
707
+
708
+ if (match.pathnameBase !== "/") {
709
+ matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
710
+ }
711
+ }
712
+
713
+ return matches;
714
+ }
715
+ /**
716
+ * Returns a path with params interpolated.
717
+ *
718
+ * @see https://reactrouter.com/docs/en/v6/utils/generate-path
719
+ */
720
+
721
+
722
+ function generatePath(path, params) {
723
+ if (params === void 0) {
724
+ params = {};
725
+ }
726
+
727
+ return path.replace(/:(\w+)/g, (_, key) => {
728
+ invariant(params[key] != null, "Missing \":" + key + "\" param");
729
+ return params[key];
730
+ }).replace(/(\/?)\*/, (_, prefix, __, str) => {
731
+ const star = "*";
732
+
733
+ if (params[star] == null) {
734
+ // If no splat was provided, trim the trailing slash _unless_ it's
735
+ // the entire path
736
+ return str === "/*" ? "/" : "";
737
+ } // Apply the splat
738
+
739
+
740
+ return "" + prefix + params[star];
741
+ });
742
+ }
743
+ /**
744
+ * A PathPattern is used to match on some portion of a URL pathname.
745
+ */
746
+
747
+ /**
748
+ * Performs pattern matching on a URL pathname and returns information about
749
+ * the match.
750
+ *
751
+ * @see https://reactrouter.com/docs/en/v6/utils/match-path
752
+ */
753
+ function matchPath(pattern, pathname) {
754
+ if (typeof pattern === "string") {
755
+ pattern = {
756
+ path: pattern,
757
+ caseSensitive: false,
758
+ end: true
759
+ };
760
+ }
761
+
762
+ let [matcher, paramNames] = compilePath(pattern.path, pattern.caseSensitive, pattern.end);
763
+ let match = pathname.match(matcher);
764
+ if (!match) return null;
765
+ let matchedPathname = match[0];
766
+ let pathnameBase = matchedPathname.replace(/(.)\/+$/, "$1");
767
+ let captureGroups = match.slice(1);
768
+ let params = paramNames.reduce((memo, paramName, index) => {
769
+ // We need to compute the pathnameBase here using the raw splat value
770
+ // instead of using params["*"] later because it will be decoded then
771
+ if (paramName === "*") {
772
+ let splatValue = captureGroups[index] || "";
773
+ pathnameBase = matchedPathname.slice(0, matchedPathname.length - splatValue.length).replace(/(.)\/+$/, "$1");
774
+ }
775
+
776
+ memo[paramName] = safelyDecodeURIComponent(captureGroups[index] || "", paramName);
777
+ return memo;
778
+ }, {});
779
+ return {
780
+ params,
781
+ pathname: matchedPathname,
782
+ pathnameBase,
783
+ pattern
784
+ };
785
+ }
786
+
787
+ function compilePath(path, caseSensitive, end) {
788
+ if (caseSensitive === void 0) {
789
+ caseSensitive = false;
790
+ }
791
+
792
+ if (end === void 0) {
793
+ end = true;
794
+ }
795
+
796
+ 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(/\*$/, "/*") + "\"."));
797
+ let paramNames = [];
798
+ let regexpSource = "^" + path.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
799
+ .replace(/^\/*/, "/") // Make sure it has a leading /
800
+ .replace(/[\\.*+^$?{}|()[\]]/g, "\\$&") // Escape special regex chars
801
+ .replace(/:(\w+)/g, (_, paramName) => {
802
+ paramNames.push(paramName);
803
+ return "([^\\/]+)";
804
+ });
805
+
806
+ if (path.endsWith("*")) {
807
+ paramNames.push("*");
808
+ regexpSource += path === "*" || path === "/*" ? "(.*)$" // Already matched the initial /, just match the rest
809
+ : "(?:\\/(.+)|\\/*)$"; // Don't include the / in params["*"]
810
+ } else if (end) {
811
+ // When matching to the end, ignore trailing slashes
812
+ regexpSource += "\\/*$";
813
+ } else if (path !== "" && path !== "/") {
814
+ // If our path is non-empty and contains anything beyond an initial slash,
815
+ // then we have _some_ form of path in our regex so we should expect to
816
+ // match only if we find the end of this path segment. Look for an optional
817
+ // non-captured trailing slash (to match a portion of the URL) or the end
818
+ // of the path (if we've matched to the end). We used to do this with a
819
+ // word boundary but that gives false positives on routes like
820
+ // /user-preferences since `-` counts as a word boundary.
821
+ regexpSource += "(?:(?=\\/|$))";
822
+ } else ;
823
+
824
+ let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
825
+ return [matcher, paramNames];
826
+ }
827
+
828
+ function safelyDecodeURI(value) {
829
+ try {
830
+ return decodeURI(value);
831
+ } catch (error) {
832
+ 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 + ")."));
833
+ return value;
834
+ }
835
+ }
836
+
837
+ function safelyDecodeURIComponent(value, paramName) {
838
+ try {
839
+ return decodeURIComponent(value);
840
+ } catch (error) {
841
+ 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 + ")."));
842
+ return value;
843
+ }
844
+ }
845
+ /**
846
+ * @private
847
+ */
848
+
849
+
850
+ function stripBasename(pathname, basename) {
851
+ if (basename === "/") return pathname;
852
+
853
+ if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
854
+ return null;
855
+ } // We want to leave trailing slash behavior in the user's control, so if they
856
+ // specify a basename with a trailing slash, we should support it
857
+
858
+
859
+ let startIndex = basename.endsWith("/") ? basename.length - 1 : basename.length;
860
+ let nextChar = pathname.charAt(startIndex);
861
+
862
+ if (nextChar && nextChar !== "/") {
863
+ // pathname does not start with basename/
864
+ return null;
865
+ }
866
+
867
+ return pathname.slice(startIndex) || "/";
868
+ }
869
+ /**
870
+ * @private
871
+ */
872
+
873
+ function invariant(value, message) {
874
+ if (value === false || value === null || typeof value === "undefined") {
875
+ throw new Error(message);
876
+ }
877
+ }
878
+ /**
879
+ * @private
880
+ */
881
+
882
+ function warning(cond, message) {
883
+ if (!cond) {
884
+ // eslint-disable-next-line no-console
885
+ if (typeof console !== "undefined") console.warn(message);
886
+
887
+ try {
888
+ // Welcome to debugging React Router!
889
+ //
890
+ // This error is thrown as a convenience so you can more easily
891
+ // find the source for a warning that appears in the console by
892
+ // enabling "pause on exceptions" in your JavaScript debugger.
893
+ throw new Error(message); // eslint-disable-next-line no-empty
894
+ } catch (e) {}
895
+ }
896
+ }
897
+ /**
898
+ * Returns a resolved path object relative to the given pathname.
899
+ *
900
+ * @see https://reactrouter.com/docs/en/v6/utils/resolve-path
901
+ */
902
+
903
+ function resolvePath(to, fromPathname) {
904
+ if (fromPathname === void 0) {
905
+ fromPathname = "/";
906
+ }
907
+
908
+ let {
909
+ pathname: toPathname,
910
+ search = "",
911
+ hash = ""
912
+ } = typeof to === "string" ? parsePath(to) : to;
913
+ let pathname = toPathname ? toPathname.startsWith("/") ? toPathname : resolvePathname(toPathname, fromPathname) : fromPathname;
914
+ return {
915
+ pathname,
916
+ search: normalizeSearch(search),
917
+ hash: normalizeHash(hash)
918
+ };
919
+ }
920
+
921
+ function resolvePathname(relativePath, fromPathname) {
922
+ let segments = fromPathname.replace(/\/+$/, "").split("/");
923
+ let relativeSegments = relativePath.split("/");
924
+ relativeSegments.forEach(segment => {
925
+ if (segment === "..") {
926
+ // Keep the root "" segment so the pathname starts at /
927
+ if (segments.length > 1) segments.pop();
928
+ } else if (segment !== ".") {
929
+ segments.push(segment);
930
+ }
931
+ });
932
+ return segments.length > 1 ? segments.join("/") : "/";
933
+ }
934
+
935
+ function getInvalidPathError(char, field, dest, path) {
936
+ 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.";
937
+ }
938
+ /**
939
+ * @private
940
+ *
941
+ * When processing relative navigation we want to ignore ancestor routes that
942
+ * do not contribute to the path, such that index/pathless layout routes don't
943
+ * interfere.
944
+ *
945
+ * For example, when moving a route element into an index route and/or a
946
+ * pathless layout route, relative link behavior contained within should stay
947
+ * the same. Both of the following examples should link back to the root:
948
+ *
949
+ * <Route path="/">
950
+ * <Route path="accounts" element={<Link to=".."}>
951
+ * </Route>
952
+ *
953
+ * <Route path="/">
954
+ * <Route path="accounts">
955
+ * <Route element={<AccountsLayout />}> // <-- Does not contribute
956
+ * <Route index element={<Link to=".."} /> // <-- Does not contribute
957
+ * </Route
958
+ * </Route>
959
+ * </Route>
960
+ */
961
+
962
+
963
+ function getPathContributingMatches(matches) {
964
+ return matches.filter((match, index) => index === 0 || match.route.path && match.route.path.length > 0);
965
+ }
966
+ /**
967
+ * @private
968
+ */
969
+
970
+ function resolveTo(toArg, routePathnames, locationPathname, isPathRelative) {
971
+ if (isPathRelative === void 0) {
972
+ isPathRelative = false;
973
+ }
974
+
975
+ let to;
976
+
977
+ if (typeof toArg === "string") {
978
+ to = parsePath(toArg);
979
+ } else {
980
+ to = _extends({}, toArg);
981
+ invariant(!to.pathname || !to.pathname.includes("?"), getInvalidPathError("?", "pathname", "search", to));
982
+ invariant(!to.pathname || !to.pathname.includes("#"), getInvalidPathError("#", "pathname", "hash", to));
983
+ invariant(!to.search || !to.search.includes("#"), getInvalidPathError("#", "search", "hash", to));
984
+ }
985
+
986
+ let isEmptyPath = toArg === "" || to.pathname === "";
987
+ let toPathname = isEmptyPath ? "/" : to.pathname;
988
+ let from; // Routing is relative to the current pathname if explicitly requested.
989
+ //
990
+ // If a pathname is explicitly provided in `to`, it should be relative to the
991
+ // route context. This is explained in `Note on `<Link to>` values` in our
992
+ // migration guide from v5 as a means of disambiguation between `to` values
993
+ // that begin with `/` and those that do not. However, this is problematic for
994
+ // `to` values that do not provide a pathname. `to` can simply be a search or
995
+ // hash string, in which case we should assume that the navigation is relative
996
+ // to the current location's pathname and *not* the route pathname.
997
+
998
+ if (isPathRelative || toPathname == null) {
999
+ from = locationPathname;
1000
+ } else {
1001
+ let routePathnameIndex = routePathnames.length - 1;
1002
+
1003
+ if (toPathname.startsWith("..")) {
1004
+ let toSegments = toPathname.split("/"); // Each leading .. segment means "go up one route" instead of "go up one
1005
+ // URL segment". This is a key difference from how <a href> works and a
1006
+ // major reason we call this a "to" value instead of a "href".
1007
+
1008
+ while (toSegments[0] === "..") {
1009
+ toSegments.shift();
1010
+ routePathnameIndex -= 1;
1011
+ }
1012
+
1013
+ to.pathname = toSegments.join("/");
1014
+ } // If there are more ".." segments than parent routes, resolve relative to
1015
+ // the root / URL.
1016
+
1017
+
1018
+ from = routePathnameIndex >= 0 ? routePathnames[routePathnameIndex] : "/";
1019
+ }
1020
+
1021
+ let path = resolvePath(to, from); // Ensure the pathname has a trailing slash if the original "to" had one
1022
+
1023
+ let hasExplicitTrailingSlash = toPathname && toPathname !== "/" && toPathname.endsWith("/"); // Or if this was a link to the current path which has a trailing slash
1024
+
1025
+ let hasCurrentTrailingSlash = (isEmptyPath || toPathname === ".") && locationPathname.endsWith("/");
1026
+
1027
+ if (!path.pathname.endsWith("/") && (hasExplicitTrailingSlash || hasCurrentTrailingSlash)) {
1028
+ path.pathname += "/";
1029
+ }
1030
+
1031
+ return path;
1032
+ }
1033
+ /**
1034
+ * @private
1035
+ */
1036
+
1037
+ function getToPathname(to) {
1038
+ // Empty strings should be treated the same as / paths
1039
+ return to === "" || to.pathname === "" ? "/" : typeof to === "string" ? parsePath(to).pathname : to.pathname;
1040
+ }
1041
+ /**
1042
+ * @private
1043
+ */
1044
+
1045
+ const joinPaths = paths => paths.join("/").replace(/\/\/+/g, "/");
1046
+ /**
1047
+ * @private
1048
+ */
1049
+
1050
+ const normalizePathname = pathname => pathname.replace(/\/+$/, "").replace(/^\/*/, "/");
1051
+ /**
1052
+ * @private
1053
+ */
1054
+
1055
+ const normalizeSearch = search => !search || search === "?" ? "" : search.startsWith("?") ? search : "?" + search;
1056
+ /**
1057
+ * @private
1058
+ */
1059
+
1060
+ const normalizeHash = hash => !hash || hash === "#" ? "" : hash.startsWith("#") ? hash : "#" + hash;
1061
+
1062
+ /**
1063
+ * This is a shortcut for creating `application/json` responses. Converts `data`
1064
+ * to JSON and sets the `Content-Type` header.
1065
+ */
1066
+ const json = function json(data, init) {
1067
+ if (init === void 0) {
1068
+ init = {};
1069
+ }
1070
+
1071
+ let responseInit = typeof init === "number" ? {
1072
+ status: init
1073
+ } : init;
1074
+ let headers = new Headers(responseInit.headers);
1075
+
1076
+ if (!headers.has("Content-Type")) {
1077
+ headers.set("Content-Type", "application/json; charset=utf-8");
1078
+ }
1079
+
1080
+ return new Response(JSON.stringify(data), _extends({}, responseInit, {
1081
+ headers
1082
+ }));
1083
+ };
1084
+ class AbortedDeferredError extends Error {}
1085
+ class DeferredData {
1086
+ constructor(data) {
1087
+ this.pendingKeys = new Set();
1088
+ this.subscriber = undefined;
1089
+ 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
1090
+ // cancellation
1091
+
1092
+ let reject;
1093
+ this.abortPromise = new Promise((_, r) => reject = r);
1094
+ this.controller = new AbortController();
1095
+
1096
+ let onAbort = () => reject(new AbortedDeferredError("Deferred data aborted"));
1097
+
1098
+ this.unlistenAbortSignal = () => this.controller.signal.removeEventListener("abort", onAbort);
1099
+
1100
+ this.controller.signal.addEventListener("abort", onAbort);
1101
+ this.data = Object.entries(data).reduce((acc, _ref) => {
1102
+ let [key, value] = _ref;
1103
+ return Object.assign(acc, {
1104
+ [key]: this.trackPromise(key, value)
1105
+ });
1106
+ }, {});
1107
+ }
1108
+
1109
+ trackPromise(key, value) {
1110
+ if (!(value instanceof Promise)) {
1111
+ return value;
1112
+ }
1113
+
1114
+ this.pendingKeys.add(key); // We store a little wrapper promise that will be extended with
1115
+ // _data/_error props upon resolve/reject
1116
+
1117
+ 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
1118
+ // errors or aborted deferred values
1119
+
1120
+ promise.catch(() => {});
1121
+ Object.defineProperty(promise, "_tracked", {
1122
+ get: () => true
1123
+ });
1124
+ return promise;
1125
+ }
1126
+
1127
+ onSettle(promise, key, error, data) {
1128
+ if (this.controller.signal.aborted && error instanceof AbortedDeferredError) {
1129
+ this.unlistenAbortSignal();
1130
+ Object.defineProperty(promise, "_error", {
1131
+ get: () => error
1132
+ });
1133
+ return Promise.reject(error);
1134
+ }
1135
+
1136
+ this.pendingKeys.delete(key);
1137
+
1138
+ if (this.done) {
1139
+ // Nothing left to abort!
1140
+ this.unlistenAbortSignal();
1141
+ }
1142
+
1143
+ const subscriber = this.subscriber;
1144
+
1145
+ if (error) {
1146
+ Object.defineProperty(promise, "_error", {
1147
+ get: () => error
1148
+ });
1149
+ subscriber && subscriber(false);
1150
+ return Promise.reject(error);
1151
+ }
1152
+
1153
+ Object.defineProperty(promise, "_data", {
1154
+ get: () => data
1155
+ });
1156
+ subscriber && subscriber(false);
1157
+ return data;
1158
+ }
1159
+
1160
+ subscribe(fn) {
1161
+ this.subscriber = fn;
1162
+ }
1163
+
1164
+ cancel() {
1165
+ this.controller.abort();
1166
+ this.pendingKeys.forEach((v, k) => this.pendingKeys.delete(k));
1167
+ let subscriber = this.subscriber;
1168
+ subscriber && subscriber(true);
1169
+ }
1170
+
1171
+ async resolveData(signal) {
1172
+ let aborted = false;
1173
+
1174
+ if (!this.done) {
1175
+ let onAbort = () => this.cancel();
1176
+
1177
+ signal.addEventListener("abort", onAbort);
1178
+ aborted = await new Promise(resolve => {
1179
+ this.subscribe(aborted => {
1180
+ signal.removeEventListener("abort", onAbort);
1181
+
1182
+ if (aborted || this.done) {
1183
+ resolve(aborted);
1184
+ }
1185
+ });
1186
+ });
1187
+ }
1188
+
1189
+ return aborted;
1190
+ }
1191
+
1192
+ get done() {
1193
+ return this.pendingKeys.size === 0;
1194
+ }
1195
+
1196
+ get unwrappedData() {
1197
+ invariant(this.data !== null && this.done, "Can only unwrap data on initialized and settled deferreds");
1198
+ return Object.entries(this.data).reduce((acc, _ref2) => {
1199
+ let [key, value] = _ref2;
1200
+ return Object.assign(acc, {
1201
+ [key]: unwrapTrackedPromise(value)
1202
+ });
1203
+ }, {});
1204
+ }
1205
+
1206
+ }
1207
+
1208
+ function isTrackedPromise(value) {
1209
+ return value instanceof Promise && value._tracked === true;
1210
+ }
1211
+
1212
+ function unwrapTrackedPromise(value) {
1213
+ if (!isTrackedPromise(value)) {
1214
+ return value;
1215
+ }
1216
+
1217
+ if (value._error) {
1218
+ throw value._error;
1219
+ }
1220
+
1221
+ return value._data;
1222
+ }
1223
+
1224
+ function defer(data) {
1225
+ return new DeferredData(data);
1226
+ }
1227
+
1228
+ /**
1229
+ * A redirect response. Sets the status code and the `Location` header.
1230
+ * Defaults to "302 Found".
1231
+ */
1232
+ const redirect = function redirect(url, init) {
1233
+ if (init === void 0) {
1234
+ init = 302;
1235
+ }
1236
+
1237
+ let responseInit = init;
1238
+
1239
+ if (typeof responseInit === "number") {
1240
+ responseInit = {
1241
+ status: responseInit
1242
+ };
1243
+ } else if (typeof responseInit.status === "undefined") {
1244
+ responseInit.status = 302;
1245
+ }
1246
+
1247
+ let headers = new Headers(responseInit.headers);
1248
+ headers.set("Location", url);
1249
+ return new Response(null, _extends({}, responseInit, {
1250
+ headers
1251
+ }));
1252
+ };
1253
+ /**
1254
+ * @private
1255
+ * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
1256
+ */
1257
+
1258
+ class ErrorResponse {
1259
+ constructor(status, statusText, data) {
1260
+ this.status = status;
1261
+ this.statusText = statusText || "";
1262
+ this.data = data;
1263
+ }
1264
+
1265
+ }
1266
+ /**
1267
+ * Check if the given error is an ErrorResponse generated from a 4xx/5xx
1268
+ * Response throw from an action/loader
1269
+ */
1270
+
1271
+ function isRouteErrorResponse(e) {
1272
+ return e instanceof ErrorResponse;
1273
+ }
1274
+
1275
+ //#region Types and Constants
1276
+ ////////////////////////////////////////////////////////////////////////////////
1277
+
1278
+ /**
1279
+ * A Router instance manages all navigation and data loading/mutations
1280
+ */
1281
+
1282
+ const IDLE_NAVIGATION = {
1283
+ state: "idle",
1284
+ location: undefined,
1285
+ formMethod: undefined,
1286
+ formAction: undefined,
1287
+ formEncType: undefined,
1288
+ formData: undefined
1289
+ };
1290
+ const IDLE_FETCHER = {
1291
+ state: "idle",
1292
+ data: undefined,
1293
+ formMethod: undefined,
1294
+ formAction: undefined,
1295
+ formEncType: undefined,
1296
+ formData: undefined
1297
+ };
1298
+ const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined";
1299
+ const isServer = !isBrowser; //#endregion
1300
+ ////////////////////////////////////////////////////////////////////////////////
1301
+ //#region createRouter
1302
+ ////////////////////////////////////////////////////////////////////////////////
1303
+
1304
+ /**
1305
+ * Create a router and listen to history POP navigations
1306
+ */
1307
+
1308
+ function createRouter(init) {
1309
+ invariant(init.routes.length > 0, "You must provide a non-empty routes array to createRouter");
1310
+ let dataRoutes = convertRoutesToDataRoutes(init.routes); // Cleanup function for history
1311
+
1312
+ let unlistenHistory = null; // Externally-provided functions to call on all state changes
1313
+
1314
+ let subscribers = new Set(); // Externally-provided object to hold scroll restoration locations during routing
1315
+
1316
+ let savedScrollPositions = null; // Externally-provided function to get scroll restoration keys
1317
+
1318
+ let getScrollRestorationKey = null; // Externally-provided function to get current scroll position
1319
+
1320
+ let getScrollPosition = null; // One-time flag to control the initial hydration scroll restoration. Because
1321
+ // we don't get the saved positions from <ScrollRestoration /> until _after_
1322
+ // the initial render, we need to manually trigger a separate updateState to
1323
+ // send along the restoreScrollPosition
1324
+
1325
+ let initialScrollRestored = false;
1326
+ let initialMatches = matchRoutes(dataRoutes, init.history.location, init.basename);
1327
+ let initialErrors = null;
1328
+
1329
+ if (initialMatches == null) {
1330
+ // If we do not match a user-provided-route, fall back to the root
1331
+ // to allow the error boundary to take over
1332
+ let {
1333
+ matches,
1334
+ route,
1335
+ error
1336
+ } = getNotFoundMatches(dataRoutes);
1337
+ initialMatches = matches;
1338
+ initialErrors = {
1339
+ [route.id]: error
1340
+ };
1341
+ }
1342
+
1343
+ let initialized = !initialMatches.some(m => m.route.loader) || init.hydrationData != null;
1344
+ let router;
1345
+ let state = {
1346
+ historyAction: init.history.action,
1347
+ location: init.history.location,
1348
+ matches: initialMatches,
1349
+ initialized,
1350
+ navigation: IDLE_NAVIGATION,
1351
+ restoreScrollPosition: null,
1352
+ preventScrollReset: false,
1353
+ revalidation: "idle",
1354
+ loaderData: init.hydrationData && init.hydrationData.loaderData || {},
1355
+ actionData: init.hydrationData && init.hydrationData.actionData || null,
1356
+ errors: init.hydrationData && init.hydrationData.errors || initialErrors,
1357
+ fetchers: new Map()
1358
+ }; // -- Stateful internal variables to manage navigations --
1359
+ // Current navigation in progress (to be committed in completeNavigation)
1360
+
1361
+ let pendingAction = exports.Action.Pop; // Should the current navigation prevent the scroll reset if scroll cannot
1362
+ // be restored?
1363
+
1364
+ let pendingPreventScrollReset = false; // AbortController for the active navigation
1365
+
1366
+ let pendingNavigationController; // We use this to avoid touching history in completeNavigation if a
1367
+ // revalidation is entirely uninterrupted
1368
+
1369
+ let isUninterruptedRevalidation = false; // Use this internal flag to force revalidation of all loaders:
1370
+ // - submissions (completed or interrupted)
1371
+ // - useRevalidate()
1372
+ // - X-Remix-Revalidate (from redirect)
1373
+
1374
+ let isRevalidationRequired = false; // Use this internal array to capture routes that require revalidation due
1375
+ // to a cancelled deferred on action submission
1376
+
1377
+ let cancelledDeferredRoutes = []; // Use this internal array to capture fetcher loads that were cancelled by an
1378
+ // action navigation and require revalidation
1379
+
1380
+ let cancelledFetcherLoads = []; // AbortControllers for any in-flight fetchers
1381
+
1382
+ let fetchControllers = new Map(); // Track loads based on the order in which they started
1383
+
1384
+ let incrementingLoadId = 0; // Track the outstanding pending navigation data load to be compared against
1385
+ // the globally incrementing load when a fetcher load lands after a completed
1386
+ // navigation
1387
+
1388
+ let pendingNavigationLoadId = -1; // Fetchers that triggered data reloads as a result of their actions
1389
+
1390
+ let fetchReloadIds = new Map(); // Fetchers that triggered redirect navigations from their actions
1391
+
1392
+ let fetchRedirectIds = new Set(); // Most recent href/match for fetcher.load calls for fetchers
1393
+
1394
+ let fetchLoadMatches = new Map(); // Store DeferredData instances for active route matches. When a
1395
+ // route loader returns defer() we stick one in here. Then, when a nested
1396
+ // promise resolves we update loaderData. If a new navigation starts we
1397
+ // cancel active deferreds for eliminated routes.
1398
+
1399
+ let activeDeferreds = new Map(); // Initialize the router, all side effects should be kicked off from here.
1400
+ // Implemented as a Fluent API for ease of:
1401
+ // let router = createRouter(init).initialize();
1402
+
1403
+ function initialize() {
1404
+ // If history informs us of a POP navigation, start the navigation but do not update
1405
+ // state. We'll update our own state once the navigation completes
1406
+ unlistenHistory = init.history.listen(_ref => {
1407
+ let {
1408
+ action: historyAction,
1409
+ location
1410
+ } = _ref;
1411
+ return startNavigation(historyAction, location);
1412
+ }); // Kick off initial data load if needed. Use Pop to avoid modifying history
1413
+
1414
+ if (!state.initialized) {
1415
+ startNavigation(exports.Action.Pop, state.location);
1416
+ }
1417
+
1418
+ return router;
1419
+ } // Clean up a router and it's side effects
1420
+
1421
+
1422
+ function dispose() {
1423
+ if (unlistenHistory) {
1424
+ unlistenHistory();
1425
+ }
1426
+
1427
+ subscribers.clear();
1428
+ pendingNavigationController && pendingNavigationController.abort();
1429
+ state.fetchers.forEach((_, key) => deleteFetcher(key));
1430
+ } // Subscribe to state updates for the router
1431
+
1432
+
1433
+ function subscribe(fn) {
1434
+ subscribers.add(fn);
1435
+ return () => subscribers.delete(fn);
1436
+ } // Update our state and notify the calling context of the change
1437
+
1438
+
1439
+ function updateState(newState) {
1440
+ state = _extends({}, state, newState);
1441
+ subscribers.forEach(subscriber => subscriber(state));
1442
+ } // Complete a navigation returning the state.navigation back to the IDLE_NAVIGATION
1443
+ // and setting state.[historyAction/location/matches] to the new route.
1444
+ // - Location is a required param
1445
+ // - Navigation will always be set to IDLE_NAVIGATION
1446
+ // - Can pass any other state in newState
1447
+
1448
+
1449
+ function completeNavigation(location, newState) {
1450
+ var _state$navigation$for;
1451
+
1452
+ // Deduce if we're in a loading/actionReload state:
1453
+ // - We have committed actionData in the store
1454
+ // - The current navigation was a submission
1455
+ // - We're past the submitting state and into the loading state
1456
+ // - The location we've finished loading is different from the submission
1457
+ // location, indicating we redirected from the action (avoids false
1458
+ // positives for loading/submissionRedirect when actionData returned
1459
+ // on a prior submission)
1460
+ let isActionReload = state.actionData != null && state.navigation.formMethod != null && state.navigation.state === "loading" && ((_state$navigation$for = state.navigation.formAction) == null ? void 0 : _state$navigation$for.split("?")[0]) === location.pathname; // Always preserve any existing loaderData from re-used routes
1461
+
1462
+ let newLoaderData = newState.loaderData ? {
1463
+ loaderData: mergeLoaderData(state.loaderData, newState.loaderData, newState.matches || [])
1464
+ } : {};
1465
+ updateState(_extends({}, isActionReload ? {} : {
1466
+ actionData: null
1467
+ }, newState, newLoaderData, {
1468
+ historyAction: pendingAction,
1469
+ location,
1470
+ initialized: true,
1471
+ navigation: IDLE_NAVIGATION,
1472
+ revalidation: "idle",
1473
+ // Don't restore on submission navigations
1474
+ restoreScrollPosition: state.navigation.formData ? false : getSavedScrollPosition(location, newState.matches || state.matches),
1475
+ preventScrollReset: pendingPreventScrollReset
1476
+ }));
1477
+
1478
+ if (isUninterruptedRevalidation) ; else if (pendingAction === exports.Action.Pop) ; else if (pendingAction === exports.Action.Push) {
1479
+ init.history.push(location, location.state);
1480
+ } else if (pendingAction === exports.Action.Replace) {
1481
+ init.history.replace(location, location.state);
1482
+ } // Reset stateful navigation vars
1483
+
1484
+
1485
+ pendingAction = exports.Action.Pop;
1486
+ pendingPreventScrollReset = false;
1487
+ isUninterruptedRevalidation = false;
1488
+ isRevalidationRequired = false;
1489
+ cancelledDeferredRoutes = [];
1490
+ cancelledFetcherLoads = [];
1491
+ } // Trigger a navigation event, which can either be a numerical POP or a PUSH
1492
+ // replace with an optional submission
1493
+
1494
+
1495
+ async function navigate(to, opts) {
1496
+ if (typeof to === "number") {
1497
+ init.history.go(to);
1498
+ return;
1499
+ }
1500
+
1501
+ let {
1502
+ path,
1503
+ submission,
1504
+ error
1505
+ } = normalizeNavigateOptions(to, opts);
1506
+ let location = createLocation(state.location, path, opts && opts.state); // When using navigate as a PUSH/REPLACE we aren't reading an already-encoded
1507
+ // URL from window.location, so we need to encode it here so the behavior
1508
+ // remains the same as POP and non-data-router usages. new URL() does all
1509
+ // the same encoding we'd get from a history.pushState/window.location read
1510
+ // without having to touch history
1511
+
1512
+ location = init.history.encodeLocation(location);
1513
+ let historyAction = (opts && opts.replace) === true || submission != null ? exports.Action.Replace : exports.Action.Push;
1514
+ let preventScrollReset = opts && "preventScrollReset" in opts ? opts.preventScrollReset === true : undefined;
1515
+ return await startNavigation(historyAction, location, {
1516
+ submission,
1517
+ // Send through the formData serialization error if we have one so we can
1518
+ // render at the right error boundary after we match routes
1519
+ pendingError: error,
1520
+ preventScrollReset,
1521
+ replace: opts && opts.replace
1522
+ });
1523
+ } // Revalidate all current loaders. If a navigation is in progress or if this
1524
+ // is interrupted by a navigation, allow this to "succeed" by calling all
1525
+ // loaders during the next loader round
1526
+
1527
+
1528
+ function revalidate() {
1529
+ interruptActiveLoads();
1530
+ updateState({
1531
+ revalidation: "loading"
1532
+ }); // If we're currently submitting an action, we don't need to start a new
1533
+ // navigation, we'll just let the follow up loader execution call all loaders
1534
+
1535
+ if (state.navigation.state === "submitting") {
1536
+ return;
1537
+ } // If we're currently in an idle state, start a new navigation for the current
1538
+ // action/location and mark it as uninterrupted, which will skip the history
1539
+ // update in completeNavigation
1540
+
1541
+
1542
+ if (state.navigation.state === "idle") {
1543
+ startNavigation(state.historyAction, state.location, {
1544
+ startUninterruptedRevalidation: true
1545
+ });
1546
+ return;
1547
+ } // Otherwise, if we're currently in a loading state, just start a new
1548
+ // navigation to the navigation.location but do not trigger an uninterrupted
1549
+ // revalidation so that history correctly updates once the navigation completes
1550
+
1551
+
1552
+ startNavigation(pendingAction || state.historyAction, state.navigation.location, {
1553
+ overrideNavigation: state.navigation
1554
+ });
1555
+ } // Start a navigation to the given action/location. Can optionally provide a
1556
+ // overrideNavigation which will override the normalLoad in the case of a redirect
1557
+ // navigation
1558
+
1559
+
1560
+ async function startNavigation(historyAction, location, opts) {
1561
+ // Abort any in-progress navigations and start a new one. Unset any ongoing
1562
+ // uninterrupted revalidations unless told otherwise, since we want this
1563
+ // new navigation to update history normally
1564
+ pendingNavigationController && pendingNavigationController.abort();
1565
+ pendingNavigationController = null;
1566
+ pendingAction = historyAction;
1567
+ isUninterruptedRevalidation = (opts && opts.startUninterruptedRevalidation) === true; // Save the current scroll position every time we start a new navigation,
1568
+ // and track whether we should reset scroll on completion
1569
+
1570
+ saveScrollPosition(state.location, state.matches);
1571
+ pendingPreventScrollReset = (opts && opts.preventScrollReset) === true;
1572
+ let loadingNavigation = opts && opts.overrideNavigation;
1573
+ let matches = matchRoutes(dataRoutes, location, init.basename); // Short circuit with a 404 on the root error boundary if we match nothing
1574
+
1575
+ if (!matches) {
1576
+ let {
1577
+ matches: notFoundMatches,
1578
+ route,
1579
+ error
1580
+ } = getNotFoundMatches(dataRoutes); // Cancel all pending deferred on 404s since we don't keep any routes
1581
+
1582
+ cancelActiveDeferreds();
1583
+ completeNavigation(location, {
1584
+ matches: notFoundMatches,
1585
+ loaderData: {},
1586
+ errors: {
1587
+ [route.id]: error
1588
+ }
1589
+ });
1590
+ return;
1591
+ } // Short circuit if it's only a hash change
1592
+
1593
+
1594
+ if (isHashChangeOnly(state.location, location)) {
1595
+ completeNavigation(location, {
1596
+ matches
1597
+ });
1598
+ return;
1599
+ } // Create a controller/Request for this navigation
1600
+
1601
+
1602
+ pendingNavigationController = new AbortController();
1603
+ let request = createRequest(location, pendingNavigationController.signal, opts && opts.submission);
1604
+ let pendingActionData;
1605
+ let pendingError;
1606
+
1607
+ if (opts && opts.pendingError) {
1608
+ // If we have a pendingError, it means the user attempted a GET submission
1609
+ // with binary FormData so assign here and skip to handleLoaders. That
1610
+ // way we handle calling loaders above the boundary etc. It's not really
1611
+ // different from an actionError in that sense.
1612
+ pendingError = {
1613
+ [findNearestBoundary(matches).route.id]: opts.pendingError
1614
+ };
1615
+ } else if (opts && opts.submission) {
1616
+ // Call action if we received an action submission
1617
+ let actionOutput = await handleAction(request, location, opts.submission, matches, {
1618
+ replace: opts.replace
1619
+ });
1620
+
1621
+ if (actionOutput.shortCircuited) {
1622
+ return;
1623
+ }
1624
+
1625
+ pendingActionData = actionOutput.pendingActionData;
1626
+ pendingError = actionOutput.pendingActionError;
1627
+
1628
+ let navigation = _extends({
1629
+ state: "loading",
1630
+ location
1631
+ }, opts.submission);
1632
+
1633
+ loadingNavigation = navigation;
1634
+ } // Call loaders
1635
+
1636
+
1637
+ let {
1638
+ shortCircuited,
1639
+ loaderData,
1640
+ errors
1641
+ } = await handleLoaders(request, location, matches, loadingNavigation, opts && opts.submission, opts && opts.replace, pendingActionData, pendingError);
1642
+
1643
+ if (shortCircuited) {
1644
+ return;
1645
+ } // Clean up now that the action/loaders have completed. Don't clean up if
1646
+ // we short circuited because pendingNavigationController will have already
1647
+ // been assigned to a new controller for the next navigation
1648
+
1649
+
1650
+ pendingNavigationController = null;
1651
+ completeNavigation(location, {
1652
+ matches,
1653
+ loaderData,
1654
+ errors
1655
+ });
1656
+ } // Call the action matched by the leaf route for this navigation and handle
1657
+ // redirects/errors
1658
+
1659
+
1660
+ async function handleAction(request, location, submission, matches, opts) {
1661
+ interruptActiveLoads(); // Put us in a submitting state
1662
+
1663
+ let navigation = _extends({
1664
+ state: "submitting",
1665
+ location
1666
+ }, submission);
1667
+
1668
+ updateState({
1669
+ navigation
1670
+ }); // Call our action and get the result
1671
+
1672
+ let result;
1673
+ let actionMatch = getTargetMatch(matches, location);
1674
+
1675
+ if (!actionMatch.route.action) {
1676
+ result = getMethodNotAllowedResult(location);
1677
+ } else {
1678
+ result = await callLoaderOrAction("action", request, actionMatch, matches, router.basename);
1679
+
1680
+ if (request.signal.aborted) {
1681
+ return {
1682
+ shortCircuited: true
1683
+ };
1684
+ }
1685
+ }
1686
+
1687
+ if (isRedirectResult(result)) {
1688
+ let redirectNavigation = _extends({
1689
+ state: "loading",
1690
+ location: createLocation(state.location, result.location)
1691
+ }, submission);
1692
+
1693
+ await startRedirectNavigation(result, redirectNavigation, opts && opts.replace);
1694
+ return {
1695
+ shortCircuited: true
1696
+ };
1697
+ }
1698
+
1699
+ if (isErrorResult(result)) {
1700
+ // Store off the pending error - we use it to determine which loaders
1701
+ // to call and will commit it when we complete the navigation
1702
+ let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id); // By default, all submissions are REPLACE navigations, but if the
1703
+ // action threw an error that'll be rendered in an errorElement, we fall
1704
+ // back to PUSH so that the user can use the back button to get back to
1705
+ // the pre-submission form location to try again
1706
+
1707
+ if ((opts && opts.replace) !== true) {
1708
+ pendingAction = exports.Action.Push;
1709
+ }
1710
+
1711
+ return {
1712
+ pendingActionError: {
1713
+ [boundaryMatch.route.id]: result.error
1714
+ }
1715
+ };
1716
+ }
1717
+
1718
+ if (isDeferredResult(result)) {
1719
+ throw new Error("defer() is not supported in actions");
1720
+ }
1721
+
1722
+ return {
1723
+ pendingActionData: {
1724
+ [actionMatch.route.id]: result.data
1725
+ }
1726
+ };
1727
+ } // Call all applicable loaders for the given matches, handling redirects,
1728
+ // errors, etc.
1729
+
1730
+
1731
+ async function handleLoaders(request, location, matches, overrideNavigation, submission, replace, pendingActionData, pendingError) {
1732
+ // Figure out the right navigation we want to use for data loading
1733
+ let loadingNavigation = overrideNavigation;
1734
+
1735
+ if (!loadingNavigation) {
1736
+ let navigation = {
1737
+ state: "loading",
1738
+ location,
1739
+ formMethod: undefined,
1740
+ formAction: undefined,
1741
+ formEncType: undefined,
1742
+ formData: undefined
1743
+ };
1744
+ loadingNavigation = navigation;
1745
+ }
1746
+
1747
+ let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(state, matches, submission, location, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, pendingActionData, pendingError, fetchLoadMatches); // Cancel pending deferreds for no-longer-matched routes or routes we're
1748
+ // about to reload. Note that if this is an action reload we would have
1749
+ // already cancelled all pending deferreds so this would be a no-op
1750
+
1751
+ 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
1752
+
1753
+ if (matchesToLoad.length === 0 && revalidatingFetchers.length === 0) {
1754
+ completeNavigation(location, {
1755
+ matches,
1756
+ loaderData: mergeLoaderData(state.loaderData, {}, matches),
1757
+ // Commit pending error if we're short circuiting
1758
+ errors: pendingError || null,
1759
+ actionData: pendingActionData || null
1760
+ });
1761
+ return {
1762
+ shortCircuited: true
1763
+ };
1764
+ } // If this is an uninterrupted revalidation, we remain in our current idle
1765
+ // state. If not, we need to switch to our loading state and load data,
1766
+ // preserving any new action data or existing action data (in the case of
1767
+ // a revalidation interrupting an actionReload)
1768
+
1769
+
1770
+ if (!isUninterruptedRevalidation) {
1771
+ revalidatingFetchers.forEach(_ref2 => {
1772
+ let [key] = _ref2;
1773
+ let fetcher = state.fetchers.get(key);
1774
+ let revalidatingFetcher = {
1775
+ state: "loading",
1776
+ data: fetcher && fetcher.data,
1777
+ formMethod: undefined,
1778
+ formAction: undefined,
1779
+ formEncType: undefined,
1780
+ formData: undefined
1781
+ };
1782
+ state.fetchers.set(key, revalidatingFetcher);
1783
+ });
1784
+ updateState(_extends({
1785
+ navigation: loadingNavigation,
1786
+ actionData: pendingActionData || state.actionData || null
1787
+ }, revalidatingFetchers.length > 0 ? {
1788
+ fetchers: new Map(state.fetchers)
1789
+ } : {}));
1790
+ }
1791
+
1792
+ pendingNavigationLoadId = ++incrementingLoadId;
1793
+ revalidatingFetchers.forEach(_ref3 => {
1794
+ let [key] = _ref3;
1795
+ return fetchControllers.set(key, pendingNavigationController);
1796
+ });
1797
+ let {
1798
+ results,
1799
+ loaderResults,
1800
+ fetcherResults
1801
+ } = await callLoadersAndMaybeResolveData(state.matches, matches, matchesToLoad, revalidatingFetchers, request);
1802
+
1803
+ if (request.signal.aborted) {
1804
+ return {
1805
+ shortCircuited: true
1806
+ };
1807
+ } // Clean up _after_ loaders have completed. Don't clean up if we short
1808
+ // circuited because fetchControllers would have been aborted and
1809
+ // reassigned to new controllers for the next navigation
1810
+
1811
+
1812
+ revalidatingFetchers.forEach(_ref4 => {
1813
+ let [key] = _ref4;
1814
+ return fetchControllers.delete(key);
1815
+ }); // If any loaders returned a redirect Response, start a new REPLACE navigation
1816
+
1817
+ let redirect = findRedirect(results);
1818
+
1819
+ if (redirect) {
1820
+ let redirectNavigation = getLoaderRedirect(state, redirect);
1821
+ await startRedirectNavigation(redirect, redirectNavigation, replace);
1822
+ return {
1823
+ shortCircuited: true
1824
+ };
1825
+ } // Process and commit output from loaders
1826
+
1827
+
1828
+ let {
1829
+ loaderData,
1830
+ errors
1831
+ } = processLoaderData(state, matches, matchesToLoad, loaderResults, pendingError, revalidatingFetchers, fetcherResults, activeDeferreds); // Wire up subscribers to update loaderData as promises settle
1832
+
1833
+ activeDeferreds.forEach((deferredData, routeId) => {
1834
+ deferredData.subscribe(aborted => {
1835
+ // Note: No need to updateState here since the TrackedPromise on
1836
+ // loaderData is stable across resolve/reject
1837
+ // Remove this instance if we were aborted or if promises have settled
1838
+ if (aborted || deferredData.done) {
1839
+ activeDeferreds.delete(routeId);
1840
+ }
1841
+ });
1842
+ });
1843
+ markFetchRedirectsDone();
1844
+ let didAbortFetchLoads = abortStaleFetchLoads(pendingNavigationLoadId);
1845
+ return _extends({
1846
+ loaderData,
1847
+ errors
1848
+ }, didAbortFetchLoads || revalidatingFetchers.length > 0 ? {
1849
+ fetchers: new Map(state.fetchers)
1850
+ } : {});
1851
+ }
1852
+
1853
+ function getFetcher(key) {
1854
+ return state.fetchers.get(key) || IDLE_FETCHER;
1855
+ } // Trigger a fetcher load/submit for the given fetcher key
1856
+
1857
+
1858
+ function fetch(key, routeId, href, opts) {
1859
+ if (isServer) {
1860
+ 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.");
1861
+ }
1862
+
1863
+ if (fetchControllers.has(key)) abortFetcher(key);
1864
+ let matches = matchRoutes(dataRoutes, href, init.basename);
1865
+
1866
+ if (!matches) {
1867
+ setFetcherError(key, routeId, new ErrorResponse(404, "Not Found", null));
1868
+ return;
1869
+ }
1870
+
1871
+ let {
1872
+ path,
1873
+ submission
1874
+ } = normalizeNavigateOptions(href, opts, true);
1875
+ let match = getTargetMatch(matches, path);
1876
+
1877
+ if (submission) {
1878
+ handleFetcherAction(key, routeId, path, match, matches, submission);
1879
+ return;
1880
+ } // Store off the match so we can call it's shouldRevalidate on subsequent
1881
+ // revalidations
1882
+
1883
+
1884
+ fetchLoadMatches.set(key, [path, match, matches]);
1885
+ handleFetcherLoader(key, routeId, path, match, matches);
1886
+ } // Call the action for the matched fetcher.submit(), and then handle redirects,
1887
+ // errors, and revalidation
1888
+
1889
+
1890
+ async function handleFetcherAction(key, routeId, path, match, requestMatches, submission) {
1891
+ interruptActiveLoads();
1892
+ fetchLoadMatches.delete(key);
1893
+
1894
+ if (!match.route.action) {
1895
+ let {
1896
+ error
1897
+ } = getMethodNotAllowedResult(path);
1898
+ setFetcherError(key, routeId, error);
1899
+ return;
1900
+ } // Put this fetcher into it's submitting state
1901
+
1902
+
1903
+ let existingFetcher = state.fetchers.get(key);
1904
+
1905
+ let fetcher = _extends({
1906
+ state: "submitting"
1907
+ }, submission, {
1908
+ data: existingFetcher && existingFetcher.data
1909
+ });
1910
+
1911
+ state.fetchers.set(key, fetcher);
1912
+ updateState({
1913
+ fetchers: new Map(state.fetchers)
1914
+ }); // Call the action for the fetcher
1915
+
1916
+ let abortController = new AbortController();
1917
+ let fetchRequest = createRequest(path, abortController.signal, submission);
1918
+ fetchControllers.set(key, abortController);
1919
+ let actionResult = await callLoaderOrAction("action", fetchRequest, match, requestMatches, router.basename);
1920
+
1921
+ if (fetchRequest.signal.aborted) {
1922
+ // We can delete this so long as we weren't aborted by ou our own fetcher
1923
+ // re-submit which would have put _new_ controller is in fetchControllers
1924
+ if (fetchControllers.get(key) === abortController) {
1925
+ fetchControllers.delete(key);
1926
+ }
1927
+
1928
+ return;
1929
+ }
1930
+
1931
+ if (isRedirectResult(actionResult)) {
1932
+ fetchControllers.delete(key);
1933
+ fetchRedirectIds.add(key);
1934
+
1935
+ let loadingFetcher = _extends({
1936
+ state: "loading"
1937
+ }, submission, {
1938
+ data: undefined
1939
+ });
1940
+
1941
+ state.fetchers.set(key, loadingFetcher);
1942
+ updateState({
1943
+ fetchers: new Map(state.fetchers)
1944
+ });
1945
+
1946
+ let redirectNavigation = _extends({
1947
+ state: "loading",
1948
+ location: createLocation(state.location, actionResult.location)
1949
+ }, submission);
1950
+
1951
+ await startRedirectNavigation(actionResult, redirectNavigation);
1952
+ return;
1953
+ } // Process any non-redirect errors thrown
1954
+
1955
+
1956
+ if (isErrorResult(actionResult)) {
1957
+ setFetcherError(key, routeId, actionResult.error);
1958
+ return;
1959
+ }
1960
+
1961
+ if (isDeferredResult(actionResult)) {
1962
+ invariant(false, "defer() is not supported in actions");
1963
+ } // Start the data load for current matches, or the next location if we're
1964
+ // in the middle of a navigation
1965
+
1966
+
1967
+ let nextLocation = state.navigation.location || state.location;
1968
+ let revalidationRequest = createRequest(nextLocation, abortController.signal);
1969
+ let matches = state.navigation.state !== "idle" ? matchRoutes(dataRoutes, state.navigation.location, init.basename) : state.matches;
1970
+ invariant(matches, "Didn't find any matches after fetcher action");
1971
+ let loadId = ++incrementingLoadId;
1972
+ fetchReloadIds.set(key, loadId);
1973
+
1974
+ let loadFetcher = _extends({
1975
+ state: "loading",
1976
+ data: actionResult.data
1977
+ }, submission);
1978
+
1979
+ state.fetchers.set(key, loadFetcher);
1980
+ let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(state, matches, submission, nextLocation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, {
1981
+ [match.route.id]: actionResult.data
1982
+ }, undefined, // No need to send through errors since we short circuit above
1983
+ fetchLoadMatches); // Put all revalidating fetchers into the loading state, except for the
1984
+ // current fetcher which we want to keep in it's current loading state which
1985
+ // contains it's action submission info + action data
1986
+
1987
+ revalidatingFetchers.filter(_ref5 => {
1988
+ let [staleKey] = _ref5;
1989
+ return staleKey !== key;
1990
+ }).forEach(_ref6 => {
1991
+ let [staleKey] = _ref6;
1992
+ let existingFetcher = state.fetchers.get(staleKey);
1993
+ let revalidatingFetcher = {
1994
+ state: "loading",
1995
+ data: existingFetcher && existingFetcher.data,
1996
+ formMethod: undefined,
1997
+ formAction: undefined,
1998
+ formEncType: undefined,
1999
+ formData: undefined
2000
+ };
2001
+ state.fetchers.set(staleKey, revalidatingFetcher);
2002
+ fetchControllers.set(staleKey, abortController);
2003
+ });
2004
+ updateState({
2005
+ fetchers: new Map(state.fetchers)
2006
+ });
2007
+ let {
2008
+ results,
2009
+ loaderResults,
2010
+ fetcherResults
2011
+ } = await callLoadersAndMaybeResolveData(state.matches, matches, matchesToLoad, revalidatingFetchers, revalidationRequest);
2012
+
2013
+ if (abortController.signal.aborted) {
2014
+ return;
2015
+ }
2016
+
2017
+ fetchReloadIds.delete(key);
2018
+ fetchControllers.delete(key);
2019
+ revalidatingFetchers.forEach(_ref7 => {
2020
+ let [staleKey] = _ref7;
2021
+ return fetchControllers.delete(staleKey);
2022
+ });
2023
+ let redirect = findRedirect(results);
2024
+
2025
+ if (redirect) {
2026
+ let redirectNavigation = getLoaderRedirect(state, redirect);
2027
+ await startRedirectNavigation(redirect, redirectNavigation);
2028
+ return;
2029
+ } // Process and commit output from loaders
2030
+
2031
+
2032
+ let {
2033
+ loaderData,
2034
+ errors
2035
+ } = processLoaderData(state, state.matches, matchesToLoad, loaderResults, undefined, revalidatingFetchers, fetcherResults, activeDeferreds);
2036
+ let doneFetcher = {
2037
+ state: "idle",
2038
+ data: actionResult.data,
2039
+ formMethod: undefined,
2040
+ formAction: undefined,
2041
+ formEncType: undefined,
2042
+ formData: undefined
2043
+ };
2044
+ state.fetchers.set(key, doneFetcher);
2045
+ let didAbortFetchLoads = abortStaleFetchLoads(loadId); // If we are currently in a navigation loading state and this fetcher is
2046
+ // more recent than the navigation, we want the newer data so abort the
2047
+ // navigation and complete it with the fetcher data
2048
+
2049
+ if (state.navigation.state === "loading" && loadId > pendingNavigationLoadId) {
2050
+ invariant(pendingAction, "Expected pending action");
2051
+ pendingNavigationController && pendingNavigationController.abort();
2052
+ completeNavigation(state.navigation.location, {
2053
+ matches,
2054
+ loaderData,
2055
+ errors,
2056
+ fetchers: new Map(state.fetchers)
2057
+ });
2058
+ } else {
2059
+ // otherwise just update with the fetcher data, preserving any existing
2060
+ // loaderData for loaders that did not need to reload. We have to
2061
+ // manually merge here since we aren't going through completeNavigation
2062
+ updateState(_extends({
2063
+ errors,
2064
+ loaderData: mergeLoaderData(state.loaderData, loaderData, matches)
2065
+ }, didAbortFetchLoads ? {
2066
+ fetchers: new Map(state.fetchers)
2067
+ } : {}));
2068
+ isRevalidationRequired = false;
2069
+ }
2070
+ } // Call the matched loader for fetcher.load(), handling redirects, errors, etc.
2071
+
2072
+
2073
+ async function handleFetcherLoader(key, routeId, path, match, matches) {
2074
+ let existingFetcher = state.fetchers.get(key); // Put this fetcher into it's loading state
2075
+
2076
+ let loadingFetcher = {
2077
+ state: "loading",
2078
+ formMethod: undefined,
2079
+ formAction: undefined,
2080
+ formEncType: undefined,
2081
+ formData: undefined,
2082
+ data: existingFetcher && existingFetcher.data
2083
+ };
2084
+ state.fetchers.set(key, loadingFetcher);
2085
+ updateState({
2086
+ fetchers: new Map(state.fetchers)
2087
+ }); // Call the loader for this fetcher route match
2088
+
2089
+ let abortController = new AbortController();
2090
+ let fetchRequest = createRequest(path, abortController.signal);
2091
+ fetchControllers.set(key, abortController);
2092
+ let result = await callLoaderOrAction("loader", fetchRequest, match, matches, router.basename); // Deferred isn't supported or fetcher loads, await everything and treat it
2093
+ // as a normal load. resolveDeferredData will return undefined if this
2094
+ // fetcher gets aborted, so we just leave result untouched and short circuit
2095
+ // below if that happens
2096
+
2097
+ if (isDeferredResult(result)) {
2098
+ result = (await resolveDeferredData(result, fetchRequest.signal, true)) || result;
2099
+ } // We can delete this so long as we weren't aborted by ou our own fetcher
2100
+ // re-load which would have put _new_ controller is in fetchControllers
2101
+
2102
+
2103
+ if (fetchControllers.get(key) === abortController) {
2104
+ fetchControllers.delete(key);
2105
+ }
2106
+
2107
+ if (fetchRequest.signal.aborted) {
2108
+ return;
2109
+ } // If the loader threw a redirect Response, start a new REPLACE navigation
2110
+
2111
+
2112
+ if (isRedirectResult(result)) {
2113
+ let redirectNavigation = getLoaderRedirect(state, result);
2114
+ await startRedirectNavigation(result, redirectNavigation);
2115
+ return;
2116
+ } // Process any non-redirect errors thrown
2117
+
2118
+
2119
+ if (isErrorResult(result)) {
2120
+ let boundaryMatch = findNearestBoundary(state.matches, routeId);
2121
+ state.fetchers.delete(key); // TODO: In remix, this would reset to IDLE_NAVIGATION if it was a catch -
2122
+ // do we need to behave any differently with our non-redirect errors?
2123
+ // What if it was a non-redirect Response?
2124
+
2125
+ updateState({
2126
+ fetchers: new Map(state.fetchers),
2127
+ errors: {
2128
+ [boundaryMatch.route.id]: result.error
2129
+ }
2130
+ });
2131
+ return;
2132
+ }
2133
+
2134
+ invariant(!isDeferredResult(result), "Unhandled fetcher deferred data"); // Put the fetcher back into an idle state
2135
+
2136
+ let doneFetcher = {
2137
+ state: "idle",
2138
+ data: result.data,
2139
+ formMethod: undefined,
2140
+ formAction: undefined,
2141
+ formEncType: undefined,
2142
+ formData: undefined
2143
+ };
2144
+ state.fetchers.set(key, doneFetcher);
2145
+ updateState({
2146
+ fetchers: new Map(state.fetchers)
2147
+ });
2148
+ }
2149
+ /**
2150
+ * Utility function to handle redirects returned from an action or loader.
2151
+ * Normally, a redirect "replaces" the navigation that triggered it. So, for
2152
+ * example:
2153
+ *
2154
+ * - user is on /a
2155
+ * - user clicks a link to /b
2156
+ * - loader for /b redirects to /c
2157
+ *
2158
+ * In a non-JS app the browser would track the in-flight navigation to /b and
2159
+ * then replace it with /c when it encountered the redirect response. In
2160
+ * the end it would only ever update the URL bar with /c.
2161
+ *
2162
+ * In client-side routing using pushState/replaceState, we aim to emulate
2163
+ * this behavior and we also do not update history until the end of the
2164
+ * navigation (including processed redirects). This means that we never
2165
+ * actually touch history until we've processed redirects, so we just use
2166
+ * the history action from the original navigation (PUSH or REPLACE).
2167
+ */
2168
+
2169
+
2170
+ async function startRedirectNavigation(redirect, navigation, replace) {
2171
+ if (redirect.revalidate) {
2172
+ isRevalidationRequired = true;
2173
+ }
2174
+
2175
+ invariant(navigation.location, "Expected a location on the redirect navigation"); // There's no need to abort on redirects, since we don't detect the
2176
+ // redirect until the action/loaders have settled
2177
+
2178
+ pendingNavigationController = null;
2179
+ let redirectHistoryAction = replace === true ? exports.Action.Replace : exports.Action.Push;
2180
+ await startNavigation(redirectHistoryAction, navigation.location, {
2181
+ overrideNavigation: navigation
2182
+ });
2183
+ }
2184
+
2185
+ async function callLoadersAndMaybeResolveData(currentMatches, matches, matchesToLoad, fetchersToLoad, request) {
2186
+ // Call all navigation loaders and revalidating fetcher loaders in parallel,
2187
+ // then slice off the results into separate arrays so we can handle them
2188
+ // accordingly
2189
+ let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, router.basename)), ...fetchersToLoad.map(_ref8 => {
2190
+ let [, href, match, fetchMatches] = _ref8;
2191
+ return callLoaderOrAction("loader", createRequest(href, request.signal), match, fetchMatches, router.basename);
2192
+ })]);
2193
+ let loaderResults = results.slice(0, matchesToLoad.length);
2194
+ let fetcherResults = results.slice(matchesToLoad.length);
2195
+ await Promise.all([resolveDeferredResults(currentMatches, matchesToLoad, loaderResults, request.signal, false, state.loaderData), resolveDeferredResults(currentMatches, fetchersToLoad.map(_ref9 => {
2196
+ let [,, match] = _ref9;
2197
+ return match;
2198
+ }), fetcherResults, request.signal, true)]);
2199
+ return {
2200
+ results,
2201
+ loaderResults,
2202
+ fetcherResults
2203
+ };
2204
+ }
2205
+
2206
+ function interruptActiveLoads() {
2207
+ // Every interruption triggers a revalidation
2208
+ isRevalidationRequired = true; // Cancel pending route-level deferreds and mark cancelled routes for
2209
+ // revalidation
2210
+
2211
+ cancelledDeferredRoutes.push(...cancelActiveDeferreds()); // Abort in-flight fetcher loads
2212
+
2213
+ fetchLoadMatches.forEach((_, key) => {
2214
+ if (fetchControllers.has(key)) {
2215
+ cancelledFetcherLoads.push(key);
2216
+ abortFetcher(key);
2217
+ }
2218
+ });
2219
+ }
2220
+
2221
+ function setFetcherError(key, routeId, error) {
2222
+ let boundaryMatch = findNearestBoundary(state.matches, routeId);
2223
+ deleteFetcher(key);
2224
+ updateState({
2225
+ errors: {
2226
+ [boundaryMatch.route.id]: error
2227
+ },
2228
+ fetchers: new Map(state.fetchers)
2229
+ });
2230
+ }
2231
+
2232
+ function deleteFetcher(key) {
2233
+ if (fetchControllers.has(key)) abortFetcher(key);
2234
+ fetchLoadMatches.delete(key);
2235
+ fetchReloadIds.delete(key);
2236
+ fetchRedirectIds.delete(key);
2237
+ state.fetchers.delete(key);
2238
+ }
2239
+
2240
+ function abortFetcher(key) {
2241
+ let controller = fetchControllers.get(key);
2242
+ invariant(controller, "Expected fetch controller: " + key);
2243
+ controller.abort();
2244
+ fetchControllers.delete(key);
2245
+ }
2246
+
2247
+ function markFetchersDone(keys) {
2248
+ for (let key of keys) {
2249
+ let fetcher = getFetcher(key);
2250
+ let doneFetcher = {
2251
+ state: "idle",
2252
+ data: fetcher.data,
2253
+ formMethod: undefined,
2254
+ formAction: undefined,
2255
+ formEncType: undefined,
2256
+ formData: undefined
2257
+ };
2258
+ state.fetchers.set(key, doneFetcher);
2259
+ }
2260
+ }
2261
+
2262
+ function markFetchRedirectsDone() {
2263
+ let doneKeys = [];
2264
+
2265
+ for (let key of fetchRedirectIds) {
2266
+ let fetcher = state.fetchers.get(key);
2267
+ invariant(fetcher, "Expected fetcher: " + key);
2268
+
2269
+ if (fetcher.state === "loading") {
2270
+ fetchRedirectIds.delete(key);
2271
+ doneKeys.push(key);
2272
+ }
2273
+ }
2274
+
2275
+ markFetchersDone(doneKeys);
2276
+ }
2277
+
2278
+ function abortStaleFetchLoads(landedId) {
2279
+ let yeetedKeys = [];
2280
+
2281
+ for (let [key, id] of fetchReloadIds) {
2282
+ if (id < landedId) {
2283
+ let fetcher = state.fetchers.get(key);
2284
+ invariant(fetcher, "Expected fetcher: " + key);
2285
+
2286
+ if (fetcher.state === "loading") {
2287
+ abortFetcher(key);
2288
+ fetchReloadIds.delete(key);
2289
+ yeetedKeys.push(key);
2290
+ }
2291
+ }
2292
+ }
2293
+
2294
+ markFetchersDone(yeetedKeys);
2295
+ return yeetedKeys.length > 0;
2296
+ }
2297
+
2298
+ function cancelActiveDeferreds(predicate) {
2299
+ let cancelledRouteIds = [];
2300
+ activeDeferreds.forEach((dfd, routeId) => {
2301
+ if (!predicate || predicate(routeId)) {
2302
+ // Cancel the deferred - but do not remove from activeDeferreds here -
2303
+ // we rely on the subscribers to do that so our tests can assert proper
2304
+ // cleanup via _internalActiveDeferreds
2305
+ dfd.cancel();
2306
+ cancelledRouteIds.push(routeId);
2307
+ activeDeferreds.delete(routeId);
2308
+ }
2309
+ });
2310
+ return cancelledRouteIds;
2311
+ } // Opt in to capturing and reporting scroll positions during navigations,
2312
+ // used by the <ScrollRestoration> component
2313
+
2314
+
2315
+ function enableScrollRestoration(positions, getPosition, getKey) {
2316
+ savedScrollPositions = positions;
2317
+ getScrollPosition = getPosition;
2318
+
2319
+ getScrollRestorationKey = getKey || (location => location.key); // Perform initial hydration scroll restoration, since we miss the boat on
2320
+ // the initial updateState() because we've not yet rendered <ScrollRestoration/>
2321
+ // and therefore have no savedScrollPositions available
2322
+
2323
+
2324
+ if (!initialScrollRestored && state.navigation === IDLE_NAVIGATION) {
2325
+ initialScrollRestored = true;
2326
+ let y = getSavedScrollPosition(state.location, state.matches);
2327
+
2328
+ if (y != null) {
2329
+ updateState({
2330
+ restoreScrollPosition: y
2331
+ });
2332
+ }
2333
+ }
2334
+
2335
+ return () => {
2336
+ savedScrollPositions = null;
2337
+ getScrollPosition = null;
2338
+ getScrollRestorationKey = null;
2339
+ };
2340
+ }
2341
+
2342
+ function saveScrollPosition(location, matches) {
2343
+ if (savedScrollPositions && getScrollRestorationKey && getScrollPosition) {
2344
+ let userMatches = matches.map(m => createUseMatchesMatch(m, state.loaderData));
2345
+ let key = getScrollRestorationKey(location, userMatches) || location.key;
2346
+ savedScrollPositions[key] = getScrollPosition();
2347
+ }
2348
+ }
2349
+
2350
+ function getSavedScrollPosition(location, matches) {
2351
+ if (savedScrollPositions && getScrollRestorationKey && getScrollPosition) {
2352
+ let userMatches = matches.map(m => createUseMatchesMatch(m, state.loaderData));
2353
+ let key = getScrollRestorationKey(location, userMatches) || location.key;
2354
+ let y = savedScrollPositions[key];
2355
+
2356
+ if (typeof y === "number") {
2357
+ return y;
2358
+ }
2359
+ }
2360
+
2361
+ return null;
2362
+ }
2363
+
2364
+ router = {
2365
+ get basename() {
2366
+ return init.basename;
2367
+ },
2368
+
2369
+ get state() {
2370
+ return state;
2371
+ },
2372
+
2373
+ get routes() {
2374
+ return dataRoutes;
2375
+ },
2376
+
2377
+ initialize,
2378
+ subscribe,
2379
+ enableScrollRestoration,
2380
+ navigate,
2381
+ fetch,
2382
+ revalidate,
2383
+ // Passthrough to history-aware createHref used by useHref so we get proper
2384
+ // hash-aware URLs in DOM paths
2385
+ createHref: to => init.history.createHref(to),
2386
+ getFetcher,
2387
+ deleteFetcher,
2388
+ dispose,
2389
+ _internalFetchControllers: fetchControllers,
2390
+ _internalActiveDeferreds: activeDeferreds
2391
+ };
2392
+ return router;
2393
+ } //#endregion
2394
+ ////////////////////////////////////////////////////////////////////////////////
2395
+ //#region createStaticHandler
2396
+ ////////////////////////////////////////////////////////////////////////////////
2397
+
2398
+ const validActionMethods = new Set(["POST", "PUT", "PATCH", "DELETE"]);
2399
+ const validRequestMethods = new Set(["GET", "HEAD", ...validActionMethods]);
2400
+ function unstable_createStaticHandler(routes) {
2401
+ invariant(routes.length > 0, "You must provide a non-empty routes array to unstable_createStaticHandler");
2402
+ let dataRoutes = convertRoutesToDataRoutes(routes);
2403
+ /**
2404
+ * The query() method is intended for document requests, in which we want to
2405
+ * call an optional action and potentially multiple loaders for all nested
2406
+ * routes. It returns a StaticHandlerContext object, which is very similar
2407
+ * to the router state (location, loaderData, actionData, errors, etc.) and
2408
+ * also adds SSR-specific information such as the statusCode and headers
2409
+ * from action/loaders Responses.
2410
+ *
2411
+ * It _should_ never throw and should report all errors through the
2412
+ * returned context.errors object, properly associating errors to their error
2413
+ * boundary. Additionally, it tracks _deepestRenderedBoundaryId which can be
2414
+ * used to emulate React error boundaries during SSr by performing a second
2415
+ * pass only down to the boundaryId.
2416
+ *
2417
+ * The one exception where we do not return a StaticHandlerContext is when a
2418
+ * redirect response is returned or thrown from any action/loader. We
2419
+ * propagate that out and return the raw Response so the HTTP server can
2420
+ * return it directly.
2421
+ */
2422
+
2423
+ async function query(request) {
2424
+ let url = new URL(request.url);
2425
+ let location = createLocation("", createPath(url), null, "default");
2426
+ let matches = matchRoutes(dataRoutes, location);
2427
+
2428
+ if (!validRequestMethods.has(request.method)) {
2429
+ let {
2430
+ matches: methodNotAllowedMatches,
2431
+ route,
2432
+ error
2433
+ } = getMethodNotAllowedMatches(dataRoutes);
2434
+ return {
2435
+ location,
2436
+ matches: methodNotAllowedMatches,
2437
+ loaderData: {},
2438
+ actionData: null,
2439
+ errors: {
2440
+ [route.id]: error
2441
+ },
2442
+ statusCode: error.status,
2443
+ loaderHeaders: {},
2444
+ actionHeaders: {}
2445
+ };
2446
+ } else if (!matches) {
2447
+ let {
2448
+ matches: notFoundMatches,
2449
+ route,
2450
+ error
2451
+ } = getNotFoundMatches(dataRoutes);
2452
+ return {
2453
+ location,
2454
+ matches: notFoundMatches,
2455
+ loaderData: {},
2456
+ actionData: null,
2457
+ errors: {
2458
+ [route.id]: error
2459
+ },
2460
+ statusCode: error.status,
2461
+ loaderHeaders: {},
2462
+ actionHeaders: {}
2463
+ };
2464
+ }
2465
+
2466
+ let result = await queryImpl(request, location, matches);
2467
+
2468
+ if (result instanceof Response) {
2469
+ return result;
2470
+ } // When returning StaticHandlerContext, we patch back in the location here
2471
+ // since we need it for React Context. But this helps keep our submit and
2472
+ // loadRouteData operating on a Request instead of a Location
2473
+
2474
+
2475
+ return _extends({
2476
+ location
2477
+ }, result);
2478
+ }
2479
+ /**
2480
+ * The queryRoute() method is intended for targeted route requests, either
2481
+ * for fetch ?_data requests or resource route requests. In this case, we
2482
+ * are only ever calling a single action or loader, and we are returning the
2483
+ * returned value directly. In most cases, this will be a Response returned
2484
+ * from the action/loader, but it may be a primitive or other value as well -
2485
+ * and in such cases the calling context should handle that accordingly.
2486
+ *
2487
+ * We do respect the throw/return differentiation, so if an action/loader
2488
+ * throws, then this method will throw the value. This is important so we
2489
+ * can do proper boundary identification in Remix where a thrown Response
2490
+ * must go to the Catch Boundary but a returned Response is happy-path.
2491
+ *
2492
+ * One thing to note is that any Router-initiated thrown Response (such as a
2493
+ * 404 or 405) will have a custom X-Remix-Router-Error: "yes" header on it
2494
+ * in order to differentiate from responses thrown from user actions/loaders.
2495
+ */
2496
+
2497
+
2498
+ async function queryRoute(request, routeId) {
2499
+ let url = new URL(request.url);
2500
+ let location = createLocation("", createPath(url), null, "default");
2501
+ let matches = matchRoutes(dataRoutes, location);
2502
+
2503
+ if (!validRequestMethods.has(request.method)) {
2504
+ throw createRouterErrorResponse(null, {
2505
+ status: 405,
2506
+ statusText: "Method Not Allowed"
2507
+ });
2508
+ } else if (!matches) {
2509
+ throw createRouterErrorResponse(null, {
2510
+ status: 404,
2511
+ statusText: "Not Found"
2512
+ });
2513
+ }
2514
+
2515
+ let match = routeId ? matches.find(m => m.route.id === routeId) : getTargetMatch(matches, location);
2516
+
2517
+ if (!match) {
2518
+ throw createRouterErrorResponse(null, {
2519
+ status: 404,
2520
+ statusText: "Not Found"
2521
+ });
2522
+ }
2523
+
2524
+ let result = await queryImpl(request, location, matches, match);
2525
+
2526
+ if (result instanceof Response) {
2527
+ return result;
2528
+ }
2529
+
2530
+ let error = result.errors ? Object.values(result.errors)[0] : undefined;
2531
+
2532
+ if (error !== undefined) {
2533
+ // If we got back result.errors, that means the loader/action threw
2534
+ // _something_ that wasn't a Response, but it's not guaranteed/required
2535
+ // to be an `instanceof Error` either, so we have to use throw here to
2536
+ // preserve the "error" state outside of queryImpl.
2537
+ throw error;
2538
+ } // Pick off the right state value to return
2539
+
2540
+
2541
+ let routeData = [result.actionData, result.loaderData].find(v => v);
2542
+ return Object.values(routeData || {})[0];
2543
+ }
2544
+
2545
+ async function queryImpl(request, location, matches, routeMatch) {
2546
+ invariant(request.signal, "query()/queryRoute() requests must contain an AbortController signal");
2547
+
2548
+ try {
2549
+ if (validActionMethods.has(request.method)) {
2550
+ let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), routeMatch != null);
2551
+ return result;
2552
+ }
2553
+
2554
+ let result = await loadRouteData(request, matches, routeMatch);
2555
+ return result instanceof Response ? result : _extends({}, result, {
2556
+ actionData: null,
2557
+ actionHeaders: {}
2558
+ });
2559
+ } catch (e) {
2560
+ // If the user threw/returned a Response in callLoaderOrAction, we throw
2561
+ // it to bail out and then return or throw here based on whether the user
2562
+ // returned or threw
2563
+ if (isQueryRouteResponse(e)) {
2564
+ if (e.type === ResultType.error && !isRedirectResponse(e.response)) {
2565
+ throw e.response;
2566
+ }
2567
+
2568
+ return e.response;
2569
+ } // Redirects are always returned since they don't propagate to catch
2570
+ // boundaries
2571
+
2572
+
2573
+ if (isRedirectResponse(e)) {
2574
+ return e;
2575
+ }
2576
+
2577
+ throw e;
2578
+ }
2579
+ }
2580
+
2581
+ async function submit(request, matches, actionMatch, isRouteRequest) {
2582
+ let result;
2583
+
2584
+ if (!actionMatch.route.action) {
2585
+ if (isRouteRequest) {
2586
+ throw createRouterErrorResponse(null, {
2587
+ status: 405,
2588
+ statusText: "Method Not Allowed"
2589
+ });
2590
+ }
2591
+
2592
+ result = getMethodNotAllowedResult(request.url);
2593
+ } else {
2594
+ result = await callLoaderOrAction("action", request, actionMatch, matches, undefined, // Basename not currently supported in static handlers
2595
+ true, isRouteRequest);
2596
+
2597
+ if (request.signal.aborted) {
2598
+ let method = isRouteRequest ? "queryRoute" : "query";
2599
+ throw new Error(method + "() call aborted");
2600
+ }
2601
+ }
2602
+
2603
+ if (isRedirectResult(result)) {
2604
+ // Uhhhh - this should never happen, we should always throw these from
2605
+ // callLoaderOrAction, but the type narrowing here keeps TS happy and we
2606
+ // can get back on the "throw all redirect responses" train here should
2607
+ // this ever happen :/
2608
+ throw new Response(null, {
2609
+ status: result.status,
2610
+ headers: {
2611
+ Location: result.location
2612
+ }
2613
+ });
2614
+ }
2615
+
2616
+ if (isDeferredResult(result)) {
2617
+ throw new Error("defer() is not supported in actions");
2618
+ }
2619
+
2620
+ if (isRouteRequest) {
2621
+ // Note: This should only be non-Response values if we get here, since
2622
+ // isRouteRequest should throw any Response received in callLoaderOrAction
2623
+ if (isErrorResult(result)) {
2624
+ let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
2625
+ return {
2626
+ matches: [actionMatch],
2627
+ loaderData: {},
2628
+ actionData: null,
2629
+ errors: {
2630
+ [boundaryMatch.route.id]: result.error
2631
+ },
2632
+ // Note: statusCode + headers are unused here since queryRoute will
2633
+ // return the raw Response or value
2634
+ statusCode: 500,
2635
+ loaderHeaders: {},
2636
+ actionHeaders: {}
2637
+ };
2638
+ }
2639
+
2640
+ return {
2641
+ matches: [actionMatch],
2642
+ loaderData: {},
2643
+ actionData: {
2644
+ [actionMatch.route.id]: result.data
2645
+ },
2646
+ errors: null,
2647
+ // Note: statusCode + headers are unused here since queryRoute will
2648
+ // return the raw Response or value
2649
+ statusCode: 200,
2650
+ loaderHeaders: {},
2651
+ actionHeaders: {}
2652
+ };
2653
+ }
2654
+
2655
+ if (isErrorResult(result)) {
2656
+ // Store off the pending error - we use it to determine which loaders
2657
+ // to call and will commit it when we complete the navigation
2658
+ let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
2659
+ let context = await loadRouteData(request, matches, undefined, {
2660
+ [boundaryMatch.route.id]: result.error
2661
+ }); // action status codes take precedence over loader status codes
2662
+
2663
+ return _extends({}, context, {
2664
+ statusCode: isRouteErrorResponse(result.error) ? result.error.status : 500,
2665
+ actionData: null,
2666
+ actionHeaders: _extends({}, result.headers ? {
2667
+ [actionMatch.route.id]: result.headers
2668
+ } : {})
2669
+ });
2670
+ }
2671
+
2672
+ let context = await loadRouteData(request, matches);
2673
+ return _extends({}, context, result.statusCode ? {
2674
+ statusCode: result.statusCode
2675
+ } : {}, {
2676
+ actionData: {
2677
+ [actionMatch.route.id]: result.data
2678
+ },
2679
+ actionHeaders: _extends({}, result.headers ? {
2680
+ [actionMatch.route.id]: result.headers
2681
+ } : {})
2682
+ });
2683
+ }
2684
+
2685
+ async function loadRouteData(request, matches, routeMatch, pendingActionError) {
2686
+ let isRouteRequest = routeMatch != null;
2687
+ let requestMatches = routeMatch ? [routeMatch] : getLoaderMatchesUntilBoundary(matches, Object.keys(pendingActionError || {})[0]);
2688
+ let matchesToLoad = requestMatches.filter(m => m.route.loader); // Short circuit if we have no loaders to run
2689
+
2690
+ if (matchesToLoad.length === 0) {
2691
+ return {
2692
+ matches,
2693
+ loaderData: {},
2694
+ errors: pendingActionError || null,
2695
+ statusCode: 200,
2696
+ loaderHeaders: {}
2697
+ };
2698
+ }
2699
+
2700
+ let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, undefined, // Basename not currently supported in static handlers
2701
+ true, isRouteRequest))]);
2702
+
2703
+ if (request.signal.aborted) {
2704
+ let method = isRouteRequest ? "queryRoute" : "query";
2705
+ throw new Error(method + "() call aborted");
2706
+ } // Can't do anything with these without the Remix side of things, so just
2707
+ // cancel them for now
2708
+
2709
+
2710
+ results.forEach(result => {
2711
+ if (isDeferredResult(result)) {
2712
+ result.deferredData.cancel();
2713
+ }
2714
+ }); // Process and commit output from loaders
2715
+
2716
+ let context = processRouteLoaderData(matches, matchesToLoad, results, pendingActionError);
2717
+ return _extends({}, context, {
2718
+ matches
2719
+ });
2720
+ }
2721
+
2722
+ function createRouterErrorResponse(body, init) {
2723
+ return new Response(body, _extends({}, init, {
2724
+ headers: _extends({}, init.headers, {
2725
+ "X-Remix-Router-Error": "yes"
2726
+ })
2727
+ }));
2728
+ }
2729
+
2730
+ return {
2731
+ dataRoutes,
2732
+ query,
2733
+ queryRoute
2734
+ };
2735
+ } //#endregion
2736
+ ////////////////////////////////////////////////////////////////////////////////
2737
+ //#region Helpers
2738
+ ////////////////////////////////////////////////////////////////////////////////
2739
+
2740
+ /**
2741
+ * Given an existing StaticHandlerContext and an error thrown at render time,
2742
+ * provide an updated StaticHandlerContext suitable for a second SSR render
2743
+ */
2744
+
2745
+ function getStaticContextFromError(routes, context, error) {
2746
+ let newContext = _extends({}, context, {
2747
+ statusCode: 500,
2748
+ errors: {
2749
+ [context._deepestRenderedBoundaryId || routes[0].id]: error
2750
+ }
2751
+ });
2752
+
2753
+ return newContext;
2754
+ } // Normalize navigation options by converting formMethod=GET formData objects to
2755
+ // URLSearchParams so they behave identically to links with query params
2756
+
2757
+ function normalizeNavigateOptions(to, opts, isFetcher) {
2758
+ if (isFetcher === void 0) {
2759
+ isFetcher = false;
2760
+ }
2761
+
2762
+ let path = typeof to === "string" ? to : createPath(to); // Return location verbatim on non-submission navigations
2763
+
2764
+ if (!opts || !("formMethod" in opts) && !("formData" in opts)) {
2765
+ return {
2766
+ path
2767
+ };
2768
+ } // Create a Submission on non-GET navigations
2769
+
2770
+
2771
+ if (opts.formMethod != null && opts.formMethod !== "get") {
2772
+ return {
2773
+ path,
2774
+ submission: {
2775
+ formMethod: opts.formMethod,
2776
+ formAction: stripHashFromPath(path),
2777
+ formEncType: opts && opts.formEncType || "application/x-www-form-urlencoded",
2778
+ formData: opts.formData
2779
+ }
2780
+ };
2781
+ } // No formData to flatten for GET submission
2782
+
2783
+
2784
+ if (!opts.formData) {
2785
+ return {
2786
+ path
2787
+ };
2788
+ } // Flatten submission onto URLSearchParams for GET submissions
2789
+
2790
+
2791
+ let parsedPath = parsePath(path);
2792
+
2793
+ try {
2794
+ let searchParams = convertFormDataToSearchParams(opts.formData); // Since fetcher GET submissions only run a single loader (as opposed to
2795
+ // navigation GET submissions which run all loaders), we need to preserve
2796
+ // any incoming ?index params
2797
+
2798
+ if (isFetcher && parsedPath.search && hasNakedIndexQuery(parsedPath.search)) {
2799
+ searchParams.append("index", "");
2800
+ }
2801
+
2802
+ parsedPath.search = "?" + searchParams;
2803
+ } catch (e) {
2804
+ return {
2805
+ path,
2806
+ error: new ErrorResponse(400, "Bad Request", "Cannot submit binary form data using GET")
2807
+ };
2808
+ }
2809
+
2810
+ return {
2811
+ path: createPath(parsedPath)
2812
+ };
2813
+ }
2814
+
2815
+ function getLoaderRedirect(state, redirect) {
2816
+ let {
2817
+ formMethod,
2818
+ formAction,
2819
+ formEncType,
2820
+ formData
2821
+ } = state.navigation;
2822
+ let navigation = {
2823
+ state: "loading",
2824
+ location: createLocation(state.location, redirect.location),
2825
+ formMethod: formMethod || undefined,
2826
+ formAction: formAction || undefined,
2827
+ formEncType: formEncType || undefined,
2828
+ formData: formData || undefined
2829
+ };
2830
+ return navigation;
2831
+ } // Filter out all routes below any caught error as they aren't going to
2832
+ // render so we don't need to load them
2833
+
2834
+
2835
+ function getLoaderMatchesUntilBoundary(matches, boundaryId) {
2836
+ let boundaryMatches = matches;
2837
+
2838
+ if (boundaryId) {
2839
+ let index = matches.findIndex(m => m.route.id === boundaryId);
2840
+
2841
+ if (index >= 0) {
2842
+ boundaryMatches = matches.slice(0, index);
2843
+ }
2844
+ }
2845
+
2846
+ return boundaryMatches;
2847
+ }
2848
+
2849
+ function getMatchesToLoad(state, matches, submission, location, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, pendingActionData, pendingError, fetchLoadMatches) {
2850
+ let actionResult = pendingError ? Object.values(pendingError)[0] : pendingActionData ? Object.values(pendingActionData)[0] : null; // Pick navigation matches that are net-new or qualify for revalidation
2851
+
2852
+ let boundaryId = pendingError ? Object.keys(pendingError)[0] : undefined;
2853
+ let boundaryMatches = getLoaderMatchesUntilBoundary(matches, boundaryId);
2854
+ let navigationMatches = boundaryMatches.filter((match, index) => match.route.loader != null && (isNewLoader(state.loaderData, state.matches[index], match) || // If this route had a pending deferred cancelled it must be revalidated
2855
+ cancelledDeferredRoutes.some(id => id === match.route.id) || shouldRevalidateLoader(state.location, state.matches[index], submission, location, match, isRevalidationRequired, actionResult))); // Pick fetcher.loads that need to be revalidated
2856
+
2857
+ let revalidatingFetchers = [];
2858
+ fetchLoadMatches && fetchLoadMatches.forEach((_ref10, key) => {
2859
+ let [href, match, fetchMatches] = _ref10;
2860
+
2861
+ // This fetcher was cancelled from a prior action submission - force reload
2862
+ if (cancelledFetcherLoads.includes(key)) {
2863
+ revalidatingFetchers.push([key, href, match, fetchMatches]);
2864
+ } else if (isRevalidationRequired) {
2865
+ let shouldRevalidate = shouldRevalidateLoader(href, match, submission, href, match, isRevalidationRequired, actionResult);
2866
+
2867
+ if (shouldRevalidate) {
2868
+ revalidatingFetchers.push([key, href, match, fetchMatches]);
2869
+ }
2870
+ }
2871
+ });
2872
+ return [navigationMatches, revalidatingFetchers];
2873
+ }
2874
+
2875
+ function isNewLoader(currentLoaderData, currentMatch, match) {
2876
+ let isNew = // [a] -> [a, b]
2877
+ !currentMatch || // [a, b] -> [a, c]
2878
+ match.route.id !== currentMatch.route.id; // Handle the case that we don't have data for a re-used route, potentially
2879
+ // from a prior error or from a cancelled pending deferred
2880
+
2881
+ let isMissingData = currentLoaderData[match.route.id] === undefined; // Always load if this is a net-new route or we don't yet have data
2882
+
2883
+ return isNew || isMissingData;
2884
+ }
2885
+
2886
+ function isNewRouteInstance(currentMatch, match) {
2887
+ let currentPath = currentMatch.route.path;
2888
+ return (// param change for this match, /users/123 -> /users/456
2889
+ currentMatch.pathname !== match.pathname || // splat param changed, which is not present in match.path
2890
+ // e.g. /files/images/avatar.jpg -> files/finances.xls
2891
+ currentPath && currentPath.endsWith("*") && currentMatch.params["*"] !== match.params["*"]
2892
+ );
2893
+ }
2894
+
2895
+ function shouldRevalidateLoader(currentLocation, currentMatch, submission, location, match, isRevalidationRequired, actionResult) {
2896
+ let currentUrl = createURL(currentLocation);
2897
+ let currentParams = currentMatch.params;
2898
+ let nextUrl = createURL(location);
2899
+ let nextParams = match.params; // This is the default implementation as to when we revalidate. If the route
2900
+ // provides it's own implementation, then we give them full control but
2901
+ // provide this value so they can leverage it if needed after they check
2902
+ // their own specific use cases
2903
+ // Note that fetchers always provide the same current/next locations so the
2904
+ // URL-based checks here don't apply to fetcher shouldRevalidate calls
2905
+
2906
+ let defaultShouldRevalidate = isNewRouteInstance(currentMatch, match) || // Clicked the same link, resubmitted a GET form
2907
+ currentUrl.toString() === nextUrl.toString() || // Search params affect all loaders
2908
+ currentUrl.search !== nextUrl.search || // Forced revalidation due to submission, useRevalidate, or X-Remix-Revalidate
2909
+ isRevalidationRequired;
2910
+
2911
+ if (match.route.shouldRevalidate) {
2912
+ let routeChoice = match.route.shouldRevalidate(_extends({
2913
+ currentUrl,
2914
+ currentParams,
2915
+ nextUrl,
2916
+ nextParams
2917
+ }, submission, {
2918
+ actionResult,
2919
+ defaultShouldRevalidate
2920
+ }));
2921
+
2922
+ if (typeof routeChoice === "boolean") {
2923
+ return routeChoice;
2924
+ }
2925
+ }
2926
+
2927
+ return defaultShouldRevalidate;
2928
+ }
2929
+
2930
+ async function callLoaderOrAction(type, request, match, matches, basename, isStaticRequest, isRouteRequest) {
2931
+ if (isStaticRequest === void 0) {
2932
+ isStaticRequest = false;
2933
+ }
2934
+
2935
+ if (isRouteRequest === void 0) {
2936
+ isRouteRequest = false;
2937
+ }
2938
+
2939
+ let resultType;
2940
+ let result; // Setup a promise we can race against so that abort signals short circuit
2941
+
2942
+ let reject;
2943
+ let abortPromise = new Promise((_, r) => reject = r);
2944
+
2945
+ let onReject = () => reject();
2946
+
2947
+ request.signal.addEventListener("abort", onReject);
2948
+
2949
+ try {
2950
+ let handler = match.route[type];
2951
+ invariant(handler, "Could not find the " + type + " to run on the \"" + match.route.id + "\" route");
2952
+ result = await Promise.race([handler({
2953
+ request,
2954
+ params: match.params
2955
+ }), abortPromise]);
2956
+ } catch (e) {
2957
+ resultType = ResultType.error;
2958
+ result = e;
2959
+ } finally {
2960
+ request.signal.removeEventListener("abort", onReject);
2961
+ }
2962
+
2963
+ if (result instanceof Response) {
2964
+ let status = result.status; // Process redirects
2965
+
2966
+ if (status >= 300 && status <= 399) {
2967
+ let location = result.headers.get("Location");
2968
+ invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header"); // Support relative routing in redirects
2969
+
2970
+ let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
2971
+ let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
2972
+ let requestPath = createURL(request.url).pathname;
2973
+ let resolvedLocation = resolveTo(location, routePathnames, requestPath);
2974
+ invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + result.headers.get("Location")); // Prepend the basename to the redirect location if we have one
2975
+
2976
+ if (basename) {
2977
+ let path = resolvedLocation.pathname;
2978
+ resolvedLocation.pathname = path === "/" ? basename : joinPaths([basename, path]);
2979
+ }
2980
+
2981
+ location = createPath(resolvedLocation); // Don't process redirects in the router during static requests requests.
2982
+ // Instead, throw the Response and let the server handle it with an HTTP
2983
+ // redirect. We also update the Location header in place in this flow so
2984
+ // basename and relative routing is taken into account
2985
+
2986
+ if (isStaticRequest) {
2987
+ result.headers.set("Location", location);
2988
+ throw result;
2989
+ }
2990
+
2991
+ return {
2992
+ type: ResultType.redirect,
2993
+ status,
2994
+ location,
2995
+ revalidate: result.headers.get("X-Remix-Revalidate") !== null
2996
+ };
2997
+ } // For SSR single-route requests, we want to hand Responses back directly
2998
+ // without unwrapping. We do this with the QueryRouteResponse wrapper
2999
+ // interface so we can know whether it was returned or thrown
3000
+
3001
+
3002
+ if (isRouteRequest) {
3003
+ // eslint-disable-next-line no-throw-literal
3004
+ throw {
3005
+ type: resultType || ResultType.data,
3006
+ response: result
3007
+ };
3008
+ }
3009
+
3010
+ let data;
3011
+ let contentType = result.headers.get("Content-Type");
3012
+
3013
+ if (contentType && contentType.startsWith("application/json")) {
3014
+ data = await result.json();
3015
+ } else {
3016
+ data = await result.text();
3017
+ }
3018
+
3019
+ if (resultType === ResultType.error) {
3020
+ return {
3021
+ type: resultType,
3022
+ error: new ErrorResponse(status, result.statusText, data),
3023
+ headers: result.headers
3024
+ };
3025
+ }
3026
+
3027
+ return {
3028
+ type: ResultType.data,
3029
+ data,
3030
+ statusCode: result.status,
3031
+ headers: result.headers
3032
+ };
3033
+ }
3034
+
3035
+ if (resultType === ResultType.error) {
3036
+ return {
3037
+ type: resultType,
3038
+ error: result
3039
+ };
3040
+ }
3041
+
3042
+ if (result instanceof DeferredData) {
3043
+ return {
3044
+ type: ResultType.deferred,
3045
+ deferredData: result
3046
+ };
3047
+ }
3048
+
3049
+ return {
3050
+ type: ResultType.data,
3051
+ data: result
3052
+ };
3053
+ }
3054
+
3055
+ function createRequest(location, signal, submission) {
3056
+ let url = createURL(stripHashFromPath(location)).toString();
3057
+ let init = {
3058
+ signal
3059
+ };
3060
+
3061
+ if (submission) {
3062
+ let {
3063
+ formMethod,
3064
+ formEncType,
3065
+ formData
3066
+ } = submission;
3067
+ init.method = formMethod.toUpperCase();
3068
+ init.body = formEncType === "application/x-www-form-urlencoded" ? convertFormDataToSearchParams(formData) : formData;
3069
+ } // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
3070
+
3071
+
3072
+ return new Request(url, init);
3073
+ }
3074
+
3075
+ function convertFormDataToSearchParams(formData) {
3076
+ let searchParams = new URLSearchParams();
3077
+
3078
+ for (let [key, value] of formData.entries()) {
3079
+ invariant(typeof value === "string", 'File inputs are not supported with encType "application/x-www-form-urlencoded", ' + 'please use "multipart/form-data" instead.');
3080
+ searchParams.append(key, value);
3081
+ }
3082
+
3083
+ return searchParams;
3084
+ }
3085
+
3086
+ function processRouteLoaderData(matches, matchesToLoad, results, pendingError, activeDeferreds) {
3087
+ // Fill in loaderData/errors from our loaders
3088
+ let loaderData = {};
3089
+ let errors = null;
3090
+ let statusCode;
3091
+ let foundError = false;
3092
+ let loaderHeaders = {}; // Process loader results into state.loaderData/state.errors
3093
+
3094
+ results.forEach((result, index) => {
3095
+ let id = matchesToLoad[index].route.id;
3096
+ invariant(!isRedirectResult(result), "Cannot handle redirect results in processLoaderData");
3097
+
3098
+ if (isErrorResult(result)) {
3099
+ // Look upwards from the matched route for the closest ancestor
3100
+ // error boundary, defaulting to the root match
3101
+ let boundaryMatch = findNearestBoundary(matches, id);
3102
+ let error = result.error; // If we have a pending action error, we report it at the highest-route
3103
+ // that throws a loader error, and then clear it out to indicate that
3104
+ // it was consumed
3105
+
3106
+ if (pendingError) {
3107
+ error = Object.values(pendingError)[0];
3108
+ pendingError = undefined;
3109
+ }
3110
+
3111
+ errors = Object.assign(errors || {}, {
3112
+ [boundaryMatch.route.id]: error
3113
+ }); // Once we find our first (highest) error, we set the status code and
3114
+ // prevent deeper status codes from overriding
3115
+
3116
+ if (!foundError) {
3117
+ foundError = true;
3118
+ statusCode = isRouteErrorResponse(result.error) ? result.error.status : 500;
3119
+ }
3120
+
3121
+ if (result.headers) {
3122
+ loaderHeaders[id] = result.headers;
3123
+ }
3124
+ } else if (isDeferredResult(result)) {
3125
+ activeDeferreds && activeDeferreds.set(id, result.deferredData);
3126
+ loaderData[id] = result.deferredData.data; // TODO: Add statusCode/headers once we wire up streaming in Remix
3127
+ } else {
3128
+ loaderData[id] = result.data; // Error status codes always override success status codes, but if all
3129
+ // loaders are successful we take the deepest status code.
3130
+
3131
+ if (result.statusCode != null && result.statusCode !== 200 && !foundError) {
3132
+ statusCode = result.statusCode;
3133
+ }
3134
+
3135
+ if (result.headers) {
3136
+ loaderHeaders[id] = result.headers;
3137
+ }
3138
+ }
3139
+ }); // If we didn't consume the pending action error (i.e., all loaders
3140
+ // resolved), then consume it here
3141
+
3142
+ if (pendingError) {
3143
+ errors = pendingError;
3144
+ }
3145
+
3146
+ return {
3147
+ loaderData,
3148
+ errors,
3149
+ statusCode: statusCode || 200,
3150
+ loaderHeaders
3151
+ };
3152
+ }
3153
+
3154
+ function processLoaderData(state, matches, matchesToLoad, results, pendingError, revalidatingFetchers, fetcherResults, activeDeferreds) {
3155
+ let {
3156
+ loaderData,
3157
+ errors
3158
+ } = processRouteLoaderData(matches, matchesToLoad, results, pendingError, activeDeferreds); // Process results from our revalidating fetchers
3159
+
3160
+ for (let index = 0; index < revalidatingFetchers.length; index++) {
3161
+ let [key,, match] = revalidatingFetchers[index];
3162
+ invariant(fetcherResults !== undefined && fetcherResults[index] !== undefined, "Did not find corresponding fetcher result");
3163
+ let result = fetcherResults[index]; // Process fetcher non-redirect errors
3164
+
3165
+ if (isErrorResult(result)) {
3166
+ let boundaryMatch = findNearestBoundary(state.matches, match.route.id);
3167
+
3168
+ if (!(errors && errors[boundaryMatch.route.id])) {
3169
+ errors = _extends({}, errors, {
3170
+ [boundaryMatch.route.id]: result.error
3171
+ });
3172
+ }
3173
+
3174
+ state.fetchers.delete(key);
3175
+ } else if (isRedirectResult(result)) {
3176
+ // Should never get here, redirects should get processed above, but we
3177
+ // keep this to type narrow to a success result in the else
3178
+ throw new Error("Unhandled fetcher revalidation redirect");
3179
+ } else if (isDeferredResult(result)) {
3180
+ // Should never get here, deferred data should be awaited for fetchers
3181
+ // in resolveDeferredResults
3182
+ throw new Error("Unhandled fetcher deferred data");
3183
+ } else {
3184
+ let doneFetcher = {
3185
+ state: "idle",
3186
+ data: result.data,
3187
+ formMethod: undefined,
3188
+ formAction: undefined,
3189
+ formEncType: undefined,
3190
+ formData: undefined
3191
+ };
3192
+ state.fetchers.set(key, doneFetcher);
3193
+ }
3194
+ }
3195
+
3196
+ return {
3197
+ loaderData,
3198
+ errors
3199
+ };
3200
+ }
3201
+
3202
+ function mergeLoaderData(loaderData, newLoaderData, matches) {
3203
+ let mergedLoaderData = _extends({}, newLoaderData);
3204
+
3205
+ matches.forEach(match => {
3206
+ let id = match.route.id;
3207
+
3208
+ if (newLoaderData[id] === undefined && loaderData[id] !== undefined) {
3209
+ mergedLoaderData[id] = loaderData[id];
3210
+ }
3211
+ });
3212
+ return mergedLoaderData;
3213
+ } // Find the nearest error boundary, looking upwards from the leaf route (or the
3214
+ // route specified by routeId) for the closest ancestor error boundary,
3215
+ // defaulting to the root match
3216
+
3217
+
3218
+ function findNearestBoundary(matches, routeId) {
3219
+ let eligibleMatches = routeId ? matches.slice(0, matches.findIndex(m => m.route.id === routeId) + 1) : [...matches];
3220
+ return eligibleMatches.reverse().find(m => m.route.hasErrorBoundary === true) || matches[0];
3221
+ }
3222
+
3223
+ function getShortCircuitMatches(routes, status, statusText) {
3224
+ // Prefer a root layout route if present, otherwise shim in a route object
3225
+ let route = routes.find(r => r.index || !r.path || r.path === "/") || {
3226
+ id: "__shim-" + status + "-route__"
3227
+ };
3228
+ return {
3229
+ matches: [{
3230
+ params: {},
3231
+ pathname: "",
3232
+ pathnameBase: "",
3233
+ route
3234
+ }],
3235
+ route,
3236
+ error: new ErrorResponse(status, statusText, null)
3237
+ };
3238
+ }
3239
+
3240
+ function getNotFoundMatches(routes) {
3241
+ return getShortCircuitMatches(routes, 404, "Not Found");
3242
+ }
3243
+
3244
+ function getMethodNotAllowedMatches(routes) {
3245
+ return getShortCircuitMatches(routes, 405, "Method Not Allowed");
3246
+ }
3247
+
3248
+ function getMethodNotAllowedResult(path) {
3249
+ let href = typeof path === "string" ? path : createPath(path);
3250
+ console.warn("You're trying to submit to a route that does not have an action. To " + "fix this, please add an `action` function to the route for " + ("[" + href + "]"));
3251
+ return {
3252
+ type: ResultType.error,
3253
+ error: new ErrorResponse(405, "Method Not Allowed", "")
3254
+ };
3255
+ } // Find any returned redirect errors, starting from the lowest match
3256
+
3257
+
3258
+ function findRedirect(results) {
3259
+ for (let i = results.length - 1; i >= 0; i--) {
3260
+ let result = results[i];
3261
+
3262
+ if (isRedirectResult(result)) {
3263
+ return result;
3264
+ }
3265
+ }
3266
+ }
3267
+
3268
+ function stripHashFromPath(path) {
3269
+ let parsedPath = typeof path === "string" ? parsePath(path) : path;
3270
+ return createPath(_extends({}, parsedPath, {
3271
+ hash: ""
3272
+ }));
3273
+ }
3274
+
3275
+ function isHashChangeOnly(a, b) {
3276
+ return a.pathname === b.pathname && a.search === b.search && a.hash !== b.hash;
3277
+ }
3278
+
3279
+ function isDeferredResult(result) {
3280
+ return result.type === ResultType.deferred;
3281
+ }
3282
+
3283
+ function isErrorResult(result) {
3284
+ return result.type === ResultType.error;
3285
+ }
3286
+
3287
+ function isRedirectResult(result) {
3288
+ return (result && result.type) === ResultType.redirect;
3289
+ }
3290
+
3291
+ function isRedirectResponse(result) {
3292
+ if (!(result instanceof Response)) {
3293
+ return false;
3294
+ }
3295
+
3296
+ let status = result.status;
3297
+ let location = result.headers.get("Location");
3298
+ return status >= 300 && status <= 399 && location != null;
3299
+ }
3300
+
3301
+ function isQueryRouteResponse(obj) {
3302
+ return obj && obj.response instanceof Response && (obj.type === ResultType.data || ResultType.error);
3303
+ }
3304
+
3305
+ async function resolveDeferredResults(currentMatches, matchesToLoad, results, signal, isFetcher, currentLoaderData) {
3306
+ for (let index = 0; index < results.length; index++) {
3307
+ let result = results[index];
3308
+ let match = matchesToLoad[index];
3309
+ let currentMatch = currentMatches.find(m => m.route.id === match.route.id);
3310
+ let isRevalidatingLoader = currentMatch != null && !isNewRouteInstance(currentMatch, match) && (currentLoaderData && currentLoaderData[match.route.id]) !== undefined;
3311
+
3312
+ if (isDeferredResult(result) && (isFetcher || isRevalidatingLoader)) {
3313
+ // Note: we do not have to touch activeDeferreds here since we race them
3314
+ // against the signal in resolveDeferredData and they'll get aborted
3315
+ // there if needed
3316
+ await resolveDeferredData(result, signal, isFetcher).then(result => {
3317
+ if (result) {
3318
+ results[index] = result || results[index];
3319
+ }
3320
+ });
3321
+ }
3322
+ }
3323
+ }
3324
+
3325
+ async function resolveDeferredData(result, signal, unwrap) {
3326
+ if (unwrap === void 0) {
3327
+ unwrap = false;
3328
+ }
3329
+
3330
+ let aborted = await result.deferredData.resolveData(signal);
3331
+
3332
+ if (aborted) {
3333
+ return;
3334
+ }
3335
+
3336
+ if (unwrap) {
3337
+ try {
3338
+ return {
3339
+ type: ResultType.data,
3340
+ data: result.deferredData.unwrappedData
3341
+ };
3342
+ } catch (e) {
3343
+ // Handle any TrackedPromise._error values encountered while unwrapping
3344
+ return {
3345
+ type: ResultType.error,
3346
+ error: e
3347
+ };
3348
+ }
3349
+ }
3350
+
3351
+ return {
3352
+ type: ResultType.data,
3353
+ data: result.deferredData.data
3354
+ };
3355
+ }
3356
+
3357
+ function hasNakedIndexQuery(search) {
3358
+ return new URLSearchParams(search).getAll("index").some(v => v === "");
3359
+ } // Note: This should match the format exported by useMatches, so if you change
3360
+ // this please also change that :) Eventually we'll DRY this up
3361
+
3362
+
3363
+ function createUseMatchesMatch(match, loaderData) {
3364
+ let {
3365
+ route,
3366
+ pathname,
3367
+ params
3368
+ } = match;
3369
+ return {
3370
+ id: route.id,
3371
+ pathname,
3372
+ params,
3373
+ data: loaderData[route.id],
3374
+ handle: route.handle
3375
+ };
3376
+ }
3377
+
3378
+ function getTargetMatch(matches, location) {
3379
+ let search = typeof location === "string" ? parsePath(location).search : location.search;
3380
+
3381
+ if (matches[matches.length - 1].route.index && hasNakedIndexQuery(search || "")) {
3382
+ // Return the leaf index route when index is present
3383
+ return matches[matches.length - 1];
3384
+ } // Otherwise grab the deepest "path contributing" match (ignoring index and
3385
+ // pathless layout routes)
3386
+
3387
+
3388
+ let pathMatches = getPathContributingMatches(matches);
3389
+ return pathMatches[pathMatches.length - 1];
3390
+ } //#endregion
3391
+
3392
+ exports.AbortedDeferredError = AbortedDeferredError;
3393
+ exports.ErrorResponse = ErrorResponse;
3394
+ exports.IDLE_FETCHER = IDLE_FETCHER;
3395
+ exports.IDLE_NAVIGATION = IDLE_NAVIGATION;
3396
+ exports.UNSAFE_convertRoutesToDataRoutes = convertRoutesToDataRoutes;
3397
+ exports.UNSAFE_getPathContributingMatches = getPathContributingMatches;
3398
+ exports.createBrowserHistory = createBrowserHistory;
3399
+ exports.createHashHistory = createHashHistory;
3400
+ exports.createMemoryHistory = createMemoryHistory;
3401
+ exports.createPath = createPath;
3402
+ exports.createRouter = createRouter;
3403
+ exports.defer = defer;
3404
+ exports.generatePath = generatePath;
3405
+ exports.getStaticContextFromError = getStaticContextFromError;
3406
+ exports.getToPathname = getToPathname;
3407
+ exports.invariant = invariant;
3408
+ exports.isRouteErrorResponse = isRouteErrorResponse;
3409
+ exports.joinPaths = joinPaths;
3410
+ exports.json = json;
3411
+ exports.matchPath = matchPath;
3412
+ exports.matchRoutes = matchRoutes;
3413
+ exports.normalizePathname = normalizePathname;
3414
+ exports.parsePath = parsePath;
3415
+ exports.redirect = redirect;
3416
+ exports.resolvePath = resolvePath;
3417
+ exports.resolveTo = resolveTo;
3418
+ exports.stripBasename = stripBasename;
3419
+ exports.unstable_createStaticHandler = unstable_createStaticHandler;
3420
+ exports.warning = warning;
3421
+
3422
+ Object.defineProperty(exports, '__esModule', { value: true });
3423
+
3424
+ }));
3425
+ //# sourceMappingURL=router.umd.js.map