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:
- Spin up a server on
http://localhost:4000
where you can log or inspect the body of a request. - Run
next dev
to start the development server for Next.js - Load the home page.
- 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
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 21
- Comments: 44 (4 by maintainers)
Commits related to this issue
- Fixes #48040 — committed to Xetoxyc/next.js by deleted user a year ago
- Fix external rewrite with body (#49487) This ensures we use the proper body stream when rewriting to an external resource as currently it tries to consume an already read body stream. Fixes: http... — committed to vercel/next.js by ijjk a year ago
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 therewrites
innext.config.js
because we make the host name configurable via an environment variable.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.
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.