@tanstack/react-router 0.0.1-beta.9 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/build/cjs/CatchBoundary.js +128 -0
- package/build/cjs/CatchBoundary.js.map +1 -0
- package/build/cjs/Matches.js +233 -0
- package/build/cjs/Matches.js.map +1 -0
- package/build/cjs/RouterProvider.js +170 -0
- package/build/cjs/RouterProvider.js.map +1 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/_virtual/with-selector.development.js +16 -0
- package/build/cjs/_virtual/with-selector.development.js.map +1 -0
- package/build/cjs/_virtual/with-selector.js +16 -0
- package/build/cjs/_virtual/with-selector.js.map +1 -0
- package/build/cjs/_virtual/with-selector.production.min.js +16 -0
- package/build/cjs/_virtual/with-selector.production.min.js.map +1 -0
- package/build/cjs/awaited.js +43 -0
- package/build/cjs/awaited.js.map +1 -0
- package/build/cjs/build/esm/index.js +79 -0
- package/build/cjs/build/esm/index.js.map +1 -0
- package/build/cjs/defer.js +37 -0
- package/build/cjs/defer.js.map +1 -0
- package/build/cjs/fileRoute.js +27 -0
- package/build/cjs/fileRoute.js.map +1 -0
- package/build/cjs/index.js +130 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/lazyRouteComponent.js +54 -0
- package/build/cjs/lazyRouteComponent.js.map +1 -0
- package/build/cjs/link.js +223 -0
- package/build/cjs/link.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_react-store@0.2.1_react-dom@18.2.0_react@18.2.0/node_modules/@tanstack/react-store/build/modern/index.js +47 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_react-store@0.2.1_react-dom@18.2.0_react@18.2.0/node_modules/@tanstack/react-store/build/modern/index.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_store@0.1.3/node_modules/@tanstack/store/build/modern/index.js +70 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_store@0.1.3/node_modules/@tanstack/store/build/modern/index.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +188 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +39 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/shim/with-selector.js +26 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/shim/with-selector.js.map +1 -0
- package/build/cjs/packages/react-router/src/CatchBoundary.js +123 -0
- package/build/cjs/packages/react-router/src/CatchBoundary.js.map +1 -0
- package/build/cjs/packages/react-router/src/Matches.js +235 -0
- package/build/cjs/packages/react-router/src/Matches.js.map +1 -0
- package/build/cjs/packages/react-router/src/RouterProvider.js +144 -0
- package/build/cjs/packages/react-router/src/RouterProvider.js.map +1 -0
- package/build/cjs/packages/react-router/src/awaited.js +43 -0
- package/build/cjs/packages/react-router/src/awaited.js.map +1 -0
- package/build/cjs/packages/react-router/src/defer.js +37 -0
- package/build/cjs/packages/react-router/src/defer.js.map +1 -0
- package/build/cjs/packages/react-router/src/fileRoute.js +27 -0
- package/build/cjs/packages/react-router/src/fileRoute.js.map +1 -0
- package/build/cjs/packages/react-router/src/index.js +61 -0
- package/build/cjs/packages/react-router/src/index.js.map +1 -0
- package/build/cjs/packages/react-router/src/lazyRouteComponent.js +54 -0
- package/build/cjs/packages/react-router/src/lazyRouteComponent.js.map +1 -0
- package/build/cjs/packages/react-router/src/link.js +148 -0
- package/build/cjs/packages/react-router/src/link.js.map +1 -0
- package/build/cjs/packages/react-router/src/path.js +209 -0
- package/build/cjs/packages/react-router/src/path.js.map +1 -0
- package/build/cjs/packages/react-router/src/qss.js +63 -0
- package/build/cjs/packages/react-router/src/qss.js.map +1 -0
- package/build/cjs/packages/react-router/src/react.js +634 -0
- package/build/cjs/packages/react-router/src/react.js.map +1 -0
- package/build/cjs/packages/react-router/src/redirects.js +25 -0
- package/build/cjs/packages/react-router/src/redirects.js.map +1 -0
- package/build/cjs/packages/react-router/src/route.js +134 -0
- package/build/cjs/packages/react-router/src/route.js.map +1 -0
- package/build/cjs/packages/react-router/src/router.js +1111 -0
- package/build/cjs/packages/react-router/src/router.js.map +1 -0
- package/build/cjs/packages/react-router/src/scroll-restoration.js +53 -0
- package/build/cjs/packages/react-router/src/scroll-restoration.js.map +1 -0
- package/build/cjs/packages/react-router/src/searchParams.js +81 -0
- package/build/cjs/packages/react-router/src/searchParams.js.map +1 -0
- package/build/cjs/packages/react-router/src/useBlocker.js +61 -0
- package/build/cjs/packages/react-router/src/useBlocker.js.map +1 -0
- package/build/cjs/packages/react-router/src/useNavigate.js +75 -0
- package/build/cjs/packages/react-router/src/useNavigate.js.map +1 -0
- package/build/cjs/packages/react-router/src/useParams.js +26 -0
- package/build/cjs/packages/react-router/src/useParams.js.map +1 -0
- package/build/cjs/packages/react-router/src/useSearch.js +25 -0
- package/build/cjs/packages/react-router/src/useSearch.js.map +1 -0
- package/build/cjs/packages/react-router/src/utils.js +239 -0
- package/build/cjs/packages/react-router/src/utils.js.map +1 -0
- package/build/cjs/path.js +214 -0
- package/build/cjs/path.js.map +1 -0
- package/build/cjs/qss.js +63 -0
- package/build/cjs/qss.js.map +1 -0
- package/build/cjs/react/CatchBoundary.js +123 -0
- package/build/cjs/react/CatchBoundary.js.map +1 -0
- package/build/cjs/react/awaited.js +43 -0
- package/build/cjs/react/awaited.js.map +1 -0
- package/build/cjs/react/defer.js +37 -0
- package/build/cjs/react/defer.js.map +1 -0
- package/build/cjs/react.js +650 -0
- package/build/cjs/react.js.map +1 -0
- package/build/cjs/redirects.js +28 -0
- package/build/cjs/redirects.js.map +1 -0
- package/build/cjs/route.js +191 -0
- package/build/cjs/route.js.map +1 -0
- package/build/cjs/router.js +1085 -0
- package/build/cjs/router.js.map +1 -0
- package/build/cjs/routerConfig.js +209 -0
- package/build/cjs/routerConfig.js.map +1 -0
- package/build/cjs/scroll-restoration.js +202 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/cjs/searchParams.js +81 -0
- package/build/cjs/searchParams.js.map +1 -0
- package/build/cjs/src/CatchBoundary.js +126 -0
- package/build/cjs/src/CatchBoundary.js.map +1 -0
- package/build/cjs/src/Matches.js +235 -0
- package/build/cjs/src/Matches.js.map +1 -0
- package/build/cjs/src/RouterProvider.js +1051 -0
- package/build/cjs/src/RouterProvider.js.map +1 -0
- package/build/cjs/src/awaited.js +45 -0
- package/build/cjs/src/awaited.js.map +1 -0
- package/build/cjs/src/defer.js +39 -0
- package/build/cjs/src/defer.js.map +1 -0
- package/build/cjs/src/fileRoute.js +29 -0
- package/build/cjs/src/fileRoute.js.map +1 -0
- package/build/cjs/src/index.js +134 -0
- package/build/cjs/src/index.js.map +1 -0
- package/build/cjs/src/lazyRouteComponent.js +57 -0
- package/build/cjs/src/lazyRouteComponent.js.map +1 -0
- package/build/cjs/src/link.js +151 -0
- package/build/cjs/src/link.js.map +1 -0
- package/build/cjs/src/path.js +211 -0
- package/build/cjs/src/path.js.map +1 -0
- package/build/cjs/src/qss.js +65 -0
- package/build/cjs/src/qss.js.map +1 -0
- package/build/cjs/src/redirects.js +27 -0
- package/build/cjs/src/redirects.js.map +1 -0
- package/build/cjs/src/route.js +139 -0
- package/build/cjs/src/route.js.map +1 -0
- package/build/cjs/src/router.js +203 -0
- package/build/cjs/src/router.js.map +1 -0
- package/build/cjs/src/scroll-restoration.js +186 -0
- package/build/cjs/src/scroll-restoration.js.map +1 -0
- package/build/cjs/src/searchParams.js +83 -0
- package/build/cjs/src/searchParams.js.map +1 -0
- package/build/cjs/src/useBlocker.js +64 -0
- package/build/cjs/src/useBlocker.js.map +1 -0
- package/build/cjs/src/useNavigate.js +78 -0
- package/build/cjs/src/useNavigate.js.map +1 -0
- package/build/cjs/src/useParams.js +28 -0
- package/build/cjs/src/useParams.js.map +1 -0
- package/build/cjs/src/useSearch.js +27 -0
- package/build/cjs/src/useSearch.js.map +1 -0
- package/build/cjs/src/utils.js +230 -0
- package/build/cjs/src/utils.js.map +1 -0
- package/build/cjs/useBlocker.js +55 -0
- package/build/cjs/useBlocker.js.map +1 -0
- package/build/cjs/useNavigate.js +86 -0
- package/build/cjs/useNavigate.js.map +1 -0
- package/build/cjs/useParams.js +26 -0
- package/build/cjs/useParams.js.map +1 -0
- package/build/cjs/useSearch.js +25 -0
- package/build/cjs/useSearch.js.map +1 -0
- package/build/cjs/useStore.js +99 -0
- package/build/cjs/useStore.js.map +1 -0
- package/build/cjs/utils.js +241 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +2300 -2534
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +3498 -2694
- package/build/stats-react.json +1204 -44
- package/build/types/CatchBoundary.d.ts +36 -0
- package/build/types/Matches.d.ts +64 -0
- package/build/types/RouteMatch.d.ts +23 -0
- package/build/types/RouterProvider.d.ts +35 -0
- package/build/types/awaited.d.ts +9 -0
- package/build/types/defer.d.ts +19 -0
- package/build/types/fileRoute.d.ts +38 -0
- package/build/types/history.d.ts +7 -0
- package/build/types/index.d.ts +919 -58
- package/build/types/injectHtml.d.ts +0 -0
- package/build/types/lazyRouteComponent.d.ts +2 -0
- package/build/types/link.d.ts +93 -0
- package/build/types/location.d.ts +12 -0
- package/build/types/path.d.ts +17 -0
- package/build/types/qss.d.ts +2 -0
- package/build/types/react/CatchBoundary.d.ts +33 -0
- package/build/types/react/awaited.d.ts +9 -0
- package/build/types/react/defer.d.ts +19 -0
- package/build/types/react.d.ts +141 -0
- package/build/types/redirects.d.ts +11 -0
- package/build/types/route.d.ts +283 -0
- package/build/types/routeInfo.d.ts +31 -0
- package/build/types/router.d.ts +186 -0
- package/build/types/scroll-restoration.d.ts +18 -0
- package/build/types/searchParams.d.ts +7 -0
- package/build/types/useBlocker.d.ts +9 -0
- package/build/types/useNavigate.d.ts +19 -0
- package/build/types/useParams.d.ts +7 -0
- package/build/types/useSearch.d.ts +7 -0
- package/build/types/useStore.d.ts +12 -0
- package/build/types/utils.d.ts +69 -0
- package/build/umd/index.development.js +2897 -2493
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +4 -4
- package/build/umd/index.production.js.map +1 -1
- package/package.json +12 -10
- package/src/CatchBoundary.tsx +101 -0
- package/src/Matches.tsx +423 -0
- package/src/RouterProvider.tsx +252 -0
- package/src/awaited.tsx +40 -0
- package/src/defer.ts +55 -0
- package/src/fileRoute.ts +152 -0
- package/src/history.ts +8 -0
- package/src/index.tsx +28 -619
- package/src/lazyRouteComponent.tsx +33 -0
- package/src/link.tsx +603 -0
- package/src/location.ts +13 -0
- package/src/path.ts +261 -0
- package/src/qss.ts +53 -0
- package/src/redirects.ts +39 -0
- package/src/route.ts +882 -0
- package/src/routeInfo.ts +84 -0
- package/src/router.ts +1671 -0
- package/src/scroll-restoration.tsx +230 -0
- package/src/searchParams.ts +79 -0
- package/src/useBlocker.tsx +27 -0
- package/src/useNavigate.tsx +111 -0
- package/src/useParams.tsx +25 -0
- package/src/useSearch.tsx +25 -0
- package/src/utils.ts +360 -0
- package/build/cjs/react-router/src/index.js +0 -458
- package/build/cjs/react-router/src/index.js.map +0 -1
- package/build/cjs/router-core/build/esm/index.js +0 -2524
- package/build/cjs/router-core/build/esm/index.js.map +0 -1
|
@@ -0,0 +1,1111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tanstack/react-router/src/index.tsx
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
var history = require('@tanstack/history');
|
|
14
|
+
var searchParams = require('./searchParams.js');
|
|
15
|
+
var utils = require('./utils.js');
|
|
16
|
+
var RouterProvider = require('./RouterProvider.js');
|
|
17
|
+
var path = require('./path.js');
|
|
18
|
+
var invariant = require('tiny-invariant');
|
|
19
|
+
var redirects = require('./redirects.js');
|
|
20
|
+
var index = require('../../../node_modules/.pnpm/@tanstack_store@0.1.3/node_modules/@tanstack/store/build/modern/index.js');
|
|
21
|
+
|
|
22
|
+
// import warning from 'tiny-warning'
|
|
23
|
+
|
|
24
|
+
//
|
|
25
|
+
|
|
26
|
+
const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
|
|
27
|
+
const preloadWarning = 'Error preloading route! ☝️';
|
|
28
|
+
class Router {
|
|
29
|
+
// Option-independent properties
|
|
30
|
+
tempLocationKey = `${Math.round(Math.random() * 10000000)}`;
|
|
31
|
+
resetNextScroll = true;
|
|
32
|
+
navigateTimeout = null;
|
|
33
|
+
latestLoadPromise = Promise.resolve();
|
|
34
|
+
subscribers = new Set();
|
|
35
|
+
pendingMatches = [];
|
|
36
|
+
injectedHtml = [];
|
|
37
|
+
|
|
38
|
+
// Must build in constructor
|
|
39
|
+
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.update({
|
|
42
|
+
defaultPreloadDelay: 50,
|
|
43
|
+
defaultPendingMs: 1000,
|
|
44
|
+
defaultPendingMinMs: 500,
|
|
45
|
+
context: undefined,
|
|
46
|
+
...options,
|
|
47
|
+
stringifySearch: options?.stringifySearch ?? searchParams.defaultStringifySearch,
|
|
48
|
+
parseSearch: options?.parseSearch ?? searchParams.defaultParseSearch
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// These are default implementations that can optionally be overridden
|
|
53
|
+
// by the router provider once rendered. We provide these so that the
|
|
54
|
+
// router can be used in a non-react environment if necessary
|
|
55
|
+
startReactTransition = fn => fn();
|
|
56
|
+
isTransitioning = false;
|
|
57
|
+
update = newOptions => {
|
|
58
|
+
this.options = {
|
|
59
|
+
...this.options,
|
|
60
|
+
...newOptions
|
|
61
|
+
};
|
|
62
|
+
this.basepath = `/${path.trimPath(newOptions.basepath ?? '') ?? ''}`;
|
|
63
|
+
if (!this.history || this.options.history && this.options.history !== this.history) {
|
|
64
|
+
this.history = this.options.history ?? (typeof document !== 'undefined' ? history.createBrowserHistory() : history.createMemoryHistory());
|
|
65
|
+
this.latestLocation = this.parseLocation();
|
|
66
|
+
}
|
|
67
|
+
if (this.options.routeTree !== this.routeTree) {
|
|
68
|
+
this.routeTree = this.options.routeTree;
|
|
69
|
+
this.buildRouteTree();
|
|
70
|
+
}
|
|
71
|
+
if (!this.__store) {
|
|
72
|
+
this.__store = new index.Store(getInitialRouterState(this.latestLocation), {
|
|
73
|
+
onUpdate: () => {
|
|
74
|
+
const isAnyTransitioning = this.isTransitioning || this.__store.state.matches.some(d => d.status === 'pending');
|
|
75
|
+
this.__store.state = {
|
|
76
|
+
...this.__store.state,
|
|
77
|
+
status: isAnyTransitioning ? 'pending' : 'idle',
|
|
78
|
+
location: this.isTransitioning ? this.latestLocation : this.__store.state.location,
|
|
79
|
+
pendingMatches: this.pendingMatches
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
get state() {
|
|
86
|
+
return this.__store.state;
|
|
87
|
+
}
|
|
88
|
+
buildRouteTree = () => {
|
|
89
|
+
this.routesById = {};
|
|
90
|
+
this.routesByPath = {};
|
|
91
|
+
const recurseRoutes = childRoutes => {
|
|
92
|
+
childRoutes.forEach((childRoute, i) => {
|
|
93
|
+
// if (typeof childRoute === 'function') {
|
|
94
|
+
// childRoute = (childRoute as any)()
|
|
95
|
+
// }
|
|
96
|
+
childRoute.init({
|
|
97
|
+
originalIndex: i
|
|
98
|
+
});
|
|
99
|
+
const existingRoute = this.routesById[childRoute.id];
|
|
100
|
+
invariant(!existingRoute, `Duplicate routes found with id: ${String(childRoute.id)}`);
|
|
101
|
+
this.routesById[childRoute.id] = childRoute;
|
|
102
|
+
if (!childRoute.isRoot && childRoute.path) {
|
|
103
|
+
const trimmedFullPath = path.trimPathRight(childRoute.fullPath);
|
|
104
|
+
if (!this.routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith('/')) {
|
|
105
|
+
this.routesByPath[trimmedFullPath] = childRoute;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const children = childRoute.children;
|
|
109
|
+
if (children?.length) {
|
|
110
|
+
recurseRoutes(children);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
recurseRoutes([this.routeTree]);
|
|
115
|
+
this.flatRoutes = Object.values(this.routesByPath).map((d, i) => {
|
|
116
|
+
const trimmed = path.trimPath(d.fullPath);
|
|
117
|
+
const parsed = path.parsePathname(trimmed);
|
|
118
|
+
while (parsed.length > 1 && parsed[0]?.value === '/') {
|
|
119
|
+
parsed.shift();
|
|
120
|
+
}
|
|
121
|
+
const score = parsed.map(d => {
|
|
122
|
+
if (d.type === 'param') {
|
|
123
|
+
return 0.5;
|
|
124
|
+
}
|
|
125
|
+
if (d.type === 'wildcard') {
|
|
126
|
+
return 0.25;
|
|
127
|
+
}
|
|
128
|
+
return 1;
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
child: d,
|
|
132
|
+
trimmed,
|
|
133
|
+
parsed,
|
|
134
|
+
index: i,
|
|
135
|
+
score
|
|
136
|
+
};
|
|
137
|
+
}).sort((a, b) => {
|
|
138
|
+
let isIndex = a.trimmed === '/' ? 1 : b.trimmed === '/' ? -1 : 0;
|
|
139
|
+
if (isIndex !== 0) return isIndex;
|
|
140
|
+
const length = Math.min(a.score.length, b.score.length);
|
|
141
|
+
|
|
142
|
+
// Sort by length of score
|
|
143
|
+
if (a.score.length !== b.score.length) {
|
|
144
|
+
return b.score.length - a.score.length;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Sort by min available score
|
|
148
|
+
for (let i = 0; i < length; i++) {
|
|
149
|
+
if (a.score[i] !== b.score[i]) {
|
|
150
|
+
return b.score[i] - a.score[i];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Sort by min available parsed value
|
|
155
|
+
for (let i = 0; i < length; i++) {
|
|
156
|
+
if (a.parsed[i].value !== b.parsed[i].value) {
|
|
157
|
+
return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Sort by length of trimmed full path
|
|
162
|
+
if (a.trimmed !== b.trimmed) {
|
|
163
|
+
return a.trimmed > b.trimmed ? 1 : -1;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Sort by original index
|
|
167
|
+
return a.index - b.index;
|
|
168
|
+
}).map((d, i) => {
|
|
169
|
+
d.child.rank = i;
|
|
170
|
+
return d.child;
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
subscribe = (eventType, fn) => {
|
|
174
|
+
const listener = {
|
|
175
|
+
eventType,
|
|
176
|
+
fn
|
|
177
|
+
};
|
|
178
|
+
this.subscribers.add(listener);
|
|
179
|
+
return () => {
|
|
180
|
+
this.subscribers.delete(listener);
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
emit = routerEvent => {
|
|
184
|
+
this.subscribers.forEach(listener => {
|
|
185
|
+
if (listener.eventType === routerEvent.type) {
|
|
186
|
+
listener.fn(routerEvent);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
checkLatest = promise => {
|
|
191
|
+
return this.latestLoadPromise !== promise ? this.latestLoadPromise : undefined;
|
|
192
|
+
};
|
|
193
|
+
parseLocation = previousLocation => {
|
|
194
|
+
const parse = ({
|
|
195
|
+
pathname,
|
|
196
|
+
search,
|
|
197
|
+
hash,
|
|
198
|
+
state
|
|
199
|
+
}) => {
|
|
200
|
+
const parsedSearch = this.options.parseSearch(search);
|
|
201
|
+
return {
|
|
202
|
+
pathname: pathname,
|
|
203
|
+
searchStr: search,
|
|
204
|
+
search: utils.replaceEqualDeep(previousLocation?.search, parsedSearch),
|
|
205
|
+
hash: hash.split('#').reverse()[0] ?? '',
|
|
206
|
+
href: `${pathname}${search}${hash}`,
|
|
207
|
+
state: utils.replaceEqualDeep(previousLocation?.state, state)
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
const location = parse(this.history.location);
|
|
211
|
+
let {
|
|
212
|
+
__tempLocation,
|
|
213
|
+
__tempKey
|
|
214
|
+
} = location.state;
|
|
215
|
+
if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) {
|
|
216
|
+
// Sync up the location keys
|
|
217
|
+
const parsedTempLocation = parse(__tempLocation);
|
|
218
|
+
parsedTempLocation.state.key = location.state.key;
|
|
219
|
+
delete parsedTempLocation.state.__tempLocation;
|
|
220
|
+
return {
|
|
221
|
+
...parsedTempLocation,
|
|
222
|
+
maskedLocation: location
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return location;
|
|
226
|
+
};
|
|
227
|
+
resolvePathWithBase = (from, path$1) => {
|
|
228
|
+
return path.resolvePath(this.basepath, from, path.cleanPath(path$1));
|
|
229
|
+
};
|
|
230
|
+
get looseRoutesById() {
|
|
231
|
+
return this.routesById;
|
|
232
|
+
}
|
|
233
|
+
matchRoutes = (pathname, locationSearch, opts) => {
|
|
234
|
+
let routeParams = {};
|
|
235
|
+
let foundRoute = this.flatRoutes.find(route => {
|
|
236
|
+
const matchedParams = path.matchPathname(this.basepath, path.trimPathRight(pathname), {
|
|
237
|
+
to: route.fullPath,
|
|
238
|
+
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
|
|
239
|
+
fuzzy: false
|
|
240
|
+
});
|
|
241
|
+
if (matchedParams) {
|
|
242
|
+
routeParams = matchedParams;
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
return false;
|
|
246
|
+
});
|
|
247
|
+
let routeCursor = foundRoute || this.routesById['__root__'];
|
|
248
|
+
let matchedRoutes = [routeCursor];
|
|
249
|
+
// let includingLayouts = true
|
|
250
|
+
while (routeCursor?.parentRoute) {
|
|
251
|
+
routeCursor = routeCursor.parentRoute;
|
|
252
|
+
if (routeCursor) matchedRoutes.unshift(routeCursor);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Existing matches are matches that are already loaded along with
|
|
256
|
+
// pending matches that are still loading
|
|
257
|
+
|
|
258
|
+
const parseErrors = matchedRoutes.map(route => {
|
|
259
|
+
let parsedParamsError;
|
|
260
|
+
if (route.options.parseParams) {
|
|
261
|
+
try {
|
|
262
|
+
const parsedParams = route.options.parseParams(routeParams);
|
|
263
|
+
// Add the parsed params to the accumulated params bag
|
|
264
|
+
Object.assign(routeParams, parsedParams);
|
|
265
|
+
} catch (err) {
|
|
266
|
+
parsedParamsError = new PathParamError(err.message, {
|
|
267
|
+
cause: err
|
|
268
|
+
});
|
|
269
|
+
if (opts?.throwOnError) {
|
|
270
|
+
throw parsedParamsError;
|
|
271
|
+
}
|
|
272
|
+
return parsedParamsError;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return;
|
|
276
|
+
});
|
|
277
|
+
const matches = [];
|
|
278
|
+
matchedRoutes.forEach((route, index) => {
|
|
279
|
+
// Take each matched route and resolve + validate its search params
|
|
280
|
+
// This has to happen serially because each route's search params
|
|
281
|
+
// can depend on the parent route's search params
|
|
282
|
+
// It must also happen before we create the match so that we can
|
|
283
|
+
// pass the search params to the route's potential key function
|
|
284
|
+
// which is used to uniquely identify the route match in state
|
|
285
|
+
|
|
286
|
+
const parentMatch = matches[index - 1];
|
|
287
|
+
const [preMatchSearch, searchError] = (() => {
|
|
288
|
+
// Validate the search params and stabilize them
|
|
289
|
+
const parentSearch = parentMatch?.search ?? locationSearch;
|
|
290
|
+
try {
|
|
291
|
+
const validator = typeof route.options.validateSearch === 'object' ? route.options.validateSearch.parse : route.options.validateSearch;
|
|
292
|
+
let search = validator?.(parentSearch) ?? {};
|
|
293
|
+
return [{
|
|
294
|
+
...parentSearch,
|
|
295
|
+
...search
|
|
296
|
+
}, undefined];
|
|
297
|
+
} catch (err) {
|
|
298
|
+
const searchError = new SearchParamError(err.message, {
|
|
299
|
+
cause: err
|
|
300
|
+
});
|
|
301
|
+
if (opts?.throwOnError) {
|
|
302
|
+
throw searchError;
|
|
303
|
+
}
|
|
304
|
+
return [parentSearch, searchError];
|
|
305
|
+
}
|
|
306
|
+
})();
|
|
307
|
+
const interpolatedPath = path.interpolatePath(route.path, routeParams);
|
|
308
|
+
const matchId = path.interpolatePath(route.id, routeParams, true) + (route.options.key?.({
|
|
309
|
+
search: preMatchSearch,
|
|
310
|
+
location: this.state.location
|
|
311
|
+
}) ?? '');
|
|
312
|
+
|
|
313
|
+
// Waste not, want not. If we already have a match for this route,
|
|
314
|
+
// reuse it. This is important for layout routes, which might stick
|
|
315
|
+
// around between navigation actions that only change leaf routes.
|
|
316
|
+
const existingMatch = RouterProvider.getRouteMatch(this.state, matchId);
|
|
317
|
+
const cause = this.state.matches.find(d => d.id === matchId) ? 'stay' : 'enter';
|
|
318
|
+
|
|
319
|
+
// Create a fresh route match
|
|
320
|
+
const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload));
|
|
321
|
+
const match = existingMatch ? {
|
|
322
|
+
...existingMatch,
|
|
323
|
+
cause
|
|
324
|
+
} : {
|
|
325
|
+
id: matchId,
|
|
326
|
+
routeId: route.id,
|
|
327
|
+
params: routeParams,
|
|
328
|
+
pathname: path.joinPaths([this.basepath, interpolatedPath]),
|
|
329
|
+
updatedAt: Date.now(),
|
|
330
|
+
search: {},
|
|
331
|
+
searchError: undefined,
|
|
332
|
+
status: hasLoaders ? 'pending' : 'success',
|
|
333
|
+
showPending: false,
|
|
334
|
+
isFetching: false,
|
|
335
|
+
invalid: false,
|
|
336
|
+
error: undefined,
|
|
337
|
+
paramsError: parseErrors[index],
|
|
338
|
+
loadPromise: Promise.resolve(),
|
|
339
|
+
context: undefined,
|
|
340
|
+
abortController: new AbortController(),
|
|
341
|
+
shouldReloadDeps: undefined,
|
|
342
|
+
fetchedAt: 0,
|
|
343
|
+
cause
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// Regardless of whether we're reusing an existing match or creating
|
|
347
|
+
// a new one, we need to update the match's search params
|
|
348
|
+
match.search = utils.replaceEqualDeep(match.search, preMatchSearch);
|
|
349
|
+
// And also update the searchError if there is one
|
|
350
|
+
match.searchError = searchError;
|
|
351
|
+
matches.push(match);
|
|
352
|
+
});
|
|
353
|
+
return matches;
|
|
354
|
+
};
|
|
355
|
+
cancelMatch = id => {
|
|
356
|
+
RouterProvider.getRouteMatch(this.state, id)?.abortController?.abort();
|
|
357
|
+
};
|
|
358
|
+
cancelMatches = () => {
|
|
359
|
+
this.state.matches.forEach(match => {
|
|
360
|
+
this.cancelMatch(match.id);
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
buildLocation = opts => {
|
|
364
|
+
const build = (dest = {}, matches) => {
|
|
365
|
+
const from = this.latestLocation;
|
|
366
|
+
const fromPathname = dest.from ?? from.pathname;
|
|
367
|
+
let pathname = this.resolvePathWithBase(fromPathname, `${dest.to ?? ''}`);
|
|
368
|
+
const fromMatches = this.matchRoutes(fromPathname, from.search);
|
|
369
|
+
const stayingMatches = matches?.filter(d => fromMatches?.find(e => e.routeId === d.routeId));
|
|
370
|
+
const prevParams = {
|
|
371
|
+
...utils.last(fromMatches)?.params
|
|
372
|
+
};
|
|
373
|
+
let nextParams = (dest.params ?? true) === true ? prevParams : utils.functionalUpdate(dest.params, prevParams);
|
|
374
|
+
if (nextParams) {
|
|
375
|
+
matches?.map(d => this.looseRoutesById[d.routeId].options.stringifyParams).filter(Boolean).forEach(fn => {
|
|
376
|
+
nextParams = {
|
|
377
|
+
...nextParams,
|
|
378
|
+
...fn(nextParams)
|
|
379
|
+
};
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
pathname = path.interpolatePath(pathname, nextParams ?? {});
|
|
383
|
+
const preSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.preSearchFilters ?? []).flat().filter(Boolean) ?? [];
|
|
384
|
+
const postSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.postSearchFilters ?? []).flat().filter(Boolean) ?? [];
|
|
385
|
+
|
|
386
|
+
// Pre filters first
|
|
387
|
+
const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), from.search) : from.search;
|
|
388
|
+
|
|
389
|
+
// Then the link/navigate function
|
|
390
|
+
const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
|
|
391
|
+
: dest.search ? utils.functionalUpdate(dest.search, preFilteredSearch) ?? {} // Updater
|
|
392
|
+
: preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters
|
|
393
|
+
: {};
|
|
394
|
+
|
|
395
|
+
// Then post filters
|
|
396
|
+
const postFilteredSearch = postSearchFilters?.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
|
|
397
|
+
const search = utils.replaceEqualDeep(from.search, postFilteredSearch);
|
|
398
|
+
const searchStr = this.options.stringifySearch(search);
|
|
399
|
+
const hash = dest.hash === true ? from.hash : dest.hash ? utils.functionalUpdate(dest.hash, from.hash) : from.hash;
|
|
400
|
+
const hashStr = hash ? `#${hash}` : '';
|
|
401
|
+
let nextState = dest.state === true ? from.state : dest.state ? utils.functionalUpdate(dest.state, from.state) : from.state;
|
|
402
|
+
nextState = utils.replaceEqualDeep(from.state, nextState);
|
|
403
|
+
return {
|
|
404
|
+
pathname,
|
|
405
|
+
search,
|
|
406
|
+
searchStr,
|
|
407
|
+
state: nextState,
|
|
408
|
+
hash,
|
|
409
|
+
href: this.history.createHref(`${pathname}${searchStr}${hashStr}`),
|
|
410
|
+
unmaskOnReload: dest.unmaskOnReload
|
|
411
|
+
};
|
|
412
|
+
};
|
|
413
|
+
const buildWithMatches = (dest = {}, maskedDest) => {
|
|
414
|
+
let next = build(dest);
|
|
415
|
+
let maskedNext = maskedDest ? build(maskedDest) : undefined;
|
|
416
|
+
if (!maskedNext) {
|
|
417
|
+
let params = {};
|
|
418
|
+
let foundMask = this.options.routeMasks?.find(d => {
|
|
419
|
+
const match = path.matchPathname(this.basepath, next.pathname, {
|
|
420
|
+
to: d.from,
|
|
421
|
+
caseSensitive: false,
|
|
422
|
+
fuzzy: false
|
|
423
|
+
});
|
|
424
|
+
if (match) {
|
|
425
|
+
params = match;
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
return false;
|
|
429
|
+
});
|
|
430
|
+
if (foundMask) {
|
|
431
|
+
foundMask = {
|
|
432
|
+
...foundMask,
|
|
433
|
+
from: path.interpolatePath(foundMask.from, params)
|
|
434
|
+
};
|
|
435
|
+
maskedDest = foundMask;
|
|
436
|
+
maskedNext = build(maskedDest);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const nextMatches = this.matchRoutes(next.pathname, next.search);
|
|
440
|
+
const maskedMatches = maskedNext ? this.matchRoutes(maskedNext.pathname, maskedNext.search) : undefined;
|
|
441
|
+
const maskedFinal = maskedNext ? build(maskedDest, maskedMatches) : undefined;
|
|
442
|
+
const final = build(dest, nextMatches);
|
|
443
|
+
if (maskedFinal) {
|
|
444
|
+
final.maskedLocation = maskedFinal;
|
|
445
|
+
}
|
|
446
|
+
return final;
|
|
447
|
+
};
|
|
448
|
+
if (opts.mask) {
|
|
449
|
+
return buildWithMatches(opts, {
|
|
450
|
+
...utils.pick(opts, ['from']),
|
|
451
|
+
...opts.mask
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
return buildWithMatches(opts);
|
|
455
|
+
};
|
|
456
|
+
commitLocation = async ({
|
|
457
|
+
startTransition,
|
|
458
|
+
...next
|
|
459
|
+
}) => {
|
|
460
|
+
if (this.navigateTimeout) clearTimeout(this.navigateTimeout);
|
|
461
|
+
const isSameUrl = this.latestLocation.href === next.href;
|
|
462
|
+
|
|
463
|
+
// If the next urls are the same and we're not replacing,
|
|
464
|
+
// do nothing
|
|
465
|
+
if (!isSameUrl || !next.replace) {
|
|
466
|
+
let {
|
|
467
|
+
maskedLocation,
|
|
468
|
+
...nextHistory
|
|
469
|
+
} = next;
|
|
470
|
+
if (maskedLocation) {
|
|
471
|
+
nextHistory = {
|
|
472
|
+
...maskedLocation,
|
|
473
|
+
state: {
|
|
474
|
+
...maskedLocation.state,
|
|
475
|
+
__tempKey: undefined,
|
|
476
|
+
__tempLocation: {
|
|
477
|
+
...nextHistory,
|
|
478
|
+
search: nextHistory.searchStr,
|
|
479
|
+
state: {
|
|
480
|
+
...nextHistory.state,
|
|
481
|
+
__tempKey: undefined,
|
|
482
|
+
__tempLocation: undefined,
|
|
483
|
+
key: undefined
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
if (nextHistory.unmaskOnReload ?? this.options.unmaskOnReload ?? false) {
|
|
489
|
+
nextHistory.state.__tempKey = this.tempLocationKey;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
const apply = () => {
|
|
493
|
+
this.history[next.replace ? 'replace' : 'push'](nextHistory.href, nextHistory.state);
|
|
494
|
+
};
|
|
495
|
+
if (startTransition ?? true) {
|
|
496
|
+
this.startReactTransition(apply);
|
|
497
|
+
} else {
|
|
498
|
+
apply();
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
this.resetNextScroll = next.resetScroll ?? true;
|
|
502
|
+
return this.latestLoadPromise;
|
|
503
|
+
};
|
|
504
|
+
buildAndCommitLocation = ({
|
|
505
|
+
replace,
|
|
506
|
+
resetScroll,
|
|
507
|
+
startTransition,
|
|
508
|
+
...rest
|
|
509
|
+
} = {}) => {
|
|
510
|
+
const location = this.buildLocation(rest);
|
|
511
|
+
return this.commitLocation({
|
|
512
|
+
...location,
|
|
513
|
+
startTransition,
|
|
514
|
+
replace,
|
|
515
|
+
resetScroll
|
|
516
|
+
});
|
|
517
|
+
};
|
|
518
|
+
navigate = ({
|
|
519
|
+
from,
|
|
520
|
+
to = '',
|
|
521
|
+
...rest
|
|
522
|
+
}) => {
|
|
523
|
+
// If this link simply reloads the current route,
|
|
524
|
+
// make sure it has a new key so it will trigger a data refresh
|
|
525
|
+
|
|
526
|
+
// If this `to` is a valid external URL, return
|
|
527
|
+
// null for LinkUtils
|
|
528
|
+
const toString = String(to);
|
|
529
|
+
const fromString = typeof from === 'undefined' ? from : String(from);
|
|
530
|
+
let isExternal;
|
|
531
|
+
try {
|
|
532
|
+
new URL(`${toString}`);
|
|
533
|
+
isExternal = true;
|
|
534
|
+
} catch (e) {}
|
|
535
|
+
invariant(!isExternal, 'Attempting to navigate to external url with this.navigate!');
|
|
536
|
+
return this.buildAndCommitLocation({
|
|
537
|
+
...rest,
|
|
538
|
+
from: fromString,
|
|
539
|
+
to: toString
|
|
540
|
+
});
|
|
541
|
+
};
|
|
542
|
+
loadMatches = async ({
|
|
543
|
+
checkLatest,
|
|
544
|
+
matches,
|
|
545
|
+
preload,
|
|
546
|
+
invalidate
|
|
547
|
+
}) => {
|
|
548
|
+
let latestPromise;
|
|
549
|
+
let firstBadMatchIndex;
|
|
550
|
+
|
|
551
|
+
// Check each match middleware to see if the route can be accessed
|
|
552
|
+
try {
|
|
553
|
+
for (let [index, match] of matches.entries()) {
|
|
554
|
+
const parentMatch = matches[index - 1];
|
|
555
|
+
const route = this.looseRoutesById[match.routeId];
|
|
556
|
+
const handleError = (err, code) => {
|
|
557
|
+
err.routerCode = code;
|
|
558
|
+
firstBadMatchIndex = firstBadMatchIndex ?? index;
|
|
559
|
+
if (redirects.isRedirect(err)) {
|
|
560
|
+
throw err;
|
|
561
|
+
}
|
|
562
|
+
try {
|
|
563
|
+
route.options.onError?.(err);
|
|
564
|
+
} catch (errorHandlerErr) {
|
|
565
|
+
err = errorHandlerErr;
|
|
566
|
+
if (redirects.isRedirect(errorHandlerErr)) {
|
|
567
|
+
throw errorHandlerErr;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
matches[index] = match = {
|
|
571
|
+
...match,
|
|
572
|
+
error: err,
|
|
573
|
+
status: 'error',
|
|
574
|
+
updatedAt: Date.now()
|
|
575
|
+
};
|
|
576
|
+
};
|
|
577
|
+
try {
|
|
578
|
+
if (match.paramsError) {
|
|
579
|
+
handleError(match.paramsError, 'PARSE_PARAMS');
|
|
580
|
+
}
|
|
581
|
+
if (match.searchError) {
|
|
582
|
+
handleError(match.searchError, 'VALIDATE_SEARCH');
|
|
583
|
+
}
|
|
584
|
+
const parentContext = parentMatch?.context ?? this.options.context ?? {};
|
|
585
|
+
const beforeLoadContext = (await route.options.beforeLoad?.({
|
|
586
|
+
search: match.search,
|
|
587
|
+
abortController: match.abortController,
|
|
588
|
+
params: match.params,
|
|
589
|
+
preload: !!preload,
|
|
590
|
+
context: parentContext,
|
|
591
|
+
location: this.state.location,
|
|
592
|
+
// TOOD: just expose state and router, etc
|
|
593
|
+
navigate: opts => this.navigate({
|
|
594
|
+
...opts,
|
|
595
|
+
from: match.pathname
|
|
596
|
+
}),
|
|
597
|
+
buildLocation: this.buildLocation,
|
|
598
|
+
cause: match.cause
|
|
599
|
+
})) ?? {};
|
|
600
|
+
const context = {
|
|
601
|
+
...parentContext,
|
|
602
|
+
...beforeLoadContext
|
|
603
|
+
};
|
|
604
|
+
matches[index] = match = {
|
|
605
|
+
...match,
|
|
606
|
+
context: utils.replaceEqualDeep(match.context, context)
|
|
607
|
+
};
|
|
608
|
+
} catch (err) {
|
|
609
|
+
handleError(err, 'BEFORE_LOAD');
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
} catch (err) {
|
|
614
|
+
if (redirects.isRedirect(err)) {
|
|
615
|
+
if (!preload) this.navigate(err);
|
|
616
|
+
return matches;
|
|
617
|
+
}
|
|
618
|
+
throw err;
|
|
619
|
+
}
|
|
620
|
+
const validResolvedMatches = matches.slice(0, firstBadMatchIndex);
|
|
621
|
+
const matchPromises = [];
|
|
622
|
+
validResolvedMatches.forEach((match, index) => {
|
|
623
|
+
matchPromises.push((async () => {
|
|
624
|
+
const parentMatchPromise = matchPromises[index - 1];
|
|
625
|
+
const route = this.looseRoutesById[match.routeId];
|
|
626
|
+
const handleIfRedirect = err => {
|
|
627
|
+
if (redirects.isRedirect(err)) {
|
|
628
|
+
if (!preload) {
|
|
629
|
+
this.navigate(err);
|
|
630
|
+
}
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
return false;
|
|
634
|
+
};
|
|
635
|
+
let loadPromise;
|
|
636
|
+
matches[index] = match = {
|
|
637
|
+
...match,
|
|
638
|
+
fetchedAt: Date.now(),
|
|
639
|
+
invalid: false,
|
|
640
|
+
showPending: false
|
|
641
|
+
};
|
|
642
|
+
const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
|
|
643
|
+
let pendingPromise;
|
|
644
|
+
if (!preload && pendingMs && (route.options.pendingComponent ?? this.options.defaultPendingComponent)) {
|
|
645
|
+
pendingPromise = new Promise(r => setTimeout(r, pendingMs));
|
|
646
|
+
}
|
|
647
|
+
if (match.isFetching) {
|
|
648
|
+
loadPromise = RouterProvider.getRouteMatch(this.state, match.id)?.loadPromise;
|
|
649
|
+
} else {
|
|
650
|
+
const loaderContext = {
|
|
651
|
+
params: match.params,
|
|
652
|
+
search: match.search,
|
|
653
|
+
preload: !!preload,
|
|
654
|
+
parentMatchPromise,
|
|
655
|
+
abortController: match.abortController,
|
|
656
|
+
context: match.context,
|
|
657
|
+
location: this.state.location,
|
|
658
|
+
navigate: opts => this.navigate({
|
|
659
|
+
...opts,
|
|
660
|
+
from: match.pathname
|
|
661
|
+
}),
|
|
662
|
+
cause: match.cause
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
// Default to reloading the route all the time
|
|
666
|
+
let shouldReload = true;
|
|
667
|
+
let shouldReloadDeps = typeof route.options.shouldReload === 'function' ? route.options.shouldReload?.(loaderContext) : !!(route.options.shouldReload ?? true);
|
|
668
|
+
if (match.cause === 'enter' || invalidate) {
|
|
669
|
+
match.shouldReloadDeps = shouldReloadDeps;
|
|
670
|
+
} else if (match.cause === 'stay') {
|
|
671
|
+
if (typeof shouldReloadDeps === 'object') {
|
|
672
|
+
// compare the deps to see if they've changed
|
|
673
|
+
shouldReload = !utils.deepEqual(shouldReloadDeps, match.shouldReloadDeps);
|
|
674
|
+
match.shouldReloadDeps = shouldReloadDeps;
|
|
675
|
+
} else {
|
|
676
|
+
shouldReload = !!shouldReloadDeps;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// If the user doesn't want the route to reload, just
|
|
681
|
+
// resolve with the existing loader data
|
|
682
|
+
|
|
683
|
+
if (!shouldReload) {
|
|
684
|
+
loadPromise = Promise.resolve(match.loaderData);
|
|
685
|
+
} else {
|
|
686
|
+
// Otherwise, load the route
|
|
687
|
+
matches[index] = match = {
|
|
688
|
+
...match,
|
|
689
|
+
isFetching: true
|
|
690
|
+
};
|
|
691
|
+
const componentsPromise = Promise.all(componentTypes.map(async type => {
|
|
692
|
+
const component = route.options[type];
|
|
693
|
+
if (component?.preload) {
|
|
694
|
+
await component.preload();
|
|
695
|
+
}
|
|
696
|
+
}));
|
|
697
|
+
const loaderPromise = route.options.loader?.(loaderContext);
|
|
698
|
+
loadPromise = Promise.all([componentsPromise, loaderPromise]).then(d => d[1]);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
matches[index] = match = {
|
|
702
|
+
...match,
|
|
703
|
+
loadPromise
|
|
704
|
+
};
|
|
705
|
+
if (!preload) {
|
|
706
|
+
this.__store.setState(s => ({
|
|
707
|
+
...s,
|
|
708
|
+
matches: s.matches.map(d => d.id === match.id ? match : d)
|
|
709
|
+
}));
|
|
710
|
+
}
|
|
711
|
+
let didShowPending = false;
|
|
712
|
+
await new Promise(async resolve => {
|
|
713
|
+
// If the route has a pending component and a pendingMs option,
|
|
714
|
+
// forcefully show the pending component
|
|
715
|
+
if (pendingPromise) {
|
|
716
|
+
pendingPromise.then(() => {
|
|
717
|
+
if (latestPromise = checkLatest()) return;
|
|
718
|
+
didShowPending = true;
|
|
719
|
+
matches[index] = match = {
|
|
720
|
+
...match,
|
|
721
|
+
showPending: true
|
|
722
|
+
};
|
|
723
|
+
this.__store.setState(s => ({
|
|
724
|
+
...s,
|
|
725
|
+
matches: s.matches.map(d => d.id === match.id ? match : d)
|
|
726
|
+
}));
|
|
727
|
+
resolve();
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
try {
|
|
731
|
+
const loaderData = await loadPromise;
|
|
732
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
733
|
+
const pendingMinMs = route.options.pendingMinMs ?? this.options.defaultPendingMinMs;
|
|
734
|
+
if (didShowPending && pendingMinMs) {
|
|
735
|
+
await new Promise(r => setTimeout(r, pendingMinMs));
|
|
736
|
+
}
|
|
737
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
738
|
+
matches[index] = match = {
|
|
739
|
+
...match,
|
|
740
|
+
error: undefined,
|
|
741
|
+
status: 'success',
|
|
742
|
+
isFetching: false,
|
|
743
|
+
updatedAt: Date.now(),
|
|
744
|
+
loaderData,
|
|
745
|
+
loadPromise: undefined
|
|
746
|
+
};
|
|
747
|
+
} catch (error) {
|
|
748
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
749
|
+
if (handleIfRedirect(error)) return;
|
|
750
|
+
try {
|
|
751
|
+
route.options.onError?.(error);
|
|
752
|
+
} catch (onErrorError) {
|
|
753
|
+
error = onErrorError;
|
|
754
|
+
if (handleIfRedirect(onErrorError)) return;
|
|
755
|
+
}
|
|
756
|
+
matches[index] = match = {
|
|
757
|
+
...match,
|
|
758
|
+
error,
|
|
759
|
+
status: 'error',
|
|
760
|
+
isFetching: false,
|
|
761
|
+
updatedAt: Date.now()
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
if (!preload) {
|
|
765
|
+
this.__store.setState(s => ({
|
|
766
|
+
...s,
|
|
767
|
+
matches: s.matches.map(d => d.id === match.id ? match : d)
|
|
768
|
+
}));
|
|
769
|
+
}
|
|
770
|
+
resolve();
|
|
771
|
+
});
|
|
772
|
+
})());
|
|
773
|
+
});
|
|
774
|
+
await Promise.all(matchPromises);
|
|
775
|
+
return matches;
|
|
776
|
+
};
|
|
777
|
+
invalidate = () => this.load({
|
|
778
|
+
invalidate: true
|
|
779
|
+
});
|
|
780
|
+
load = async opts => {
|
|
781
|
+
const promise = new Promise(async (resolve, reject) => {
|
|
782
|
+
const next = this.latestLocation;
|
|
783
|
+
const prevLocation = this.state.resolvedLocation;
|
|
784
|
+
const pathDidChange = prevLocation.href !== next.href;
|
|
785
|
+
let latestPromise;
|
|
786
|
+
|
|
787
|
+
// Cancel any pending matches
|
|
788
|
+
this.cancelMatches();
|
|
789
|
+
this.emit({
|
|
790
|
+
type: 'onBeforeLoad',
|
|
791
|
+
fromLocation: prevLocation,
|
|
792
|
+
toLocation: next,
|
|
793
|
+
pathChanged: pathDidChange
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
// Match the routes
|
|
797
|
+
let matches = this.matchRoutes(next.pathname, next.search, {
|
|
798
|
+
debug: true
|
|
799
|
+
});
|
|
800
|
+
this.pendingMatches = matches;
|
|
801
|
+
const previousMatches = this.state.matches;
|
|
802
|
+
|
|
803
|
+
// Ingest the new matches
|
|
804
|
+
this.__store.setState(s => ({
|
|
805
|
+
...s,
|
|
806
|
+
// status: 'pending',
|
|
807
|
+
location: next,
|
|
808
|
+
matches
|
|
809
|
+
}));
|
|
810
|
+
try {
|
|
811
|
+
try {
|
|
812
|
+
// Load the matches
|
|
813
|
+
await this.loadMatches({
|
|
814
|
+
matches,
|
|
815
|
+
checkLatest: () => this.checkLatest(promise),
|
|
816
|
+
invalidate: opts?.invalidate
|
|
817
|
+
});
|
|
818
|
+
} catch (err) {
|
|
819
|
+
// swallow this error, since we'll display the
|
|
820
|
+
// errors on the route components
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// Only apply the latest transition
|
|
824
|
+
if (latestPromise = this.checkLatest(promise)) {
|
|
825
|
+
return latestPromise;
|
|
826
|
+
}
|
|
827
|
+
const exitingMatchIds = previousMatches.filter(id => !this.pendingMatches.includes(id));
|
|
828
|
+
const enteringMatchIds = this.pendingMatches.filter(id => !previousMatches.includes(id));
|
|
829
|
+
const stayingMatchIds = previousMatches.filter(id => this.pendingMatches.includes(id))
|
|
830
|
+
|
|
831
|
+
// setState((s) => ({
|
|
832
|
+
// ...s,
|
|
833
|
+
// status: 'idle',
|
|
834
|
+
// resolvedLocation: s.location,
|
|
835
|
+
// matches,
|
|
836
|
+
// }))
|
|
837
|
+
|
|
838
|
+
//
|
|
839
|
+
;
|
|
840
|
+
[[exitingMatchIds, 'onLeave'], [enteringMatchIds, 'onEnter'], [stayingMatchIds, 'onTransition']].forEach(([matches, hook]) => {
|
|
841
|
+
matches.forEach(match => {
|
|
842
|
+
this.looseRoutesById[match.routeId].options[hook]?.(match);
|
|
843
|
+
});
|
|
844
|
+
});
|
|
845
|
+
this.emit({
|
|
846
|
+
type: 'onLoad',
|
|
847
|
+
fromLocation: prevLocation,
|
|
848
|
+
toLocation: next,
|
|
849
|
+
pathChanged: pathDidChange
|
|
850
|
+
});
|
|
851
|
+
resolve();
|
|
852
|
+
} catch (err) {
|
|
853
|
+
// Only apply the latest transition
|
|
854
|
+
if (latestPromise = this.checkLatest(promise)) {
|
|
855
|
+
return latestPromise;
|
|
856
|
+
}
|
|
857
|
+
reject(err);
|
|
858
|
+
}
|
|
859
|
+
});
|
|
860
|
+
this.latestLoadPromise = promise;
|
|
861
|
+
return this.latestLoadPromise;
|
|
862
|
+
};
|
|
863
|
+
preloadRoute = async (navigateOpts = this.state.location) => {
|
|
864
|
+
let next = this.buildLocation(navigateOpts);
|
|
865
|
+
let matches = this.matchRoutes(next.pathname, next.search, {
|
|
866
|
+
throwOnError: true
|
|
867
|
+
});
|
|
868
|
+
await this.loadMatches({
|
|
869
|
+
matches,
|
|
870
|
+
preload: true,
|
|
871
|
+
checkLatest: () => undefined
|
|
872
|
+
});
|
|
873
|
+
return [utils.last(matches), matches];
|
|
874
|
+
};
|
|
875
|
+
buildLink = dest => {
|
|
876
|
+
// If this link simply reloads the current route,
|
|
877
|
+
// make sure it has a new key so it will trigger a data refresh
|
|
878
|
+
|
|
879
|
+
// If this `to` is a valid external URL, return
|
|
880
|
+
// null for LinkUtils
|
|
881
|
+
|
|
882
|
+
const {
|
|
883
|
+
to,
|
|
884
|
+
preload: userPreload,
|
|
885
|
+
preloadDelay: userPreloadDelay,
|
|
886
|
+
activeOptions,
|
|
887
|
+
disabled,
|
|
888
|
+
target,
|
|
889
|
+
replace,
|
|
890
|
+
resetScroll,
|
|
891
|
+
startTransition
|
|
892
|
+
} = dest;
|
|
893
|
+
try {
|
|
894
|
+
new URL(`${to}`);
|
|
895
|
+
return {
|
|
896
|
+
type: 'external',
|
|
897
|
+
href: to
|
|
898
|
+
};
|
|
899
|
+
} catch (e) {}
|
|
900
|
+
const nextOpts = dest;
|
|
901
|
+
const next = this.buildLocation(nextOpts);
|
|
902
|
+
const preload = userPreload ?? this.options.defaultPreload;
|
|
903
|
+
const preloadDelay = userPreloadDelay ?? this.options.defaultPreloadDelay ?? 0;
|
|
904
|
+
|
|
905
|
+
// Compare path/hash for matches
|
|
906
|
+
const currentPathSplit = this.latestLocation.pathname.split('/');
|
|
907
|
+
const nextPathSplit = next.pathname.split('/');
|
|
908
|
+
const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
|
|
909
|
+
// Combine the matches based on user this.options
|
|
910
|
+
const pathTest = activeOptions?.exact ? this.latestLocation.pathname === next.pathname : pathIsFuzzyEqual;
|
|
911
|
+
const hashTest = activeOptions?.includeHash ? this.latestLocation.hash === next.hash : true;
|
|
912
|
+
const searchTest = activeOptions?.includeSearch ?? true ? utils.deepEqual(this.latestLocation.search, next.search, true) : true;
|
|
913
|
+
|
|
914
|
+
// The final "active" test
|
|
915
|
+
const isActive = pathTest && hashTest && searchTest;
|
|
916
|
+
|
|
917
|
+
// The click handler
|
|
918
|
+
const handleClick = e => {
|
|
919
|
+
if (!disabled && !isCtrlEvent(e) && !e.defaultPrevented && (!target || target === '_self') && e.button === 0) {
|
|
920
|
+
e.preventDefault();
|
|
921
|
+
|
|
922
|
+
// All is well? Navigate!
|
|
923
|
+
this.commitLocation({
|
|
924
|
+
...next,
|
|
925
|
+
replace,
|
|
926
|
+
resetScroll,
|
|
927
|
+
startTransition
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
|
|
932
|
+
// The click handler
|
|
933
|
+
const handleFocus = e => {
|
|
934
|
+
if (preload) {
|
|
935
|
+
this.preloadRoute(nextOpts).catch(err => {
|
|
936
|
+
console.warn(err);
|
|
937
|
+
console.warn(preloadWarning);
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
};
|
|
941
|
+
const handleTouchStart = e => {
|
|
942
|
+
this.preloadRoute(nextOpts).catch(err => {
|
|
943
|
+
console.warn(err);
|
|
944
|
+
console.warn(preloadWarning);
|
|
945
|
+
});
|
|
946
|
+
};
|
|
947
|
+
const handleEnter = e => {
|
|
948
|
+
const target = e.target || {};
|
|
949
|
+
if (preload) {
|
|
950
|
+
if (target.preloadTimeout) {
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
target.preloadTimeout = setTimeout(() => {
|
|
954
|
+
target.preloadTimeout = null;
|
|
955
|
+
this.preloadRoute(nextOpts).catch(err => {
|
|
956
|
+
console.warn(err);
|
|
957
|
+
console.warn(preloadWarning);
|
|
958
|
+
});
|
|
959
|
+
}, preloadDelay);
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
const handleLeave = e => {
|
|
963
|
+
const target = e.target || {};
|
|
964
|
+
if (target.preloadTimeout) {
|
|
965
|
+
clearTimeout(target.preloadTimeout);
|
|
966
|
+
target.preloadTimeout = null;
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
return {
|
|
970
|
+
type: 'internal',
|
|
971
|
+
next,
|
|
972
|
+
handleFocus,
|
|
973
|
+
handleClick,
|
|
974
|
+
handleEnter,
|
|
975
|
+
handleLeave,
|
|
976
|
+
handleTouchStart,
|
|
977
|
+
isActive,
|
|
978
|
+
disabled
|
|
979
|
+
};
|
|
980
|
+
};
|
|
981
|
+
matchRoute = (location, opts) => {
|
|
982
|
+
location = {
|
|
983
|
+
...location,
|
|
984
|
+
to: location.to ? this.resolvePathWithBase(location.from || '', location.to) : undefined
|
|
985
|
+
};
|
|
986
|
+
const next = this.buildLocation(location);
|
|
987
|
+
if (opts?.pending && this.state.status !== 'pending') {
|
|
988
|
+
return false;
|
|
989
|
+
}
|
|
990
|
+
const baseLocation = opts?.pending ? this.latestLocation : this.state.resolvedLocation;
|
|
991
|
+
|
|
992
|
+
// const baseLocation = state.resolvedLocation
|
|
993
|
+
|
|
994
|
+
if (!baseLocation) {
|
|
995
|
+
return false;
|
|
996
|
+
}
|
|
997
|
+
const match = path.matchPathname(this.basepath, baseLocation.pathname, {
|
|
998
|
+
...opts,
|
|
999
|
+
to: next.pathname
|
|
1000
|
+
});
|
|
1001
|
+
if (!match) {
|
|
1002
|
+
return false;
|
|
1003
|
+
}
|
|
1004
|
+
if (match && (opts?.includeSearch ?? true)) {
|
|
1005
|
+
return utils.deepEqual(baseLocation.search, next.search, true) ? match : false;
|
|
1006
|
+
}
|
|
1007
|
+
return match;
|
|
1008
|
+
};
|
|
1009
|
+
injectHtml = async html => {
|
|
1010
|
+
this.injectedHtml.push(html);
|
|
1011
|
+
};
|
|
1012
|
+
dehydrateData = (key, getData) => {
|
|
1013
|
+
if (typeof document === 'undefined') {
|
|
1014
|
+
const strKey = typeof key === 'string' ? key : JSON.stringify(key);
|
|
1015
|
+
this.injectHtml(async () => {
|
|
1016
|
+
const id = `__TSR_DEHYDRATED__${strKey}`;
|
|
1017
|
+
const data = typeof getData === 'function' ? await getData() : getData;
|
|
1018
|
+
return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${utils.escapeJSON(strKey)}"] = ${JSON.stringify(data)}
|
|
1019
|
+
;(() => {
|
|
1020
|
+
var el = document.getElementById('${id}')
|
|
1021
|
+
el.parentElement.removeChild(el)
|
|
1022
|
+
})()
|
|
1023
|
+
</script>`;
|
|
1024
|
+
});
|
|
1025
|
+
return () => this.hydrateData(key);
|
|
1026
|
+
}
|
|
1027
|
+
return () => undefined;
|
|
1028
|
+
};
|
|
1029
|
+
hydrateData = key => {
|
|
1030
|
+
if (typeof document !== 'undefined') {
|
|
1031
|
+
const strKey = typeof key === 'string' ? key : JSON.stringify(key);
|
|
1032
|
+
return window[`__TSR_DEHYDRATED__${strKey}`];
|
|
1033
|
+
}
|
|
1034
|
+
return undefined;
|
|
1035
|
+
};
|
|
1036
|
+
dehydrate = () => {
|
|
1037
|
+
return {
|
|
1038
|
+
state: {
|
|
1039
|
+
dehydratedMatches: this.state.matches.map(d => utils.pick(d, ['fetchedAt', 'invalid', 'id', 'status', 'updatedAt', 'loaderData']))
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
};
|
|
1043
|
+
hydrate = async __do_not_use_server_ctx => {
|
|
1044
|
+
let _ctx = __do_not_use_server_ctx;
|
|
1045
|
+
// Client hydrates from window
|
|
1046
|
+
if (typeof document !== 'undefined') {
|
|
1047
|
+
_ctx = window.__TSR_DEHYDRATED__;
|
|
1048
|
+
}
|
|
1049
|
+
invariant(_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?');
|
|
1050
|
+
const ctx = _ctx;
|
|
1051
|
+
this.dehydratedData = ctx.payload;
|
|
1052
|
+
this.options.hydrate?.(ctx.payload);
|
|
1053
|
+
const dehydratedState = ctx.router.state;
|
|
1054
|
+
let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
|
|
1055
|
+
const dehydratedMatch = dehydratedState.dehydratedMatches.find(d => d.id === match.id);
|
|
1056
|
+
invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
|
|
1057
|
+
if (dehydratedMatch) {
|
|
1058
|
+
return {
|
|
1059
|
+
...match,
|
|
1060
|
+
...dehydratedMatch
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
return match;
|
|
1064
|
+
});
|
|
1065
|
+
this.__store.setState(s => {
|
|
1066
|
+
return {
|
|
1067
|
+
...s,
|
|
1068
|
+
matches: matches
|
|
1069
|
+
};
|
|
1070
|
+
});
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
// resolveMatchPromise = (matchId: string, key: string, value: any) => {
|
|
1074
|
+
// state.matches
|
|
1075
|
+
// .find((d) => d.id === matchId)
|
|
1076
|
+
// ?.__promisesByKey[key]?.resolve(value)
|
|
1077
|
+
// }
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
// A function that takes an import() argument which is a function and returns a new function that will
|
|
1081
|
+
// proxy arguments from the caller to the imported function, retaining all type
|
|
1082
|
+
// information along the way
|
|
1083
|
+
function lazyFn(fn, key) {
|
|
1084
|
+
return async (...args) => {
|
|
1085
|
+
const imported = await fn();
|
|
1086
|
+
return imported[key || 'default'](...args);
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
function isCtrlEvent(e) {
|
|
1090
|
+
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
|
|
1091
|
+
}
|
|
1092
|
+
class SearchParamError extends Error {}
|
|
1093
|
+
class PathParamError extends Error {}
|
|
1094
|
+
function getInitialRouterState(location) {
|
|
1095
|
+
return {
|
|
1096
|
+
status: 'idle',
|
|
1097
|
+
resolvedLocation: location,
|
|
1098
|
+
location,
|
|
1099
|
+
matches: [],
|
|
1100
|
+
pendingMatches: [],
|
|
1101
|
+
lastUpdated: Date.now()
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
exports.PathParamError = PathParamError;
|
|
1106
|
+
exports.Router = Router;
|
|
1107
|
+
exports.SearchParamError = SearchParamError;
|
|
1108
|
+
exports.componentTypes = componentTypes;
|
|
1109
|
+
exports.getInitialRouterState = getInitialRouterState;
|
|
1110
|
+
exports.lazyFn = lazyFn;
|
|
1111
|
+
//# sourceMappingURL=router.js.map
|