@tanstack/router-core 0.0.1-beta.51 → 0.0.1-beta.53

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.
@@ -271,7 +271,7 @@ declare class Router<TRouteConfig extends AnyRouteConfig = RouteConfig, TAllRout
271
271
  gcMaxAge?: never;
272
272
  }) => Promise<void>;
273
273
  loadMatchData: (routeMatch: RouteMatch<any, any>) => Promise<Record<string, unknown>>;
274
- invalidateRoute: (opts: MatchLocation) => Promise<void>;
274
+ invalidateRoute: <TFrom extends ValidFromPath<TAllRouteInfo> = "/", TTo extends string = ".">(opts: ToOptions<TAllRouteInfo, TFrom, TTo, ResolveRelativePath<TFrom, NoInfer<TTo>>>) => Promise<void>;
275
275
  reload: () => void;
276
276
  resolvePath: (from: string, path: string) => string;
277
277
  navigate: <TFrom extends ValidFromPath<TAllRouteInfo> = "/", TTo extends string = ".">({ from, to, search, hash, replace, params, }: NavigateOptions<TAllRouteInfo, TFrom, TTo>) => Promise<void>;
@@ -320,6 +320,7 @@ declare class RouteMatch<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRoute
320
320
  params: TRouteInfo['allParams'];
321
321
  pathname: string;
322
322
  });
323
+ setLoaderData: (loaderData: TRouteInfo['routeLoaderData']) => void;
323
324
  cancel: () => void;
324
325
  load: (loaderOpts?: {
325
326
  preload: true;
@@ -119,13 +119,8 @@
119
119
  const getLocation = () => parseLocation(entries[index], currentState);
120
120
  return createHistory({
121
121
  getLocation,
122
- listener: onUpdate => {
123
- window.addEventListener(popStateEvent, onUpdate);
124
- // We might need to handle the hashchange event in the future
125
- // window.addEventListener(hashChangeEvent, onUpdate)
126
- return () => {
127
- window.removeEventListener(popStateEvent, onUpdate);
128
- };
122
+ listener: () => {
123
+ return () => {};
129
124
  },
130
125
  pushState: (path, state) => {
131
126
  currentState = {
@@ -154,10 +149,9 @@
154
149
  function parseLocation(href, state) {
155
150
  let hashIndex = href.indexOf('#');
156
151
  let searchIndex = href.indexOf('?');
157
- const pathEnd = Math.min(hashIndex, searchIndex);
158
152
  return {
159
153
  href,
160
- pathname: pathEnd > -1 ? href.substring(0, pathEnd) : href,
154
+ pathname: href.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : href.length),
161
155
  hash: hashIndex > -1 ? href.substring(hashIndex, searchIndex) : '',
162
156
  search: searchIndex > -1 ? href.substring(searchIndex) : '',
163
157
  state
@@ -696,7 +690,7 @@
696
690
  this.store.setState(s => s.status = 'success');
697
691
  }
698
692
  }
699
- #setLoaderData = loaderData => {
693
+ setLoaderData = loaderData => {
700
694
  batch(() => {
701
695
  this.store.setState(s => {
702
696
  s.routeLoaderData = loaderData;
@@ -771,7 +765,7 @@
771
765
  if (this.route.options.loader) {
772
766
  const data = await this.router.loadMatchData(this);
773
767
  if (latestPromise = checkLatest()) return latestPromise;
774
- this.#setLoaderData(data);
768
+ this.setLoaderData(data);
775
769
  }
776
770
  this.store.setState(s => {
777
771
  s.error = undefined;
@@ -956,8 +950,7 @@
956
950
  throw new Error('Failed to fetch match data');
957
951
  };
958
952
  class Router {
959
- // __location: Location<TAllRouteInfo['fullSearchSchema']>
960
-
953
+ #unsubHistory;
961
954
  startedLoadingAt = Date.now();
962
955
  resolveNavigation = () => {};
963
956
  constructor(options) {
@@ -989,9 +982,6 @@
989
982
  if (!this.store.state.currentMatches.length) {
990
983
  this.load();
991
984
  }
992
- const unsubHistory = this.history.listen(() => {
993
- this.load(this.#parseLocation(this.store.state.latestLocation));
994
- });
995
985
  const visibilityChangeEvent = 'visibilitychange';
996
986
  const focusEvent = 'focus';
997
987
 
@@ -1004,7 +994,6 @@
1004
994
  window.addEventListener(focusEvent, this.#onFocus, false);
1005
995
  }
1006
996
  return () => {
1007
- unsubHistory();
1008
997
  if (window.removeEventListener) {
1009
998
  // Be sure to unsubscribe if a new handler is set
1010
999
 
@@ -1018,11 +1007,17 @@
1018
1007
  update = opts => {
1019
1008
  Object.assign(this.options, opts);
1020
1009
  if (!this.history || this.options.history && this.options.history !== this.history) {
1021
- this.history = this.options?.history ?? isServer ? createMemoryHistory() : createBrowserHistory();
1010
+ if (this.#unsubHistory) {
1011
+ this.#unsubHistory();
1012
+ }
1013
+ this.history = this.options.history ?? (isServer ? createMemoryHistory() : createBrowserHistory());
1022
1014
  this.store.setState(s => {
1023
1015
  s.latestLocation = this.#parseLocation();
1024
1016
  s.currentLocation = s.latestLocation;
1025
1017
  });
1018
+ this.#unsubHistory = this.history.listen(() => {
1019
+ this.load(this.#parseLocation(this.store.state.latestLocation));
1020
+ });
1026
1021
  }
1027
1022
  const {
1028
1023
  basepath,
@@ -1536,7 +1531,10 @@
1536
1531
  currentMatches.forEach((match, index) => {
1537
1532
  const dehydratedMatch = dehydratedRouter.state.currentMatches[index];
1538
1533
  invariant(dehydratedMatch && dehydratedMatch.id === match.id, 'Oh no! There was a hydration mismatch when attempting to rethis.store the state of the router! 😬');
1539
- Object.assign(match, dehydratedMatch);
1534
+ match.store.setState(s => {
1535
+ Object.assign(s, dehydratedMatch.state);
1536
+ });
1537
+ match.setLoaderData(dehydratedMatch.state.routeLoaderData);
1540
1538
  });
1541
1539
  currentMatches.forEach(match => match.__validate());
1542
1540
  Object.assign(s, {