Repository: TanStack/router
Pull request #2603 (2024-10-20 22:00:09)

fix(react-router): run search middlewares also on entering matches
this will make search middlewares correctly execute upon redirect

nx-cloud[bot]: # ☁️ Nx Cloud Report

CI is running for commit fbae5a13ae4139b317ee2eb6c5dbb6f6777dfd40.

[📂 Click to track the progress, see the status, the terminal output, and the build insights.](https://cloud.nx.app/cipes/67157d8083a9e2124a328c71?utm_source=pull-request&utm_medium=comment)


---

Sent with 💌 from [NxCloud.](https://nx.app?utm_source=pull-request&utm_medium=comment)
pkg-pr-new[bot]: 
[Open in Stackblitz](https://pkg.pr.new/template/f0d33b72-7921-4665-8128-11522c483ba3)
<details><summary><b>More templates</b></summary><p>

- [tanstack-router-react-example-authenticated-routes](https://pkg.pr.new/template/a85f92c3-f502-4607-b6a2-4a33824996c8)
- [tanstack-router-react-example-basic](https://pkg.pr.new/template/1037e86f-fe7f-4560-92f8-f078f0f01147)
- [tanstack-router-react-example-basic-default-search-params](https://pkg.pr.new/template/f06fde3a-b11b-4d70-b5fe-30aa65ed0c5c)
- [tanstack-router-react-example-basic-file-based](https://pkg.pr.new/template/e6d92a64-c6e5-4ad0-a160-bee9867563eb)
- [tanstack-router-react-example-basic-file-based-codesplitting](https://pkg.pr.new/template/68766fc6-b996-4c61-a9b0-c49d0619daa3)
- [tanstack-router-react-example-react-query](https://pkg.pr.new/template/64429b14-5a06-4b03-8da1-a4f45d3a0645)
- [tanstack-router-react-example-basic-react-query-file-based](https://pkg.pr.new/template/8076546d-1560-4a8c-be99-6be97b63e6de)
- [tanstack-router-react-example-basic-ssr-streaming-file-based](https://pkg.pr.new/template/a60898ca-64e4-409b-ba25-34d7ec2cbe06)
- [tanstack-router-react-example-basic-virtual-file-based](https://pkg.pr.new/template/60b0f2be-b6c2-4546-851b-40a98be13523)
- [tanstack-router-react-example-basic-virtual-inside-file-based](https://pkg.pr.new/template/ba4f408b-47f7-4415-8afc-bcd169290922)
- [tanstack-router-react-example-deferred-data](https://pkg.pr.new/template/1b9555dc-8ba1-428c-b65d-d242d0628d3f)
- [tanstack-router-react-example-kitchen-sink](https://pkg.pr.new/template/03ffa373-41c6-43b1-be05-d8eac9c6259a)
- [tanstack-router-react-example-kitchen-sink-file-based](https://pkg.pr.new/template/8d67002b-c3a4-4616-b969-4937def3c961)
- [tanstack-router-react-example-kitchen-sink-react-query](https://pkg.pr.new/template/d7a5f88a-09e6-4226-be55-377c7272daa5)
- [tanstack-router-react-example-kitchen-sink-react-query-file-based](https://pkg.pr.new/template/d66ca470-b469-40b3-8ec3-9a4bba74b48e)
- [tanstack-router-react-example-large-file-based](https://pkg.pr.new/template/8100dfd1-1588-4756-9a33-f7a79360dd25)
- [tanstack-router-react-example-basic-ssr-file-based](https://pkg.pr.new/template/845cc9c3-0f29-476f-843d-a0ff79ff931d)
- [tanstack-router-react-example-location-masking](https://pkg.pr.new/template/71ab5419-35b8-433a-8b16-2c196adc6ffe)
- [tanstack-router-react-example-navigation-blocking](https://pkg.pr.new/template/66c5d83e-8dba-4af4-b9a5-b0c8295fad3c)
- [tanstack-router-react-example-quickstart](https://pkg.pr.new/template/a0fa360b-ce4b-4efe-8768-70f678cb00ff)
- [tanstack-router-react-example-quickstart-file-based](https://pkg.pr.new/template/2955bd92-84a6-4721-94c9-ba2aa0e2cf3d)
- [tanstack-router-react-example-quickstart-rspack-file-based](https://pkg.pr.new/template/b8785324-c7ec-460f-9351-5b333f9a620d)
- [router-monorepo-react-query](https://pkg.pr.new/template/2be5d908-87cf-4fce-be1f-be371b81a305)
- [tanstack-router-react-example-quickstart-webpack-file-based](https://pkg.pr.new/template/a9f360d3-27f4-441a-9ce6-dfaf062ea663)
- [router-mono-simple](https://pkg.pr.new/template/8cf7134a-f79e-4440-be4c-3a19be96afcf)
- [tanstack-router-react-example-scroll-restoration](https://pkg.pr.new/template/1282efea-1ace-457a-bc8e-1147473b98b4)
- [tanstack-router-search-validator-adapters](https://pkg.pr.new/template/1ee3d93d-16c3-4003-9926-0ec719b49062)
- [tanstack-start-example-basic](https://pkg.pr.new/template/daed29ee-5a93-4f69-a59e-58b2acb055a8)
- [tanstack-start-example-basic-auth](https://pkg.pr.new/template/9aa30f52-c864-4670-95c3-0da91d5c2f24)
- [tanstack-start-example-basic-react-query](https://pkg.pr.new/template/1536c4b7-b181-4c93-88a1-132438f94ea4)
- [tanstack-start-example-basic-rsc](https://pkg.pr.new/template/3afdf09c-c7c0-4cef-b3cb-3659200d00ec)
- [tanstack-start-example-clerk-basic](https://pkg.pr.new/template/72fd6a07-8af9-4024-84b1-a295cdd66548)
- [tanstack-start-example-convex-trellaux](https://pkg.pr.new/template/83547ed6-a04c-49b4-b586-d4e99387a593)
- [tanstack-start-example-counter](https://pkg.pr.new/template/f6d7462a-b8f1-4c09-892e-13c48cfb0550)
- [tanstack-start-example-supabase-basic](https://pkg.pr.new/template/73d9c6ba-cb8a-4932-81bc-aaf4087438bb)
- [tanstack-router-react-example-with-framer-motion](https://pkg.pr.new/template/c2b40cb2-f33c-430b-8c60-964012571ee3)
- [tanstack-start-example-trellaux](https://pkg.pr.new/template/c1777c5f-3aa7-40dc-86c8-6c917249a0f8)
- [tanstack-router-react-example-with-trpc](https://pkg.pr.new/template/b8b35100-45fe-4216-a257-86ee255626b0)
- [tanstack-router-react-example-with-trpc-react-query](https://pkg.pr.new/template/cc65a52b-48f5-4cb5-b9e2-807b8d393b3a)

</p></details>
      
    


<details><summary><b>@tanstack/create-router</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/create-router@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/eslint-plugin-router</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/eslint-plugin-router@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/react-cross-context</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-cross-context@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/react-router</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-router@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/react-router-with-query</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-router-with-query@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-cli</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-cli@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-arktype-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-arktype-adapter@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-devtools</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-devtools@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-generator</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-generator@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-plugin@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-valibot-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-valibot-adapter@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-vite-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-vite-plugin@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/router-zod-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-zod-adapter@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/start-vite-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/start-vite-plugin@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/start</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/start@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/virtual-file-routes</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/virtual-file-routes@2603
```

</p></details>
      
    

<details><summary><b>@tanstack/history</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/history@2603
```

</p></details>
      
    

_commit: <a href="https://github.com/TanStack/router/runs/31797070912"><code>1fefeea</code></a>_


Patch:
diff --git a/packages/react-router/package.json b/packages/react-router/package.json
index 6eb7dfe1a..b9be58c21 100644
--- a/packages/react-router/package.json
+++ b/packages/react-router/package.json
@@ -81,9 +81,9 @@
     "zod": "^3.23.8"
   },
   "peerDependencies": {
+    "@tanstack/router-generator": "workspace:*",
     "react": ">=18",
-    "react-dom": ">=18",
-    "@tanstack/router-generator": "workspace:*"
+    "react-dom": ">=18"
   },
   "peerDependenciesMeta": {
     "@tanstack/router-generator": {
diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts
index edeb675b2..e06457fb7 100644
--- a/packages/react-router/src/router.ts
+++ b/packages/react-router/src/router.ts
@@ -1390,7 +1390,6 @@ export class Router<
       const stayingMatches = matchedRoutesResult?.matchedRoutes.filter((d) =>
         fromMatches.find((e) => e.routeId === d.id),
       )
-
       let pathname: string
       if (dest.to) {
         pathname = this.resolvePathWithBase(fromPath, `${dest.to}`)
@@ -1463,7 +1462,7 @@ export class Router<
 
       const applyMiddlewares = (search: any) => {
         const allMiddlewares =
-          stayingMatches?.reduce(
+          matchedRoutesResult?.matchedRoutes.reduce(
             (acc, route) => {
               let middlewares: Array<SearchMiddleware<any>> = []
               if ('search' in route.options) {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 8bc14048f..4ec7212bc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1578,10 +1578,10 @@ importers:
         version: 18.3.0
       html-webpack-plugin:
         specifier: ^5.6.0
-        version: 5.6.0(@rspack/core@1.0.10(@swc/helpers@0.5.13))(webpack@5.95.0)
+        version: 5.6.0(@rspack/core@1.0.10(@swc/helpers@0.5.13))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       swc-loader:
         specifier: ^0.2.6
-        version: 0.2.6(@swc/core@1.7.35(@swc/helpers@0.5.13))(webpack@5.95.0)
+        version: 0.2.6(@swc/core@1.7.35(@swc/helpers@0.5.13))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       typescript:
         specifier: ^5.6.2
         version: 5.6.2
@@ -2726,7 +2726,7 @@ importers:
         version: 4.3.2(vite@5.4.8(@types/node@22.7.4)(terser@5.31.1))
       html-webpack-plugin:
         specifier: ^5.6.0
-        version: 5.6.0(@rspack/core@1.0.10(@swc/helpers@0.5.13))(webpack@5.95.0)
+        version: 5.6.0(@rspack/core@1.0.10(@swc/helpers@0.5.13))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       react:
         specifier: ^18.3.1
         version: 18.3.1
@@ -2735,7 +2735,7 @@ importers:
         version: 18.3.1(react@18.3.1)
       swc-loader:
         specifier: ^0.2.6
-        version: 0.2.6(@swc/core@1.7.35(@swc/helpers@0.5.13))(webpack@5.95.0)
+        version: 0.2.6(@swc/core@1.7.35(@swc/helpers@0.5.13))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       typescript:
         specifier: ^5.6.2
         version: 5.6.2
@@ -5973,7 +5973,7 @@ packages:
     resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
 
   batch@0.6.1:
-    resolution: {integrity: sha1-3DQxT05nkxgJP8dgJyUl+UvyXBY=}
+    resolution: {integrity: sha512-x+VAiMRL6UPkx+kudNvxTl6hB2XNNCG2r+7wixVfIYwu/2HKRXimwQyaumLjMveWvT2Hkd/cAJw+QBMfJ/EKVw==}
 
   binary-extensions@2.2.0:
     resolution: {integrity: sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==}
@@ -6039,7 +6039,7 @@ packages:
     engines: {node: '>=18'}
 
   bytes@3.0.0:
-    resolution: {integrity: sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=}
+    resolution: {integrity: sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==}
     engines: {node: '>= 0.8'}
 
   bytes@3.1.2:
@@ -6285,7 +6285,7 @@ packages:
     resolution: {integrity: sha512-7CEBgcMjVmitjYo5q8JTJVra6X5mQ20uTThdK+0kR7UEaDrAWEQcRiBtWJzga4eRpP6afNwwLsX2SET2JhVB1Q==}
 
   concat-map@0.0.1:
-    resolution: {integrity: sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=}
+    resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}
 
   concurrently@8.2.2:
     resolution: {integrity: sha512-1dP4gpXFhei8IOtlXRE/T/4H88ElHgTiUzh71YUmtjTEHMSRS2Z/fgOxHSxxusGHogsRfxNq1vyAwxSC+EVyDg==}
@@ -6349,7 +6349,7 @@ packages:
     resolution: {integrity: sha512-L2rLOcK0wzWSfSDA33YR+PUHDG10a8px7rUHKWbGLP4YfbsMed2KFUw5fczvDPbT98DDe3LEzviswl810apTEw==}
 
   cookie-signature@1.0.6:
-    resolution: {integrity: sha1-4wOogrNCzD7oylE6eZmXNNqzriw=}
+    resolution: {integrity: sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==}
 
   cookie@0.5.0:
     resolution: {integrity: sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==}
@@ -6705,7 +6705,7 @@ packages:
     resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
 
   ee-first@1.1.1:
-    resolution: {integrity: sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=}
+    resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==}
 
   electron-to-chromium@1.5.32:
     resolution: {integrity: sha512-M+7ph0VGBQqqpTT2YrabjNKSQ2fEl9PVx6AK3N558gDH9NO8O6XN9SXXFWRo9u9PbEg/bWq+tjXQr+eXmxubCw==}
@@ -7156,7 +7156,7 @@ packages:
         optional: true
 
   fresh@0.5.2:
-    resolution: {integrity: sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=}
+    resolution: {integrity: sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==}
     engines: {node: '>= 0.6'}
 
   front-matter@4.0.2:
@@ -7384,7 +7384,7 @@ packages:
     resolution: {integrity: sha512-Yc+BQe8SvoXH1643Qez1zqLRmbA5rCL+sSmk6TVos0LWVfNIB7PGncdlId77WzLGSIB5KaWgTaNTs2lNVEI6VQ==}
 
   hpack.js@2.1.6:
-    resolution: {integrity: sha1-h3dMCUnlE/QuhFdbPEVoH63ioLI=}
+    resolution: {integrity: sha512-zJxVehUdMGIKsRaNt7apO2Gqp0BdqW5yaiGHXXmbpvxgBYVZnAql+BJb4RO5ad2MgpbZKn5G6nMnegrH1FcNYQ==}
 
   html-encoding-sniffer@4.0.0:
     resolution: {integrity: sha512-Y22oTqIU4uuPgEemfz7NDJz6OeKf12Lsu+QC+s3BVpda64lTiMYCyGwg5ki4vFxkMwQdeZDl2adZoqUgdFuTgQ==}
@@ -7417,7 +7417,7 @@ packages:
     resolution: {integrity: sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A==}
 
   http-deceiver@1.2.7:
-    resolution: {integrity: sha1-+nFolEq5pRnTN8sL7HKE3D5yPYc=}
+    resolution: {integrity: sha512-LmpOGxTfbpgtGVxJrj5k7asXHCgNZp5nLfp+hWc8QQRqtb7fUy6kRY3BO1h9ddF6yIPYUARgxGOwB42DnxIaNw==}
 
   http-errors@1.6.3:
     resolution: {integrity: sha512-lks+lVC8dgGyh97jxvxeYTWQFvh4uw4yC12gVl63Cg30sjPX4wuGcdkICVXDAESr6OJGjqGA8Iz5mkeN6zlD7A==}
@@ -7911,7 +7911,7 @@ packages:
     resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==}
 
   lodash.uniq@4.5.0:
-    resolution: {integrity: sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=}
+    resolution: {integrity: sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==}
 
   lodash@4.17.21:
     resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==}
@@ -8000,7 +8000,7 @@ packages:
     resolution: {integrity: sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w==}
 
   media-typer@0.3.0:
-    resolution: {integrity: sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=}
+    resolution: {integrity: sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==}
     engines: {node: '>= 0.6'}
 
   memfs@4.9.3:
@@ -9152,7 +9152,7 @@ packages:
     resolution: {integrity: sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==}
 
   select-hose@2.0.0:
-    resolution: {integrity: sha1-Yl2GWPhlr0Psliv8N2o3NZpJlMo=}
+    resolution: {integrity: sha512-mEugaLK+YfkijB4fx0e6kImuJdCIt2LxCRcbEYPqRGCs4F2ogyfZU5IAZRdjCP8JPq2AtdNoC/Dux63d9Kiryg==}
 
   selfsigned@2.4.1:
     resolution: {integrity: sha512-th5B4L2U+eGLq1TVh7zNRGBapioSORUeymIydxgFpwww9d2qyKvtuPU2jJuHvYAwwqi2Y596QBL3eEqcPEYL8Q==}
@@ -9180,7 +9180,7 @@ packages:
     resolution: {integrity: sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==}
 
   serve-index@1.9.1:
-    resolution: {integrity: sha1-03aNabHn2C5c4FD/9bRTvqEqkjk=}
+    resolution: {integrity: sha512-pXHfKNP4qujrtteMrSBb0rc8HJ9Ms/GrXwcUtUtD5s4ewDJI8bT3Cz2zTVRMKtri49pLx2e0Ya8ziP5Ya2pZZw==}
     engines: {node: '>= 0.8.0'}
 
   serve-placeholder@2.0.2:
@@ -9946,10 +9946,10 @@ packages:
     resolution: {integrity: sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==}
 
   utila@0.4.0:
-    resolution: {integrity: sha1-ihagXURWV6Oupe7MWxKk+lN5dyw=}
+    resolution: {integrity: sha512-Z0DbgELS9/L/75wZbro8xAnT50pBVFQZ+hUEueGDU5FN51YSCYM+jdxsfCiHjwNP/4LCDD0i/graKpeBnOXKRA==}
 
   utils-merge@1.0.1:
-    resolution: {integrity: sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=}
+    resolution: {integrity: sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==}
     engines: {node: '>= 0.4.0'}
 
   uuid@8.3.2:
@@ -12916,17 +12916,17 @@ snapshots:
       '@webassemblyjs/ast': 1.12.1
       '@xtuc/long': 4.2.2
 
-  '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4)(webpack@5.95.0)':
+  '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))':
     dependencies:
       webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)
       webpack-cli: 5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0)
 
-  '@webpack-cli/info@2.0.2(webpack-cli@5.1.4)(webpack@5.95.0)':
+  '@webpack-cli/info@2.0.2(webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))':
     dependencies:
       webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)
       webpack-cli: 5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0)
 
-  '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4)(webpack-dev-server@5.1.0)(webpack@5.95.0)':
+  '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0))(webpack-dev-server@5.1.0(webpack-cli@5.1.4)(webpack@5.95.0))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))':
     dependencies:
       webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)
       webpack-cli: 5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0)
@@ -14872,7 +14872,7 @@ snapshots:
 
   html-void-elements@3.0.0: {}
 
-  html-webpack-plugin@5.6.0(@rspack/core@1.0.10(@swc/helpers@0.5.13))(webpack@5.95.0):
+  html-webpack-plugin@5.6.0(@rspack/core@1.0.10(@swc/helpers@0.5.13))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)):
     dependencies:
       '@types/html-minifier-terser': 6.1.0
       html-minifier-terser: 6.1.0
@@ -17020,7 +17020,7 @@ snapshots:
       csso: 5.0.5
       picocolors: 1.1.0
 
-  swc-loader@0.2.6(@swc/core@1.7.35(@swc/helpers@0.5.13))(webpack@5.95.0):
+  swc-loader@0.2.6(@swc/core@1.7.35(@swc/helpers@0.5.13))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)):
     dependencies:
       '@swc/core': 1.7.35(@swc/helpers@0.5.13)
       '@swc/counter': 0.1.3
@@ -17090,26 +17090,26 @@ snapshots:
       mkdirp: 1.0.4
       yallist: 4.0.0
 
-  terser-webpack-plugin@5.3.10(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)):
+  terser-webpack-plugin@5.3.10(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)):
     dependencies:
       '@jridgewell/trace-mapping': 0.3.25
       jest-worker: 27.5.1
       schema-utils: 3.3.0
       serialize-javascript: 6.0.2
       terser: 5.31.1
-      webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)
+      webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)
     optionalDependencies:
       '@swc/core': 1.7.35(@swc/helpers@0.5.13)
       esbuild: 0.23.1
 
-  terser-webpack-plugin@5.3.10(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack@5.95.0):
+  terser-webpack-plugin@5.3.10(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)):
     dependencies:
       '@jridgewell/trace-mapping': 0.3.25
       jest-worker: 27.5.1
       schema-utils: 3.3.0
       serialize-javascript: 6.0.2
       terser: 5.31.1
-      webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)
+      webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)
     optionalDependencies:
       '@swc/core': 1.7.35(@swc/helpers@0.5.13)
       esbuild: 0.23.1
@@ -17772,9 +17772,9 @@ snapshots:
   webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0):
     dependencies:
       '@discoveryjs/json-ext': 0.5.7
-      '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4)(webpack@5.95.0)
-      '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4)(webpack@5.95.0)
-      '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4)(webpack-dev-server@5.1.0)(webpack@5.95.0)
+      '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
+      '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
+      '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4(webpack-dev-server@5.1.0)(webpack@5.95.0))(webpack-dev-server@5.1.0(webpack-cli@5.1.4)(webpack@5.95.0))(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       colorette: 2.0.20
       commander: 10.0.1
       cross-spawn: 7.0.3
@@ -17788,7 +17788,7 @@ snapshots:
     optionalDependencies:
       webpack-dev-server: 5.1.0(webpack-cli@5.1.4)(webpack@5.95.0)
 
-  webpack-dev-middleware@7.4.2(webpack@5.95.0):
+  webpack-dev-middleware@7.4.2(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)):
     dependencies:
       colorette: 2.0.20
       memfs: 4.9.3
@@ -17827,7 +17827,7 @@ snapshots:
       serve-index: 1.9.1
       sockjs: 0.3.24
       spdy: 4.0.2
-      webpack-dev-middleware: 7.4.2(webpack@5.95.0)
+      webpack-dev-middleware: 7.4.2(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       ws: 8.18.0
     optionalDependencies:
       webpack: 5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4)
@@ -17900,7 +17900,7 @@ snapshots:
       neo-async: 2.6.2
       schema-utils: 3.3.0
       tapable: 2.2.1
-      terser-webpack-plugin: 5.3.10(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack@5.95.0)
+      terser-webpack-plugin: 5.3.10(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack@5.95.0(@swc/core@1.7.35(@swc/helpers@0.5.13))(esbuild@0.23.1)(webpack-cli@5.1.4))
       watchpack: 2.4.1
       webpack-sources: 3.2.3
     optionalDependencies:


Test patch:
diff --git a/packages/react-router/tests/link.test.tsx b/packages/react-router/tests/link.test.tsx
index bce18a322..fd62c0d3a 100644
--- a/packages/react-router/tests/link.test.tsx
+++ b/packages/react-router/tests/link.test.tsx
@@ -11,6 +11,7 @@ import {
   waitFor,
 } from '@testing-library/react'
 
+import { z } from 'zod'
 import {
   Link,
   Outlet,
@@ -23,6 +24,8 @@ import {
   createRouteMask,
   createRouter,
   redirect,
+  retainSearchParams,
+  stripSearchParams,
   useLoaderData,
   useMatchRoute,
   useParams,
@@ -4069,4 +4072,114 @@ describe('search middleware', () => {
     await checkPostsLink('newValue')
     expect(router.state.location.search).toEqual({ root: 'newValue' })
   })
+
+  test('search middlewares work with redirect', async () => {
+    const rootRoute = createRootRoute({
+      validateSearch: z.object({ root: z.string().optional() }),
+      component: () => {
+        return (
+          <>
+            <h1>Root</h1>
+            <Link
+              data-testid="root-link-posts"
+              search={{ foo: 'default' }}
+              to="/posts"
+            >
+              posts
+            </Link>{' '}
+            <Link data-testid="root-link-invoices" to="/invoices">
+              invoices
+            </Link>
+            <Outlet />
+          </>
+        )
+      },
+    })
+    const indexRoute = createRoute({
+      getParentRoute: () => rootRoute,
+      path: '/',
+      beforeLoad: () => {
+        throw redirect({ to: '/posts' })
+      },
+    })
+
+    const postsRoute = createRoute({
+      getParentRoute: () => rootRoute,
+      path: 'posts',
+      validateSearch: z.object({
+        foo: z.string().default('default'),
+      }),
+      search: {
+        middlewares: [
+          // @ts-expect-error we cannot use zodSearchValidator here to due to circular dependency
+          // this means we cannot get the correct input type for this schema
+          stripSearchParams({ foo: 'default' }),
+          retainSearchParams(true),
+        ],
+      },
+
+      component: () => {
+        const { foo } = postsRoute.useSearch()
+        return (
+          <>
+            <h1>Posts</h1>
+            <div data-testid="posts-search">{foo}</div>
+            <Link data-testid="posts-link-new" to="/posts/new">
+              new
+            </Link>
+          </>
+        )
+      },
+    })
+
+    const postsNewRoute = createRoute({
+      getParentRoute: () => postsRoute,
+      path: 'new',
+    })
+
+    const invoicesRoute = createRoute({
+      getParentRoute: () => rootRoute,
+      path: 'invoices',
+    })
+    const router = createRouter({
+      routeTree: rootRoute.addChildren([
+        indexRoute,
+        postsRoute.addChildren([postsNewRoute]),
+        invoicesRoute,
+      ]),
+    })
+
+    window.history.replaceState(null, 'root', '/?root=abc')
+
+    render(<RouterProvider router={router} />)
+
+    const searchValue = await screen.findByTestId('posts-search')
+    expect(searchValue).toHaveTextContent('default')
+
+    expect(router.state.location.pathname).toBe('/posts')
+    expect(router.state.location.search).toEqual({ root: 'abc' })
+
+    // link to sibling does not retain search param
+    const invoicesLink = await screen.findByTestId('root-link-invoices')
+    expect(invoicesLink).toHaveAttribute('href')
+    const invoicesLinkHref = invoicesLink.getAttribute('href')
+    const invoicesLinkSearch = getSearchParamsFromURI(invoicesLinkHref!)
+    expect(invoicesLinkSearch.size).toBe(0)
+
+    // link to child retains search param
+    const postsNewLink = await screen.findByTestId('posts-link-new')
+    expect(postsNewLink).toHaveAttribute('href')
+    const postsNewLinkHref = postsNewLink.getAttribute('href')
+    const postsNewLinkSearch = getSearchParamsFromURI(postsNewLinkHref!)
+    expect(postsNewLinkSearch.size).toBe(1)
+    expect(postsNewLinkSearch.get('root')).toBe('abc')
+
+    const postsLink = await screen.findByTestId('root-link-posts')
+    expect(postsLink).toHaveAttribute('href')
+    const postsLinkHref = postsNewLink.getAttribute('href')
+    const postsLinkSearch = getSearchParamsFromURI(postsLinkHref!)
+    expect(postsLinkSearch.size).toBe(1)
+    expect(postsLinkSearch.get('root')).toBe('abc')
+    expect(postsLink).toHaveAttribute('data-status', 'active')
+  })
 })
diff --git a/packages/react-router/tests/router.test.tsx b/packages/react-router/tests/router.test.tsx
index 25e669663..3a102570b 100644
--- a/packages/react-router/tests/router.test.tsx
+++ b/packages/react-router/tests/router.test.tsx
@@ -759,33 +759,6 @@ describe('search params in URL', () => {
   })
 })
 
-/*
-  it('updates search params in URL upon initial load according to the result of validateSearch', async () => {
-    const { router } = createTestRouter({
-      history: createMemoryHistory({ initialEntries: ['/'] }),
-    })
-
-    await act(() => router.load())
-
-    router.state.matches.forEach((match) => {
-      expect(match.invalid).toBe(false)
-    })
-
-    await act(() => router.invalidate())
-
-    router.state.matches.forEach((match) => {
-      expect(match.invalid).toBe(false)
-    })
-
-    await act(() => router.load())
-
-    router.state.matches.forEach((match) => {
-      expect(match.invalid).toBe(false)
-    })
-  })
-})
-  */
-
 describe('route ids should be consistent after rebuilding the route tree', () => {
   it('should have the same route ids after rebuilding the route tree', async () => {
     const { router } = createTestRouter({