[Bug]: Streaming from edge function works, but chunks arrive together at once at the end
Summary
- Edge function in
pages/api/*.tsworks technically, but is not really streaming - Next.js application without any Netlify-specific configuration
- On Vercel, things work as expected, e.g. headers are set properly, etc.
- On Netlify, it works, but there is a bug or a missing piece to make streaming work end-to-end
This issue template is killing me. Just wanted to ask whether this is a known limitation/issue? Trying to fill out the fields as requested. Happy to provide more details!
I'm pointing to the reproduction repository I'm working with, but it's not really a repro since you would need API keys etc. so it's just to get an idea of what I'm working on.
(By the way, can't debug this locally properly, since netlify dev eventually results in self is not defined coming from the streaming edge function (this does not happen using next dev; it also happens using netlify dev --live); not sure how to go about this. I expected Next.js didn't use Webpack anymore, but it does and somehow netlify is different from next here.)
A link to a reproduction repository
https://github.com/kadena-community/7-docs-demo/blob/main/src/pages/api/completion.ts
Expected Result
See a streaming response with the first chunk coming in early on and then chunks arriving over time subsequently
Actual Result
Technically a streaming response, but the first and last chunk are one millisecond apart
Steps to reproduce
To see the issue right away:
- Visit https://thunderous-medovik-731432.netlify.app
- Select "Need suggestions?" and pick one
- See that the response takes some time and is printed at once
- See in the network tab that the response was indeed streaming, but all chunks arrive at the same time (see screenshots)
To get something going locally:
- Create a Next.js application
- Add a API function like
pages/api/my-function.js - Call OpenAI's
/chat/completionsfunction and setstream: true - See that the response takes some time and is printed at once
- See in the network tab that the response was indeed streaming, but all chunks arrive at the same time (see screenshots)
Next Runtime version
4.36.1
Is your issue related to the app directory?
- [ ] Yes, I am using the
appdirectory
More information about your build
- [ ] I am building using the CLI
- [ ] I am building using file-based configuration (
netlify.toml)
What OS are you using?
None
Your netlify.toml file
`netlify.toml`
# Paste content of your `netlify.toml` file here
Your public/_redirects file
`_redirects`
# Paste content of your `_redirects` file here
Your next.config.js file
`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.
Builds logs (or link to your logs)
Build logs
# Paste logs here
Function logs
Function logs
# Paste logs here
.next JSON files
generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.