这是indexloc提供的服务,不要输入任何密码
Skip to content

Middleware rewrites are not applied on 404 routes #82043

@r34son

Description

@r34son

Link to the code that reproduces this issue

https://github.com/r34son/middleware-rewrite-repro

To Reproduce

  1. pnpm dev
  2. go to http://localhost:3000/unknown - default 404 (not from [locale]/not-found)
  3. go to http://localhost:3000/en/unknown - default 404 (not from [locale]/not-found)

Current vs. Expected behavior

Current Behavior

When a user visits a non-existent route (e.g., /non-existent-page), the middleware rewrite is not applied, resulting in a standard Next.js 404 page instead of the expected rewritten route.
Example:
User visits: /non-existent-page
Expected: Should be rewritten to /en/non-existent-page and serve content from that path
Actual: Shows Next.js default 404 page without any middleware processing

Expected Behavior

All routes, including non-existent ones (404s), should be processed by the middleware and rewritten according to the configured rules.
Example:
User visits: /non-existent-page
Middleware should: Rewrite to /en/non-existent-page
Result: Either serves content from /en/non-existent-page or shows a custom 404 page from the /en route structure

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:25 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 18.20.6
  npm: 10.8.2
  Yarn: N/A
  pnpm: 10.9.0
Relevant Packages:
  next: 15.4.2-canary.16 // Latest available version is detected (15.4.2-canary.16).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

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

Middleware

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

next start (local)

Additional context

If you add root not-found page, [locale]/not-found also do not shows on /unknown and /en/unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions