@tanstack/react-router 1.7.0 → 1.8.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.
Files changed (200) hide show
  1. package/dist/cjs/CatchBoundary.cjs +106 -0
  2. package/dist/cjs/CatchBoundary.cjs.map +1 -0
  3. package/dist/cjs/Matches.cjs +278 -0
  4. package/dist/cjs/Matches.cjs.map +1 -0
  5. package/{build/cjs/RouterProvider.js → dist/cjs/RouterProvider.cjs} +55 -58
  6. package/dist/cjs/RouterProvider.cjs.map +1 -0
  7. package/dist/cjs/awaited.cjs +51 -0
  8. package/dist/cjs/awaited.cjs.map +1 -0
  9. package/dist/cjs/defer.cjs +30 -0
  10. package/dist/cjs/defer.cjs.map +1 -0
  11. package/dist/cjs/fileRoute.cjs +19 -0
  12. package/dist/cjs/fileRoute.cjs.map +1 -0
  13. package/dist/cjs/history.d.cts +7 -0
  14. package/{build/cjs/index.js → dist/cjs/index.cjs} +39 -51
  15. package/dist/cjs/index.cjs.map +1 -0
  16. package/dist/cjs/lazyRouteComponent.cjs +40 -0
  17. package/dist/cjs/lazyRouteComponent.cjs.map +1 -0
  18. package/dist/cjs/link.cjs +196 -0
  19. package/dist/cjs/link.cjs.map +1 -0
  20. package/dist/cjs/link.d.cts +85 -0
  21. package/{build/cjs/path.js → dist/cjs/path.cjs} +77 -94
  22. package/dist/cjs/path.cjs.map +1 -0
  23. package/dist/cjs/qss.cjs +45 -0
  24. package/dist/cjs/qss.cjs.map +1 -0
  25. package/dist/cjs/redirects.cjs +15 -0
  26. package/dist/cjs/redirects.cjs.map +1 -0
  27. package/dist/cjs/route.cjs +143 -0
  28. package/dist/cjs/route.cjs.map +1 -0
  29. package/dist/cjs/router.cjs +1070 -0
  30. package/dist/cjs/router.cjs.map +1 -0
  31. package/dist/cjs/routerContext.cjs +29 -0
  32. package/dist/cjs/routerContext.cjs.map +1 -0
  33. package/{build/cjs/scroll-restoration.js → dist/cjs/scroll-restoration.cjs} +58 -75
  34. package/dist/cjs/scroll-restoration.cjs.map +1 -0
  35. package/{build/cjs/searchParams.js → dist/cjs/searchParams.cjs} +18 -36
  36. package/dist/cjs/searchParams.cjs.map +1 -0
  37. package/dist/cjs/useBlocker.cjs +36 -0
  38. package/dist/cjs/useBlocker.cjs.map +1 -0
  39. package/dist/cjs/useNavigate.cjs +55 -0
  40. package/dist/cjs/useNavigate.cjs.map +1 -0
  41. package/dist/cjs/useParams.cjs +16 -0
  42. package/dist/cjs/useParams.cjs.map +1 -0
  43. package/dist/cjs/useRouteContext.cjs +11 -0
  44. package/dist/cjs/useRouteContext.cjs.map +1 -0
  45. package/dist/cjs/useRouter.cjs +33 -0
  46. package/dist/cjs/useRouter.cjs.map +1 -0
  47. package/dist/cjs/useRouterState.cjs +12 -0
  48. package/dist/cjs/useRouterState.cjs.map +1 -0
  49. package/dist/cjs/useSearch.cjs +13 -0
  50. package/dist/cjs/useSearch.cjs.map +1 -0
  51. package/{build/cjs/utils.js → dist/cjs/utils.cjs} +25 -116
  52. package/dist/cjs/utils.cjs.map +1 -0
  53. package/dist/esm/CatchBoundary.d.ts +36 -0
  54. package/dist/esm/CatchBoundary.js +89 -0
  55. package/dist/esm/CatchBoundary.js.map +1 -0
  56. package/dist/esm/Matches.d.ts +71 -0
  57. package/dist/esm/Matches.js +261 -0
  58. package/{build/cjs → dist/esm}/Matches.js.map +1 -1
  59. package/dist/esm/RouterProvider.d.ts +27 -0
  60. package/dist/esm/RouterProvider.js +136 -0
  61. package/{build/cjs → dist/esm}/RouterProvider.js.map +1 -1
  62. package/dist/esm/awaited.d.ts +9 -0
  63. package/dist/esm/awaited.js +51 -0
  64. package/dist/esm/awaited.js.map +1 -0
  65. package/dist/esm/defer.d.ts +22 -0
  66. package/dist/esm/defer.js +30 -0
  67. package/{build/cjs → dist/esm}/defer.js.map +1 -1
  68. package/dist/esm/fileRoute.d.ts +21 -0
  69. package/dist/esm/fileRoute.js +19 -0
  70. package/dist/esm/fileRoute.js.map +1 -0
  71. package/{build/types → dist/esm}/history.d.ts +1 -1
  72. package/dist/esm/index.d.ts +30 -0
  73. package/dist/esm/index.js +118 -0
  74. package/dist/esm/index.js.map +1 -0
  75. package/dist/esm/lazyRouteComponent.d.ts +2 -0
  76. package/dist/esm/lazyRouteComponent.js +23 -0
  77. package/{build/cjs → dist/esm}/lazyRouteComponent.js.map +1 -1
  78. package/{build/types → dist/esm}/link.d.ts +5 -1
  79. package/dist/esm/link.js +179 -0
  80. package/dist/esm/link.js.map +1 -0
  81. package/dist/esm/location.d.ts +12 -0
  82. package/dist/esm/path.d.ts +17 -0
  83. package/dist/esm/path.js +200 -0
  84. package/dist/esm/path.js.map +1 -0
  85. package/dist/esm/qss.d.ts +2 -0
  86. package/dist/esm/qss.js +45 -0
  87. package/dist/esm/qss.js.map +1 -0
  88. package/dist/esm/redirects.d.ts +11 -0
  89. package/dist/esm/redirects.js +15 -0
  90. package/{build/cjs → dist/esm}/redirects.js.map +1 -1
  91. package/dist/esm/route.d.ts +300 -0
  92. package/dist/esm/route.js +143 -0
  93. package/dist/esm/route.js.map +1 -0
  94. package/dist/esm/routeInfo.d.ts +31 -0
  95. package/dist/esm/router.d.ts +201 -0
  96. package/dist/esm/router.js +1070 -0
  97. package/{build/cjs → dist/esm}/router.js.map +1 -1
  98. package/dist/esm/routerContext.d.ts +3 -0
  99. package/dist/esm/routerContext.js +13 -0
  100. package/{build/cjs → dist/esm}/routerContext.js.map +1 -1
  101. package/dist/esm/scroll-restoration.d.ts +18 -0
  102. package/dist/esm/scroll-restoration.js +168 -0
  103. package/dist/esm/scroll-restoration.js.map +1 -0
  104. package/dist/esm/searchParams.d.ts +7 -0
  105. package/dist/esm/searchParams.js +63 -0
  106. package/{build/cjs → dist/esm}/searchParams.js.map +1 -1
  107. package/dist/esm/useBlocker.d.ts +9 -0
  108. package/dist/esm/useBlocker.js +19 -0
  109. package/{build/cjs → dist/esm}/useBlocker.js.map +1 -1
  110. package/dist/esm/useNavigate.d.ts +20 -0
  111. package/dist/esm/useNavigate.js +38 -0
  112. package/{build/cjs → dist/esm}/useNavigate.js.map +1 -1
  113. package/dist/esm/useParams.d.ts +7 -0
  114. package/dist/esm/useParams.js +16 -0
  115. package/{build/cjs → dist/esm}/useParams.js.map +1 -1
  116. package/dist/esm/useRouteContext.d.ts +7 -0
  117. package/dist/esm/useRouteContext.js +11 -0
  118. package/{build/cjs → dist/esm}/useRouteContext.js.map +1 -1
  119. package/dist/esm/useRouter.d.ts +5 -0
  120. package/dist/esm/useRouter.js +16 -0
  121. package/{build/cjs → dist/esm}/useRouter.js.map +1 -1
  122. package/dist/esm/useRouterState.d.ts +6 -0
  123. package/dist/esm/useRouterState.js +12 -0
  124. package/{build/cjs → dist/esm}/useRouterState.js.map +1 -1
  125. package/dist/esm/useSearch.d.ts +7 -0
  126. package/dist/esm/useSearch.js +13 -0
  127. package/dist/esm/useSearch.js.map +1 -0
  128. package/dist/esm/utils.d.ts +46 -0
  129. package/dist/esm/utils.js +137 -0
  130. package/{build/cjs → dist/esm}/utils.js.map +1 -1
  131. package/package.json +43 -22
  132. package/src/fileRoute.ts +4 -5
  133. package/src/link.tsx +25 -16
  134. package/src/route.ts +5 -6
  135. package/src/useSearch.tsx +5 -2
  136. package/build/cjs/CatchBoundary.js +0 -128
  137. package/build/cjs/CatchBoundary.js.map +0 -1
  138. package/build/cjs/Matches.js +0 -260
  139. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -29
  140. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  141. package/build/cjs/awaited.js +0 -60
  142. package/build/cjs/awaited.js.map +0 -1
  143. package/build/cjs/defer.js +0 -42
  144. package/build/cjs/fileRoute.js +0 -31
  145. package/build/cjs/fileRoute.js.map +0 -1
  146. package/build/cjs/index.js.map +0 -1
  147. package/build/cjs/lazyRouteComponent.js +0 -54
  148. package/build/cjs/link.js +0 -224
  149. package/build/cjs/link.js.map +0 -1
  150. package/build/cjs/path.js.map +0 -1
  151. package/build/cjs/qss.js +0 -63
  152. package/build/cjs/qss.js.map +0 -1
  153. package/build/cjs/redirects.js +0 -28
  154. package/build/cjs/route.js +0 -292
  155. package/build/cjs/route.js.map +0 -1
  156. package/build/cjs/router.js +0 -1116
  157. package/build/cjs/routerContext.js +0 -42
  158. package/build/cjs/scroll-restoration.js.map +0 -1
  159. package/build/cjs/useBlocker.js +0 -55
  160. package/build/cjs/useNavigate.js +0 -88
  161. package/build/cjs/useParams.js +0 -27
  162. package/build/cjs/useRouteContext.js +0 -23
  163. package/build/cjs/useRouter.js +0 -44
  164. package/build/cjs/useRouterState.js +0 -24
  165. package/build/cjs/useSearch.js +0 -25
  166. package/build/cjs/useSearch.js.map +0 -1
  167. package/build/esm/index.js +0 -2874
  168. package/build/esm/index.js.map +0 -1
  169. package/build/stats-html.html +0 -4838
  170. package/build/stats-react.json +0 -1471
  171. package/build/umd/index.development.js +0 -3573
  172. package/build/umd/index.development.js.map +0 -1
  173. package/build/umd/index.production.js +0 -22
  174. package/build/umd/index.production.js.map +0 -1
  175. /package/{build/types/CatchBoundary.d.ts → dist/cjs/CatchBoundary.d.cts} +0 -0
  176. /package/{build/types/Matches.d.ts → dist/cjs/Matches.d.cts} +0 -0
  177. /package/{build/types/RouterProvider.d.ts → dist/cjs/RouterProvider.d.cts} +0 -0
  178. /package/{build/types/awaited.d.ts → dist/cjs/awaited.d.cts} +0 -0
  179. /package/{build/types/defer.d.ts → dist/cjs/defer.d.cts} +0 -0
  180. /package/{build/types/fileRoute.d.ts → dist/cjs/fileRoute.d.cts} +0 -0
  181. /package/{build/types/index.d.ts → dist/cjs/index.d.cts} +0 -0
  182. /package/{build/types/lazyRouteComponent.d.ts → dist/cjs/lazyRouteComponent.d.cts} +0 -0
  183. /package/{build/types/location.d.ts → dist/cjs/location.d.cts} +0 -0
  184. /package/{build/types/path.d.ts → dist/cjs/path.d.cts} +0 -0
  185. /package/{build/types/qss.d.ts → dist/cjs/qss.d.cts} +0 -0
  186. /package/{build/types/redirects.d.ts → dist/cjs/redirects.d.cts} +0 -0
  187. /package/{build/types/route.d.ts → dist/cjs/route.d.cts} +0 -0
  188. /package/{build/types/routeInfo.d.ts → dist/cjs/routeInfo.d.cts} +0 -0
  189. /package/{build/types/router.d.ts → dist/cjs/router.d.cts} +0 -0
  190. /package/{build/types/routerContext.d.ts → dist/cjs/routerContext.d.cts} +0 -0
  191. /package/{build/types/scroll-restoration.d.ts → dist/cjs/scroll-restoration.d.cts} +0 -0
  192. /package/{build/types/searchParams.d.ts → dist/cjs/searchParams.d.cts} +0 -0
  193. /package/{build/types/useBlocker.d.ts → dist/cjs/useBlocker.d.cts} +0 -0
  194. /package/{build/types/useNavigate.d.ts → dist/cjs/useNavigate.d.cts} +0 -0
  195. /package/{build/types/useParams.d.ts → dist/cjs/useParams.d.cts} +0 -0
  196. /package/{build/types/useRouteContext.d.ts → dist/cjs/useRouteContext.d.cts} +0 -0
  197. /package/{build/types/useRouter.d.ts → dist/cjs/useRouter.d.cts} +0 -0
  198. /package/{build/types/useRouterState.d.ts → dist/cjs/useRouterState.d.cts} +0 -0
  199. /package/{build/types/useSearch.d.ts → dist/cjs/useSearch.d.cts} +0 -0
  200. /package/{build/types/utils.d.ts → dist/cjs/utils.d.cts} +0 -0
