-
Notifications
You must be signed in to change notification settings - Fork 27.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regression: fetch requests in middleware fail with a body #38963
Comments
Maybe take a look in https://nextjs.org/docs/messages/middleware-upgrade-guide#no-response-body seems like this has changed in 12.2 so the behaviour is intentional : "If your Middleware does respond with a body, a runtime error will be thrown" |
@joaogarin that only applies to responding (ie: I could verify in |
is there a solution for that yet? |
@Charlie-007-star Yes, see vercel/edge-runtime#68 |
#39219 included the fix for this and will be available in the next |
This should be out on |
@balazsorban44 This error started occurring in a project using [email protected]. The code you see below is executed directly in the middleware and still causes the import { NextRequest } from 'next/server'
/**
* Function to fetch the roles of the user and updated cookies
* @param req
*/
async function fetchMeObject(
req: NextRequest,
): Promise<{ me: null | { roles: string[] }; cookie: string }> {
const response = await fetch(process.env.API_URL, {
method: 'POST',
body: JSON.stringify({ query: 'query { me { roles } }' }),
headers: {
'Content-Type': 'application/json',
// Attach headers to the request to ensure `me` is returned
Cookie: req.headers.get('cookie'),
},
})
if (response.ok) {
const { data } = await response.json()
return {
me: data?.me,
cookie: response.headers.get('set-cookie'),
}
}
// Return the updated cookie-header in case the request was not successful
return {
me: null,
cookie: response.headers.get('set-cookie'),
}
}
export default fetchMeObject |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Making a
fetch
request from middleware that includes a request body fails withTypeError: fetch failed
.Tested in
12.2.2
- succeeds. Fails in12.2.3
andcanary
.Expected Behavior
The fetch request should succeed.
Link to reproduction
https://codesandbox.io/s/jovial-fast-yflq3w?file=/middleware.ts
To Reproduce
body: ""
The text was updated successfully, but these errors were encountered: