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
Using ky with edge functions works on prod but fails on local Next.js dev server #41531
Using ky with edge functions works on prod but fails on local Next.js dev server #41531
Comments
The Edge Runtime (that powers local development in Next.js) uses I suspect there is an issue because So there might be an issue around that. 🤔 For some reason the following works. - json: { test: true }
+ body: JSON.stringify({ test: true }) |
Thanks for the quick response, @balazsorban44. To be clear though,
I can confirm that this change works as well for me locally, though I'm not sure why. Maybe I should follow this up with some |
Either way, it is rather odd that |
My bad, I actually noticed I was on the wrong repo. 🤦. I am investigating this still since there should be no difference between local/production deployment behavior anyway. FWIW, Node.js now uses |
I tried to understand what's happening. If I create a minimal import { fetch, Headers, Request, Response } from 'undici'
globalThis.fetch = fetch
globalThis.Headers = Headers
globalThis.Request = Request
globalThis.Response = Response
const { default: ky } = await import('ky')
;(async () => {
const data = await ky
.post('https://httpbin.org/post', {
json: { test: true }
})
.json()
console.log(data)
})() so I suspect the issue is actually in the Next.js transpilation layer, or in the Edge Runtime tweaks over undici. I have to go deep. |
Any updates on this @Kikobeats ? |
I am running into a similar issue with an API proxy route. I am not using ky, just "native" (nextjs patched) fetch and runtime edge in the app router. If I fix it to work locally then it fails when hosted on vercel edge function and vice versa. I see this thread was from a year, did you guys figure out any solution? |
Probably related to: sindresorhus/ky#531 Also, see: sindresorhus/ky#516 (comment) |
I ran into this issue as well, and filed #57905 (I hadn't found this issue before filing mine). I don't think this has anything to do with ky, other than an implementation detail of how they implement support for the
when run on dev server, it will render
So the request method, headers (and everything else) is lost when using the copy constructor. This works correctly in all other environments (node on dev, edge or node in prod). While the workaround is trivial as @balazsorban44 pointed out above (#41531 (comment)) it would still be nice to have this fixed - it's disconcerting to encounter these various differences in dev and production which seem to be fairly common for the edge runtime. |
Not really. I ported the reproduction to a standalone Vercel Function: import ky from 'ky'
export const config = { runtime: 'edge' }
export default async function handler () {
const data = await ky
.post('https://httpbin.org/post', {
json: { test: true }
})
.json()
console.log(data)
return Response.json(data)
} Deployed using
So the issue is still in the way ky and Next.js surface. |
@Kikobeats I did mention in my comment that the incorrect behavior only occurs on nextjs dev server (its in the part of my comment you quoted) Your demonstration shows that it works in production, which I mentioned also.
So yes it is definitely a Next.js issue, and can be reproduced with no involvement of ky. It is only a ky issue insofar as ky depends on the |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
106.0.5249.119
How are you deploying your application? (if relevant)
next dev
Describe the Bug
ky is a small wrapper around
fetch
. The following edge function works fine in production: https://next-edge-test-yl6y.vercel.app/api/ky, but it produces an error when run locally withnext dev
:(source)
Visiting this edge function locally results in the error "TypeError: Request with GET/HEAD method cannot have body.":
Expected Behavior
Visiting this edge function locally should work as it does in production, producing a valid JSON output: https://next-edge-test-yl6y.vercel.app/api/ky
This is presumably due to some inconsistency with how Next.js is polyfilling
fetch
or one of it's related globals. Is Next.js properly polyfilling all of the related globals asky-universal
does here? https://github.com/sindresorhus/ky-universal/blob/main/index.jsLink to reproduction
https://github.com/transitive-bullshit/next-edge-test
To Reproduce
http://localhost:3000/api/ky
The text was updated successfully, but these errors were encountered: