@tanstack/router-core 0.0.1-alpha.9 → 0.0.1-beta.2

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.
@@ -43,6 +43,7 @@ function createRouter(userOptions) {
43
43
  });
44
44
 
45
45
  let router = {
46
+ history,
46
47
  options: originalOptions,
47
48
  listeners: [],
48
49
  removeActionQueue: [],
@@ -61,6 +62,7 @@ function createRouter(userOptions) {
61
62
  location: null,
62
63
  matches: [],
63
64
  actions: {},
65
+ loaders: {},
64
66
  loaderData: {},
65
67
  lastUpdated: Date.now(),
66
68
  isFetching: false,
@@ -82,24 +84,25 @@ function createRouter(userOptions) {
82
84
  isPreloading: Object.values(router.matchCache).some(d => d.match.isFetching && !router.state.matches.find(dd => dd.matchId === d.match.matchId))
83
85
  });
84
86
  route.cascadeLoaderData(router.state.matches);
85
- router.listeners.forEach(listener => listener());
87
+ router.listeners.forEach(listener => listener(router));
86
88
  },
87
89
  mount: () => {
88
- const next = router.buildLocation({
90
+ const next = router.__.buildLocation({
89
91
  to: '.',
90
92
  search: true,
91
93
  hash: true
92
94
  }); // If the current location isn't updated, trigger a navigation
93
95
  // to the current location. Otherwise, load the current location.
94
96
 
97
+
95
98
  if (next.href !== router.location.href) {
96
- router.commitLocation(next, true);
99
+ router.__.commitLocation(next, true);
97
100
  } else {
98
101
  router.loadLocation();
99
102
  }
100
103
 
101
104
  const unsub = history.listen(event => {
102
- router.loadLocation(router.parseLocation(event.location, router.location));
105
+ router.loadLocation(router.__.parseLocation(event.location, router.location));
103
106
  }); // addEventListener does not exist in React Native, but window does
104
107
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
105
108
 
@@ -129,169 +132,11 @@ function createRouter(userOptions) {
129
132
 
130
133
  if (routeConfig) {
131
134
  router.routesById = {};
132
- router.routeTree = router.buildRouteTree(routeConfig);
135
+ router.routeTree = router.__.buildRouteTree(routeConfig);
133
136
  }
134
137
 
135
138
  return router;
136
139
  },
137
- buildRouteTree: rootRouteConfig => {
138
- const recurseRoutes = (routeConfigs, parent) => {
139
- return routeConfigs.map(routeConfig => {
140
- const routeOptions = routeConfig.options;
141
- const route$1 = route.createRoute(routeConfig, routeOptions, parent, router); // {
142
- // pendingMs: routeOptions.pendingMs ?? router.defaultPendingMs,
143
- // pendingMinMs: routeOptions.pendingMinMs ?? router.defaultPendingMinMs,
144
- // }
145
-
146
- const existingRoute = router.routesById[route$1.routeId];
147
-
148
- if (existingRoute) {
149
- if (process.env.NODE_ENV !== 'production') {
150
- console.warn("Duplicate routes found with id: " + String(route$1.routeId), router.routesById, route$1);
151
- }
152
-
153
- throw new Error();
154
- }
155
- router.routesById[route$1.routeId] = route$1;
156
- const children = routeConfig.children;
157
- route$1.childRoutes = children != null && children.length ? recurseRoutes(children, route$1) : undefined;
158
- return route$1;
159
- });
160
- };
161
-
162
- const routes = recurseRoutes([rootRouteConfig]);
163
- return routes[0];
164
- },
165
- parseLocation: (location, previousLocation) => {
166
- var _location$hash$split$;
167
-
168
- const parsedSearch = router.options.parseSearch(location.search);
169
- return {
170
- pathname: location.pathname,
171
- searchStr: location.search,
172
- search: utils.replaceEqualDeep(previousLocation == null ? void 0 : previousLocation.search, parsedSearch),
173
- hash: (_location$hash$split$ = location.hash.split('#').reverse()[0]) != null ? _location$hash$split$ : '',
174
- href: "" + location.pathname + location.search + location.hash,
175
- state: location.state,
176
- key: location.key
177
- };
178
- },
179
- buildLocation: function buildLocation(dest) {
180
- var _dest$from, _router$basepath, _dest$to, _last, _dest$params, _dest$__preSearchFilt, _functionalUpdate, _dest$__preSearchFilt2, _dest$__postSearchFil;
181
-
182
- if (dest === void 0) {
183
- dest = {};
184
- }
185
-
186
- // const resolvedFrom: Location = {
187
- // ...router.location,
188
- const fromPathname = dest.fromCurrent ? router.location.pathname : (_dest$from = dest.from) != null ? _dest$from : router.location.pathname;
189
-
190
- let pathname = path.resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
191
-
192
- const fromMatches = router.matchRoutes(router.location.pathname, {
193
- strictParseParams: true
194
- });
195
- const toMatches = router.matchRoutes(pathname);
196
-
197
- const prevParams = _rollupPluginBabelHelpers["extends"]({}, (_last = utils.last(fromMatches)) == null ? void 0 : _last.params);
198
-
199
- let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : utils.functionalUpdate(dest.params, prevParams);
200
-
201
- if (nextParams) {
202
- toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
203
- Object.assign({}, nextParams, fn(nextParams));
204
- });
205
- }
206
-
207
- pathname = path.interpolatePath(pathname, nextParams != null ? nextParams : {}); // Pre filters first
208
-
209
- const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.location.search) : router.location.search; // Then the link/navigate function
210
-
211
- const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
212
- : dest.search ? (_functionalUpdate = utils.functionalUpdate(dest.search, preFilteredSearch)) != null ? _functionalUpdate : {} // Updater
213
- : (_dest$__preSearchFilt2 = dest.__preSearchFilters) != null && _dest$__preSearchFilt2.length ? preFilteredSearch // Preserve resolvedFrom filters
214
- : {}; // Then post filters
215
-
216
- const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
217
- const search = utils.replaceEqualDeep(router.location.search, postFilteredSearch);
218
- const searchStr = router.options.stringifySearch(search);
219
- let hash = dest.hash === true ? router.location.hash : utils.functionalUpdate(dest.hash, router.location.hash);
220
- hash = hash ? "#" + hash : '';
221
- return {
222
- pathname,
223
- search,
224
- searchStr,
225
- state: router.location.state,
226
- hash,
227
- href: "" + pathname + searchStr + hash,
228
- key: dest.key
229
- };
230
- },
231
- commitLocation: (next, replace) => {
232
- const id = '' + Date.now() + Math.random();
233
- if (router.navigateTimeout) clearTimeout(router.navigateTimeout);
234
- let nextAction = 'replace';
235
-
236
- if (!replace) {
237
- nextAction = 'push';
238
- }
239
-
240
- const isSameUrl = router.parseLocation(history.location).href === next.href;
241
-
242
- if (isSameUrl && !next.key) {
243
- nextAction = 'replace';
244
- }
245
-
246
- if (nextAction === 'replace') {
247
- history.replace({
248
- pathname: next.pathname,
249
- hash: next.hash,
250
- search: next.searchStr
251
- }, {
252
- id
253
- });
254
- } else {
255
- history.push({
256
- pathname: next.pathname,
257
- hash: next.hash,
258
- search: next.searchStr
259
- }, {
260
- id
261
- });
262
- }
263
-
264
- router.navigationPromise = new Promise(resolve => {
265
- const previousNavigationResolve = router.resolveNavigation;
266
-
267
- router.resolveNavigation = () => {
268
- previousNavigationResolve();
269
- resolve();
270
- };
271
- });
272
- return router.navigationPromise;
273
- },
274
- buildNext: opts => {
275
- const next = router.buildLocation(opts);
276
- const matches = router.matchRoutes(next.pathname);
277
-
278
- const __preSearchFilters = matches.map(match => {
279
- var _match$options$preSea;
280
-
281
- return (_match$options$preSea = match.options.preSearchFilters) != null ? _match$options$preSea : [];
282
- }).flat().filter(Boolean);
283
-
284
- const __postSearchFilters = matches.map(match => {
285
- var _match$options$postSe;
286
-
287
- return (_match$options$postSe = match.options.postSearchFilters) != null ? _match$options$postSe : [];
288
- }).flat().filter(Boolean);
289
-
290
- return router.buildLocation(_rollupPluginBabelHelpers["extends"]({}, opts, {
291
- __preSearchFilters,
292
- __postSearchFilters
293
- }));
294
- },
295
140
  cancelMatches: () => {
296
141
  var _router$state$pending, _router$state$pending2;
297
142
  [...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 => {
@@ -394,6 +239,7 @@ function createRouter(userOptions) {
394
239
  params: d.params,
395
240
  search: d.search
396
241
  });
242
+ delete router.matchCache[d.matchId];
397
243
  });
398
244
 
399
245
  if (matches.some(d => d.status === 'loading')) {
@@ -553,32 +399,11 @@ function createRouter(userOptions) {
553
399
  return matches;
554
400
  },
555
401
  loadMatches: async (resolvedMatches, loaderOpts) => {
556
- const now = Date.now();
557
- const minMaxAge = loaderOpts != null && loaderOpts.preload ? Math.max(loaderOpts == null ? void 0 : loaderOpts.maxAge, loaderOpts == null ? void 0 : loaderOpts.gcMaxAge) : 0;
558
402
  const matchPromises = resolvedMatches.map(async match => {
559
403
  // Validate the match (loads search params etc)
560
- match.__.validate(); // If this is a preload, add it to the preload cache
561
-
562
-
563
- if (loaderOpts != null && loaderOpts.preload && minMaxAge > 0) {
564
- // If the match is currently active, don't preload it
565
- if (router.state.matches.find(d => d.matchId === match.matchId)) {
566
- return;
567
- }
404
+ match.__.validate();
568
405
 
569
- router.matchCache[match.matchId] = {
570
- gc: now + loaderOpts.gcMaxAge,
571
- match
572
- };
573
- } // If the match is invalid, errored or idle, trigger it to load
574
-
575
-
576
- if (match.status === 'success' && match.getIsInvalid() || match.status === 'error' || match.status === 'idle') {
577
- const maxAge = loaderOpts != null && loaderOpts.preload ? loaderOpts == null ? void 0 : loaderOpts.maxAge : undefined;
578
- match.load({
579
- maxAge
580
- });
581
- }
406
+ match.load(loaderOpts);
582
407
 
583
408
  if (match.status === 'loading') {
584
409
  // If requested, start the pending timers
@@ -602,7 +427,7 @@ function createRouter(userOptions) {
602
427
  }
603
428
  });
604
429
  },
605
- reload: () => router._navigate({
430
+ reload: () => router.__.navigate({
606
431
  fromCurrent: true,
607
432
  replace: true,
608
433
  search: true
@@ -635,10 +460,6 @@ function createRouter(userOptions) {
635
460
  to: next.pathname
636
461
  }));
637
462
  },
638
- _navigate: location => {
639
- const next = router.buildNext(location);
640
- return router.commitLocation(next, location.replace);
641
- },
642
463
  navigate: async _ref8 => {
643
464
  let {
644
465
  from,
@@ -662,7 +483,7 @@ function createRouter(userOptions) {
662
483
  } catch (e) {}
663
484
 
664
485
  tinyInvariant["default"](!isExternal, 'Attempting to navigate to external url with router.navigate!');
665
- return router._navigate({
486
+ return router.__.navigate({
666
487
  from: fromString,
667
488
  to: toString,
668
489
  search,
@@ -734,7 +555,7 @@ function createRouter(userOptions) {
734
555
  } // All is well? Navigate!)
735
556
 
736
557
 
737
- router._navigate(nextOpts);
558
+ router.__.navigate(nextOpts);
738
559
  }
739
560
  }; // The click handler
740
561
 
@@ -785,9 +606,174 @@ function createRouter(userOptions) {
785
606
  isActive,
786
607
  disabled
787
608
  };
609
+ },
610
+ buildNext: opts => {
611
+ const next = router.__.buildLocation(opts);
612
+
613
+ const matches = router.matchRoutes(next.pathname);
614
+
615
+ const __preSearchFilters = matches.map(match => {
616
+ var _match$options$preSea;
617
+
618
+ return (_match$options$preSea = match.options.preSearchFilters) != null ? _match$options$preSea : [];
619
+ }).flat().filter(Boolean);
620
+
621
+ const __postSearchFilters = matches.map(match => {
622
+ var _match$options$postSe;
623
+
624
+ return (_match$options$postSe = match.options.postSearchFilters) != null ? _match$options$postSe : [];
625
+ }).flat().filter(Boolean);
626
+
627
+ return router.__.buildLocation(_rollupPluginBabelHelpers["extends"]({}, opts, {
628
+ __preSearchFilters,
629
+ __postSearchFilters
630
+ }));
631
+ },
632
+ __: {
633
+ buildRouteTree: rootRouteConfig => {
634
+ const recurseRoutes = (routeConfigs, parent) => {
635
+ return routeConfigs.map(routeConfig => {
636
+ const routeOptions = routeConfig.options;
637
+ const route$1 = route.createRoute(routeConfig, routeOptions, parent, router); // {
638
+ // pendingMs: routeOptions.pendingMs ?? router.defaultPendingMs,
639
+ // pendingMinMs: routeOptions.pendingMinMs ?? router.defaultPendingMinMs,
640
+ // }
641
+
642
+ const existingRoute = router.routesById[route$1.routeId];
643
+
644
+ if (existingRoute) {
645
+ if (process.env.NODE_ENV !== 'production') {
646
+ console.warn("Duplicate routes found with id: " + String(route$1.routeId), router.routesById, route$1);
647
+ }
648
+
649
+ throw new Error();
650
+ }
651
+ router.routesById[route$1.routeId] = route$1;
652
+ const children = routeConfig.children;
653
+ route$1.childRoutes = children != null && children.length ? recurseRoutes(children, route$1) : undefined;
654
+ return route$1;
655
+ });
656
+ };
657
+
658
+ const routes = recurseRoutes([rootRouteConfig]);
659
+ return routes[0];
660
+ },
661
+ parseLocation: (location, previousLocation) => {
662
+ var _location$hash$split$;
663
+
664
+ const parsedSearch = router.options.parseSearch(location.search);
665
+ return {
666
+ pathname: location.pathname,
667
+ searchStr: location.search,
668
+ search: utils.replaceEqualDeep(previousLocation == null ? void 0 : previousLocation.search, parsedSearch),
669
+ hash: (_location$hash$split$ = location.hash.split('#').reverse()[0]) != null ? _location$hash$split$ : '',
670
+ href: "" + location.pathname + location.search + location.hash,
671
+ state: location.state,
672
+ key: location.key
673
+ };
674
+ },
675
+ navigate: location => {
676
+ const next = router.buildNext(location);
677
+ return router.__.commitLocation(next, location.replace);
678
+ },
679
+ buildLocation: function buildLocation(dest) {
680
+ var _dest$from, _router$basepath, _dest$to, _last, _dest$params, _dest$__preSearchFilt, _functionalUpdate, _dest$__preSearchFilt2, _dest$__postSearchFil;
681
+
682
+ if (dest === void 0) {
683
+ dest = {};
684
+ }
685
+
686
+ // const resolvedFrom: Location = {
687
+ // ...router.location,
688
+ const fromPathname = dest.fromCurrent ? router.location.pathname : (_dest$from = dest.from) != null ? _dest$from : router.location.pathname;
689
+
690
+ let pathname = path.resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
691
+
692
+ const fromMatches = router.matchRoutes(router.location.pathname, {
693
+ strictParseParams: true
694
+ });
695
+ const toMatches = router.matchRoutes(pathname);
696
+
697
+ const prevParams = _rollupPluginBabelHelpers["extends"]({}, (_last = utils.last(fromMatches)) == null ? void 0 : _last.params);
698
+
699
+ let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : utils.functionalUpdate(dest.params, prevParams);
700
+
701
+ if (nextParams) {
702
+ toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
703
+ Object.assign({}, nextParams, fn(nextParams));
704
+ });
705
+ }
706
+
707
+ pathname = path.interpolatePath(pathname, nextParams != null ? nextParams : {}); // Pre filters first
708
+
709
+ const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.location.search) : router.location.search; // Then the link/navigate function
710
+
711
+ const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
712
+ : dest.search ? (_functionalUpdate = utils.functionalUpdate(dest.search, preFilteredSearch)) != null ? _functionalUpdate : {} // Updater
713
+ : (_dest$__preSearchFilt2 = dest.__preSearchFilters) != null && _dest$__preSearchFilt2.length ? preFilteredSearch // Preserve resolvedFrom filters
714
+ : {}; // Then post filters
715
+
716
+ const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
717
+ const search = utils.replaceEqualDeep(router.location.search, postFilteredSearch);
718
+ const searchStr = router.options.stringifySearch(search);
719
+ let hash = dest.hash === true ? router.location.hash : utils.functionalUpdate(dest.hash, router.location.hash);
720
+ hash = hash ? "#" + hash : '';
721
+ return {
722
+ pathname,
723
+ search,
724
+ searchStr,
725
+ state: router.location.state,
726
+ hash,
727
+ href: "" + pathname + searchStr + hash,
728
+ key: dest.key
729
+ };
730
+ },
731
+ commitLocation: (next, replace) => {
732
+ const id = '' + Date.now() + Math.random();
733
+ if (router.navigateTimeout) clearTimeout(router.navigateTimeout);
734
+ let nextAction = 'replace';
735
+
736
+ if (!replace) {
737
+ nextAction = 'push';
738
+ }
739
+
740
+ const isSameUrl = router.__.parseLocation(history.location).href === next.href;
741
+
742
+ if (isSameUrl && !next.key) {
743
+ nextAction = 'replace';
744
+ }
745
+
746
+ if (nextAction === 'replace') {
747
+ history.replace({
748
+ pathname: next.pathname,
749
+ hash: next.hash,
750
+ search: next.searchStr
751
+ }, {
752
+ id
753
+ });
754
+ } else {
755
+ history.push({
756
+ pathname: next.pathname,
757
+ hash: next.hash,
758
+ search: next.searchStr
759
+ }, {
760
+ id
761
+ });
762
+ }
763
+
764
+ router.navigationPromise = new Promise(resolve => {
765
+ const previousNavigationResolve = router.resolveNavigation;
766
+
767
+ router.resolveNavigation = () => {
768
+ previousNavigationResolve();
769
+ resolve();
770
+ };
771
+ });
772
+ return router.navigationPromise;
773
+ }
788
774
  }
789
775
  };
790
- router.location = router.parseLocation(history.location);
776
+ router.location = router.__.parseLocation(history.location);
791
777
  router.state.location = router.location;
792
778
  router.update(userOptions); // Allow frameworks to hook into the router creation
793
779