@tanstack/router-core 0.0.1-beta.34 → 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,53 +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
- if (err != null && err.then) {
236
- await new Promise(() => {});
237
- }
238
- throw err;
219
+ console.log(err);
220
+ invariant__default["default"](false, 'Matches failed to load due to error above ☝️. Navigation cancelled!');
239
221
  }
240
- router.notify();
241
-
242
- // Load the matches
243
- await router.loadMatches(matches);
244
222
  if (router.startedLoadingAt !== id) {
245
223
  // Ignore side-effects of match loading
246
224
  return router.navigationPromise;
247
225
  }
248
- const previousMatches = router.state.matches;
226
+ const previousMatches = router.state.currentMatches;
249
227
  const exiting = [],
250
228
  staying = [];
251
229
  previousMatches.forEach(d => {
@@ -304,10 +282,11 @@ function createRouter(userOptions) {
304
282
  }
305
283
  });
306
284
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
307
- location: router.__location,
308
- matches,
309
- pending: undefined,
310
- status: 'idle'
285
+ status: 'idle',
286
+ currentLocation: router.state.latestLocation,
287
+ currentMatches: matches,
288
+ pendingLocation: undefined,
289
+ pendingMatches: undefined
311
290
  });
312
291
  router.notify();
313
292
  router.resolveNavigation();
@@ -333,7 +312,7 @@ function createRouter(userOptions) {
333
312
  },
334
313
  loadRoute: async function loadRoute(navigateOpts) {
335
314
  if (navigateOpts === void 0) {
336
- navigateOpts = router.__location;
315
+ navigateOpts = router.state.latestLocation;
337
316
  }
338
317
  const next = router.buildNext(navigateOpts);
339
318
  const matches = router.matchRoutes(next.pathname, {
@@ -345,7 +324,7 @@ function createRouter(userOptions) {
345
324
  preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
346
325
  var _ref3, _ref4, _loaderOpts$maxAge, _ref5, _ref6, _loaderOpts$gcMaxAge;
347
326
  if (navigateOpts === void 0) {
348
- navigateOpts = router.__location;
327
+ navigateOpts = router.state.latestLocation;
349
328
  }
350
329
  const next = router.buildNext(navigateOpts);
351
330
  const matches = router.matchRoutes(next.pathname, {
@@ -359,13 +338,13 @@ function createRouter(userOptions) {
359
338
  return matches;
360
339
  },
361
340
  matchRoutes: (pathname, opts) => {
362
- var _router$state$pending3, _router$state$pending4;
341
+ var _router$state$pending2;
363
342
  router.cleanMatchCache();
364
343
  const matches = [];
365
344
  if (!router.routeTree) {
366
345
  return matches;
367
346
  }
368
- 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 : [])];
369
348
  const recurse = async routes => {
370
349
  var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
371
350
  const parentMatch = utils.last(matches);
@@ -384,6 +363,15 @@ function createRouter(userOptions) {
384
363
  fuzzy,
385
364
  caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
386
365
  });
366
+
367
+ // console.log(
368
+ // router.basepath,
369
+ // route.fullPath,
370
+ // fuzzy,
371
+ // pathname,
372
+ // matchParams,
373
+ // )
374
+
387
375
  if (matchParams) {
388
376
  let parsedParams;
389
377
  try {
@@ -429,10 +417,27 @@ function createRouter(userOptions) {
429
417
  return matches;
430
418
  },
431
419
  loadMatches: async (resolvedMatches, loaderOpts) => {
432
- const matchPromises = resolvedMatches.map(async match => {
433
- var _search$__data;
420
+ resolvedMatches.forEach(async match => {
434
421
  // Validate the match (loads search params etc)
435
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;
436
441
  const search = match.search;
437
442
  if ((_search$__data = search.__data) != null && _search$__data.matchId && search.__data.matchId !== match.matchId) {
438
443
  return;
@@ -487,10 +492,10 @@ function createRouter(userOptions) {
487
492
  }
488
493
  },
489
494
  invalidateRoute: opts => {
490
- var _router$state$pending5, _router$state$pending6;
495
+ var _router$state$pending3;
491
496
  const next = router.buildNext(opts);
492
497
  const unloadedMatchIds = router.matchRoutes(next.pathname).map(d => d.matchId);
493
- [...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 => {
494
499
  if (unloadedMatchIds.includes(match.matchId)) {
495
500
  match.invalidate();
496
501
  }
@@ -513,15 +518,14 @@ function createRouter(userOptions) {
513
518
  });
514
519
  const next = router.buildNext(location);
515
520
  if (opts != null && opts.pending) {
516
- var _router$state$pending7;
517
- if (!((_router$state$pending7 = router.state.pending) != null && _router$state$pending7.location)) {
521
+ if (!router.state.pendingLocation) {
518
522
  return false;
519
523
  }
520
- 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, {
521
525
  to: next.pathname
522
526
  }));
523
527
  }
524
- 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, {
525
529
  to: next.pathname
526
530
  }));
527
531
  },
@@ -599,11 +603,11 @@ function createRouter(userOptions) {
599
603
  const preloadDelay = (_ref9 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref9 : 0;
600
604
 
601
605
  // Compare path/hash for matches
602
- const pathIsEqual = router.state.location.pathname === next.pathname;
603
- const currentPathSplit = router.state.location.pathname.split('/');
606
+ const pathIsEqual = router.state.currentLocation.pathname === next.pathname;
607
+ const currentPathSplit = router.state.currentLocation.pathname.split('/');
604
608
  const nextPathSplit = next.pathname.split('/');
605
609
  const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
606
- const hashIsEqual = router.state.location.hash === next.hash;
610
+ const hashIsEqual = router.state.currentLocation.hash === next.hash;
607
611
  // Combine the matches based on user options
608
612
  const pathTest = activeOptions != null && activeOptions.exact ? pathIsEqual : pathIsFuzzyEqual;
609
613
  const hashTest = activeOptions != null && activeOptions.includeHash ? hashIsEqual : true;
@@ -630,6 +634,9 @@ function createRouter(userOptions) {
630
634
  router.preloadRoute(nextOpts, {
631
635
  maxAge: userPreloadMaxAge,
632
636
  gcMaxAge: userPreloadGcMaxAge
637
+ }).catch(err => {
638
+ console.log(err);
639
+ console.warn('Error preloading route! ☝️');
633
640
  });
634
641
  }
635
642
  };
@@ -644,6 +651,9 @@ function createRouter(userOptions) {
644
651
  router.preloadRoute(nextOpts, {
645
652
  maxAge: userPreloadMaxAge,
646
653
  gcMaxAge: userPreloadGcMaxAge
654
+ }).catch(err => {
655
+ console.log(err);
656
+ console.warn('Error preloading route! ☝️');
647
657
  });
648
658
  }, preloadDelay);
649
659
  }
@@ -726,9 +736,9 @@ function createRouter(userOptions) {
726
736
  if (dest === void 0) {
727
737
  dest = {};
728
738
  }
729
- 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;
730
740
  let pathname = path.resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
731
- const fromMatches = router.matchRoutes(router.__location.pathname, {
741
+ const fromMatches = router.matchRoutes(router.state.latestLocation.pathname, {
732
742
  strictParseParams: true
733
743
  });
734
744
  const toMatches = router.matchRoutes(pathname);
@@ -742,7 +752,7 @@ function createRouter(userOptions) {
742
752
  pathname = path.interpolatePath(pathname, nextParams != null ? nextParams : {});
743
753
 
744
754
  // Pre filters first
745
- 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;
746
756
 
747
757
  // Then the link/navigate function
748
758
  const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
@@ -752,15 +762,15 @@ function createRouter(userOptions) {
752
762
 
753
763
  // Then post filters
754
764
  const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
755
- const search = utils.replaceEqualDeep(router.__location.search, postFilteredSearch);
765
+ const search = utils.replaceEqualDeep(router.state.latestLocation.search, postFilteredSearch);
756
766
  const searchStr = router.options.stringifySearch(search);
757
- 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);
758
768
  hash = hash ? "#" + hash : '';
759
769
  return {
760
770
  pathname,
761
771
  search,
762
772
  searchStr,
763
- state: router.__location.state,
773
+ state: router.state.latestLocation.state,
764
774
  hash,
765
775
  href: "" + pathname + searchStr + hash,
766
776
  key: dest.key