next.js: [NEXT-1104] NextResponse.rewrite is not forwarding the request body to external hosts in dev

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:43 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T8112
    Binaries:
      Node: 19.6.1
      npm: 9.4.0
      Yarn: N/A
      pnpm: 7.27.0
    Relevant packages:
      next: 13.2.5-canary.33
      eslint-config-next: 13.2.4
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue

https://github.com/dbk91/nextresponse-defect

To Reproduce

Steps to reproduce specific to the linked repo:

  1. Spin up a server on http://localhost:4000 where you can log or inspect the body of a request.
  2. Run next dev to start the development server for Next.js
  3. Load the home page.
  4. A POST request to http://localhost:4000/test should happen on component mount with a JSON body of {"hello": 123}

Describe the Bug

The log shows no output of the JSON body.

Expected Behavior

The server should receive {"hello":123} in the request body.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1104

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 21
  • Comments: 44 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Still no fix ahead. Seems like next team is not aware of this problem @timneutkens

Hi, this has been updated in v13.4.2-canary.3 of Next.js, please update and give it a try!

+1. This is bug has me pinned to 13.2.4 unfortunately

I also get the same issue when using the rewrites in next.config.js to an external url after upgrading to 13.3.1. Reverting back to 13.2.4 fixes it.

We have the same problem in our project as well and it breaks our login flow when developing on a local machine. Problem is as well that version 13.2.4 is breaking next/image: https://github.com/vercel/next.js/pull/48513

Thanks everyone for all of the additional information. I updated the title to reflect the issues based on the discoveries made in this thread—please correct it if it doesn’t accurately reflect it or if I’m missing something.

@steve-marmalade Yes, it would be great if this fix got picked up. Our use-case is using the middleware to proxy to different APIs based on the first slug of the pathname. The NextResponse.rewrite makes this super simple to handle. And we use middleware rather than the rewrites in next.config.js because we make the host name configurable via an environment variable.

I went back and confirmed that this is an issue with next dev. Once it’s built, it doesn’t appear to be an issue. Still exists in 13.3 and canary, AFAIK.

This exactly sums up the issue, and can confirm that the bug in next dev is still present in the latest canary.

Thanks for the extra context @rickiesmooth.

I went back and confirmed that this is an issue with next dev. Once it’s built, it doesn’t appear to be an issue. Still exists in 13.3 and canary, AFAIK.

Please open a new issue with a reproduction so that it can be triaged accordingly.

I can not reproduce anything unexpected. In my Prod app is use rewrite alot with adding header and body / without body. As i need to add license key headers to every request.

make sure to delete the .next folder and try again

still not working, test with latest canary version, params in are not being forwarded

yay for me this fixed the issue 🎉

@florianliebig you need the clone the request headers in order to modify them. This example shows you how to do that: https://vercel.com/templates/next.js/edge-functions-modify-request-header

i just created a pull request for this issue https://github.com/vercel/next.js/pull/49320

Vercel released 13.4 with stable app router and the issue still exists. 👎 Hope that we are not longer stuck in 13.2.4

It seems to me that it works fine until you try to write to a different host? If so I might change the issue title.

Here is a reproduction in one tiny commit to the vercel/app-playground

No difference even if I add headers

return NextResponse.rewrite(new URL("/login", request.url), { request: { headers: request.headers, }, });

const url = request.nextUrl.clone(); url.pathname = "/login"; return NextResponse.rewrite(url);

both don’t work!

This is my next js version “13.4.2”

13.3.1 version has the same problem. I guess the only solution is to continue from 13.2.4

strange, I’m still seeing the same issue.