-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Description
Bug report
Describe the bug
| await server.getRequestHandler()(_req, mockRes, nodeUrl.parse(href, true)) |
- Looks like we need to wait for
mockRes._finalto be called — notawaitpromise completion. - (Also we need only implement
mockRes._write, so we have no need to implementmockRes.writeso we have no need to handle strings.)
To Reproduce
-
Create an API endpoint
/pages/api/images.jsthat returns an image using.pipeexport const config = { api: { bodyParser: false } } export default async function (req, res) { var downloadStream = // from an external API (or in your case test with just a file stream) res.setHeader('Content-Type', 'image/jpeg') res.setHeader('Content-Length', ...) downloadStream.pipe(res) }
-
Visit http://localhost:3000/_next/image?url=/api/image&w=3840&q=75
(This is the endpoint normally generated by the new<Image>tags fromnext/image) -
You will have a blank page caused by empty response. (Ctrl+S saves a file to your desktop with zero length)
Expected behavior
The compressed/resized version of image
Workaround
await new Promise(function (resolve) {
// Need to use promise because of https://github.com/vercel/next.js/issues/20110
// Otherwise we could have just used a `pipe` line and no promise.
downloadStream.pipe(res)
downloadStream.on('end', resolve)
})System information
- OS: any, Browser: any
- Version of Next.js: [e.g. 10.0.3]
- Version of Node.js: [e.g. 14.3.0]
- Deployment:
next run dev
Why does this matter?
Why can we not just wait for promise completion?
A blank page is very much not useful to the developer. Having to go line-by-line and debug a webpacked node_modules is not the experience we need to give to the developer.
Normally, if the developer tried to send a response from his/her API endpoint with res.write and res.end they would have gotten
API resolved without sending a response for /api/uploads/image, this may result in stalled requests.
which would totally make having a blank response no big deal because the clear problem statement is in the logs.
However, this log clue does not appear for pipe calls. So the developer, without us doing something on this issue, has no clear recourse.