fix(dev): replace generated interception rewrites on route rescans (#102498)
### What?
In `next dev`, a dynamic intercepting route (e.g. `@modal/(.)[id]`)
stops working after you save any file. After each save the intercepted
param gets another `(.)` prefix: `(.)1`, then `(.)(.)1`, and so on.
Lookups by that param fail, so soft navigation can show a 404 until the
dev server is restarted. `generateMetadata` gets the same corrupted
value. `next build` / `next start` are not affected.
### Why?
`startWatcher` in `setup-dev-bundler.ts` regenerates the interception
rewrites on every watcher pass. It then pushes them onto
`fsChecker.rewrites.beforeFiles` without removing the copies pushed by
earlier passes. `beforeFiles` rewrites are applied one after another,
and a dynamic interception rewrite matches its own output (`/list/:id`
also matches `/list/(.)1`). So every stale copy adds one more marker.
Stale copies also survive after the intercepting route is deleted.
### How?
The watcher now remembers the rewrite objects it added on the previous
pass. On each pass it removes exactly those objects from `beforeFiles`,
in place, before appending the current set.
- User-defined `beforeFiles` rewrites stay first, in their original
order. This includes rewrites that are conditioned on `Next-Url`, like
the generated ones.
- The array is edited in place because other code holds references to
it.
This is the same approach as #101483 by @mezotv, which I found while
comparing related PRs. #94088 by @bobu-putheeckal and #91364 by
@aleewains fix the same accumulation. They identify generated rewrites
by their header condition, which could also remove user rewrites that
use the same condition. Their route-update scenarios informed the tests
here.
Fixes #91265
### Verification
- New dev test
`test/development/app-dir/interception-dynamic-segment-rebuild`. It uses
real browser soft navigations only; no hand-built RSC/`Next-Url`
requests. It covers:
- three comment-only saves;
- params read inside `<Suspense>` and in `generateMetadata`;
- the `x-nextjs-rewritten-path` header seen by the browser;
- user `beforeFiles` rewrites, with and without a `Next-Url` condition;
- deleting and re-adding the intercepting route.
- Without the fix, both tests fail on Turbopack and webpack: `Modal
item: (.)1` and `Full page item: (.)(.)1`.
- With the fix, the new suite passed 3/3 runs with `pnpm
test-dev-turbo`, 3/3 with `pnpm test-dev-webpack`, and 1/1 with
`__NEXT_CACHE_COMPONENTS=true`.
- `pnpm test-dev-turbo test/development/app-dir/hmr-intercept-routes`:
1/1 tests passed.
- `pnpm test-dev-turbo test/e2e/app-dir/interception-dynamic-segment`:
8/8 tests passed.
- `pnpm test-dev-turbo
test/e2e/app-dir/parallel-routes-and-interception`: 63/63 tests passed.
- `pnpm lint-typescript`, Prettier and ESLint on the changed files:
passed.
- Not run: production build/start or deploy tests. The change only
affects the dev server's route watcher.
<!-- NEXT_JS_LLM -->
<!-- fleet b7b81c5b-f2a3-4f41-a367-b4724af7c207 -->
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>