@tanstack/router-core 0.0.1-beta.35 → 0.0.1-beta.36

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.
@@ -34,8 +34,9 @@ const createDefaultHistory = () => isServer ? history.createMemoryHistory() : hi
34
34
  function getInitialRouterState() {
35
35
  return {
36
36
  status: 'idle',
37
- location: null,
38
- matches: [],
37
+ latestLocation: null,
38
+ currentLocation: null,
39
+ currentMatches: [],
39
40
  actions: {},
40
41
  loaders: {},
41
42
  lastUpdated: Date.now(),
@@ -66,7 +67,6 @@ function createRouter(userOptions) {
66
67
  basepath: '',
67
68
  routeTree: undefined,
68
69
  routesById: {},
69
- __location: undefined,
70
70
  //
71
71
  resolveNavigation: () => {},
72
72
  matchCache: {},
@@ -86,65 +86,53 @@ function createRouter(userOptions) {
86
86
  return router.routesById[id];
87
87
  },
88
88
  notify: () => {
89
- const isFetching = router.state.status === 'loading' || router.state.matches.some(d => d.isFetching);
90
- const isPreloading = Object.values(router.matchCache).some(d => d.match.isFetching && !router.state.matches.find(dd => dd.matchId === d.match.matchId));
89
+ const isFetching = router.state.status === 'loading' || router.state.currentMatches.some(d => d.isFetching);
90
+ const isPreloading = Object.values(router.matchCache).some(d => d.match.isFetching && !router.state.currentMatches.find(dd => dd.matchId === d.match.matchId));
91
91
  if (router.state.isFetching !== isFetching || router.state.isPreloading !== isPreloading) {
92
92
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
93
93
  isFetching,
94
94
  isPreloading
95
95
  });
96
96
  }
97
- cascadeLoaderData(router.state.matches);
97
+ cascadeLoaderData(router.state.currentMatches);
98
98
  router.listeners.forEach(listener => listener(router));
99
99
  },
100
100
  dehydrate: () => {
101
101
  return {
102
- location: router.__location,
103
- state: _rollupPluginBabelHelpers["extends"]({}, utils.pick(router.state, ['status', 'location', 'lastUpdated', 'location']), {
104
- matches: router.state.matches.map(match => utils.pick(match, ['matchId', 'status', 'routeLoaderData', 'loaderData', 'isInvalid', 'invalidAt']))
102
+ state: _rollupPluginBabelHelpers["extends"]({}, utils.pick(router.state, ['latestLocation', 'currentLocation', 'status', 'lastUpdated']), {
103
+ currentMatches: router.state.currentMatches.map(match => utils.pick(match, ['matchId', 'status', 'routeLoaderData', 'loaderData', 'isInvalid', 'invalidAt']))
105
104
  }),
106
105
  context: router.options.context
107
106
  };
108
107
  },
109
108
  hydrate: dehydratedState => {
110
109
  // Update the location
111
- router.__location = dehydratedState.location;
110
+ router.state.latestLocation = dehydratedState.state.latestLocation;
111
+ router.state.currentLocation = dehydratedState.state.currentLocation;
112
112
 
113
113
  // Update the context
114
114
  router.options.context = dehydratedState.context;
115
115
 
116
116
  // Match the routes
117
- const matches = router.matchRoutes(router.__location.pathname, {
117
+ const currentMatches = router.matchRoutes(router.state.latestLocation.pathname, {
118
118
  strictParseParams: true
119
119
  });
120
- matches.forEach((match, index) => {
121
- const dehydratedMatch = dehydratedState.state.matches[index];
120
+ currentMatches.forEach((match, index) => {
121
+ const dehydratedMatch = dehydratedState.state.currentMatches[index];
122
122
  invariant__default["default"](dehydratedMatch, 'Oh no! Dehydrated route matches did not match the active state of the router 😬');
123
123
  Object.assign(match, dehydratedMatch);
124
124
  });
125
- matches.forEach(match => match.__.validate());
125
+ currentMatches.forEach(match => match.__.validate());
126
126
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, dehydratedState, {
127
- matches
127
+ currentMatches
128
128
  });
129
129
  },
130
130
  mount: () => {
131
- router.__.buildLocation({
132
- to: '.',
133
- search: true,
134
- hash: true
135
- });
136
-
137
- // If the current location isn't updated, trigger a navigation
138
- // to the current location. Otherwise, load the current location.
139
- // if (next.href !== router.__location.href) {
140
- // router.__.commitLocation(next, true)
141
- // }
142
-
143
- if (!router.state.matches.length) {
131
+ if (!router.state.currentMatches.length) {
144
132
  router.load();
145
133
  }
146
134
  const unsub = router.history.listen(event => {
147
- router.load(router.__.parseLocation(event.location, router.__location));
135
+ router.load(router.__.parseLocation(event.location, router.state.latestLocation));
148
136
  });
149
137
 
150
138
  // addEventListener does not exist in React Native, but window does
@@ -169,12 +157,12 @@ function createRouter(userOptions) {
169
157
  update: opts => {
170
158
  var _trimPath;
171
159
  const newHistory = (opts == null ? void 0 : opts.history) !== router.history;
172
- if (!router.__location || newHistory) {
160
+ if (!router.state.latestLocation || newHistory) {
173
161
  if (opts != null && opts.history) {
174
162
  router.history = opts.history;
175
163
  }
176
- router.__location = router.__.parseLocation(router.history.location);
177
- router.state.location = router.__location;
164
+ router.state.latestLocation = router.__.parseLocation(router.history.location);
165
+ router.state.currentLocation = router.state.latestLocation;
178
166
  }
179
167
  Object.assign(router.options, opts);
180
168
  const {
@@ -189,8 +177,8 @@ function createRouter(userOptions) {
189
177
  return router;
190
178
  },
191
179
  cancelMatches: () => {
192
- var _router$state$pending, _router$state$pending2;
193
- [...router.state.matches, ...((_router$state$pending = (_router$state$pending2 = router.state.pending) == null ? void 0 : _router$state$pending2.matches) != null ? _router$state$pending : [])].forEach(match => {
180
+ var _router$state$pending;
181
+ [...router.state.currentMatches, ...((_router$state$pending = router.state.pendingMatches) != null ? _router$state$pending : [])].forEach(match => {
194
182
  match.cancel();
195
183
  });
196
184
  },
@@ -199,51 +187,43 @@ function createRouter(userOptions) {
199
187
  router.startedLoadingAt = id;
200
188
  if (next) {
201
189
  // Ingest the new location
202
- router.__location = next;
190
+ router.state.latestLocation = next;
203
191
  }
204
192
 
205
193
  // Cancel any pending matches
206
194
  router.cancelMatches();
207
195
 
208
196
  // Match the routes
209
- const matches = router.matchRoutes(router.__location.pathname, {
197
+ const matches = router.matchRoutes(router.state.latestLocation.pathname, {
210
198
  strictParseParams: true
211
199
  });
212
200
  if (typeof document !== 'undefined') {
213
201
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
214
- pending: {
215
- matches: matches,
216
- location: router.__location
217
- },
218
- status: 'loading'
202
+ status: 'loading',
203
+ pendingMatches: matches,
204
+ pendingLocation: router.state.latestLocation
219
205
  });
220
206
  } else {
221
207
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
222
- matches: matches,
223
- location: router.__location,
224
- status: 'loading'
208
+ status: 'loading',
209
+ currentMatches: matches,
210
+ currentLocation: router.state.latestLocation
225
211
  });
226
212
  }
213
+ router.notify();
227
214
 
228
- // Check if each match middleware to see if the route can be accessed
215
+ // Load the matches
229
216
  try {
230
- await Promise.all(matches.map(match => match.options.beforeLoad == null ? void 0 : match.options.beforeLoad({
231
- router: router,
232
- match
233
- })));
217
+ await router.loadMatches(matches);
234
218
  } catch (err) {
235
- console.info(err);
236
- invariant__default["default"](false, "A route's beforeLoad middleware failed! \uD83D\uDC46");
219
+ console.log(err);
220
+ invariant__default["default"](false, 'Matches failed to load due to error above ☝️. Navigation cancelled!');
237
221
  }
238
- router.notify();
239
-
240
- // Load the matches
241
- await router.loadMatches(matches);
242
222
  if (router.startedLoadingAt !== id) {
243
223
  // Ignore side-effects of match loading
244
224
  return router.navigationPromise;
245
225
  }
246
- const previousMatches = router.state.matches;
226
+ const previousMatches = router.state.currentMatches;
247
227
  const exiting = [],
248
228
  staying = [];
249
229
  previousMatches.forEach(d => {
@@ -302,10 +282,11 @@ function createRouter(userOptions) {
302
282
  }
303
283
  });
304
284
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
305
- location: router.__location,
306
- matches,
307
- pending: undefined,
308
- status: 'idle'
285
+ status: 'idle',
286
+ currentLocation: router.state.latestLocation,
287
+ currentMatches: matches,
288
+ pendingLocation: undefined,
289
+ pendingMatches: undefined
309
290
  });
310
291
  router.notify();
311
292
  router.resolveNavigation();
@@ -331,7 +312,7 @@ function createRouter(userOptions) {
331
312
  },
332
313
  loadRoute: async function loadRoute(navigateOpts) {
333
314
  if (navigateOpts === void 0) {
334
- navigateOpts = router.__location;
315
+ navigateOpts = router.state.latestLocation;
335
316
  }
336
317
  const next = router.buildNext(navigateOpts);
337
318
  const matches = router.matchRoutes(next.pathname, {
@@ -343,7 +324,7 @@ function createRouter(userOptions) {
343
324
  preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
344
325
  var _ref3, _ref4, _loaderOpts$maxAge, _ref5, _ref6, _loaderOpts$gcMaxAge;
345
326
  if (navigateOpts === void 0) {
346
- navigateOpts = router.__location;
327
+ navigateOpts = router.state.latestLocation;
347
328
  }
348
329
  const next = router.buildNext(navigateOpts);
349
330
  const matches = router.matchRoutes(next.pathname, {
@@ -357,13 +338,13 @@ function createRouter(userOptions) {
357
338
  return matches;
358
339
  },
359
340
  matchRoutes: (pathname, opts) => {
360
- var _router$state$pending3, _router$state$pending4;
341
+ var _router$state$pending2;
361
342
  router.cleanMatchCache();
362
343
  const matches = [];
363
344
  if (!router.routeTree) {
364
345
  return matches;
365
346
  }
366
- const existingMatches = [...router.state.matches, ...((_router$state$pending3 = (_router$state$pending4 = router.state.pending) == null ? void 0 : _router$state$pending4.matches) != null ? _router$state$pending3 : [])];
347
+ const existingMatches = [...router.state.currentMatches, ...((_router$state$pending2 = router.state.pendingMatches) != null ? _router$state$pending2 : [])];
367
348
  const recurse = async routes => {
368
349
  var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
369
350
  const parentMatch = utils.last(matches);
@@ -382,7 +363,15 @@ function createRouter(userOptions) {
382
363
  fuzzy,
383
364
  caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
384
365
  });
385
- console.log(router.basepath, route.fullPath, fuzzy, pathname, matchParams);
366
+
367
+ // console.log(
368
+ // router.basepath,
369
+ // route.fullPath,
370
+ // fuzzy,
371
+ // pathname,
372
+ // matchParams,
373
+ // )
374
+
386
375
  if (matchParams) {
387
376
  let parsedParams;
388
377
  try {
@@ -428,10 +417,27 @@ function createRouter(userOptions) {
428
417
  return matches;
429
418
  },
430
419
  loadMatches: async (resolvedMatches, loaderOpts) => {
431
- const matchPromises = resolvedMatches.map(async match => {
432
- var _search$__data;
420
+ resolvedMatches.forEach(async match => {
433
421
  // Validate the match (loads search params etc)
434
422
  match.__.validate();
423
+ });
424
+
425
+ // Check each match middleware to see if the route can be accessed
426
+ await Promise.all(resolvedMatches.map(async match => {
427
+ try {
428
+ await (match.options.beforeLoad == null ? void 0 : match.options.beforeLoad({
429
+ router: router,
430
+ match
431
+ }));
432
+ } catch (err) {
433
+ if (!(loaderOpts != null && loaderOpts.preload)) {
434
+ match.options.onLoadError == null ? void 0 : match.options.onLoadError(err);
435
+ }
436
+ throw err;
437
+ }
438
+ }));
439
+ const matchPromises = resolvedMatches.map(async match => {
440
+ var _search$__data;
435
441
  const search = match.search;
436
442
  if ((_search$__data = search.__data) != null && _search$__data.matchId && search.__data.matchId !== match.matchId) {
437
443
  return;
@@ -486,10 +492,10 @@ function createRouter(userOptions) {
486
492
  }
487
493
  },
488
494
  invalidateRoute: opts => {
489
- var _router$state$pending5, _router$state$pending6;
495
+ var _router$state$pending3;
490
496
  const next = router.buildNext(opts);
491
497
  const unloadedMatchIds = router.matchRoutes(next.pathname).map(d => d.matchId);
492
- [...router.state.matches, ...((_router$state$pending5 = (_router$state$pending6 = router.state.pending) == null ? void 0 : _router$state$pending6.matches) != null ? _router$state$pending5 : [])].forEach(match => {
498
+ [...router.state.currentMatches, ...((_router$state$pending3 = router.state.pendingMatches) != null ? _router$state$pending3 : [])].forEach(match => {
493
499
  if (unloadedMatchIds.includes(match.matchId)) {
494
500
  match.invalidate();
495
501
  }
@@ -512,15 +518,14 @@ function createRouter(userOptions) {
512
518
  });
513
519
  const next = router.buildNext(location);
514
520
  if (opts != null && opts.pending) {
515
- var _router$state$pending7;
516
- if (!((_router$state$pending7 = router.state.pending) != null && _router$state$pending7.location)) {
521
+ if (!router.state.pendingLocation) {
517
522
  return false;
518
523
  }
519
- return !!path.matchPathname(router.basepath, router.state.pending.location.pathname, _rollupPluginBabelHelpers["extends"]({}, opts, {
524
+ return !!path.matchPathname(router.basepath, router.state.pendingLocation.pathname, _rollupPluginBabelHelpers["extends"]({}, opts, {
520
525
  to: next.pathname
521
526
  }));
522
527
  }
523
- return !!path.matchPathname(router.basepath, router.state.location.pathname, _rollupPluginBabelHelpers["extends"]({}, opts, {
528
+ return !!path.matchPathname(router.basepath, router.state.currentLocation.pathname, _rollupPluginBabelHelpers["extends"]({}, opts, {
524
529
  to: next.pathname
525
530
  }));
526
531
  },
@@ -598,11 +603,11 @@ function createRouter(userOptions) {
598
603
  const preloadDelay = (_ref9 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref9 : 0;
599
604
 
600
605
  // Compare path/hash for matches
601
- const pathIsEqual = router.state.location.pathname === next.pathname;
602
- const currentPathSplit = router.state.location.pathname.split('/');
606
+ const pathIsEqual = router.state.currentLocation.pathname === next.pathname;
607
+ const currentPathSplit = router.state.currentLocation.pathname.split('/');
603
608
  const nextPathSplit = next.pathname.split('/');
604
609
  const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
605
- const hashIsEqual = router.state.location.hash === next.hash;
610
+ const hashIsEqual = router.state.currentLocation.hash === next.hash;
606
611
  // Combine the matches based on user options
607
612
  const pathTest = activeOptions != null && activeOptions.exact ? pathIsEqual : pathIsFuzzyEqual;
608
613
  const hashTest = activeOptions != null && activeOptions.includeHash ? hashIsEqual : true;
@@ -629,6 +634,9 @@ function createRouter(userOptions) {
629
634
  router.preloadRoute(nextOpts, {
630
635
  maxAge: userPreloadMaxAge,
631
636
  gcMaxAge: userPreloadGcMaxAge
637
+ }).catch(err => {
638
+ console.log(err);
639
+ console.warn('Error preloading route! ☝️');
632
640
  });
633
641
  }
634
642
  };
@@ -643,6 +651,9 @@ function createRouter(userOptions) {
643
651
  router.preloadRoute(nextOpts, {
644
652
  maxAge: userPreloadMaxAge,
645
653
  gcMaxAge: userPreloadGcMaxAge
654
+ }).catch(err => {
655
+ console.log(err);
656
+ console.warn('Error preloading route! ☝️');
646
657
  });
647
658
  }, preloadDelay);
648
659
  }
@@ -725,9 +736,9 @@ function createRouter(userOptions) {
725
736
  if (dest === void 0) {
726
737
  dest = {};
727
738
  }
728
- const fromPathname = dest.fromCurrent ? router.__location.pathname : (_dest$from = dest.from) != null ? _dest$from : router.__location.pathname;
739
+ const fromPathname = dest.fromCurrent ? router.state.latestLocation.pathname : (_dest$from = dest.from) != null ? _dest$from : router.state.latestLocation.pathname;
729
740
  let pathname = path.resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
730
- const fromMatches = router.matchRoutes(router.__location.pathname, {
741
+ const fromMatches = router.matchRoutes(router.state.latestLocation.pathname, {
731
742
  strictParseParams: true
732
743
  });
733
744
  const toMatches = router.matchRoutes(pathname);
@@ -741,7 +752,7 @@ function createRouter(userOptions) {
741
752
  pathname = path.interpolatePath(pathname, nextParams != null ? nextParams : {});
742
753
 
743
754
  // Pre filters first
744
- const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.__location.search) : router.__location.search;
755
+ const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.state.latestLocation.search) : router.state.latestLocation.search;
745
756
 
746
757
  // Then the link/navigate function
747
758
  const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
@@ -751,15 +762,15 @@ function createRouter(userOptions) {
751
762
 
752
763
  // Then post filters
753
764
  const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
754
- const search = utils.replaceEqualDeep(router.__location.search, postFilteredSearch);
765
+ const search = utils.replaceEqualDeep(router.state.latestLocation.search, postFilteredSearch);
755
766
  const searchStr = router.options.stringifySearch(search);
756
- let hash = dest.hash === true ? router.__location.hash : utils.functionalUpdate(dest.hash, router.__location.hash);
767
+ let hash = dest.hash === true ? router.state.latestLocation.hash : utils.functionalUpdate(dest.hash, router.state.latestLocation.hash);
757
768
  hash = hash ? "#" + hash : '';
758
769
  return {
759
770
  pathname,
760
771
  search,
761
772
  searchStr,
762
- state: router.__location.state,
773
+ state: router.state.latestLocation.state,
763
774
  hash,
764
775
  href: "" + pathname + searchStr + hash,
765
776
  key: dest.key