@tanstack/react-router 0.0.1-beta.253 → 0.0.1-beta.255

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.
@@ -1613,9 +1613,10 @@ class Router {
1613
1613
  buildLocation = opts => {
1614
1614
  const build = (dest = {}, matches) => {
1615
1615
  const from = this.latestLocation;
1616
+ const fromSearch = (this.state.pendingMatches || this.state.matches).at(-1)?.search || from.search;
1616
1617
  const fromPathname = dest.from ?? from.pathname;
1617
1618
  let pathname = this.resolvePathWithBase(fromPathname, `${dest.to ?? ''}`);
1618
- const fromMatches = this.matchRoutes(fromPathname, from.search);
1619
+ const fromMatches = this.matchRoutes(fromPathname, fromSearch);
1619
1620
  const stayingMatches = matches?.filter(d => fromMatches?.find(e => e.routeId === d.routeId));
1620
1621
  const prevParams = {
1621
1622
  ...last(fromMatches)?.params
@@ -1634,7 +1635,7 @@ class Router {
1634
1635
  const postSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.postSearchFilters ?? []).flat().filter(Boolean) ?? [];
1635
1636
 
1636
1637
  // Pre filters first
1637
- const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), from.search) : from.search;
1638
+ const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), fromSearch) : fromSearch;
1638
1639
 
1639
1640
  // Then the link/navigate function
1640
1641
  const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
@@ -1644,7 +1645,7 @@ class Router {
1644
1645
 
1645
1646
  // Then post filters
1646
1647
  const postFilteredSearch = postSearchFilters?.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
1647
- const search = replaceEqualDeep(from.search, postFilteredSearch);
1648
+ const search = replaceEqualDeep(fromSearch, postFilteredSearch);
1648
1649
  const searchStr = this.options.stringifySearch(search);
1649
1650
  const hash = dest.hash === true ? from.hash : dest.hash ? functionalUpdate(dest.hash, from.hash) : from.hash;
1650
1651
  const hashStr = hash ? `#${hash}` : '';
@@ -1809,7 +1810,7 @@ class Router {
1809
1810
  for (let [index, match] of matches.entries()) {
1810
1811
  const parentMatch = matches[index - 1];
1811
1812
  const route = this.looseRoutesById[match.routeId];
1812
- const handleError = (err, code) => {
1813
+ const handleErrorAndRedirect = (err, code) => {
1813
1814
  err.routerCode = code;
1814
1815
  firstBadMatchIndex = firstBadMatchIndex ?? index;
1815
1816
  if (isRedirect(err)) {
@@ -1832,10 +1833,10 @@ class Router {
1832
1833
  };
1833
1834
  try {
1834
1835
  if (match.paramsError) {
1835
- handleError(match.paramsError, 'PARSE_PARAMS');
1836
+ handleErrorAndRedirect(match.paramsError, 'PARSE_PARAMS');
1836
1837
  }
1837
1838
  if (match.searchError) {
1838
- handleError(match.searchError, 'VALIDATE_SEARCH');
1839
+ handleErrorAndRedirect(match.searchError, 'VALIDATE_SEARCH');
1839
1840
  }
1840
1841
  const parentContext = parentMatch?.context ?? this.options.context ?? {};
1841
1842
  const beforeLoadContext = (await route.options.beforeLoad?.({
@@ -1853,6 +1854,9 @@ class Router {
1853
1854
  buildLocation: this.buildLocation,
1854
1855
  cause: match.cause
1855
1856
  })) ?? {};
1857
+ if (isRedirect(beforeLoadContext)) {
1858
+ throw beforeLoadContext;
1859
+ }
1856
1860
  const context = {
1857
1861
  ...parentContext,
1858
1862
  ...beforeLoadContext
@@ -1862,7 +1866,7 @@ class Router {
1862
1866
  context: replaceEqualDeep(match.context, context)
1863
1867
  };
1864
1868
  } catch (err) {
1865
- handleError(err, 'BEFORE_LOAD');
1869
+ handleErrorAndRedirect(err, 'BEFORE_LOAD');
1866
1870
  break;
1867
1871
  }
1868
1872
  }
@@ -1879,7 +1883,7 @@ class Router {
1879
1883
  matchPromises.push((async () => {
1880
1884
  const parentMatchPromise = matchPromises[index - 1];
1881
1885
  const route = this.looseRoutesById[match.routeId];
1882
- const handleIfRedirect = err => {
1886
+ const handleErrorAndRedirect = err => {
1883
1887
  if (isRedirect(err)) {
1884
1888
  if (!preload) {
1885
1889
  this.navigate(err);
@@ -1981,6 +1985,9 @@ class Router {
1981
1985
  try {
1982
1986
  const loaderData = await loadPromise;
1983
1987
  if (latestPromise = checkLatest()) return await latestPromise;
1988
+ if (isRedirect(loaderData)) {
1989
+ if (handleErrorAndRedirect(loaderData)) return;
1990
+ }
1984
1991
  if (didShowPending && pendingMinMs) {
1985
1992
  await new Promise(r => setTimeout(r, pendingMinMs));
1986
1993
  }
@@ -1996,12 +2003,12 @@ class Router {
1996
2003
  };
1997
2004
  } catch (error) {
1998
2005
  if (latestPromise = checkLatest()) return await latestPromise;
1999
- if (handleIfRedirect(error)) return;
2006
+ if (handleErrorAndRedirect(error)) return;
2000
2007
  try {
2001
2008
  route.options.onError?.(error);
2002
2009
  } catch (onErrorError) {
2003
2010
  error = onErrorError;
2004
- if (handleIfRedirect(onErrorError)) return;
2011
+ if (handleErrorAndRedirect(onErrorError)) return;
2005
2012
  }
2006
2013
  matches[index] = match = {
2007
2014
  ...match,