@tanstack/router-core 1.154.12 → 1.154.13

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.
@@ -1442,76 +1442,83 @@ function applySearchMiddleware({
1442
1442
  destRoutes,
1443
1443
  _includeValidateSearch
1444
1444
  }) {
1445
- const allMiddlewares = destRoutes.reduce(
1446
- (acc, route) => {
1447
- const middlewares = [];
1448
- if ("search" in route.options) {
1449
- if (route.options.search?.middlewares) {
1450
- middlewares.push(...route.options.search.middlewares);
1445
+ const middleware = buildMiddlewareChain(destRoutes);
1446
+ return middleware(search, dest, _includeValidateSearch ?? false);
1447
+ }
1448
+ function buildMiddlewareChain(destRoutes) {
1449
+ const context = {
1450
+ dest: null,
1451
+ _includeValidateSearch: false,
1452
+ middlewares: []
1453
+ };
1454
+ for (const route of destRoutes) {
1455
+ if ("search" in route.options) {
1456
+ if (route.options.search?.middlewares) {
1457
+ context.middlewares.push(...route.options.search.middlewares);
1458
+ }
1459
+ } else if (route.options.preSearchFilters || route.options.postSearchFilters) {
1460
+ const legacyMiddleware = ({ search, next }) => {
1461
+ let nextSearch = search;
1462
+ if ("preSearchFilters" in route.options && route.options.preSearchFilters) {
1463
+ nextSearch = route.options.preSearchFilters.reduce(
1464
+ (prev, next2) => next2(prev),
1465
+ search
1466
+ );
1451
1467
  }
1452
- } else if (route.options.preSearchFilters || route.options.postSearchFilters) {
1453
- const legacyMiddleware = ({
1454
- search: search2,
1455
- next
1456
- }) => {
1457
- let nextSearch = search2;
1458
- if ("preSearchFilters" in route.options && route.options.preSearchFilters) {
1459
- nextSearch = route.options.preSearchFilters.reduce(
1460
- (prev, next2) => next2(prev),
1461
- search2
1462
- );
1463
- }
1464
- const result = next(nextSearch);
1465
- if ("postSearchFilters" in route.options && route.options.postSearchFilters) {
1466
- return route.options.postSearchFilters.reduce(
1467
- (prev, next2) => next2(prev),
1468
- result
1469
- );
1470
- }
1468
+ const result = next(nextSearch);
1469
+ if ("postSearchFilters" in route.options && route.options.postSearchFilters) {
1470
+ return route.options.postSearchFilters.reduce(
1471
+ (prev, next2) => next2(prev),
1472
+ result
1473
+ );
1474
+ }
1475
+ return result;
1476
+ };
1477
+ context.middlewares.push(legacyMiddleware);
1478
+ }
1479
+ if (route.options.validateSearch) {
1480
+ const validate = ({ search, next }) => {
1481
+ const result = next(search);
1482
+ if (!context._includeValidateSearch) return result;
1483
+ try {
1484
+ const validatedSearch = {
1485
+ ...result,
1486
+ ...validateSearch(route.options.validateSearch, result) ?? void 0
1487
+ };
1488
+ return validatedSearch;
1489
+ } catch {
1471
1490
  return result;
1472
- };
1473
- middlewares.push(legacyMiddleware);
1474
- }
1475
- if (_includeValidateSearch && route.options.validateSearch) {
1476
- const validate = ({ search: search2, next }) => {
1477
- const result = next(search2);
1478
- try {
1479
- const validatedSearch = {
1480
- ...result,
1481
- ...validateSearch(route.options.validateSearch, result) ?? void 0
1482
- };
1483
- return validatedSearch;
1484
- } catch {
1485
- return result;
1486
- }
1487
- };
1488
- middlewares.push(validate);
1489
- }
1490
- return acc.concat(middlewares);
1491
- },
1492
- []
1493
- ) ?? [];
1494
- const final = ({ search: search2 }) => {
1491
+ }
1492
+ };
1493
+ context.middlewares.push(validate);
1494
+ }
1495
+ }
1496
+ const final = ({ search }) => {
1497
+ const dest = context.dest;
1495
1498
  if (!dest.search) {
1496
1499
  return {};
1497
1500
  }
1498
1501
  if (dest.search === true) {
1499
- return search2;
1502
+ return search;
1500
1503
  }
1501
- return utils.functionalUpdate(dest.search, search2);
1504
+ return utils.functionalUpdate(dest.search, search);
1502
1505
  };
1503
- allMiddlewares.push(final);
1504
- const applyNext = (index, currentSearch) => {
1505
- if (index >= allMiddlewares.length) {
1506
+ context.middlewares.push(final);
1507
+ const applyNext = (index, currentSearch, middlewares) => {
1508
+ if (index >= middlewares.length) {
1506
1509
  return currentSearch;
1507
1510
  }
1508
- const middleware = allMiddlewares[index];
1511
+ const middleware = middlewares[index];
1509
1512
  const next = (newSearch) => {
1510
- return applyNext(index + 1, newSearch);
1513
+ return applyNext(index + 1, newSearch, middlewares);
1511
1514
  };
1512
1515
  return middleware({ search: currentSearch, next });
1513
1516
  };
1514
- return applyNext(0, search);
1517
+ return function middleware(search, dest, _includeValidateSearch) {
1518
+ context.dest = dest;
1519
+ context._includeValidateSearch = _includeValidateSearch;
1520
+ return applyNext(0, search, context.middlewares);
1521
+ };
1515
1522
  }
1516
1523
  function findGlobalNotFoundRouteId(notFoundMode, routes) {
1517
1524
  if (notFoundMode !== "root") {