Skip to content

[BUG] status 404 not respected for NextResponse.rewrite #924

Open
@yoohahn

Description

@yoohahn

Describe the bug

When I send a rewrite with status 404 this is not respected and we get a 200 out to the client.

export async function middleware(req: NextRequest) {
  if (req.nextUrl.pathname === "/nope") {
    const url = new URL("/notFound", req.url);
    return NextResponse.rewrite(url.toString(), {
      status: 404,
    });
  }
  return NextResponse.next();
}

Steps to reproduce

  1. Fork https://github.com/yoohahn/opennext-404-rewrites
  2. yarn preview
  3. http://localhost:8787/nope

Expected behavior

Status should be 404 but is 200

@opennextjs/cloudflare version

1.3.1

Wrangler version

4.24.0

next info output

Operating System:
  Platform: linux
  Arch: x64
  Version: #202412060638~1748542656~22.04~663e4dc SMP PREEMPT_DYNAMIC Thu M
  Available memory (MB): 31765
  Available CPU cores: 20
Binaries:
  Node: 24.0.1
  npm: 11.3.0
  Yarn: 4.6.0
  pnpm: N/A
Relevant Packages:
  next: 15.3.4
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Additional context

It looks like the same issue I reported for next on pages and added a patch for;

cloudflare/next-on-pages#953

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions