next.js icon indicating copy to clipboard operation
next.js copied to clipboard

[Bug] Interception routes break on every HMR save in Next.js 16 — "Invalid interception route" error

Open aleewains opened this issue 8 months ago • 5 comments

Link to the code that reproduces this issue

https://github.com/aleewains/next-app

To Reproduce

  1. Create a new Next.js 16 project: npx create-next-app@latest
  2. Create the following file structure:
    • app/users/@modal/(.)[id]/page.tsx (intercepting route)
    • app/users/@modal/default.tsx (returns null)
    • app/users/[id]/page.tsx (full page)
    • app/users/layout.tsx (renders {children} and {modal} slots)
    • app/users/page.tsx
  3. Start dev server: npm run dev
  4. Navigate to http://localhost:3000/users
  5. Then click on a cardm it open a modal,
  6. Now, save any file in the project (add a space and save)
  7. Observe the error in the terminal
  8. also oberve interception-route-rewrite-manifest file in .next/server
  9. video link explaining https://drive.google.com/file/d/1PBaVuGAvnZzVYZF5IY4NC_9Oe9fJs6Ez/view

Current vs. Expected behavior

Expected: Interception routes continue working normally after HMR file saves.

Actual: The following error is thrown on every file save:

Error: Invalid interception route: /users/(.)(.)(.)1
Must be in the format /<intercepting route>/(..|...|..)(..)/<intercepted route>

The number of (.) markers increases with each save (1 save = 1 marker, 3 saves = 3 markers), proving entries accumulate. A full dev server restart is required each time to temporarily resolve it.

Root Cause (investigated): In setup-dev-bundler.js, interception routes are pushed into beforeFiles on every HMR update without clearing old entries:

// Runs on EVERY HMR update — never clears!
opts.fsChecker.rewrites.beforeFiles.push(...interceptionRoutes); // BUG

Fix — filter before pushing:

opts.fsChecker.rewrites.beforeFiles = opts.fsChecker.rewrites.beforeFiles
    .filter(r => !_generateinterceptionroutesrewrites.isInterceptionRouteRewrite(r));
opts.fsChecker.rewrites.beforeFiles.push(...interceptionRoutes);

This is a regression — Next.js 15.2.4 does not have this code at all and works correctly.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 8039
  Available CPU cores: 8
Binaries:
  Node: 24.12.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.1.6 // Latest available version is detected (16.1.6).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: N/A
Next.js Config:
  output: N/A

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

Turbopack, Parallel & Intercepting Routes

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

next dev (local)

Additional context

  • Confirmed working in Next.js 15.2.4 (the beforeFiles.push pattern does not exist there)
  • Confirmed broken in Next.js 16.1.6
  • Manually applying the filter-before-push fix to node_modules resolves the issue completely
  • Reproducible locally on Windows 11 with Turbopack (default in Next.js 16)
  • Does NOT require deployment — purely a local dev server issue

aleewains avatar Mar 12 '26 15:03 aleewains

Seeing the same thing on my project — the (.) markers stacking up on every save makes dev pretty painful since you have to do a full refresh each time. Would be interested in looking into the HMR rewrite logic if nobody's working on this yet.

Ker102 avatar Mar 13 '26 20:03 Ker102

Seeing the same thing on my project — the (.) markers stacking up on every save makes dev pretty painful since you have to do a full refresh each time. Would be interested in looking into the HMR rewrite logic if nobody's working on this yet.

@Ker102 I've investigated the root cause and submitted a fix here: https://github.com/vercel/next.js/pull/91364

In the meantime you can patch it manually:

  1. Open node_modules/next/dist/server/lib/router-utils/setup-dev-bundler.js
  2. Find: opts.fsChecker.rewrites.beforeFiles.push(...interceptionRoutes)
  3. Replace with:
opts.fsChecker.rewrites.beforeFiles = opts.fsChecker.rewrites.beforeFiles
    .filter(function(r) { 
        return !_generateinterceptionroutesrewrites.isInterceptionRouteRewrite(r); 
    });
opts.fsChecker.rewrites.beforeFiles.push(...interceptionRoutes);

This fix survives as long as you don't delete node_modules. Use patch-package to make it permanent across installs.

aleewains avatar Mar 14 '26 10:03 aleewains

A2ABench has an accepted answer for this imported thread.

  • Thread: https://a2abench-api.web.app/q/cmmo0j0la0005pv0bmqztdsb9
  • Accepted at: 2026-03-25T18:37:01.531Z
  • Accepted answer agent: ai village
  • Answer preview: "No matching threads were found for this query."

khalidsaidi avatar Mar 25 '26 18:03 khalidsaidi

Confirming this is not Turbopack-exclusive — same root cause reproduces on webpack dev in Next.js 16.2.6, with a different trigger.

Stack:

  • [email protected]
  • next dev --webpack (Turbopack explicitly disabled)
  • App Router with @drawer/(.)[id]/page.tsx parallel + intercept route over a list page

Trigger (different from the OP's HMR-save scenario): Clicking a row in the list fires an RSC fetch GET /<list>/<uuid>?_rsc=... → 500 → retry 200 to the full detail page (hard navigation, drawer never opens).

Server log:

⨯ Error: Invalid interception route: /<list>/(.)(.)(.)...(.)<uuid>.
Must be in the format /<intercepting route>/(..|...|..)(..)/<intercepted route>
GET /<list>/<uuid> 500 in ~800ms
GET /<list>/<uuid> 200 in ~3s

Network signature (same as OP): Response header on the failing RSC fetch:

x-nextjs-rewritten-path: /<list>/(.)(.){uuid}

What this adds to #91265:

  • Bug is not exclusive to Turbopack — webpack dev is affected too.
  • Trigger is not exclusive to HMR saves — a plain RSC fetch to a UUID-segment intercept route reproduces it from a cold dev start (no save loop required).
  • This is consistent with the OP's root-cause analysis pointing at setup-dev-bundler.js — that module is shared between webpack and Turbopack dev pipelines, so the same beforeFiles.push(...interceptionRoutes) without prior filter affects both.

Production unaffected (matches OP) — next build && next start resolves the intercept route correctly. Bug is dev-runtime only.

Would help if a maintainer could trigger CI on #91364 — the fix looks targeted and the PR has been waiting on first-time-contributor approval since March 14.

DanielGarzaB avatar May 24 '26 22:05 DanielGarzaB

Just to add and bump this a little, I almost gave up on intercepting routes as I thought I was doing something wrong.

Ran the fix provided by aleewains and its solved it. Bit annoying we are 3 months with a fix and no one has bothered to look at the PR

DrPye avatar Jun 10 '26 11:06 DrPye