@@ -1,3573 +0,0 @@
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
- (function (global, factory) {
12
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('use-sync-external-store/shim')) :
13
- typeof define === 'function' && define.amd ? define(['exports', 'react', 'use-sync-external-store/shim'], factory) :
14
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactRouter = {}, global.React, global.require$$1));
15
- })(this, (function (exports, React, require$$1) { 'use strict';
16
-
17
- function _interopNamespaceDefault(e) {
18
- var n = Object.create(null);
19
- if (e) {
20
- Object.keys(e).forEach(function (k) {
21
- if (k !== 'default') {
22
- var d = Object.getOwnPropertyDescriptor(e, k);
23
- Object.defineProperty(n, k, d.get ? d : {
24
- enumerable: true,
25
- get: function () { return e[k]; }
26
- });
27
- }
28
- });
29
- }
30
- n.default = e;
31
- return Object.freeze(n);
32
- }
33
-
34
- var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
35
-
36
- /**
37
- * @tanstack/history/src/index.ts
38
- *
39
- * Copyright (c) TanStack
40
- *
41
- * This source code is licensed under the MIT license found in the
42
- * LICENSE.md file in the root directory of this source tree.
43
- *
44
- * @license MIT
45
- */
46
- // While the public API was clearly inspired by the "history" npm package,
47
- // This implementation attempts to be more lightweight by
48
- // making assumptions about the way TanStack Router works
49
-
50
- const pushStateEvent = 'pushstate';
51
- const popStateEvent = 'popstate';
52
- const beforeUnloadEvent = 'beforeunload';
53
- const beforeUnloadListener = event => {
54
- event.preventDefault();
55
- // @ts-ignore
56
- return event.returnValue = '';
57
- };
58
- const stopBlocking = () => {
59
- removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
60
- capture: true
61
- });
62
- };
63
- function createHistory(opts) {
64
- let location = opts.getLocation();
65
- let subscribers = new Set();
66
- let blockers = [];
67
- const onUpdate = () => {
68
- location = opts.getLocation();
69
- subscribers.forEach(subscriber => subscriber());
70
- };
71
- const tryNavigation = async task => {
72
- if (typeof document !== 'undefined' && blockers.length) {
73
- for (let blocker of blockers) {
74
- const allowed = await blocker();
75
- if (!allowed) {
76
- opts.onBlocked?.(onUpdate);
77
- return;
78
- }
79
- }
80
- }
81
- task();
82
- };
83
- return {
84
- get location() {
85
- return location;
86
- },
87
- subscribe: cb => {
88
- subscribers.add(cb);
89
- return () => {
90
- subscribers.delete(cb);
91
- };
92
- },
93
- push: (path, state) => {
94
- state = assignKey(state);
95
- tryNavigation(() => {
96
- opts.pushState(path, state);
97
- onUpdate();
98
- });
99
- },
100
- replace: (path, state) => {
101
- state = assignKey(state);
102
- tryNavigation(() => {
103
- opts.replaceState(path, state);
104
- onUpdate();
105
- });
106
- },
107
- go: index => {
108
- tryNavigation(() => {
109
- opts.go(index);
110
- });
111
- },
112
- back: () => {
113
- tryNavigation(() => {
114
- opts.back();
115
- });
116
- },
117
- forward: () => {
118
- tryNavigation(() => {
119
- opts.forward();
120
- });
121
- },
122
- createHref: str => opts.createHref(str),
123
- block: blocker => {
124
- blockers.push(blocker);
125
- if (blockers.length === 1) {
126
- addEventListener(beforeUnloadEvent, beforeUnloadListener, {
127
- capture: true
128
- });
129
- }
130
- return () => {
131
- blockers = blockers.filter(b => b !== blocker);
132
- if (!blockers.length) {
133
- stopBlocking();
134
- }
135
- };
136
- },
137
- flush: () => opts.flush?.(),
138
- destroy: () => opts.destroy?.(),
139
- notify: onUpdate
140
- };
141
- }
142
- function assignKey(state) {
143
- if (!state) {
144
- state = {};
145
- }
146
- return {
147
- ...state,
148
- key: createRandomKey()
149
- };
150
- }
151
-
152
- /**
153
- * Creates a history object that can be used to interact with the browser's
154
- * navigation. This is a lightweight API wrapping the browser's native methods.
155
- * It is designed to work with TanStack Router, but could be used as a standalone API as well.
156
- * IMPORTANT: This API implements history throttling via a microtask to prevent
157
- * excessive calls to the history API. In some browsers, calling history.pushState or
158
- * history.replaceState in quick succession can cause the browser to ignore subsequent
159
- * calls. This API smooths out those differences and ensures that your application
160
- * state will *eventually* match the browser state. In most cases, this is not a problem,
161
- * but if you need to ensure that the browser state is up to date, you can use the
162
- * `history.flush` method to immediately flush all pending state changes to the browser URL.
163
- * @param opts
164
- * @param opts.getHref A function that returns the current href (path + search + hash)
165
- * @param opts.createHref A function that takes a path and returns a href (path + search + hash)
166
- * @returns A history instance
167
- */
168
- function createBrowserHistory(opts) {
169
- const win = opts?.window ?? (typeof document !== 'undefined' ? window : undefined);
170
- const createHref = opts?.createHref ?? (path => path);
171
- const parseLocation = opts?.parseLocation ?? (() => parseHref(`${win.location.pathname}${win.location.search}${win.location.hash}`, win.history.state));
172
- let currentLocation = parseLocation();
173
- let rollbackLocation;
174
- const getLocation = () => currentLocation;
175
- let next;
176
-
177
- // Because we are proactively updating the location
178
- // in memory before actually updating the browser history,
179
- // we need to track when we are doing this so we don't
180
- // notify subscribers twice on the last update.
181
- let tracking = true;
182
-
183
- // We need to track the current scheduled update to prevent
184
- // multiple updates from being scheduled at the same time.
185
- let scheduled;
186
-
187
- // This function is a wrapper to prevent any of the callback's
188
- // side effects from causing a subscriber notification
189
- const untrack = fn => {
190
- tracking = false;
191
- fn();
192
- tracking = true;
193
- };
194
-
195
- // This function flushes the next update to the browser history
196
- const flush = () => {
197
- // Do not notify subscribers about this push/replace call
198
- untrack(() => {
199
- if (!next) return;
200
- win.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
201
- // Reset the nextIsPush flag and clear the scheduled update
202
- next = undefined;
203
- scheduled = undefined;
204
- rollbackLocation = undefined;
205
- });
206
- };
207
-
208
- // This function queues up a call to update the browser history
209
- const queueHistoryAction = (type, destHref, state) => {
210
- const href = createHref(destHref);
211
- if (!scheduled) {
212
- rollbackLocation = currentLocation;
213
- }
214
-
215
- // Update the location in memory
216
- currentLocation = parseHref(destHref, state);
217
-
218
- // Keep track of the next location we need to flush to the URL
219
- next = {
220
- href,
221
- state,
222
- isPush: next?.isPush || type === 'push'
223
- };
224
- if (!scheduled) {
225
- // Schedule an update to the browser history
226
- scheduled = Promise.resolve().then(() => flush());
227
- }
228
- };
229
- const onPushPop = () => {
230
- currentLocation = parseLocation();
231
- history.notify();
232
- };
233
- var originalPushState = win.history.pushState;
234
- var originalReplaceState = win.history.replaceState;
235
- const history = createHistory({
236
- getLocation,
237
- pushState: (href, state) => queueHistoryAction('push', href, state),
238
- replaceState: (href, state) => queueHistoryAction('replace', href, state),
239
- back: () => win.history.back(),
240
- forward: () => win.history.forward(),
241
- go: n => win.history.go(n),
242
- createHref: href => createHref(href),
243
- flush,
244
- destroy: () => {
245
- win.history.pushState = originalPushState;
246
- win.history.replaceState = originalReplaceState;
247
- win.removeEventListener(pushStateEvent, onPushPop);
248
- win.removeEventListener(popStateEvent, onPushPop);
249
- },
250
- onBlocked: onUpdate => {
251
- // If a navigation is blocked, we need to rollback the location
252
- // that we optimistically updated in memory.
253
- if (rollbackLocation && currentLocation !== rollbackLocation) {
254
- currentLocation = rollbackLocation;
255
- // Notify subscribers
256
- onUpdate();
257
- }
258
- }
259
- });
260
- win.addEventListener(pushStateEvent, onPushPop);
261
- win.addEventListener(popStateEvent, onPushPop);
262
- win.history.pushState = function () {
263
- let res = originalPushState.apply(win.history, arguments);
264
- if (tracking) history.notify();
265
- return res;
266
- };
267
- win.history.replaceState = function () {
268
- let res = originalReplaceState.apply(win.history, arguments);
269
- if (tracking) history.notify();
270
- return res;
271
- };
272
- return history;
273
- }
274
- function createHashHistory(opts) {
275
- const win = opts?.window ?? (typeof document !== 'undefined' ? window : undefined);
276
- return createBrowserHistory({
277
- window: win,
278
- parseLocation: () => {
279
- const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/';
280
- return parseHref(hashHref, win.history.state);
281
- },
282
- createHref: href => `${win.location.pathname}${win.location.search}#${href}`
283
- });
284
- }
285
- function createMemoryHistory(opts = {
286
- initialEntries: ['/']
287
- }) {
288
- const entries = opts.initialEntries;
289
- let index = opts.initialIndex ?? entries.length - 1;
290
- let currentState = {
291
- key: createRandomKey()
292
- };
293
- const getLocation = () => parseHref(entries[index], currentState);
294
- return createHistory({
295
- getLocation,
296
- pushState: (path, state) => {
297
- currentState = state;
298
- entries.push(path);
299
- index++;
300
- },
301
- replaceState: (path, state) => {
302
- currentState = state;
303
- entries[index] = path;
304
- },
305
- back: () => {
306
- index--;
307
- },
308
- forward: () => {
309
- index = Math.min(index + 1, entries.length - 1);
310
- },
311
- go: n => {
312
- index = Math.min(Math.max(index + n, 0), entries.length - 1);
313
- },
314
- createHref: path => path
315
- });
316
- }
317
- function parseHref(href, state) {
318
- let hashIndex = href.indexOf('#');
319
- let searchIndex = href.indexOf('?');
320
- return {
321
- href,
322
- pathname: href.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : href.length),
323
- hash: hashIndex > -1 ? href.substring(hashIndex) : '',
324
- search: searchIndex > -1 ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) : '',
325
- state: state || {}
326
- };
327
- }
328
-
329
- // Thanks co-pilot!
330
- function createRandomKey() {
331
- return (Math.random() + 1).toString(36).substring(7);
332
- }
333
-
334
- var prefix = 'Invariant failed';
335
- function invariant(condition, message) {
336
- if (condition) {
337
- return;
338
- }
339
- var provided = typeof message === 'function' ? message() : message;
340
- var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
341
- throw new Error(value);
342
- }
343
-
344
- function warning(condition, message) {
345
- {
346
- if (condition) {
347
- return;
348
- }
349
-
350
- var text = "Warning: " + message;
351
-
352
- if (typeof console !== 'undefined') {
353
- console.warn(text);
354
- }
355
-
356
- try {
357
- throw Error(text);
358
- } catch (x) {}
359
- }
360
- }
361
-
362
- function CatchBoundary(props) {
363
- const errorComponent = props.errorComponent ?? ErrorComponent;
364
- return /*#__PURE__*/React__namespace.createElement(CatchBoundaryImpl, {
365
- getResetKey: props.getResetKey,
366
- onCatch: props.onCatch,
367
- children: ({
368
- error
369
- }) => {
370
- if (error) {
371
- return /*#__PURE__*/React__namespace.createElement(errorComponent, {
372
- error
373
- });
374
- }
375
- return props.children;
376
- }
377
- });
378
- }
379
- class CatchBoundaryImpl extends React__namespace.Component {
380
- state = {
381
- error: null
382
- };
383
- static getDerivedStateFromProps(props) {
384
- return {
385
- resetKey: props.getResetKey()
386
- };
387
- }
388
- static getDerivedStateFromError(error) {
389
- return {
390
- error
391
- };
392
- }
393
- componentDidUpdate(prevProps, prevState) {
394
- if (prevState.error && prevState.resetKey !== this.state.resetKey) {
395
- this.setState({
396
- error: null
397
- });
398
- }
399
- }
400
- componentDidCatch(error) {
401
- console.error(error);
402
- this.props.onCatch?.(error);
403
- }
404
- render() {
405
- return this.props.children(this.state);
406
- }
407
- }
408
- function ErrorComponent({
409
- error
410
- }) {
411
- const [show, setShow] = React__namespace.useState("development" !== 'production');
412
- return /*#__PURE__*/React__namespace.createElement("div", {
413
- style: {
414
- padding: '.5rem',
415
- maxWidth: '100%'
416
- }
417
- }, /*#__PURE__*/React__namespace.createElement("div", {
418
- style: {
419
- display: 'flex',
420
- alignItems: 'center',
421
- gap: '.5rem'
422
- }
423
- }, /*#__PURE__*/React__namespace.createElement("strong", {
424
- style: {
425
- fontSize: '1rem'
426
- }
427
- }, "Something went wrong!"), /*#__PURE__*/React__namespace.createElement("button", {
428
- style: {
429
- appearance: 'none',
430
- fontSize: '.6em',
431
- border: '1px solid currentColor',
432
- padding: '.1rem .2rem',
433
- fontWeight: 'bold',
434
- borderRadius: '.25rem'
435
- },
436
- onClick: () => setShow(d => !d)
437
- }, show ? 'Hide Error' : 'Show Error')), /*#__PURE__*/React__namespace.createElement("div", {
438
- style: {
439
- height: '.25rem'
440
- }
441
- }), show ? /*#__PURE__*/React__namespace.createElement("div", null, /*#__PURE__*/React__namespace.createElement("pre", {
442
- style: {
443
- fontSize: '.7em',
444
- border: '1px solid red',
445
- borderRadius: '.25rem',
446
- padding: '.3rem',
447
- color: 'red',
448
- overflow: 'auto'
449
- }
450
- }, error.message ? /*#__PURE__*/React__namespace.createElement("code", null, error.message) : null)) : null);
451
- }
452
-
453
- var withSelector = {exports: {}};
454
-
455
- var withSelector_development = {};
456
-
457
- /**
458
- * @license React
459
- * use-sync-external-store-shim/with-selector.development.js
460
- *
461
- * Copyright (c) Facebook, Inc. and its affiliates.
462
- *
463
- * This source code is licensed under the MIT license found in the
464
- * LICENSE file in the root directory of this source tree.
465
- */
466
-
467
- var hasRequiredWithSelector_development;
468
-
469
- function requireWithSelector_development () {
470
- if (hasRequiredWithSelector_development) return withSelector_development;
471
- hasRequiredWithSelector_development = 1;
472
-
473
- {
474
- (function() {
475
-
476
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
477
- if (
478
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
479
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
480
- 'function'
481
- ) {
482
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
483
- }
484
- var React$1 = React;
485
- var shim = require$$1;
486
-
487
- /**
488
- * inlined Object.is polyfill to avoid requiring consumers ship their own
489
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
490
- */
491
- function is(x, y) {
492
- return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
493
- ;
494
- }
495
-
496
- var objectIs = typeof Object.is === 'function' ? Object.is : is;
497
-
498
- var useSyncExternalStore = shim.useSyncExternalStore;
499
-
500
- // for CommonJS interop.
501
-
502
- var useRef = React$1.useRef,
503
- useEffect = React$1.useEffect,
504
- useMemo = React$1.useMemo,
505
- useDebugValue = React$1.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments.
506
-
507
- function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
508
- // Use this to track the rendered snapshot.
509
- var instRef = useRef(null);
510
- var inst;
511
-
512
- if (instRef.current === null) {
513
- inst = {
514
- hasValue: false,
515
- value: null
516
- };
517
- instRef.current = inst;
518
- } else {
519
- inst = instRef.current;
520
- }
521
-
522
- var _useMemo = useMemo(function () {
523
- // Track the memoized state using closure variables that are local to this
524
- // memoized instance of a getSnapshot function. Intentionally not using a
525
- // useRef hook, because that state would be shared across all concurrent
526
- // copies of the hook/component.
527
- var hasMemo = false;
528
- var memoizedSnapshot;
529
- var memoizedSelection;
530
-
531
- var memoizedSelector = function (nextSnapshot) {
532
- if (!hasMemo) {
533
- // The first time the hook is called, there is no memoized result.
534
- hasMemo = true;
535
- memoizedSnapshot = nextSnapshot;
536
-
537
- var _nextSelection = selector(nextSnapshot);
538
-
539
- if (isEqual !== undefined) {
540
- // Even if the selector has changed, the currently rendered selection
541
- // may be equal to the new selection. We should attempt to reuse the
542
- // current value if possible, to preserve downstream memoizations.
543
- if (inst.hasValue) {
544
- var currentSelection = inst.value;
545
-
546
- if (isEqual(currentSelection, _nextSelection)) {
547
- memoizedSelection = currentSelection;
548
- return currentSelection;
549
- }
550
- }
551
- }
552
-
553
- memoizedSelection = _nextSelection;
554
- return _nextSelection;
555
- } // We may be able to reuse the previous invocation's result.
556
-
557
-
558
- // We may be able to reuse the previous invocation's result.
559
- var prevSnapshot = memoizedSnapshot;
560
- var prevSelection = memoizedSelection;
561
-
562
- if (objectIs(prevSnapshot, nextSnapshot)) {
563
- // The snapshot is the same as last time. Reuse the previous selection.
564
- return prevSelection;
565
- } // The snapshot has changed, so we need to compute a new selection.
566
-
567
-
568
- // The snapshot has changed, so we need to compute a new selection.
569
- var nextSelection = selector(nextSnapshot); // If a custom isEqual function is provided, use that to check if the data
570
- // has changed. If it hasn't, return the previous selection. That signals
571
- // to React that the selections are conceptually equal, and we can bail
572
- // out of rendering.
573
-
574
- // If a custom isEqual function is provided, use that to check if the data
575
- // has changed. If it hasn't, return the previous selection. That signals
576
- // to React that the selections are conceptually equal, and we can bail
577
- // out of rendering.
578
- if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
579
- return prevSelection;
580
- }
581
-
582
- memoizedSnapshot = nextSnapshot;
583
- memoizedSelection = nextSelection;
584
- return nextSelection;
585
- }; // Assigning this to a constant so that Flow knows it can't change.
586
-
587
-
588
- // Assigning this to a constant so that Flow knows it can't change.
589
- var maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;
590
-
591
- var getSnapshotWithSelector = function () {
592
- return memoizedSelector(getSnapshot());
593
- };
594
-
595
- var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? undefined : function () {
596
- return memoizedSelector(maybeGetServerSnapshot());
597
- };
598
- return [getSnapshotWithSelector, getServerSnapshotWithSelector];
599
- }, [getSnapshot, getServerSnapshot, selector, isEqual]),
600
- getSelection = _useMemo[0],
601
- getServerSelection = _useMemo[1];
602
-
603
- var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
604
- useEffect(function () {
605
- inst.hasValue = true;
606
- inst.value = value;
607
- }, [value]);
608
- useDebugValue(value);
609
- return value;
610
- }
611
-
612
- withSelector_development.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;
613
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
614
- if (
615
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
616
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
617
- 'function'
618
- ) {
619
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
620
- }
621
-
622
- })();
623
- }
624
- return withSelector_development;
625
- }
626
-
627
- {
628
- withSelector.exports = requireWithSelector_development();
629
- }
630
-
631
- var withSelectorExports = withSelector.exports;
632
-
633
- // src/index.ts
634
- var Store = class {
635
- constructor(initialState, options) {
636
- this.listeners = /* @__PURE__ */ new Set();
637
- this._batching = false;
638
- this._flushing = 0;
639
- this._nextPriority = null;
640
- this.subscribe = (listener) => {
641
- this.listeners.add(listener);
642
- const unsub = this.options?.onSubscribe?.(listener, this);
643
- return () => {
644
- this.listeners.delete(listener);
645
- unsub?.();
646
- };
647
- };
648
- this.setState = (updater, opts) => {
649
- const previous = this.state;
650
- this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
651
- const priority = opts?.priority ?? this.options?.defaultPriority ?? "high";
652
- if (this._nextPriority === null) {
653
- this._nextPriority = priority;
654
- } else if (this._nextPriority === "high") {
655
- this._nextPriority = priority;
656
- } else {
657
- this._nextPriority = this.options?.defaultPriority ?? "high";
658
- }
659
- this.options?.onUpdate?.({
660
- priority: this._nextPriority
661
- });
662
- this._flush();
663
- };
664
- this._flush = () => {
665
- if (this._batching)
666
- return;
667
- const flushId = ++this._flushing;
668
- this.listeners.forEach((listener) => {
669
- if (this._flushing !== flushId)
670
- return;
671
- listener({
672
- priority: this._nextPriority ?? "high"
673
- });
674
- });
675
- };
676
- this.batch = (cb) => {
677
- if (this._batching)
678
- return cb();
679
- this._batching = true;
680
- cb();
681
- this._batching = false;
682
- this._flush();
683
- };
684
- this.state = initialState;
685
- this.options = options;
686
- }
687
- };
688
-
689
- // src/index.ts
690
- function useStore(store, selector = (d) => d) {
691
- const slice = withSelectorExports.useSyncExternalStoreWithSelector(
692
- store.subscribe,
693
- () => store.state,
694
- () => store.state,
695
- selector,
696
- shallow$1
697
- );
698
- return slice;
699
- }
700
- function shallow$1(objA, objB) {
701
- if (Object.is(objA, objB)) {
702
- return true;
703
- }
704
- if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
705
- return false;
706
- }
707
- const keysA = Object.keys(objA);
708
- if (keysA.length !== Object.keys(objB).length) {
709
- return false;
710
- }
711
- for (let i = 0; i < keysA.length; i++) {
712
- if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
713
- return false;
714
- }
715
- }
716
- return true;
717
- }
718
-
719
- exports.routerContext = /*#__PURE__*/React__namespace.createContext(null);
720
- if (typeof document !== 'undefined') {
721
- if (window.__TSR_ROUTER_CONTEXT__) {
722
- exports.routerContext = window.__TSR_ROUTER_CONTEXT__;
723
- } else {
724
- window.__TSR_ROUTER_CONTEXT__ = exports.routerContext;
725
- }
726
- }
727
-
728
- function useRouter(opts) {
729
- const resolvedContext = typeof document !== 'undefined' ? window.__TSR_ROUTER_CONTEXT__ || exports.routerContext : exports.routerContext;
730
- const value = React__namespace.useContext(resolvedContext);
731
- warning(!((opts?.warn ?? true) && !value), 'useRouter must be used inside a <RouterProvider> component!');
732
- return value;
733
- }
734
-
735
- function useRouterState(opts) {
736
- const contextRouter = useRouter({
737
- warn: opts?.router === undefined
738
- });
739
- return useStore((opts?.router || contextRouter).__store, opts?.select);
740
- }
741
-
742
- // from https://stackoverflow.com/a/76458160
743
-
744
- // export type Expand<T> = T
745
-
746
- // type Compute<T> = { [K in keyof T]: T[K] } | never
747
-
748
- // type AllKeys<T> = T extends any ? keyof T : never
749
-
750
- // export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<
751
- // {
752
- // [K in Keys]: T[Keys]
753
- // } & {
754
- // [K in AllKeys<T>]?: T extends any
755
- // ? K extends keyof T
756
- // ? T[K]
757
- // : never
758
- // : never
759
- // }
760
- // >
761
-
762
- // // Sample types to merge
763
- // type TypeA = {
764
- // shared: string
765
- // onlyInA: string
766
- // nested: {
767
- // shared: string
768
- // aProp: string
769
- // }
770
- // array: string[]
771
- // }
772
-
773
- // type TypeB = {
774
- // shared: number
775
- // onlyInB: number
776
- // nested: {
777
- // shared: number
778
- // bProp: number
779
- // }
780
- // array: number[]
781
- // }
782
-
783
- // type TypeC = {
784
- // shared: boolean
785
- // onlyInC: boolean
786
- // nested: {
787
- // shared: boolean
788
- // cProp: boolean
789
- // }
790
- // array: boolean[]
791
- // }
792
-
793
- // type Test = Expand<Assign<TypeA, TypeB>>
794
-
795
- // // Using DeepMerge to merge TypeA and TypeB
796
- // type MergedType = Expand<AssignAll<[TypeA, TypeB, TypeC]>>
797
-
798
- // from https://github.com/type-challenges/type-challenges/issues/737
799
-
800
- //
801
-
802
- const isServer = typeof document === 'undefined';
803
- function last(arr) {
804
- return arr[arr.length - 1];
805
- }
806
- function isFunction(d) {
807
- return typeof d === 'function';
808
- }
809
- function functionalUpdate(updater, previous) {
810
- if (isFunction(updater)) {
811
- return updater(previous);
812
- }
813
- return updater;
814
- }
815
- function pick(parent, keys) {
816
- return keys.reduce((obj, key) => {
817
- obj[key] = parent[key];
818
- return obj;
819
- }, {});
820
- }
821
-
822
- /**
823
- * This function returns `prev` if `_next` is deeply equal.
824
- * If not, it will replace any deeply equal children of `b` with those of `a`.
825
- * This can be used for structural sharing between immutable JSON values for example.
826
- * Do not use this with signals
827
- */
828
- function replaceEqualDeep(prev, _next) {
829
- if (prev === _next) {
830
- return prev;
831
- }
832
- const next = _next;
833
- const array = isPlainArray(prev) && isPlainArray(next);
834
- if (array || isPlainObject(prev) && isPlainObject(next)) {
835
- const prevItems = array ? prev : Object.keys(prev);
836
- const prevSize = prevItems.length;
837
- const nextItems = array ? next : Object.keys(next);
838
- const nextSize = nextItems.length;
839
- const copy = array ? [] : {};
840
- let equalItems = 0;
841
- for (let i = 0; i < nextSize; i++) {
842
- const key = array ? i : nextItems[i];
843
- if (!array && prev[key] === undefined && next[key] === undefined && prevItems.includes(key)) {
844
- copy[key] = undefined;
845
- equalItems++;
846
- } else {
847
- copy[key] = replaceEqualDeep(prev[key], next[key]);
848
- if (copy[key] === prev[key] && prev[key] !== undefined) {
849
- equalItems++;
850
- }
851
- }
852
- }
853
- return prevSize === nextSize && equalItems === prevSize ? prev : copy;
854
- }
855
- return next;
856
- }
857
-
858
- // Copied from: https://github.com/jonschlinkert/is-plain-object
859
- function isPlainObject(o) {
860
- if (!hasObjectPrototype(o)) {
861
- return false;
862
- }
863
-
864
- // If has modified constructor
865
- const ctor = o.constructor;
866
- if (typeof ctor === 'undefined') {
867
- return true;
868
- }
869
-
870
- // If has modified prototype
871
- const prot = ctor.prototype;
872
- if (!hasObjectPrototype(prot)) {
873
- return false;
874
- }
875
-
876
- // If constructor does not have an Object-specific method
877
- if (!prot.hasOwnProperty('isPrototypeOf')) {
878
- return false;
879
- }
880
-
881
- // Most likely a plain Object
882
- return true;
883
- }
884
- function hasObjectPrototype(o) {
885
- return Object.prototype.toString.call(o) === '[object Object]';
886
- }
887
- function isPlainArray(value) {
888
- return Array.isArray(value) && value.length === Object.keys(value).length;
889
- }
890
- function deepEqual(a, b, partial = false) {
891
- if (a === b) {
892
- return true;
893
- }
894
- if (typeof a !== typeof b) {
895
- return false;
896
- }
897
- if (isPlainObject(a) && isPlainObject(b)) {
898
- const aKeys = Object.keys(a);
899
- const bKeys = Object.keys(b);
900
- if (!partial && aKeys.length !== bKeys.length) {
901
- return false;
902
- }
903
- return !bKeys.some(key => !(key in a) || !deepEqual(a[key], b[key], partial));
904
- }
905
- if (Array.isArray(a) && Array.isArray(b)) {
906
- return !a.some((item, index) => !deepEqual(item, b[index], partial));
907
- }
908
- return false;
909
- }
910
- function useStableCallback(fn) {
911
- const fnRef = React__namespace.useRef(fn);
912
- fnRef.current = fn;
913
- const ref = React__namespace.useRef((...args) => fnRef.current(...args));
914
- return ref.current;
915
- }
916
- function shallow(objA, objB) {
917
- if (Object.is(objA, objB)) {
918
- return true;
919
- }
920
- if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
921
- return false;
922
- }
923
- const keysA = Object.keys(objA);
924
- if (keysA.length !== Object.keys(objB).length) {
925
- return false;
926
- }
927
- for (let i = 0; i < keysA.length; i++) {
928
- if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
929
- return false;
930
- }
931
- }
932
- return true;
933
- }
934
- const useLayoutEffect$1 = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
935
- function escapeJSON(jsonString) {
936
- return jsonString.replace(/\\/g, '\\\\') // Escape backslashes
937
- .replace(/'/g, "\\'") // Escape single quotes
938
- .replace(/"/g, '\\"'); // Escape double quotes
939
- }
940
-
941
- const matchContext = /*#__PURE__*/React__namespace.createContext(undefined);
942
- function Matches() {
943
- const router = useRouter();
944
- const matchId = useRouterState({
945
- select: s => {
946
- return getRenderedMatches(s)[0]?.id;
947
- }
948
- });
949
- return /*#__PURE__*/React__namespace.createElement(matchContext.Provider, {
950
- value: matchId
951
- }, /*#__PURE__*/React__namespace.createElement(CatchBoundary, {
952
- getResetKey: () => router.state.resolvedLocation.state?.key,
953
- errorComponent: ErrorComponent,
954
- onCatch: () => {
955
- warning(false, `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`);
956
- }
957
- }, matchId ? /*#__PURE__*/React__namespace.createElement(Match, {
958
- matchId: matchId
959
- }) : null));
960
- }
961
- function SafeFragment(props) {
962
- return /*#__PURE__*/React__namespace.createElement(React__namespace.Fragment, null, props.children);
963
- }
964
- function Match({
965
- matchId
966
- }) {
967
- const router = useRouter();
968
- const routeId = useRouterState({
969
- select: s => getRenderedMatches(s).find(d => d.id === matchId)?.routeId
970
- });
971
- invariant(routeId, `Could not find routeId for matchId "${matchId}". Please file an issue!`);
972
- const route = router.routesById[routeId];
973
- const PendingComponent = route.options.pendingComponent ?? router.options.defaultPendingComponent;
974
- const pendingElement = PendingComponent ? /*#__PURE__*/React__namespace.createElement(PendingComponent, null) : null;
975
- const routeErrorComponent = route.options.errorComponent ?? router.options.defaultErrorComponent ?? ErrorComponent;
976
- const ResolvedSuspenseBoundary = route.options.wrapInSuspense ?? PendingComponent ?? route.options.component?.preload ?? route.options.pendingComponent?.preload ?? route.options.errorComponent?.preload ? React__namespace.Suspense : SafeFragment;
977
- const ResolvedCatchBoundary = routeErrorComponent ? CatchBoundary : SafeFragment;
978
- return /*#__PURE__*/React__namespace.createElement(matchContext.Provider, {
979
- value: matchId
980
- }, /*#__PURE__*/React__namespace.createElement(ResolvedSuspenseBoundary, {
981
- fallback: pendingElement
982
- }, /*#__PURE__*/React__namespace.createElement(ResolvedCatchBoundary, {
983
- getResetKey: () => router.state.resolvedLocation.state?.key,
984
- errorComponent: routeErrorComponent,
985
- onCatch: () => {
986
- warning(false, `Error in route match: ${matchId}`);
987
- }
988
- }, /*#__PURE__*/React__namespace.createElement(MatchInner, {
989
- matchId: matchId,
990
- pendingElement: pendingElement
991
- }))));
992
- }
993
- function MatchInner({
994
- matchId,
995
- pendingElement
996
- }) {
997
- const router = useRouter();
998
- const routeId = useRouterState({
999
- select: s => getRenderedMatches(s).find(d => d.id === matchId)?.routeId
1000
- });
1001
- const route = router.routesById[routeId];
1002
- const match = useRouterState({
1003
- select: s => pick(getRenderedMatches(s).find(d => d.id === matchId), ['status', 'error', 'showPending', 'loadPromise'])
1004
- });
1005
- if (match.status === 'error') {
1006
- if (isServerSideError(match.error)) {
1007
- const deserializeError = router.options.errorSerializer?.deserialize ?? defaultDeserializeError;
1008
- throw deserializeError(match.error.data);
1009
- } else {
1010
- throw match.error;
1011
- }
1012
- }
1013
- if (match.status === 'pending') {
1014
- if (match.showPending) {
1015
- return pendingElement;
1016
- }
1017
- throw match.loadPromise;
1018
- }
1019
- if (match.status === 'success') {
1020
- let Comp = route.options.component ?? router.options.defaultComponent;
1021
- if (Comp) {
1022
- return /*#__PURE__*/React__namespace.createElement(Comp, null);
1023
- }
1024
- return /*#__PURE__*/React__namespace.createElement(Outlet, null);
1025
- }
1026
- invariant(false, 'Idle routeMatch status encountered during rendering! You should never see this. File an issue!');
1027
- }
1028
- const Outlet = /*#__PURE__*/React__namespace.memo(function Outlet() {
1029
- const matchId = React__namespace.useContext(matchContext);
1030
- const childMatchId = useRouterState({
1031
- select: s => {
1032
- const matches = getRenderedMatches(s);
1033
- const index = matches.findIndex(d => d.id === matchId);
1034
- return matches[index + 1]?.id;
1035
- }
1036
- });
1037
- if (!childMatchId) {
1038
- return null;
1039
- }
1040
- return /*#__PURE__*/React__namespace.createElement(Match, {
1041
- matchId: childMatchId
1042
- });
1043
- });
1044
- function useMatchRoute() {
1045
- useRouterState({
1046
- select: s => [s.location, s.resolvedLocation]
1047
- });
1048
- const {
1049
- matchRoute
1050
- } = useRouter();
1051
- return React__namespace.useCallback(opts => {
1052
- const {
1053
- pending,
1054
- caseSensitive,
1055
- fuzzy,
1056
- includeSearch,
1057
- ...rest
1058
- } = opts;
1059
- return matchRoute(rest, {
1060
- pending,
1061
- caseSensitive,
1062
- fuzzy,
1063
- includeSearch
1064
- });
1065
- }, []);
1066
- }
1067
- function MatchRoute(props) {
1068
- const matchRoute = useMatchRoute();
1069
- const params = matchRoute(props);
1070
- if (typeof props.children === 'function') {
1071
- return props.children(params);
1072
- }
1073
- return !!params ? props.children : null;
1074
- }
1075
- function getRenderedMatches(state) {
1076
- return state.pendingMatches?.some(d => d.showPending) ? state.pendingMatches : state.matches;
1077
- }
1078
- function useMatch(opts) {
1079
- const router = useRouter();
1080
- const nearestMatchId = React__namespace.useContext(matchContext);
1081
- const nearestMatchRouteId = getRenderedMatches(router.state).find(d => d.id === nearestMatchId)?.routeId;
1082
- const matchRouteId = (() => {
1083
- const matches = getRenderedMatches(router.state);
1084
- const match = opts?.from ? matches.find(d => d.routeId === opts?.from) : matches.find(d => d.id === nearestMatchId);
1085
- return match.routeId;
1086
- })();
1087
- if (opts?.strict ?? true) {
1088
- invariant(nearestMatchRouteId == matchRouteId, `useMatch("${matchRouteId}") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${matchRouteId}", { strict: false })' or 'useRoute("${matchRouteId}")' instead?`);
1089
- }
1090
- const matchSelection = useRouterState({
1091
- select: state => {
1092
- const match = getRenderedMatches(state).find(d => d.id === nearestMatchId);
1093
- invariant(match, `Could not find ${opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`);
1094
- return opts?.select ? opts.select(match) : match;
1095
- }
1096
- });
1097
- return matchSelection;
1098
- }
1099
- function useMatches(opts) {
1100
- return useRouterState({
1101
- select: state => {
1102
- let matches = getRenderedMatches(state);
1103
- return opts?.select ? opts.select(matches) : matches;
1104
- }
1105
- });
1106
- }
1107
- function useParentMatches(opts) {
1108
- const contextMatchId = React__namespace.useContext(matchContext);
1109
- return useMatches({
1110
- select: matches => {
1111
- matches = matches.slice(matches.findIndex(d => d.id === contextMatchId));
1112
- return opts?.select ? opts.select(matches) : matches;
1113
- }
1114
- });
1115
- }
1116
- function useLoaderDeps(opts) {
1117
- return useMatch({
1118
- ...opts,
1119
- select: s => {
1120
- return typeof opts.select === 'function' ? opts.select(s?.loaderDeps) : s?.loaderDeps;
1121
- }
1122
- });
1123
- }
1124
- function useLoaderData(opts) {
1125
- return useMatch({
1126
- ...opts,
1127
- select: s => {
1128
- return typeof opts.select === 'function' ? opts.select(s?.loaderData) : s?.loaderData;
1129
- }
1130
- });
1131
- }
1132
- function isServerSideError(error) {
1133
- if (!(typeof error === 'object' && error && 'data' in error)) return false;
1134
- if (!('__isServerError' in error && error.__isServerError)) return false;
1135
- if (!(typeof error.data === 'object' && error.data)) return false;
1136
- return error.__isServerError === true;
1137
- }
1138
- function defaultDeserializeError(serializedData) {
1139
- if ('name' in serializedData && 'message' in serializedData) {
1140
- const error = new Error(serializedData.message);
1141
- error.name = serializedData.name;
1142
- return error;
1143
- }
1144
- return serializedData.data;
1145
- }
1146
-
1147
- // @ts-nocheck
1148
-
1149
- // qss has been slightly modified and inlined here for our use cases (and compression's sake). We've included it as a hard dependency for MIT license attribution.
1150
-
1151
- function encode(obj, pfx) {
1152
- var k,
1153
- i,
1154
- tmp,
1155
- str = '';
1156
- for (k in obj) {
1157
- if ((tmp = obj[k]) !== void 0) {
1158
- if (Array.isArray(tmp)) {
1159
- for (i = 0; i < tmp.length; i++) {
1160
- str && (str += '&');
1161
- str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp[i]);
1162
- }
1163
- } else {
1164
- str && (str += '&');
1165
- str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp);
1166
- }
1167
- }
1168
- }
1169
- return (pfx || '') + str;
1170
- }
1171
- function toValue(mix) {
1172
- if (!mix) return '';
1173
- var str = decodeURIComponent(mix);
1174
- if (str === 'false') return false;
1175
- if (str === 'true') return true;
1176
- return +str * 0 === 0 && +str + '' === str ? +str : str;
1177
- }
1178
- function decode(str) {
1179
- var tmp,
1180
- k,
1181
- out = {},
1182
- arr = str.split('&');
1183
- while (tmp = arr.shift()) {
1184
- tmp = tmp.split('=');
1185
- k = tmp.shift();
1186
- if (out[k] !== void 0) {
1187
- out[k] = [].concat(out[k], toValue(tmp.shift()));
1188
- } else {
1189
- out[k] = toValue(tmp.shift());
1190
- }
1191
- }
1192
- return out;
1193
- }
1194
-
1195
- const defaultParseSearch = parseSearchWith(JSON.parse);
1196
- const defaultStringifySearch = stringifySearchWith(JSON.stringify, JSON.parse);
1197
- function parseSearchWith(parser) {
1198
- return searchStr => {
1199
- if (searchStr.substring(0, 1) === '?') {
1200
- searchStr = searchStr.substring(1);
1201
- }
1202
- let query = decode(searchStr);
1203
-
1204
- // Try to parse any query params that might be json
1205
- for (let key in query) {
1206
- const value = query[key];
1207
- if (typeof value === 'string') {
1208
- try {
1209
- query[key] = parser(value);
1210
- } catch (err) {
1211
- //
1212
- }
1213
- }
1214
- }
1215
- return query;
1216
- };
1217
- }
1218
- function stringifySearchWith(stringify, parser) {
1219
- function stringifyValue(val) {
1220
- if (typeof val === 'object' && val !== null) {
1221
- try {
1222
- return stringify(val);
1223
- } catch (err) {
1224
- // silent
1225
- }
1226
- } else if (typeof val === 'string' && typeof parser === 'function') {
1227
- try {
1228
- // Check if it's a valid parseable string.
1229
- // If it is, then stringify it again.
1230
- parser(val);
1231
- return stringify(val);
1232
- } catch (err) {
1233
- // silent
1234
- }
1235
- }
1236
- return val;
1237
- }
1238
- return search => {
1239
- search = {
1240
- ...search
1241
- };
1242
- if (search) {
1243
- Object.keys(search).forEach(key => {
1244
- const val = search[key];
1245
- if (typeof val === 'undefined' || val === undefined) {
1246
- delete search[key];
1247
- } else {
1248
- search[key] = stringifyValue(val);
1249
- }
1250
- });
1251
- }
1252
- const searchStr = encode(search).toString();
1253
- return searchStr ? `?${searchStr}` : '';
1254
- };
1255
- }
1256
-
1257
- const useTransition = React__namespace.useTransition || (() => [false, cb => {
1258
- cb();
1259
- }]);
1260
- function RouterProvider({
1261
- router,
1262
- ...rest
1263
- }) {
1264
- // Allow the router to update options on the router instance
1265
- router.update({
1266
- ...router.options,
1267
- ...rest,
1268
- context: {
1269
- ...router.options.context,
1270
- ...rest?.context
1271
- }
1272
- });
1273
- const matches = router.options.InnerWrap ? /*#__PURE__*/React__namespace.createElement(router.options.InnerWrap, null, /*#__PURE__*/React__namespace.createElement(Matches, null)) : /*#__PURE__*/React__namespace.createElement(Matches, null);
1274
- const provider = /*#__PURE__*/React__namespace.createElement(exports.routerContext.Provider, {
1275
- value: router
1276
- }, matches, /*#__PURE__*/React__namespace.createElement(Transitioner, null));
1277
- if (router.options.Wrap) {
1278
- return /*#__PURE__*/React__namespace.createElement(router.options.Wrap, null, provider);
1279
- }
1280
- return provider;
1281
- }
1282
- function Transitioner() {
1283
- const mountLoadCount = React__namespace.useRef(0);
1284
- const router = useRouter();
1285
- const routerState = useRouterState({
1286
- select: s => pick(s, ['isLoading', 'location', 'resolvedLocation', 'isTransitioning'])
1287
- });
1288
- const [isTransitioning, startReactTransition] = useTransition();
1289
- router.startReactTransition = startReactTransition;
1290
- React__namespace.useEffect(() => {
1291
- if (isTransitioning) {
1292
- router.__store.setState(s => ({
1293
- ...s,
1294
- isTransitioning
1295
- }));
1296
- }
1297
- }, [isTransitioning]);
1298
- const tryLoad = () => {
1299
- const apply = cb => {
1300
- if (!routerState.isTransitioning) {
1301
- startReactTransition(() => cb());
1302
- } else {
1303
- cb();
1304
- }
1305
- };
1306
- apply(() => {
1307
- try {
1308
- router.load();
1309
- } catch (err) {
1310
- console.error(err);
1311
- }
1312
- });
1313
- };
1314
- useLayoutEffect$1(() => {
1315
- const unsub = router.history.subscribe(() => {
1316
- router.latestLocation = router.parseLocation(router.latestLocation);
1317
- if (routerState.location !== router.latestLocation) {
1318
- tryLoad();
1319
- }
1320
- });
1321
- const nextLocation = router.buildLocation({
1322
- search: true,
1323
- params: true,
1324
- hash: true,
1325
- state: true
1326
- });
1327
- if (routerState.location.href !== nextLocation.href) {
1328
- router.commitLocation({
1329
- ...nextLocation,
1330
- replace: true
1331
- });
1332
- }
1333
- return () => {
1334
- unsub();
1335
- };
1336
- }, [router.history]);
1337
- useLayoutEffect$1(() => {
1338
- if (React__namespace.useTransition ? routerState.isTransitioning && !isTransitioning : !routerState.isLoading && routerState.resolvedLocation !== routerState.location) {
1339
- router.emit({
1340
- type: 'onResolved',
1341
- fromLocation: routerState.resolvedLocation,
1342
- toLocation: routerState.location,
1343
- pathChanged: routerState.location.href !== routerState.resolvedLocation?.href
1344
- });
1345
- if (document.querySelector) {
1346
- if (routerState.location.hash !== '') {
1347
- const el = document.getElementById(routerState.location.hash);
1348
- if (el) {
1349
- el.scrollIntoView();
1350
- }
1351
- }
1352
- }
1353
- router.__store.setState(s => ({
1354
- ...s,
1355
- isTransitioning: false,
1356
- resolvedLocation: s.location
1357
- }));
1358
- }
1359
- }, [routerState.isTransitioning, isTransitioning, routerState.isLoading, routerState.resolvedLocation, routerState.location]);
1360
- useLayoutEffect$1(() => {
1361
- if (!window.__TSR_DEHYDRATED__ && !mountLoadCount.current) {
1362
- mountLoadCount.current++;
1363
- tryLoad();
1364
- }
1365
- }, []);
1366
- return null;
1367
- }
1368
- function getRouteMatch(state, id) {
1369
- return [...state.cachedMatches, ...(state.pendingMatches ?? []), ...state.matches].find(d => d.id === id);
1370
- }
1371
-
1372
- function joinPaths(paths) {
1373
- return cleanPath(paths.filter(Boolean).join('/'));
1374
- }
1375
- function cleanPath(path) {
1376
- // remove double slashes
1377
- return path.replace(/\/{2,}/g, '/');
1378
- }
1379
- function trimPathLeft(path) {
1380
- return path === '/' ? path : path.replace(/^\/{1,}/, '');
1381
- }
1382
- function trimPathRight(path) {
1383
- return path === '/' ? path : path.replace(/\/{1,}$/, '');
1384
- }
1385
- function trimPath(path) {
1386
- return trimPathRight(trimPathLeft(path));
1387
- }
1388
- function resolvePath(basepath, base, to) {
1389
- base = base.replace(new RegExp(`^${basepath}`), '/');
1390
- to = to.replace(new RegExp(`^${basepath}`), '/');
1391
- let baseSegments = parsePathname(base);
1392
- const toSegments = parsePathname(to);
1393
- toSegments.forEach((toSegment, index) => {
1394
- if (toSegment.value === '/') {
1395
- if (!index) {
1396
- // Leading slash
1397
- baseSegments = [toSegment];
1398
- } else if (index === toSegments.length - 1) {
1399
- // Trailing Slash
1400
- baseSegments.push(toSegment);
1401
- } else ;
1402
- } else if (toSegment.value === '..') {
1403
- // Extra trailing slash? pop it off
1404
- if (baseSegments.length > 1 && last(baseSegments)?.value === '/') {
1405
- baseSegments.pop();
1406
- }
1407
- baseSegments.pop();
1408
- } else if (toSegment.value === '.') {
1409
- return;
1410
- } else {
1411
- baseSegments.push(toSegment);
1412
- }
1413
- });
1414
- const joined = joinPaths([basepath, ...baseSegments.map(d => d.value)]);
1415
- return cleanPath(joined);
1416
- }
1417
- function parsePathname(pathname) {
1418
- if (!pathname) {
1419
- return [];
1420
- }
1421
- pathname = cleanPath(pathname);
1422
- const segments = [];
1423
- if (pathname.slice(0, 1) === '/') {
1424
- pathname = pathname.substring(1);
1425
- segments.push({
1426
- type: 'pathname',
1427
- value: '/'
1428
- });
1429
- }
1430
- if (!pathname) {
1431
- return segments;
1432
- }
1433
-
1434
- // Remove empty segments and '.' segments
1435
- const split = pathname.split('/').filter(Boolean);
1436
- segments.push(...split.map(part => {
1437
- if (part === '$' || part === '*') {
1438
- return {
1439
- type: 'wildcard',
1440
- value: part
1441
- };
1442
- }
1443
- if (part.charAt(0) === '$') {
1444
- return {
1445
- type: 'param',
1446
- value: part
1447
- };
1448
- }
1449
- return {
1450
- type: 'pathname',
1451
- value: part
1452
- };
1453
- }));
1454
- if (pathname.slice(-1) === '/') {
1455
- pathname = pathname.substring(1);
1456
- segments.push({
1457
- type: 'pathname',
1458
- value: '/'
1459
- });
1460
- }
1461
- return segments;
1462
- }
1463
- function interpolatePath(path, params, leaveWildcards = false) {
1464
- const interpolatedPathSegments = parsePathname(path);
1465
- return joinPaths(interpolatedPathSegments.map(segment => {
1466
- if (segment.type === 'wildcard') {
1467
- const value = params[segment.value];
1468
- if (leaveWildcards) return `${segment.value}${value ?? ''}`;
1469
- return value;
1470
- }
1471
- if (segment.type === 'param') {
1472
- return params[segment.value.substring(1)] ?? 'undefined';
1473
- }
1474
- return segment.value;
1475
- }));
1476
- }
1477
- function matchPathname(basepath, currentPathname, matchLocation) {
1478
- const pathParams = matchByPath(basepath, currentPathname, matchLocation);
1479
- // const searchMatched = matchBySearch(location.search, matchLocation)
1480
-
1481
- if (matchLocation.to && !pathParams) {
1482
- return;
1483
- }
1484
- return pathParams ?? {};
1485
- }
1486
- function removeBasepath(basepath, pathname) {
1487
- return basepath != '/' ? pathname.substring(basepath.length) : pathname;
1488
- }
1489
- function matchByPath(basepath, from, matchLocation) {
1490
- // Remove the base path from the pathname
1491
- from = removeBasepath(basepath, from);
1492
- // Default to to $ (wildcard)
1493
- const to = `${matchLocation.to ?? '$'}`;
1494
- // Parse the from and to
1495
- const baseSegments = parsePathname(from);
1496
- const routeSegments = parsePathname(to);
1497
- if (!from.startsWith('/')) {
1498
- baseSegments.unshift({
1499
- type: 'pathname',
1500
- value: '/'
1501
- });
1502
- }
1503
- if (!to.startsWith('/')) {
1504
- routeSegments.unshift({
1505
- type: 'pathname',
1506
- value: '/'
1507
- });
1508
- }
1509
- const params = {};
1510
- let isMatch = (() => {
1511
- for (let i = 0; i < Math.max(baseSegments.length, routeSegments.length); i++) {
1512
- const baseSegment = baseSegments[i];
1513
- const routeSegment = routeSegments[i];
1514
- const isLastBaseSegment = i >= baseSegments.length - 1;
1515
- const isLastRouteSegment = i >= routeSegments.length - 1;
1516
- if (routeSegment) {
1517
- if (routeSegment.type === 'wildcard') {
1518
- if (baseSegment?.value) {
1519
- const _splat = joinPaths(baseSegments.slice(i).map(d => d.value));
1520
- // TODO: Deprecate *
1521
- params['*'] = _splat;
1522
- params['_splat'] = _splat;
1523
- return true;
1524
- }
1525
- return false;
1526
- }
1527
- if (routeSegment.type === 'pathname') {
1528
- if (routeSegment.value === '/' && !baseSegment?.value) {
1529
- return true;
1530
- }
1531
- if (baseSegment) {
1532
- if (matchLocation.caseSensitive) {
1533
- if (routeSegment.value !== baseSegment.value) {
1534
- return false;
1535
- }
1536
- } else if (routeSegment.value.toLowerCase() !== baseSegment.value.toLowerCase()) {
1537
- return false;
1538
- }
1539
- }
1540
- }
1541
- if (!baseSegment) {
1542
- return false;
1543
- }
1544
- if (routeSegment.type === 'param') {
1545
- if (baseSegment?.value === '/') {
1546
- return false;
1547
- }
1548
- if (baseSegment.value.charAt(0) !== '$') {
1549
- params[routeSegment.value.substring(1)] = baseSegment.value;
1550
- }
1551
- }
1552
- }
1553
- if (!isLastBaseSegment && isLastRouteSegment) {
1554
- params['**'] = joinPaths(baseSegments.slice(i + 1).map(d => d.value));
1555
- return !!matchLocation.fuzzy && routeSegment?.value !== '/';
1556
- }
1557
- }
1558
- return true;
1559
- })();
1560
- return isMatch ? params : undefined;
1561
- }
1562
-
1563
- // Detect if we're in the DOM
1564
-
1565
- function redirect(opts) {
1566
- opts.isRedirect = true;
1567
- if (opts.throw) {
1568
- throw opts;
1569
- }
1570
- return opts;
1571
- }
1572
- function isRedirect(obj) {
1573
- return !!obj?.isRedirect;
1574
- }
1575
-
1576
- // import warning from 'tiny-warning'
1577
-
1578
- //
1579
-
1580
- const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
1581
- class Router {
1582
- // Option-independent properties
1583
- tempLocationKey = `${Math.round(Math.random() * 10000000)}`;
1584
- resetNextScroll = true;
1585
- navigateTimeout = null;
1586
- latestLoadPromise = Promise.resolve();
1587
- subscribers = new Set();
1588
- injectedHtml = [];
1589
-
1590
- // Must build in constructor
1591
-
1592
- constructor(options) {
1593
- this.update({
1594
- defaultPreloadDelay: 50,
1595
- defaultPendingMs: 1000,
1596
- defaultPendingMinMs: 500,
1597
- context: undefined,
1598
- ...options,
1599
- stringifySearch: options?.stringifySearch ?? defaultStringifySearch,
1600
- parseSearch: options?.parseSearch ?? defaultParseSearch
1601
- });
1602
- }
1603
-
1604
- // These are default implementations that can optionally be overridden
1605
- // by the router provider once rendered. We provide these so that the
1606
- // router can be used in a non-react environment if necessary
1607
- startReactTransition = fn => fn();
1608
- update = newOptions => {
1609
- const previousOptions = this.options;
1610
- this.options = {
1611
- ...this.options,
1612
- ...newOptions
1613
- };
1614
- if (!this.basepath || newOptions.basepath && newOptions.basepath !== previousOptions.basepath) {
1615
- if (newOptions.basepath === undefined || newOptions.basepath === '' || newOptions.basepath === '/') {
1616
- this.basepath = '/';
1617
- } else {
1618
- this.basepath = `/${trimPath(newOptions.basepath)}`;
1619
- }
1620
- }
1621
- if (!this.history || this.options.history && this.options.history !== this.history) {
1622
- this.history = this.options.history ?? (typeof document !== 'undefined' ? createBrowserHistory() : createMemoryHistory({
1623
- initialEntries: [this.options.basepath || '/']
1624
- }));
1625
- this.latestLocation = this.parseLocation();
1626
- }
1627
- if (this.options.routeTree !== this.routeTree) {
1628
- this.routeTree = this.options.routeTree;
1629
- this.buildRouteTree();
1630
- }
1631
- if (!this.__store) {
1632
- this.__store = new Store(getInitialRouterState(this.latestLocation), {
1633
- onUpdate: () => {
1634
- this.__store.state = {
1635
- ...this.state,
1636
- status: this.state.isTransitioning || this.state.isLoading ? 'pending' : 'idle'
1637
- };
1638
- }
1639
- });
1640
- }
1641
- };
1642
- get state() {
1643
- return this.__store.state;
1644
- }
1645
- buildRouteTree = () => {
1646
- this.routesById = {};
1647
- this.routesByPath = {};
1648
- const notFoundRoute = this.options.notFoundRoute;
1649
- if (notFoundRoute) {
1650
- notFoundRoute.init({
1651
- originalIndex: 99999999999
1652
- });
1653
- this.routesById[notFoundRoute.id] = notFoundRoute;
1654
- }
1655
- const recurseRoutes = childRoutes => {
1656
- childRoutes.forEach((childRoute, i) => {
1657
- childRoute.init({
1658
- originalIndex: i
1659
- });
1660
- const existingRoute = this.routesById[childRoute.id];
1661
- invariant(!existingRoute, `Duplicate routes found with id: ${String(childRoute.id)}`);
1662
- this.routesById[childRoute.id] = childRoute;
1663
- if (!childRoute.isRoot && childRoute.path) {
1664
- const trimmedFullPath = trimPathRight(childRoute.fullPath);
1665
- if (!this.routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith('/')) {
1666
- this.routesByPath[trimmedFullPath] = childRoute;
1667
- }
1668
- }
1669
- const children = childRoute.children;
1670
- if (children?.length) {
1671
- recurseRoutes(children);
1672
- }
1673
- });
1674
- };
1675
- recurseRoutes([this.routeTree]);
1676
- const scoredRoutes = [];
1677
- Object.values(this.routesById).forEach((d, i) => {
1678
- if (d.isRoot || !d.path) {
1679
- return;
1680
- }
1681
- const trimmed = trimPathLeft(d.fullPath);
1682
- const parsed = parsePathname(trimmed);
1683
- while (parsed.length > 1 && parsed[0]?.value === '/') {
1684
- parsed.shift();
1685
- }
1686
- const scores = parsed.map(d => {
1687
- if (d.value === '/') {
1688
- return 0.75;
1689
- }
1690
- if (d.type === 'param') {
1691
- return 0.5;
1692
- }
1693
- if (d.type === 'wildcard') {
1694
- return 0.25;
1695
- }
1696
- return 1;
1697
- });
1698
- scoredRoutes.push({
1699
- child: d,
1700
- trimmed,
1701
- parsed,
1702
- index: i,
1703
- scores
1704
- });
1705
- });
1706
- this.flatRoutes = scoredRoutes.sort((a, b) => {
1707
- const minLength = Math.min(a.scores.length, b.scores.length);
1708
-
1709
- // Sort by min available score
1710
- for (let i = 0; i < minLength; i++) {
1711
- if (a.scores[i] !== b.scores[i]) {
1712
- return b.scores[i] - a.scores[i];
1713
- }
1714
- }
1715
-
1716
- // Sort by length of score
1717
- if (a.scores.length !== b.scores.length) {
1718
- return b.scores.length - a.scores.length;
1719
- }
1720
-
1721
- // Sort by min available parsed value
1722
- for (let i = 0; i < minLength; i++) {
1723
- if (a.parsed[i].value !== b.parsed[i].value) {
1724
- return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
1725
- }
1726
- }
1727
-
1728
- // Sort by original index
1729
- return a.index - b.index;
1730
- }).map((d, i) => {
1731
- d.child.rank = i;
1732
- return d.child;
1733
- });
1734
- };
1735
- subscribe = (eventType, fn) => {
1736
- const listener = {
1737
- eventType,
1738
- fn
1739
- };
1740
- this.subscribers.add(listener);
1741
- return () => {
1742
- this.subscribers.delete(listener);
1743
- };
1744
- };
1745
- emit = routerEvent => {
1746
- this.subscribers.forEach(listener => {
1747
- if (listener.eventType === routerEvent.type) {
1748
- listener.fn(routerEvent);
1749
- }
1750
- });
1751
- };
1752
- checkLatest = promise => {
1753
- return this.latestLoadPromise !== promise ? this.latestLoadPromise : undefined;
1754
- };
1755
- parseLocation = previousLocation => {
1756
- const parse = ({
1757
- pathname,
1758
- search,
1759
- hash,
1760
- state
1761
- }) => {
1762
- const parsedSearch = this.options.parseSearch(search);
1763
- return {
1764
- pathname: pathname,
1765
- searchStr: search,
1766
- search: replaceEqualDeep(previousLocation?.search, parsedSearch),
1767
- hash: hash.split('#').reverse()[0] ?? '',
1768
- href: `${pathname}${search}${hash}`,
1769
- state: replaceEqualDeep(previousLocation?.state, state)
1770
- };
1771
- };
1772
- const location = parse(this.history.location);
1773
- let {
1774
- __tempLocation,
1775
- __tempKey
1776
- } = location.state;
1777
- if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) {
1778
- // Sync up the location keys
1779
- const parsedTempLocation = parse(__tempLocation);
1780
- parsedTempLocation.state.key = location.state.key;
1781
- delete parsedTempLocation.state.__tempLocation;
1782
- return {
1783
- ...parsedTempLocation,
1784
- maskedLocation: location
1785
- };
1786
- }
1787
- return location;
1788
- };
1789
- resolvePathWithBase = (from, path) => {
1790
- return resolvePath(this.basepath, from, cleanPath(path));
1791
- };
1792
- get looseRoutesById() {
1793
- return this.routesById;
1794
- }
1795
- matchRoutes = (pathname, locationSearch, opts) => {
1796
- let routeParams = {};
1797
- let foundRoute = this.flatRoutes.find(route => {
1798
- const matchedParams = matchPathname(this.basepath, trimPathRight(pathname), {
1799
- to: route.fullPath,
1800
- caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
1801
- fuzzy: true
1802
- });
1803
- if (matchedParams) {
1804
- routeParams = matchedParams;
1805
- return true;
1806
- }
1807
- return false;
1808
- });
1809
- let routeCursor = foundRoute || this.routesById['__root__'];
1810
- let matchedRoutes = [routeCursor];
1811
-
1812
- // Check to see if the route needs a 404 entry
1813
- if (
1814
- // If we found a route, and it's not an index route and we have left over path
1815
- (foundRoute ? foundRoute.path !== '/' && routeParams['**'] :
1816
- // Or if we didn't find a route and we have left over path
1817
- trimPathRight(pathname)) &&
1818
- // And we have a 404 route configured
1819
- this.options.notFoundRoute) {
1820
- matchedRoutes.push(this.options.notFoundRoute);
1821
- }
1822
- while (routeCursor?.parentRoute) {
1823
- routeCursor = routeCursor.parentRoute;
1824
- if (routeCursor) matchedRoutes.unshift(routeCursor);
1825
- }
1826
-
1827
- // Existing matches are matches that are already loaded along with
1828
- // pending matches that are still loading
1829
-
1830
- const parseErrors = matchedRoutes.map(route => {
1831
- let parsedParamsError;
1832
- if (route.options.parseParams) {
1833
- try {
1834
- const parsedParams = route.options.parseParams(routeParams);
1835
- // Add the parsed params to the accumulated params bag
1836
- Object.assign(routeParams, parsedParams);
1837
- } catch (err) {
1838
- parsedParamsError = new PathParamError(err.message, {
1839
- cause: err
1840
- });
1841
- if (opts?.throwOnError) {
1842
- throw parsedParamsError;
1843
- }
1844
- return parsedParamsError;
1845
- }
1846
- }
1847
- return;
1848
- });
1849
- const matches = [];
1850
- matchedRoutes.forEach((route, index) => {
1851
- // Take each matched route and resolve + validate its search params
1852
- // This has to happen serially because each route's search params
1853
- // can depend on the parent route's search params
1854
- // It must also happen before we create the match so that we can
1855
- // pass the search params to the route's potential key function
1856
- // which is used to uniquely identify the route match in state
1857
-
1858
- const parentMatch = matches[index - 1];
1859
- const [preMatchSearch, searchError] = (() => {
1860
- // Validate the search params and stabilize them
1861
- const parentSearch = parentMatch?.search ?? locationSearch;
1862
- try {
1863
- const validator = typeof route.options.validateSearch === 'object' ? route.options.validateSearch.parse : route.options.validateSearch;
1864
- let search = validator?.(parentSearch) ?? {};
1865
- return [{
1866
- ...parentSearch,
1867
- ...search
1868
- }, undefined];
1869
- } catch (err) {
1870
- const searchError = new SearchParamError(err.message, {
1871
- cause: err
1872
- });
1873
- if (opts?.throwOnError) {
1874
- throw searchError;
1875
- }
1876
- return [parentSearch, searchError];
1877
- }
1878
- })();
1879
-
1880
- // This is where we need to call route.options.loaderDeps() to get any additional
1881
- // deps that the route's loader function might need to run. We need to do this
1882
- // before we create the match so that we can pass the deps to the route's
1883
- // potential key function which is used to uniquely identify the route match in state
1884
-
1885
- const loaderDeps = route.options.loaderDeps?.({
1886
- search: preMatchSearch
1887
- }) ?? '';
1888
- const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : '';
1889
- const interpolatedPath = interpolatePath(route.fullPath, routeParams);
1890
- const matchId = interpolatePath(route.id, routeParams, true) + loaderDepsHash;
1891
-
1892
- // Waste not, want not. If we already have a match for this route,
1893
- // reuse it. This is important for layout routes, which might stick
1894
- // around between navigation actions that only change leaf routes.
1895
- const existingMatch = getRouteMatch(this.state, matchId);
1896
- const cause = this.state.matches.find(d => d.id === matchId) ? 'stay' : 'enter';
1897
-
1898
- // Create a fresh route match
1899
- const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload));
1900
- const match = existingMatch ? {
1901
- ...existingMatch,
1902
- cause
1903
- } : {
1904
- id: matchId,
1905
- routeId: route.id,
1906
- params: routeParams,
1907
- pathname: joinPaths([this.basepath, interpolatedPath]),
1908
- updatedAt: Date.now(),
1909
- search: {},
1910
- searchError: undefined,
1911
- status: hasLoaders ? 'pending' : 'success',
1912
- showPending: false,
1913
- isFetching: false,
1914
- error: undefined,
1915
- paramsError: parseErrors[index],
1916
- loadPromise: Promise.resolve(),
1917
- routeContext: undefined,
1918
- context: undefined,
1919
- abortController: new AbortController(),
1920
- fetchCount: 0,
1921
- cause,
1922
- loaderDeps,
1923
- invalid: false,
1924
- preload: false
1925
- };
1926
-
1927
- // Regardless of whether we're reusing an existing match or creating
1928
- // a new one, we need to update the match's search params
1929
- match.search = replaceEqualDeep(match.search, preMatchSearch);
1930
- // And also update the searchError if there is one
1931
- match.searchError = searchError;
1932
- matches.push(match);
1933
- });
1934
- return matches;
1935
- };
1936
- cancelMatch = id => {
1937
- getRouteMatch(this.state, id)?.abortController?.abort();
1938
- };
1939
- cancelMatches = () => {
1940
- this.state.pendingMatches?.forEach(match => {
1941
- this.cancelMatch(match.id);
1942
- });
1943
- };
1944
- buildLocation = opts => {
1945
- const build = (dest = {}, matches) => {
1946
- const relevantMatches = this.state.pendingMatches || this.state.matches;
1947
- const fromSearch = relevantMatches[relevantMatches.length - 1]?.search || this.latestLocation.search;
1948
- let pathname = this.resolvePathWithBase(dest.from ?? this.latestLocation.pathname, `${dest.to ?? ''}`);
1949
- const fromMatches = this.matchRoutes(this.latestLocation.pathname, fromSearch);
1950
- const stayingMatches = matches?.filter(d => fromMatches?.find(e => e.routeId === d.routeId));
1951
- const prevParams = {
1952
- ...last(fromMatches)?.params
1953
- };
1954
- let nextParams = (dest.params ?? true) === true ? prevParams : functionalUpdate(dest.params, prevParams);
1955
- if (nextParams) {
1956
- matches?.map(d => this.looseRoutesById[d.routeId].options.stringifyParams).filter(Boolean).forEach(fn => {
1957
- nextParams = {
1958
- ...nextParams,
1959
- ...fn(nextParams)
1960
- };
1961
- });
1962
- }
1963
- pathname = interpolatePath(pathname, nextParams ?? {});
1964
- const preSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.preSearchFilters ?? []).flat().filter(Boolean) ?? [];
1965
- const postSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.postSearchFilters ?? []).flat().filter(Boolean) ?? [];
1966
-
1967
- // Pre filters first
1968
- const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), fromSearch) : fromSearch;
1969
-
1970
- // Then the link/navigate function
1971
- const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
1972
- : dest.search ? functionalUpdate(dest.search, preFilteredSearch) ?? {} // Updater
1973
- : preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters
1974
- : {};
1975
-
1976
- // Then post filters
1977
- const postFilteredSearch = postSearchFilters?.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
1978
- const search = replaceEqualDeep(fromSearch, postFilteredSearch);
1979
- const searchStr = this.options.stringifySearch(search);
1980
- const hash = dest.hash === true ? this.latestLocation.hash : dest.hash ? functionalUpdate(dest.hash, this.latestLocation.hash) : undefined;
1981
- const hashStr = hash ? `#${hash}` : '';
1982
- let nextState = dest.state === true ? this.latestLocation.state : dest.state ? functionalUpdate(dest.state, this.latestLocation.state) : this.latestLocation.state;
1983
- nextState = replaceEqualDeep(this.latestLocation.state, nextState);
1984
- return {
1985
- pathname,
1986
- search,
1987
- searchStr,
1988
- state: nextState,
1989
- hash: hash ?? '',
1990
- href: `${pathname}${searchStr}${hashStr}`,
1991
- unmaskOnReload: dest.unmaskOnReload
1992
- };
1993
- };
1994
- const buildWithMatches = (dest = {}, maskedDest) => {
1995
- let next = build(dest);
1996
- let maskedNext = maskedDest ? build(maskedDest) : undefined;
1997
- if (!maskedNext) {
1998
- let params = {};
1999
- let foundMask = this.options.routeMasks?.find(d => {
2000
- const match = matchPathname(this.basepath, next.pathname, {
2001
- to: d.from,
2002
- caseSensitive: false,
2003
- fuzzy: false
2004
- });
2005
- if (match) {
2006
- params = match;
2007
- return true;
2008
- }
2009
- return false;
2010
- });
2011
- if (foundMask) {
2012
- maskedDest = {
2013
- ...pick(opts, ['from']),
2014
- ...foundMask,
2015
- params
2016
- };
2017
- maskedNext = build(maskedDest);
2018
- }
2019
- }
2020
- const nextMatches = this.matchRoutes(next.pathname, next.search);
2021
- const maskedMatches = maskedNext ? this.matchRoutes(maskedNext.pathname, maskedNext.search) : undefined;
2022
- const maskedFinal = maskedNext ? build(maskedDest, maskedMatches) : undefined;
2023
- const final = build(dest, nextMatches);
2024
- if (maskedFinal) {
2025
- final.maskedLocation = maskedFinal;
2026
- }
2027
- return final;
2028
- };
2029
- if (opts.mask) {
2030
- return buildWithMatches(opts, {
2031
- ...pick(opts, ['from']),
2032
- ...opts.mask
2033
- });
2034
- }
2035
- return buildWithMatches(opts);
2036
- };
2037
- commitLocation = async ({
2038
- startTransition,
2039
- ...next
2040
- }) => {
2041
- if (this.navigateTimeout) clearTimeout(this.navigateTimeout);
2042
- const isSameUrl = this.latestLocation.href === next.href;
2043
-
2044
- // If the next urls are the same and we're not replacing,
2045
- // do nothing
2046
- if (!isSameUrl || !next.replace) {
2047
- let {
2048
- maskedLocation,
2049
- ...nextHistory
2050
- } = next;
2051
- if (maskedLocation) {
2052
- nextHistory = {
2053
- ...maskedLocation,
2054
- state: {
2055
- ...maskedLocation.state,
2056
- __tempKey: undefined,
2057
- __tempLocation: {
2058
- ...nextHistory,
2059
- search: nextHistory.searchStr,
2060
- state: {
2061
- ...nextHistory.state,
2062
- __tempKey: undefined,
2063
- __tempLocation: undefined,
2064
- key: undefined
2065
- }
2066
- }
2067
- }
2068
- };
2069
- if (nextHistory.unmaskOnReload ?? this.options.unmaskOnReload ?? false) {
2070
- nextHistory.state.__tempKey = this.tempLocationKey;
2071
- }
2072
- }
2073
- const apply = () => {
2074
- this.history[next.replace ? 'replace' : 'push'](nextHistory.href, nextHistory.state);
2075
- };
2076
- if (startTransition ?? true) {
2077
- this.startReactTransition(apply);
2078
- } else {
2079
- apply();
2080
- }
2081
- }
2082
- this.resetNextScroll = next.resetScroll ?? true;
2083
- return this.latestLoadPromise;
2084
- };
2085
- buildAndCommitLocation = ({
2086
- replace,
2087
- resetScroll,
2088
- startTransition,
2089
- ...rest
2090
- } = {}) => {
2091
- const location = this.buildLocation(rest);
2092
- return this.commitLocation({
2093
- ...location,
2094
- startTransition,
2095
- replace,
2096
- resetScroll
2097
- });
2098
- };
2099
- navigate = ({
2100
- from,
2101
- to,
2102
- ...rest
2103
- }) => {
2104
- // If this link simply reloads the current route,
2105
- // make sure it has a new key so it will trigger a data refresh
2106
-
2107
- // If this `to` is a valid external URL, return
2108
- // null for LinkUtils
2109
- const toString = String(to);
2110
- // const fromString = from !== undefined ? String(from) : from
2111
- let isExternal;
2112
- try {
2113
- new URL(`${toString}`);
2114
- isExternal = true;
2115
- } catch (e) {}
2116
- invariant(!isExternal, 'Attempting to navigate to external url with this.navigate!');
2117
- return this.buildAndCommitLocation({
2118
- ...rest,
2119
- from,
2120
- to
2121
- // to: toString,
2122
- });
2123
- };
2124
- loadMatches = async ({
2125
- checkLatest,
2126
- matches,
2127
- preload
2128
- }) => {
2129
- let latestPromise;
2130
- let firstBadMatchIndex;
2131
- const updateMatch = match => {
2132
- // const isPreload = this.state.cachedMatches.find((d) => d.id === match.id)
2133
- const isPending = this.state.pendingMatches?.find(d => d.id === match.id);
2134
- const isMatched = this.state.matches.find(d => d.id === match.id);
2135
- const matchesKey = isPending ? 'pendingMatches' : isMatched ? 'matches' : 'cachedMatches';
2136
- this.__store.setState(s => ({
2137
- ...s,
2138
- [matchesKey]: s[matchesKey]?.map(d => d.id === match.id ? match : d)
2139
- }));
2140
- };
2141
-
2142
- // Check each match middleware to see if the route can be accessed
2143
- try {
2144
- for (let [index, match] of matches.entries()) {
2145
- const parentMatch = matches[index - 1];
2146
- const route = this.looseRoutesById[match.routeId];
2147
- const abortController = new AbortController();
2148
- const handleErrorAndRedirect = (err, code) => {
2149
- err.routerCode = code;
2150
- firstBadMatchIndex = firstBadMatchIndex ?? index;
2151
- if (isRedirect(err)) {
2152
- throw err;
2153
- }
2154
- try {
2155
- route.options.onError?.(err);
2156
- } catch (errorHandlerErr) {
2157
- err = errorHandlerErr;
2158
- if (isRedirect(errorHandlerErr)) {
2159
- throw errorHandlerErr;
2160
- }
2161
- }
2162
- matches[index] = match = {
2163
- ...match,
2164
- error: err,
2165
- status: 'error',
2166
- updatedAt: Date.now(),
2167
- abortController: new AbortController()
2168
- };
2169
- };
2170
- try {
2171
- if (match.paramsError) {
2172
- handleErrorAndRedirect(match.paramsError, 'PARSE_PARAMS');
2173
- }
2174
- if (match.searchError) {
2175
- handleErrorAndRedirect(match.searchError, 'VALIDATE_SEARCH');
2176
- }
2177
- const parentContext = parentMatch?.context ?? this.options.context ?? {};
2178
- const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
2179
- const pendingPromise = typeof pendingMs === 'number' && pendingMs <= 0 ? Promise.resolve() : new Promise(r => setTimeout(r, pendingMs));
2180
- const beforeLoadContext = (await route.options.beforeLoad?.({
2181
- search: match.search,
2182
- abortController,
2183
- params: match.params,
2184
- preload: !!preload,
2185
- context: parentContext,
2186
- location: this.state.location,
2187
- // TOOD: just expose state and router, etc
2188
- navigate: opts => this.navigate({
2189
- ...opts,
2190
- from: match.pathname
2191
- }),
2192
- buildLocation: this.buildLocation,
2193
- cause: preload ? 'preload' : match.cause
2194
- })) ?? {};
2195
- if (isRedirect(beforeLoadContext)) {
2196
- throw beforeLoadContext;
2197
- }
2198
- const context = {
2199
- ...parentContext,
2200
- ...beforeLoadContext
2201
- };
2202
- matches[index] = match = {
2203
- ...match,
2204
- routeContext: replaceEqualDeep(match.routeContext, beforeLoadContext),
2205
- context: replaceEqualDeep(match.context, context),
2206
- abortController,
2207
- pendingPromise
2208
- };
2209
- } catch (err) {
2210
- handleErrorAndRedirect(err, 'BEFORE_LOAD');
2211
- break;
2212
- }
2213
- }
2214
- } catch (err) {
2215
- if (isRedirect(err)) {
2216
- if (!preload) this.navigate(err);
2217
- return matches;
2218
- }
2219
- throw err;
2220
- }
2221
- const validResolvedMatches = matches.slice(0, firstBadMatchIndex);
2222
- const matchPromises = [];
2223
- validResolvedMatches.forEach((match, index) => {
2224
- matchPromises.push(new Promise(async resolve => {
2225
- const parentMatchPromise = matchPromises[index - 1];
2226
- const route = this.looseRoutesById[match.routeId];
2227
- const handleErrorAndRedirect = err => {
2228
- if (isRedirect(err)) {
2229
- if (!preload) {
2230
- this.navigate(err);
2231
- }
2232
- return true;
2233
- }
2234
- return false;
2235
- };
2236
- let loadPromise;
2237
- matches[index] = match = {
2238
- ...match,
2239
- showPending: false
2240
- };
2241
- let didShowPending = false;
2242
- const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
2243
- const pendingMinMs = route.options.pendingMinMs ?? this.options.defaultPendingMinMs;
2244
- const shouldPending = !preload && typeof pendingMs === 'number' && (route.options.pendingComponent ?? this.options.defaultPendingComponent);
2245
- const loaderContext = {
2246
- params: match.params,
2247
- deps: match.loaderDeps,
2248
- preload: !!preload,
2249
- parentMatchPromise,
2250
- abortController: match.abortController,
2251
- context: match.context,
2252
- location: this.state.location,
2253
- navigate: opts => this.navigate({
2254
- ...opts,
2255
- from: match.pathname
2256
- }),
2257
- cause: preload ? 'preload' : match.cause
2258
- };
2259
- const fetch = async () => {
2260
- if (match.isFetching) {
2261
- loadPromise = getRouteMatch(this.state, match.id)?.loadPromise;
2262
- } else {
2263
- // If the user doesn't want the route to reload, just
2264
- // resolve with the existing loader data
2265
-
2266
- if (match.fetchCount && match.status === 'success') {
2267
- resolve();
2268
- }
2269
-
2270
- // Otherwise, load the route
2271
- matches[index] = match = {
2272
- ...match,
2273
- isFetching: true,
2274
- fetchCount: match.fetchCount + 1
2275
- };
2276
- const componentsPromise = Promise.all(componentTypes.map(async type => {
2277
- const component = route.options[type];
2278
- if (component?.preload) {
2279
- await component.preload();
2280
- }
2281
- }));
2282
- const loaderPromise = route.options.loader?.(loaderContext);
2283
- loadPromise = Promise.all([componentsPromise, loaderPromise]).then(d => d[1]);
2284
- }
2285
- matches[index] = match = {
2286
- ...match,
2287
- loadPromise
2288
- };
2289
- updateMatch(match);
2290
- try {
2291
- const loaderData = await loadPromise;
2292
- if (latestPromise = checkLatest()) return await latestPromise;
2293
- if (isRedirect(loaderData)) {
2294
- if (handleErrorAndRedirect(loaderData)) return;
2295
- }
2296
- if (didShowPending && pendingMinMs) {
2297
- await new Promise(r => setTimeout(r, pendingMinMs));
2298
- }
2299
- if (latestPromise = checkLatest()) return await latestPromise;
2300
- matches[index] = match = {
2301
- ...match,
2302
- error: undefined,
2303
- status: 'success',
2304
- isFetching: false,
2305
- updatedAt: Date.now(),
2306
- loaderData,
2307
- loadPromise: undefined
2308
- };
2309
- } catch (error) {
2310
- if (latestPromise = checkLatest()) return await latestPromise;
2311
- if (handleErrorAndRedirect(error)) return;
2312
- try {
2313
- route.options.onError?.(error);
2314
- } catch (onErrorError) {
2315
- error = onErrorError;
2316
- if (handleErrorAndRedirect(onErrorError)) return;
2317
- }
2318
- matches[index] = match = {
2319
- ...match,
2320
- error,
2321
- status: 'error',
2322
- isFetching: false
2323
- };
2324
- }
2325
- updateMatch(match);
2326
- };
2327
-
2328
- // This is where all of the stale-while-revalidate magic happens
2329
- const age = Date.now() - match.updatedAt;
2330
- let staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 30_000 // 30 seconds for preloads by default
2331
- : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
2332
-
2333
- // Default to reloading the route all the time
2334
- let shouldReload;
2335
- const shouldReloadOption = route.options.shouldReload;
2336
-
2337
- // Allow shouldReload to get the last say,
2338
- // if provided.
2339
- shouldReload = typeof shouldReloadOption === 'function' ? shouldReloadOption(loaderContext) : shouldReloadOption;
2340
- matches[index] = match = {
2341
- ...match,
2342
- preload: !!preload && !this.state.matches.find(d => d.id === match.id)
2343
- };
2344
- if (match.status !== 'success') {
2345
- // If we need to potentially show the pending component,
2346
- // start a timer to show it after the pendingMs
2347
- if (shouldPending) {
2348
- match.pendingPromise?.then(async () => {
2349
- if (latestPromise = checkLatest()) return latestPromise;
2350
- didShowPending = true;
2351
- matches[index] = match = {
2352
- ...match,
2353
- showPending: true
2354
- };
2355
- updateMatch(match);
2356
- resolve();
2357
- });
2358
- }
2359
-
2360
- // Critical Fetching, we need to await
2361
- await fetch();
2362
- } else if (match.invalid || (shouldReload ?? age > staleAge)) {
2363
- // Background Fetching, no need to wait
2364
- fetch();
2365
- }
2366
- resolve();
2367
- }));
2368
- });
2369
- await Promise.all(matchPromises);
2370
- return matches;
2371
- };
2372
- invalidate = () => {
2373
- const invalidate = d => ({
2374
- ...d,
2375
- invalid: true
2376
- });
2377
- this.__store.setState(s => ({
2378
- ...s,
2379
- matches: s.matches.map(invalidate),
2380
- cachedMatches: s.cachedMatches.map(invalidate),
2381
- pendingMatches: s.pendingMatches?.map(invalidate)
2382
- }));
2383
- this.load();
2384
- };
2385
- load = async () => {
2386
- const promise = new Promise(async (resolve, reject) => {
2387
- const next = this.latestLocation;
2388
- const prevLocation = this.state.resolvedLocation;
2389
- const pathDidChange = prevLocation.href !== next.href;
2390
- let latestPromise;
2391
-
2392
- // Cancel any pending matches
2393
- this.cancelMatches();
2394
- this.emit({
2395
- type: 'onBeforeLoad',
2396
- fromLocation: prevLocation,
2397
- toLocation: next,
2398
- pathChanged: pathDidChange
2399
- });
2400
- let pendingMatches;
2401
- const previousMatches = this.state.matches;
2402
- this.__store.batch(() => {
2403
- this.cleanCache();
2404
-
2405
- // Match the routes
2406
- pendingMatches = this.matchRoutes(next.pathname, next.search, {
2407
- debug: true
2408
- });
2409
-
2410
- // Ingest the new matches
2411
- // If a cached moved to pendingMatches, remove it from cachedMatches
2412
- this.__store.setState(s => ({
2413
- ...s,
2414
- isLoading: true,
2415
- location: next,
2416
- pendingMatches,
2417
- cachedMatches: s.cachedMatches.filter(d => {
2418
- return !pendingMatches.find(e => e.id === d.id);
2419
- })
2420
- }));
2421
- });
2422
- try {
2423
- try {
2424
- // Load the matches
2425
- await this.loadMatches({
2426
- matches: pendingMatches,
2427
- checkLatest: () => this.checkLatest(promise)
2428
- });
2429
- } catch (err) {
2430
- // swallow this error, since we'll display the
2431
- // errors on the route components
2432
- }
2433
-
2434
- // Only apply the latest transition
2435
- if (latestPromise = this.checkLatest(promise)) {
2436
- return latestPromise;
2437
- }
2438
- const exitingMatches = previousMatches.filter(match => !pendingMatches.find(d => d.id === match.id));
2439
- const enteringMatches = pendingMatches.filter(match => !previousMatches.find(d => d.id === match.id));
2440
- const stayingMatches = previousMatches.filter(match => pendingMatches.find(d => d.id === match.id));
2441
-
2442
- // Commit the pending matches. If a previous match was
2443
- // removed, place it in the cachedMatches
2444
- this.__store.batch(() => {
2445
- this.__store.setState(s => ({
2446
- ...s,
2447
- isLoading: false,
2448
- matches: s.pendingMatches,
2449
- pendingMatches: undefined,
2450
- cachedMatches: [...s.cachedMatches, ...exitingMatches.filter(d => d.status !== 'error')]
2451
- }));
2452
- this.cleanCache();
2453
- })
2454
-
2455
- //
2456
- ;
2457
- [[exitingMatches, 'onLeave'], [enteringMatches, 'onEnter'], [stayingMatches, 'onStay']].forEach(([matches, hook]) => {
2458
- matches.forEach(match => {
2459
- this.looseRoutesById[match.routeId].options[hook]?.(match);
2460
- });
2461
- });
2462
- this.emit({
2463
- type: 'onLoad',
2464
- fromLocation: prevLocation,
2465
- toLocation: next,
2466
- pathChanged: pathDidChange
2467
- });
2468
- resolve();
2469
- } catch (err) {
2470
- // Only apply the latest transition
2471
- if (latestPromise = this.checkLatest(promise)) {
2472
- return latestPromise;
2473
- }
2474
- reject(err);
2475
- }
2476
- });
2477
- this.latestLoadPromise = promise;
2478
- return this.latestLoadPromise;
2479
- };
2480
- cleanCache = () => {
2481
- // This is where all of the garbage collection magic happens
2482
- this.__store.setState(s => {
2483
- return {
2484
- ...s,
2485
- cachedMatches: s.cachedMatches.filter(d => {
2486
- const route = this.looseRoutesById[d.routeId];
2487
- if (!route.options.loader) {
2488
- return false;
2489
- }
2490
-
2491
- // If the route was preloaded, use the preloadGcTime
2492
- // otherwise, use the gcTime
2493
- const gcTime = (d.preload ? route.options.preloadGcTime ?? this.options.defaultPreloadGcTime : route.options.gcTime ?? this.options.defaultGcTime) ?? 5 * 60 * 1000;
2494
- return d.status !== 'error' && Date.now() - d.updatedAt < gcTime;
2495
- })
2496
- };
2497
- });
2498
- };
2499
- preloadRoute = async (navigateOpts = this.state.location) => {
2500
- let next = this.buildLocation(navigateOpts);
2501
- let matches = this.matchRoutes(next.pathname, next.search, {
2502
- throwOnError: true
2503
- });
2504
- const loadedMatchIds = Object.fromEntries([...this.state.matches, ...(this.state.pendingMatches ?? []), ...this.state.cachedMatches]?.map(d => [d.id, true]));
2505
- this.__store.batch(() => {
2506
- matches.forEach(match => {
2507
- if (!loadedMatchIds[match.id]) {
2508
- this.__store.setState(s => ({
2509
- ...s,
2510
- cachedMatches: [...s.cachedMatches, match]
2511
- }));
2512
- }
2513
- });
2514
- });
2515
- matches = await this.loadMatches({
2516
- matches,
2517
- preload: true,
2518
- checkLatest: () => undefined
2519
- });
2520
- return matches;
2521
- };
2522
- matchRoute = (location, opts) => {
2523
- location = {
2524
- ...location,
2525
- to: location.to ? this.resolvePathWithBase(location.from || '', location.to) : undefined
2526
- };
2527
- const next = this.buildLocation(location);
2528
- if (opts?.pending && this.state.status !== 'pending') {
2529
- return false;
2530
- }
2531
- const baseLocation = opts?.pending ? this.latestLocation : this.state.resolvedLocation;
2532
- if (!baseLocation) {
2533
- return false;
2534
- }
2535
- const match = matchPathname(this.basepath, baseLocation.pathname, {
2536
- ...opts,
2537
- to: next.pathname
2538
- });
2539
- if (!match) {
2540
- return false;
2541
- }
2542
- if (match && (opts?.includeSearch ?? true)) {
2543
- return deepEqual(baseLocation.search, next.search, true) ? match : false;
2544
- }
2545
- return match;
2546
- };
2547
- injectHtml = async html => {
2548
- this.injectedHtml.push(html);
2549
- };
2550
- dehydrateData = (key, getData) => {
2551
- if (typeof document === 'undefined') {
2552
- const strKey = typeof key === 'string' ? key : JSON.stringify(key);
2553
- this.injectHtml(async () => {
2554
- const id = `__TSR_DEHYDRATED__${strKey}`;
2555
- const data = typeof getData === 'function' ? await getData() : getData;
2556
- return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${escapeJSON(strKey)}"] = ${JSON.stringify(data)}
2557
- ;(() => {
2558
- var el = document.getElementById('${id}')
2559
- el.parentElement.removeChild(el)
2560
- })()
2561
- </script>`;
2562
- });
2563
- return () => this.hydrateData(key);
2564
- }
2565
- return () => undefined;
2566
- };
2567
- hydrateData = key => {
2568
- if (typeof document !== 'undefined') {
2569
- const strKey = typeof key === 'string' ? key : JSON.stringify(key);
2570
- return window[`__TSR_DEHYDRATED__${strKey}`];
2571
- }
2572
- return undefined;
2573
- };
2574
- dehydrate = () => {
2575
- const pickError = this.options.errorSerializer?.serialize ?? defaultSerializeError;
2576
- return {
2577
- state: {
2578
- dehydratedMatches: this.state.matches.map(d => ({
2579
- ...pick(d, ['id', 'status', 'updatedAt', 'loaderData']),
2580
- // If an error occurs server-side during SSRing,
2581
- // send a small subset of the error to the client
2582
- error: d.error ? {
2583
- data: pickError(d.error),
2584
- __isServerError: true
2585
- } : undefined
2586
- }))
2587
- }
2588
- };
2589
- };
2590
- hydrate = async __do_not_use_server_ctx => {
2591
- let _ctx = __do_not_use_server_ctx;
2592
- // Client hydrates from window
2593
- if (typeof document !== 'undefined') {
2594
- _ctx = window.__TSR_DEHYDRATED__;
2595
- }
2596
- invariant(_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?');
2597
- const ctx = _ctx;
2598
- this.dehydratedData = ctx.payload;
2599
- this.options.hydrate?.(ctx.payload);
2600
- const dehydratedState = ctx.router.state;
2601
- let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
2602
- const dehydratedMatch = dehydratedState.dehydratedMatches.find(d => d.id === match.id);
2603
- invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
2604
- if (dehydratedMatch) {
2605
- return {
2606
- ...match,
2607
- ...dehydratedMatch
2608
- };
2609
- }
2610
- return match;
2611
- });
2612
- this.__store.setState(s => {
2613
- return {
2614
- ...s,
2615
- matches: matches
2616
- };
2617
- });
2618
- };
2619
-
2620
- // resolveMatchPromise = (matchId: string, key: string, value: any) => {
2621
- // state.matches
2622
- // .find((d) => d.id === matchId)
2623
- // ?.__promisesByKey[key]?.resolve(value)
2624
- // }
2625
- }
2626
-
2627
- // A function that takes an import() argument which is a function and returns a new function that will
2628
- // proxy arguments from the caller to the imported function, retaining all type
2629
- // information along the way
2630
- function lazyFn(fn, key) {
2631
- return async (...args) => {
2632
- const imported = await fn();
2633
- return imported[key || 'default'](...args);
2634
- };
2635
- }
2636
- class SearchParamError extends Error {}
2637
- class PathParamError extends Error {}
2638
- function getInitialRouterState(location) {
2639
- return {
2640
- isLoading: false,
2641
- isTransitioning: false,
2642
- status: 'idle',
2643
- resolvedLocation: {
2644
- ...location
2645
- },
2646
- location,
2647
- matches: [],
2648
- pendingMatches: [],
2649
- cachedMatches: [],
2650
- lastUpdated: Date.now()
2651
- };
2652
- }
2653
- function defaultSerializeError(err) {
2654
- if (err instanceof Error) return {
2655
- name: err.name,
2656
- message: err.message
2657
- };
2658
- return {
2659
- data: err
2660
- };
2661
- }
2662
-
2663
- function defer(_promise, options) {
2664
- const promise = _promise;
2665
- if (!promise.__deferredState) {
2666
- promise.__deferredState = {
2667
- uid: Math.random().toString(36).slice(2),
2668
- status: 'pending'
2669
- };
2670
- const state = promise.__deferredState;
2671
- promise.then(data => {
2672
- state.status = 'success';
2673
- state.data = data;
2674
- }).catch(error => {
2675
- state.status = 'error';
2676
- state.error = {
2677
- data: (options?.serializeError ?? defaultSerializeError)(error),
2678
- __isServerError: true
2679
- };
2680
- });
2681
- }
2682
- return promise;
2683
- }
2684
- function isDehydratedDeferred(obj) {
2685
- return typeof obj === 'object' && obj !== null && !(obj instanceof Promise) && !obj.then && '__deferredState' in obj;
2686
- }
2687
-
2688
- function useAwaited({
2689
- promise
2690
- }) {
2691
- const router = useRouter();
2692
- let state = promise.__deferredState;
2693
- const key = `__TSR__DEFERRED__${state.uid}`;
2694
- if (isDehydratedDeferred(promise)) {
2695
- state = router.hydrateData(key);
2696
- if (!state) throw new Error('Could not find dehydrated data');
2697
- promise = Promise.resolve(state.data);
2698
- promise.__deferredState = state;
2699
- }
2700
- if (state.status === 'pending') {
2701
- throw promise;
2702
- }
2703
- if (state.status === 'error') {
2704
- if (typeof document !== 'undefined') {
2705
- if (isServerSideError(state.error)) {
2706
- throw (router.options.errorSerializer?.deserialize ?? defaultDeserializeError)(state.error.data);
2707
- } else {
2708
- warning(false, "Encountered a server-side error that doesn't fit the expected shape");
2709
- throw state.error;
2710
- }
2711
- } else {
2712
- router.dehydrateData(key, state);
2713
- throw {
2714
- data: (router.options.errorSerializer?.serialize ?? defaultSerializeError)(state.error),
2715
- __isServerError: true
2716
- };
2717
- }
2718
- }
2719
- router.dehydrateData(key, state);
2720
- return [state.data];
2721
- }
2722
- function Await(props) {
2723
- const awaited = useAwaited(props);
2724
- return props.children(...awaited);
2725
- }
2726
-
2727
- function useParams(opts) {
2728
- return useRouterState({
2729
- select: state => {
2730
- const params = last(getRenderedMatches(state))?.params;
2731
- return opts?.select ? opts.select(params) : params;
2732
- }
2733
- });
2734
- }
2735
-
2736
- function useSearch(opts) {
2737
- return useMatch({
2738
- ...opts,
2739
- select: match => {
2740
- return opts?.select ? opts.select(match.search) : match.search;
2741
- }
2742
- });
2743
- }
2744
-
2745
- const rootRouteId = '__root__';
2746
-
2747
- // The parse type here allows a zod schema to be passed directly to the validator
2748
-
2749
- // TODO: This is part of a future APi to move away from classes and
2750
- // towards a more functional API. It's not ready yet.
2751
-
2752
- // type RouteApiInstance<
2753
- // TId extends RouteIds<RegisteredRouter['routeTree']>,
2754
- // TRoute extends AnyRoute = RouteById<RegisteredRouter['routeTree'], TId>,
2755
- // TFullSearchSchema extends Record<
2756
- // string,
2757
- // any
2758
- // > = TRoute['types']['fullSearchSchema'],
2759
- // TAllParams extends AnyPathParams = TRoute['types']['allParams'],
2760
- // TAllContext extends Record<string, any> = TRoute['types']['allContext'],
2761
- // TLoaderDeps extends Record<string, any> = TRoute['types']['loaderDeps'],
2762
- // TLoaderData extends any = TRoute['types']['loaderData'],
2763
- // > = {
2764
- // id: TId
2765
- // useMatch: <TSelected = TAllContext>(opts?: {
2766
- // select?: (s: TAllContext) => TSelected
2767
- // }) => TSelected
2768
-
2769
- // useRouteContext: <TSelected = TAllContext>(opts?: {
2770
- // select?: (s: TAllContext) => TSelected
2771
- // }) => TSelected
2772
-
2773
- // useSearch: <TSelected = TFullSearchSchema>(opts?: {
2774
- // select?: (s: TFullSearchSchema) => TSelected
2775
- // }) => TSelected
2776
-
2777
- // useParams: <TSelected = TAllParams>(opts?: {
2778
- // select?: (s: TAllParams) => TSelected
2779
- // }) => TSelected
2780
-
2781
- // useLoaderDeps: <TSelected = TLoaderDeps>(opts?: {
2782
- // select?: (s: TLoaderDeps) => TSelected
2783
- // }) => TSelected
2784
-
2785
- // useLoaderData: <TSelected = TLoaderData>(opts?: {
2786
- // select?: (s: TLoaderData) => TSelected
2787
- // }) => TSelected
2788
- // }
2789
-
2790
- // export function RouteApi_v2<
2791
- // TId extends RouteIds<RegisteredRouter['routeTree']>,
2792
- // TRoute extends AnyRoute = RouteById<RegisteredRouter['routeTree'], TId>,
2793
- // TFullSearchSchema extends Record<
2794
- // string,
2795
- // any
2796
- // > = TRoute['types']['fullSearchSchema'],
2797
- // TAllParams extends AnyPathParams = TRoute['types']['allParams'],
2798
- // TAllContext extends Record<string, any> = TRoute['types']['allContext'],
2799
- // TLoaderDeps extends Record<string, any> = TRoute['types']['loaderDeps'],
2800
- // TLoaderData extends any = TRoute['types']['loaderData'],
2801
- // >({
2802
- // id,
2803
- // }: {
2804
- // id: TId
2805
- // }): RouteApiInstance<
2806
- // TId,
2807
- // TRoute,
2808
- // TFullSearchSchema,
2809
- // TAllParams,
2810
- // TAllContext,
2811
- // TLoaderDeps,
2812
- // TLoaderData
2813
- // > {
2814
- // return {
2815
- // id,
2816
-
2817
- // useMatch: (opts) => {
2818
- // return useMatch({ ...opts, from: id })
2819
- // },
2820
-
2821
- // useRouteContext: (opts) => {
2822
- // return useMatch({
2823
- // ...opts,
2824
- // from: id,
2825
- // select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
2826
- // } as any)
2827
- // },
2828
-
2829
- // useSearch: (opts) => {
2830
- // return useSearch({ ...opts, from: id } as any)
2831
- // },
2832
-
2833
- // useParams: (opts) => {
2834
- // return useParams({ ...opts, from: id } as any)
2835
- // },
2836
-
2837
- // useLoaderDeps: (opts) => {
2838
- // return useLoaderDeps({ ...opts, from: id } as any) as any
2839
- // },
2840
-
2841
- // useLoaderData: (opts) => {
2842
- // return useLoaderData({ ...opts, from: id } as any) as any
2843
- // },
2844
- // }
2845
- // }
2846
-
2847
- class RouteApi {
2848
- constructor({
2849
- id
2850
- }) {
2851
- this.id = id;
2852
- }
2853
- useMatch = opts => {
2854
- return useMatch({
2855
- select: opts?.select,
2856
- from: this.id
2857
- });
2858
- };
2859
- useRouteContext = opts => {
2860
- return useMatch({
2861
- from: this.id,
2862
- select: d => opts?.select ? opts.select(d.context) : d.context
2863
- });
2864
- };
2865
- useSearch = opts => {
2866
- return useSearch({
2867
- ...opts,
2868
- from: this.id
2869
- });
2870
- };
2871
- useParams = opts => {
2872
- return useParams({
2873
- ...opts,
2874
- from: this.id
2875
- });
2876
- };
2877
- useLoaderDeps = opts => {
2878
- return useLoaderDeps({
2879
- ...opts,
2880
- from: this.id
2881
- });
2882
- };
2883
- useLoaderData = opts => {
2884
- return useLoaderData({
2885
- ...opts,
2886
- from: this.id
2887
- });
2888
- };
2889
- }
2890
- class Route {
2891
- // Set up in this.init()
2892
-
2893
- // customId!: TCustomId
2894
-
2895
- // Optional
2896
-
2897
- constructor(options) {
2898
- this.options = options || {};
2899
- this.isRoot = !options?.getParentRoute;
2900
- invariant(!(options?.id && options?.path), `Route cannot have both an 'id' and a 'path' option.`);
2901
- this.$$typeof = Symbol.for('react.memo');
2902
- }
2903
- init = opts => {
2904
- this.originalIndex = opts.originalIndex;
2905
- const options = this.options;
2906
- const isRoot = !options?.path && !options?.id;
2907
- this.parentRoute = this.options?.getParentRoute?.();
2908
- if (isRoot) {
2909
- this.path = rootRouteId;
2910
- } else {
2911
- invariant(this.parentRoute, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`);
2912
- }
2913
- let path = isRoot ? rootRouteId : options.path;
2914
-
2915
- // If the path is anything other than an index path, trim it up
2916
- if (path && path !== '/') {
2917
- path = trimPath(path);
2918
- }
2919
- const customId = options?.id || path;
2920
-
2921
- // Strip the parentId prefix from the first level of children
2922
- let id = isRoot ? rootRouteId : joinPaths([this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id, customId]);
2923
- if (path === rootRouteId) {
2924
- path = '/';
2925
- }
2926
- if (id !== rootRouteId) {
2927
- id = joinPaths(['/', id]);
2928
- }
2929
- const fullPath = id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path]);
2930
- this.path = path;
2931
- this.id = id;
2932
- // this.customId = customId as TCustomId
2933
- this.fullPath = fullPath;
2934
- this.to = fullPath;
2935
- };
2936
- addChildren = children => {
2937
- this.children = children;
2938
- return this;
2939
- };
2940
- updateLoader = options => {
2941
- Object.assign(this.options, options);
2942
- return this;
2943
- };
2944
- update = options => {
2945
- Object.assign(this.options, options);
2946
- return this;
2947
- };
2948
- useMatch = opts => {
2949
- return useMatch({
2950
- ...opts,
2951
- from: this.id
2952
- });
2953
- };
2954
- useRouteContext = opts => {
2955
- return useMatch({
2956
- ...opts,
2957
- from: this.id,
2958
- select: d => opts?.select ? opts.select(d.context) : d.context
2959
- });
2960
- };
2961
- useSearch = opts => {
2962
- return useSearch({
2963
- ...opts,
2964
- from: this.id
2965
- });
2966
- };
2967
- useParams = opts => {
2968
- return useParams({
2969
- ...opts,
2970
- from: this.id
2971
- });
2972
- };
2973
- useLoaderDeps = opts => {
2974
- return useLoaderDeps({
2975
- ...opts,
2976
- from: this.id
2977
- });
2978
- };
2979
- useLoaderData = opts => {
2980
- return useLoaderData({
2981
- ...opts,
2982
- from: this.id
2983
- });
2984
- };
2985
- }
2986
- function rootRouteWithContext() {
2987
- return options => {
2988
- return new RootRoute(options);
2989
- };
2990
- }
2991
- class RootRoute extends Route {
2992
- constructor(options) {
2993
- super(options);
2994
- }
2995
- }
2996
- function createRouteMask(opts) {
2997
- return opts;
2998
- }
2999
-
3000
- //
3001
-
3002
- class NotFoundRoute extends Route {
3003
- constructor(options) {
3004
- super({
3005
- ...options,
3006
- id: '404'
3007
- });
3008
- }
3009
- }
3010
-
3011
- class FileRoute {
3012
- constructor(path) {
3013
- this.path = path;
3014
- }
3015
- createRoute = options => {
3016
- const route = new Route(options);
3017
- route.isRoot = false;
3018
- return route;
3019
- };
3020
- }
3021
- function FileRouteLoader(_path) {
3022
- return loaderFn => loaderFn;
3023
- }
3024
-
3025
- function lazyRouteComponent(importer, exportName) {
3026
- let loadPromise;
3027
- const load = () => {
3028
- if (!loadPromise) {
3029
- loadPromise = importer();
3030
- }
3031
- return loadPromise;
3032
- };
3033
- const lazyComp = /*#__PURE__*/React__namespace.lazy(async () => {
3034
- const moduleExports = await load();
3035
- const comp = moduleExports[exportName ?? 'default'];
3036
- return {
3037
- default: comp
3038
- };
3039
- });
3040
- lazyComp.preload = load;
3041
- return lazyComp;
3042
- }
3043
-
3044
- function _extends() {
3045
- _extends = Object.assign ? Object.assign.bind() : function (target) {
3046
- for (var i = 1; i < arguments.length; i++) {
3047
- var source = arguments[i];
3048
- for (var key in source) {
3049
- if (Object.prototype.hasOwnProperty.call(source, key)) {
3050
- target[key] = source[key];
3051
- }
3052
- }
3053
- }
3054
- return target;
3055
- };
3056
- return _extends.apply(this, arguments);
3057
- }
3058
-
3059
- const preloadWarning = 'Error preloading route! ☝️';
3060
- function useLinkProps(options) {
3061
- const router = useRouter();
3062
- const matchPathname = useMatch({
3063
- strict: false,
3064
- select: s => s.pathname
3065
- });
3066
- const {
3067
- // custom props
3068
- children,
3069
- target,
3070
- activeProps = () => ({
3071
- className: 'active'
3072
- }),
3073
- inactiveProps = () => ({}),
3074
- activeOptions,
3075
- disabled,
3076
- hash,
3077
- search,
3078
- params,
3079
- to,
3080
- state,
3081
- mask,
3082
- preload: userPreload,
3083
- preloadDelay: userPreloadDelay,
3084
- replace,
3085
- startTransition,
3086
- resetScroll,
3087
- // element props
3088
- style,
3089
- className,
3090
- onClick,
3091
- onFocus,
3092
- onMouseEnter,
3093
- onMouseLeave,
3094
- onTouchStart,
3095
- ...rest
3096
- } = options;
3097
-
3098
- // If this link simply reloads the current route,
3099
- // make sure it has a new key so it will trigger a data refresh
3100
-
3101
- // If this `to` is a valid external URL, return
3102
- // null for LinkUtils
3103
-
3104
- const dest = {
3105
- from: options.to ? matchPathname : undefined,
3106
- ...options
3107
- };
3108
- let type = 'internal';
3109
- try {
3110
- new URL(`${to}`);
3111
- type = 'external';
3112
- } catch {}
3113
- if (type === 'external') {
3114
- return {
3115
- href: to
3116
- };
3117
- }
3118
- const next = router.buildLocation(dest);
3119
- const preload = userPreload ?? router.options.defaultPreload;
3120
- const preloadDelay = userPreloadDelay ?? router.options.defaultPreloadDelay ?? 0;
3121
- const isActive = useRouterState({
3122
- select: s => {
3123
- // Compare path/hash for matches
3124
- const currentPathSplit = s.location.pathname.split('/');
3125
- const nextPathSplit = next.pathname.split('/');
3126
- const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
3127
- // Combine the matches based on user router.options
3128
- const pathTest = activeOptions?.exact ? s.location.pathname === next.pathname : pathIsFuzzyEqual;
3129
- const hashTest = activeOptions?.includeHash ? s.location.hash === next.hash : true;
3130
- const searchTest = activeOptions?.includeSearch ?? true ? deepEqual(s.location.search, next.search, !activeOptions?.exact) : true;
3131
-
3132
- // The final "active" test
3133
- return pathTest && hashTest && searchTest;
3134
- }
3135
- });
3136
-
3137
- // The click handler
3138
- const handleClick = e => {
3139
- if (!disabled && !isCtrlEvent(e) && !e.defaultPrevented && (!target || target === '_self') && e.button === 0) {
3140
- e.preventDefault();
3141
-
3142
- // All is well? Navigate!
3143
- router.commitLocation({
3144
- ...next,
3145
- replace,
3146
- resetScroll,
3147
- startTransition
3148
- });
3149
- }
3150
- };
3151
-
3152
- // The click handler
3153
- const handleFocus = e => {
3154
- if (preload) {
3155
- router.preloadRoute(dest).catch(err => {
3156
- console.warn(err);
3157
- console.warn(preloadWarning);
3158
- });
3159
- }
3160
- };
3161
- const handleTouchStart = e => {
3162
- if (preload) {
3163
- router.preloadRoute(dest).catch(err => {
3164
- console.warn(err);
3165
- console.warn(preloadWarning);
3166
- });
3167
- }
3168
- };
3169
- const handleEnter = e => {
3170
- const target = e.target || {};
3171
- if (preload) {
3172
- if (target.preloadTimeout) {
3173
- return;
3174
- }
3175
- target.preloadTimeout = setTimeout(() => {
3176
- target.preloadTimeout = null;
3177
- router.preloadRoute(dest).catch(err => {
3178
- console.warn(err);
3179
- console.warn(preloadWarning);
3180
- });
3181
- }, preloadDelay);
3182
- }
3183
- };
3184
- const handleLeave = e => {
3185
- const target = e.target || {};
3186
- if (target.preloadTimeout) {
3187
- clearTimeout(target.preloadTimeout);
3188
- target.preloadTimeout = null;
3189
- }
3190
- };
3191
- const composeHandlers = handlers => e => {
3192
- if (e.persist) e.persist();
3193
- handlers.filter(Boolean).forEach(handler => {
3194
- if (e.defaultPrevented) return;
3195
- handler(e);
3196
- });
3197
- };
3198
-
3199
- // Get the active props
3200
- const resolvedActiveProps = isActive ? functionalUpdate(activeProps, {}) ?? {} : {};
3201
-
3202
- // Get the inactive props
3203
- const resolvedInactiveProps = isActive ? {} : functionalUpdate(inactiveProps, {}) ?? {};
3204
- return {
3205
- ...resolvedActiveProps,
3206
- ...resolvedInactiveProps,
3207
- ...rest,
3208
- href: disabled ? undefined : next.maskedLocation ? next.maskedLocation.href : next.href,
3209
- onClick: composeHandlers([onClick, handleClick]),
3210
- onFocus: composeHandlers([onFocus, handleFocus]),
3211
- onMouseEnter: composeHandlers([onMouseEnter, handleEnter]),
3212
- onMouseLeave: composeHandlers([onMouseLeave, handleLeave]),
3213
- onTouchStart: composeHandlers([onTouchStart, handleTouchStart]),
3214
- target,
3215
- style: {
3216
- ...style,
3217
- ...resolvedActiveProps.style,
3218
- ...resolvedInactiveProps.style
3219
- },
3220
- className: [className, resolvedActiveProps.className, resolvedInactiveProps.className].filter(Boolean).join(' ') || undefined,
3221
- ...(disabled ? {
3222
- role: 'link',
3223
- 'aria-disabled': true
3224
- } : undefined),
3225
- ['data-status']: isActive ? 'active' : undefined
3226
- };
3227
- }
3228
- const Link = /*#__PURE__*/React__namespace.forwardRef((props, ref) => {
3229
- const linkProps = useLinkProps(props);
3230
- return /*#__PURE__*/React__namespace.createElement("a", _extends({
3231
- ref: ref
3232
- }, linkProps, {
3233
- children: typeof props.children === 'function' ? props.children({
3234
- isActive: linkProps['data-status'] === 'active'
3235
- }) : props.children
3236
- }));
3237
- });
3238
- function isCtrlEvent(e) {
3239
- return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
3240
- }
3241
-
3242
- const useLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
3243
- const windowKey = 'window';
3244
- const delimiter = '___';
3245
- let weakScrolledElements = new WeakSet();
3246
- const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage;
3247
- let cache = sessionsStorage ? (() => {
3248
- const storageKey = 'tsr-scroll-restoration-v2';
3249
- const state = JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {
3250
- cached: {},
3251
- next: {}
3252
- };
3253
- return {
3254
- state,
3255
- set: updater => {
3256
- cache.state = functionalUpdate(updater, cache.state);
3257
- window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state));
3258
- }
3259
- };
3260
- })() : undefined;
3261
- const defaultGetKey = location => location.state.key;
3262
- function useScrollRestoration(options) {
3263
- const router = useRouter();
3264
- useLayoutEffect(() => {
3265
- const getKey = options?.getKey || defaultGetKey;
3266
- const {
3267
- history
3268
- } = window;
3269
- if (history.scrollRestoration) {
3270
- history.scrollRestoration = 'manual';
3271
- }
3272
- const onScroll = event => {
3273
- if (weakScrolledElements.has(event.target)) return;
3274
- weakScrolledElements.add(event.target);
3275
- let elementSelector = '';
3276
- if (event.target === document || event.target === window) {
3277
- elementSelector = windowKey;
3278
- } else {
3279
- const attrId = event.target.getAttribute('data-scroll-restoration-id');
3280
- if (attrId) {
3281
- elementSelector = `[data-scroll-restoration-id="${attrId}"]`;
3282
- } else {
3283
- elementSelector = getCssSelector(event.target);
3284
- }
3285
- }
3286
- if (!cache.state.next[elementSelector]) {
3287
- cache.set(c => ({
3288
- ...c,
3289
- next: {
3290
- ...c.next,
3291
- [elementSelector]: {
3292
- scrollX: NaN,
3293
- scrollY: NaN
3294
- }
3295
- }
3296
- }));
3297
- }
3298
- };
3299
- if (typeof document !== 'undefined') {
3300
- document.addEventListener('scroll', onScroll, true);
3301
- }
3302
- const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', event => {
3303
- if (event.pathChanged) {
3304
- const restoreKey = getKey(event.fromLocation);
3305
- for (const elementSelector in cache.state.next) {
3306
- const entry = cache.state.next[elementSelector];
3307
- if (elementSelector === windowKey) {
3308
- entry.scrollX = window.scrollX || 0;
3309
- entry.scrollY = window.scrollY || 0;
3310
- } else if (elementSelector) {
3311
- const element = document.querySelector(elementSelector);
3312
- entry.scrollX = element?.scrollLeft || 0;
3313
- entry.scrollY = element?.scrollTop || 0;
3314
- }
3315
- cache.set(c => {
3316
- const next = {
3317
- ...c.next
3318
- };
3319
- delete next[elementSelector];
3320
- return {
3321
- ...c,
3322
- next,
3323
- cached: {
3324
- ...c.cached,
3325
- [[restoreKey, elementSelector].join(delimiter)]: entry
3326
- }
3327
- };
3328
- });
3329
- }
3330
- }
3331
- });
3332
- const unsubOnResolved = router.subscribe('onResolved', event => {
3333
- if (event.pathChanged) {
3334
- if (!router.resetNextScroll) {
3335
- return;
3336
- }
3337
- router.resetNextScroll = true;
3338
- const getKey = options?.getKey || defaultGetKey;
3339
- const restoreKey = getKey(event.toLocation);
3340
- let windowRestored = false;
3341
- for (const cacheKey in cache.state.cached) {
3342
- const entry = cache.state.cached[cacheKey];
3343
- const [key, elementSelector] = cacheKey.split(delimiter);
3344
- if (key === restoreKey) {
3345
- if (elementSelector === windowKey) {
3346
- windowRestored = true;
3347
- window.scrollTo(entry.scrollX, entry.scrollY);
3348
- } else if (elementSelector) {
3349
- const element = document.querySelector(elementSelector);
3350
- if (element) {
3351
- element.scrollLeft = entry.scrollX;
3352
- element.scrollTop = entry.scrollY;
3353
- }
3354
- }
3355
- }
3356
- }
3357
- if (!windowRestored) {
3358
- window.scrollTo(0, 0);
3359
- }
3360
- cache.set(c => ({
3361
- ...c,
3362
- next: {}
3363
- }));
3364
- weakScrolledElements = new WeakSet();
3365
- }
3366
- });
3367
- return () => {
3368
- document.removeEventListener('scroll', onScroll);
3369
- unsubOnBeforeLoad();
3370
- unsubOnResolved();
3371
- };
3372
- }, []);
3373
- }
3374
- function ScrollRestoration(props) {
3375
- useScrollRestoration(props);
3376
- return null;
3377
- }
3378
- function useElementScrollRestoration(options) {
3379
- const router = useRouter();
3380
- const getKey = options?.getKey || defaultGetKey;
3381
- let elementSelector = '';
3382
- if (options.id) {
3383
- elementSelector = `[data-scroll-restoration-id="${options.id}"]`;
3384
- } else {
3385
- const element = options.getElement?.();
3386
- if (!element) {
3387
- return;
3388
- }
3389
- elementSelector = getCssSelector(element);
3390
- }
3391
- const restoreKey = getKey(router.latestLocation);
3392
- const cacheKey = [restoreKey, elementSelector].join(delimiter);
3393
- return cache.state.cached[cacheKey];
3394
- }
3395
- function getCssSelector(el) {
3396
- let path = [],
3397
- parent;
3398
- while (parent = el.parentNode) {
3399
- path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
3400
- el = parent;
3401
- }
3402
- return `${path.join(' > ')}`.toLowerCase();
3403
- }
3404
-
3405
- function useBlocker(blockerFn, condition = true) {
3406
- const {
3407
- history
3408
- } = useRouter();
3409
- React__namespace.useEffect(() => {
3410
- if (!condition) return;
3411
- return history.block(blockerFn);
3412
- });
3413
- }
3414
- function Block({
3415
- blocker,
3416
- condition,
3417
- children
3418
- }) {
3419
- useBlocker(blocker, condition);
3420
- return children ?? null;
3421
- }
3422
-
3423
- function useNavigate(_defaultOpts) {
3424
- const {
3425
- navigate
3426
- } = useRouter();
3427
- const matchPathname = useMatch({
3428
- strict: false,
3429
- select: s => s.pathname
3430
- });
3431
- return React__namespace.useCallback(({
3432
- from,
3433
- ...rest
3434
- }) => {
3435
- return navigate({
3436
- from: rest?.to ? matchPathname : undefined,
3437
- ...rest
3438
- });
3439
- }, []);
3440
- }
3441
-
3442
- // NOTE: I don't know of anyone using this. It's undocumented, so let's wait until someone needs it
3443
- // export function typedNavigate<
3444
- // TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
3445
- // TDefaultFrom extends RoutePaths<TRouteTree> = '/',
3446
- // >(navigate: (opts: NavigateOptions<any>) => Promise<void>) {
3447
- // return navigate as <
3448
- // TFrom extends RoutePaths<TRouteTree> = TDefaultFrom,
3449
- // TTo extends string = '',
3450
- // TMaskFrom extends RoutePaths<TRouteTree> = '/',
3451
- // TMaskTo extends string = '',
3452
- // >(
3453
- // opts?: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
3454
- // ) => Promise<void>
3455
- // } //
3456
-
3457
- function Navigate(props) {
3458
- const {
3459
- navigate
3460
- } = useRouter();
3461
- const match = useMatch({
3462
- strict: false
3463
- });
3464
- React__namespace.useEffect(() => {
3465
- navigate({
3466
- from: props.to ? match.pathname : undefined,
3467
- ...props
3468
- });
3469
- }, []);
3470
- return null;
3471
- }
3472
-
3473
- function useRouteContext(opts) {
3474
- return useMatch({
3475
- ...opts,
3476
- select: match => opts?.select ? opts.select(match.context) : match.context
3477
- });
3478
- }
3479
-
3480
- exports.Await = Await;
3481
- exports.Block = Block;
3482
- exports.CatchBoundary = CatchBoundary;
3483
- exports.CatchBoundaryImpl = CatchBoundaryImpl;
3484
- exports.ErrorComponent = ErrorComponent;
3485
- exports.FileRoute = FileRoute;
3486
- exports.FileRouteLoader = FileRouteLoader;
3487
- exports.Link = Link;
3488
- exports.Match = Match;
3489
- exports.MatchRoute = MatchRoute;
3490
- exports.Matches = Matches;
3491
- exports.Navigate = Navigate;
3492
- exports.NotFoundRoute = NotFoundRoute;
3493
- exports.Outlet = Outlet;
3494
- exports.PathParamError = PathParamError;
3495
- exports.RootRoute = RootRoute;
3496
- exports.Route = Route;
3497
- exports.RouteApi = RouteApi;
3498
- exports.Router = Router;
3499
- exports.RouterProvider = RouterProvider;
3500
- exports.ScrollRestoration = ScrollRestoration;
3501
- exports.SearchParamError = SearchParamError;
3502
- exports.cleanPath = cleanPath;
3503
- exports.componentTypes = componentTypes;
3504
- exports.createBrowserHistory = createBrowserHistory;
3505
- exports.createHashHistory = createHashHistory;
3506
- exports.createHistory = createHistory;
3507
- exports.createMemoryHistory = createMemoryHistory;
3508
- exports.createRouteMask = createRouteMask;
3509
- exports.decode = decode;
3510
- exports.deepEqual = deepEqual;
3511
- exports.defaultDeserializeError = defaultDeserializeError;
3512
- exports.defaultParseSearch = defaultParseSearch;
3513
- exports.defaultSerializeError = defaultSerializeError;
3514
- exports.defaultStringifySearch = defaultStringifySearch;
3515
- exports.defer = defer;
3516
- exports.encode = encode;
3517
- exports.escapeJSON = escapeJSON;
3518
- exports.functionalUpdate = functionalUpdate;
3519
- exports.getInitialRouterState = getInitialRouterState;
3520
- exports.getRenderedMatches = getRenderedMatches;
3521
- exports.getRouteMatch = getRouteMatch;
3522
- exports.interpolatePath = interpolatePath;
3523
- exports.invariant = invariant;
3524
- exports.isDehydratedDeferred = isDehydratedDeferred;
3525
- exports.isPlainArray = isPlainArray;
3526
- exports.isPlainObject = isPlainObject;
3527
- exports.isRedirect = isRedirect;
3528
- exports.isServer = isServer;
3529
- exports.isServerSideError = isServerSideError;
3530
- exports.joinPaths = joinPaths;
3531
- exports.last = last;
3532
- exports.lazyFn = lazyFn;
3533
- exports.lazyRouteComponent = lazyRouteComponent;
3534
- exports.matchByPath = matchByPath;
3535
- exports.matchContext = matchContext;
3536
- exports.matchPathname = matchPathname;
3537
- exports.parsePathname = parsePathname;
3538
- exports.parseSearchWith = parseSearchWith;
3539
- exports.pick = pick;
3540
- exports.redirect = redirect;
3541
- exports.removeBasepath = removeBasepath;
3542
- exports.replaceEqualDeep = replaceEqualDeep;
3543
- exports.resolvePath = resolvePath;
3544
- exports.rootRouteId = rootRouteId;
3545
- exports.rootRouteWithContext = rootRouteWithContext;
3546
- exports.shallow = shallow;
3547
- exports.stringifySearchWith = stringifySearchWith;
3548
- exports.trimPath = trimPath;
3549
- exports.trimPathLeft = trimPathLeft;
3550
- exports.trimPathRight = trimPathRight;
3551
- exports.useAwaited = useAwaited;
3552
- exports.useBlocker = useBlocker;
3553
- exports.useElementScrollRestoration = useElementScrollRestoration;
3554
- exports.useLayoutEffect = useLayoutEffect$1;
3555
- exports.useLinkProps = useLinkProps;
3556
- exports.useLoaderData = useLoaderData;
3557
- exports.useLoaderDeps = useLoaderDeps;
3558
- exports.useMatch = useMatch;
3559
- exports.useMatchRoute = useMatchRoute;
3560
- exports.useMatches = useMatches;
3561
- exports.useNavigate = useNavigate;
3562
- exports.useParams = useParams;
3563
- exports.useParentMatches = useParentMatches;
3564
- exports.useRouteContext = useRouteContext;
3565
- exports.useRouter = useRouter;
3566
- exports.useRouterState = useRouterState;
3567
- exports.useScrollRestoration = useScrollRestoration;
3568
- exports.useSearch = useSearch;
3569
- exports.useStableCallback = useStableCallback;
3570
- exports.warning = warning;
3571
-
3572
- }));
3573
- //# sourceMappingURL=index.development.js.map