Skip to content

/_next/image can give empty body with /pages/api served images #20110

@700software

Description

@700software

Bug report

Describe the bug

await server.getRequestHandler()(_req, mockRes, nodeUrl.parse(href, true))

  • Looks like we need to wait for mockRes._final to be called — not await promise completion.
  • (Also we need only implement mockRes._write, so we have no need to implement mockRes.write so we have no need to handle strings.)

To Reproduce

  1. Create an API endpoint /pages/api/images.js that returns an image using .pipe

    export 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)
    }
  2. Visit http://localhost:3000/_next/image?url=/api/image&w=3840&q=75
    (This is the endpoint normally generated by the new <Image> tags from next/image)

  3. 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.

Metadata

Metadata

Assignees

Labels

Image (next/image)Related to Next.js Image Optimization.good first issueEasy to fix issues, good for newcomersplease verify canaryPlease verify the issue with the latest canary branch.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions