vercel / vercel/next.js

Config redirects fire on _next/data requests when middleware.ts is present, breaking client-side navigation

Open
#98,142 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Middleware Redirects
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/jpgorman/minimal-next-issue-reproduction

To Reproduce
  1. start the application yarn dev
  2. go to localhost:3000/start
  3. click link on screen
  4. routeType from getStaticProps is undefined

remove middleware.ts and repeat 1-3
4. routeType from getStaticProps: "foo"

Current vs. Expected behavior

Expected behaviour

Config redirects should only fire on full browser navigations, not on internal _next/data requests. The presence of middleware.ts should not change how config redirects interact with _next/data.

Actual behaviour

With middleware.ts present, _next/data/[buildId]/type/details.json is normalised to /type/details at resolve-routes.ts#L511 before config redirects run. The redirect matches and returns props from the wrong
page.

Root cause

In resolve-routes.ts, the middleware_next_data route (which runs before config redirects in the pipeline) normalises _next/data URLs only when fsChecker.getMiddlewareMatchers()?.length is truthy. Without
middleware.ts the normalisation is skipped and _next/data URLs never match config redirects.

Workaround

Add a missing condition to every redirect in next.config.js to skip them when the x-nextjs-data header is present (which Next.js sets on all _next/data requests at the same step as normalisation):

{
source: "/:type/details",
destination: "/",
permanent: false,
missing: [{ type: "header", key: "x-nextjs-data" }]
}

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.5.0: Mon Apr 27 20:41:12 PDT 2026; root:xnu-12377.121.6~2/RELEASE_ARM64_T6050
  Available memory (MB): 24576
  Available CPU cores: 15
Binaries:
  Node: 24.18.0
  npm: 11.16.0
  Yarn: 1.22.22
  pnpm: 11.9.0
Relevant Packages:
  next: 16.4.0-canary.13 // Latest available version is detected (16.4.0-canary.13).
  eslint-config-next: N/A
  react: 19.2.8
  react-dom: 19.2.8
  typescript: 5.9.3
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Redirects, Middleware

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

next dev (local), next build (local), next start (local)

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction linked in the issue, then inspect middleware_next_data in resolve-routes.ts around line 511 and its ordering relative to config redirects. Verify the behavior with and without middleware.ts and with the x-nextjs-data header; done means _next/data requests no longer trigger the config redirect while full navigations still do.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, typescript
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.