Repository: next.js
Issue: Inconsistent `context.resolvedUrl` behavior in `getServerSideProps` after middleware rewrite
Labels: Middleware,Pages Router

### Link to the code that reproduces this issue

https://github.com/guinnod/next-js-issue

### To Reproduce

1. Start the application `(npm run build & npm run start)`
2. Go to the page `/old?param=oldValue`


### Current vs. Expected behavior

When accessing `/old?param=oldValue`
Expected resolvedUrl: `/new?param=newValue`
Actual resolvedUrl: `/new?param=oldValue`
### Expected Behavior
After middleware rewrites both the pathname and query parameters, `context.resolvedUrl` in `getServerSideProps` should reflect the complete rewritten URL, including both the new pathname and the new query parameters.
### Actual Behavior
While the middleware successfully rewrites the URL and the application correctly routes to the new path, context.resolvedUrl in getServerSideProps shows a mixed state:
- Pathname in `resolvedUrl` is correctly updated to the rewritten value
- Query parameters in `resolvedUrl` remain as the original values before rewrite

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.14.0
  npm: 10.7.0
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 15.2.0-canary.3 // Latest available version is detected (15.2.0-canary.3).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

Middleware, Pages Router

### Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Vercel (Deployed)

### Additional context

The bug exhibits inconsistent behavior across different projects and environments:

1. Original Production Project (Next.js 14.1.2):

- Deployed on Vercel: Bug present with incorrect pathname in resolvedUrl but correct query parameters
- Local development: Bug not reproducible even with identical Vercel environment variables
- Local production build (`next build && next start`): Bug not reproducible
- With locales in pathname (`e.g., / and /en`): Bug does not manifest
- Vercel deployment logs confirm the bug's existence in production


2. Reproduction Project (created with `npx create-next-app -e reproduction-template`):

- Shows opposite behavior: pathname is correct but query parameters are wrong
- Demonstrates that the bug's behavior is not consistent even when attempting to reproduce it



This inconsistency in behavior makes the bug particularly concerning:

- The same codebase behaves differently between Vercel deployment and local environment
- Different projects exhibit opposite symptoms (either pathname or query parameters being incorrect)
- The presence of locale routing appears to affect the bug's manifestation
- The bug's behavior is not sustainable across different setups and configurations

This variation in behavior suggests a deeper underlying issue with how URL rewrites are handled in different contexts and configurations within Next.js.