Repository: next.js
Issue: Middlewares not properly interacting with basePath
Labels: bug,Navigation,Runtime

### Link to the code that reproduces this issue

https://github.com/Rioos/next-base-bath

### To Reproduce

#### Configuring the app (you can skip that and strat from [here](https://github.com/Rioos/next-base-bath))
1. Create a NextJS app with npx
2. Add 3 routes `/index`, `/[catchRoute]`, `/subpage/[subpage]`
3. The `/index` route must have a <Link> component that directs the user to the `/subpage/[subpage]`
4. Add a `middleware.js` that does anything
5. Add a `basePath` configuration on `next.config.js`

#### Running and reproducing (you can start from [here](https://github.com/Rioos/next-base-bath))
1. Run the application with `npm run dev`
2. Go to the `/index` page `https://localhost:300/nextjs`
3. Click on the button `Magic happens here` it will navigate you to `/subpage/[subpage]`
4. Once in the subpage click the navigator back button
5. You will notice that it will try to render `/[whatever]` in your NextJS server console


### Current vs. Expected behavior

I expected it to Navigate me back to the index page but instead it navigated me to the `/[whatever]` page.

### Verify canary release

- [X] I verified that the issue exists in the latest Next.js canary release

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Thu Aug 17 21:23:05 PDT 2023; root:xnu-10002.1.11~3/RELEASE_ARM64_T6000
Binaries:
  Node: 18.18.0
  npm: 9.8.1
  Yarn: 1.22.10
  pnpm: N/A
Relevant Packages:
  next: 13.5.4-canary.0
  eslint-config-next: 13.5.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

### Additional context

It looks like it was working on version 13.4.12 and broke on 13.4.13.

Important things:
- Issue only happens if I go to the `/subpage/[subpage]` using the Next.js <Link> component
- Issue will only happen if there's a basePath configured on next.config.js
- Issue only happens if there's a `middleware.js` file in place.
- Adding a matcher like it was described [here](https://github.com/vercel/next.js/blob/56c324fb766b4500784bb6d5e6f7a37125c223b1/packages/next/src/server/lib/router-utils/resolve-routes.ts#L384) does not look to help


My investigation:
It looks like when there's a `middleware.js` file and NextJS starts handling the data request `/_next/data/build/index.json` on the `resolve-routes.ts` file it goes into that `if (route.name === "middleware_next_data")` it will then do some handling on the pathname but it looks like on [line 384](https://github.com/vercel/next.js/blob/56c324fb766b4500784bb6d5e6f7a37125c223b1/packages/next/src/server/lib/router-utils/resolve-routes.ts#L384) it does not consider the basePath in order to remove the `/index` prefix which will cascade and later on will cause the app to try rendering `/[whatever]` page.

I couldn't look much further on the issue but seems to be something like that.

Thanks.