vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.51k stars 26.22k forks source link

Writing gzipped data to response stream doesn't write content #66833

Open togakangaroo opened 1 week ago

togakangaroo commented 1 week ago

Link to the code that reproduces this issue

https://github.com/togakangaroo/nextjs-stream-compression?tab=readme-ov-file

To Reproduce

See this repo with a full writeup in the README file.

Essentially this pattern within a pages api handler returns 0 bytes

const { createGzip } = require(['zlib'](https://nodejs.org/api/zlib.html));
  export default function handler(req, res) {
    res.status(200)
    const gzip = createGzip()
    gzip.pipe(res)
    gzip.write(JSON.stringify({ message: 'fanny fatnugget says hello' }))
    gzip.end();
    res.end();
  }

Current vs. Expected behavior

This is the correct way of streaming compressed content to a response. The following works fine (example included in repo as well)

  const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Encoding': 'gzip' });
    const gzip = zlib.createGzip();
    gzip.pipe(res);
    gzip.write('hello world');
    gzip.end();
  });

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  eslint-config-next: 14.2.4
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Developer Experience, Pages Router

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

In my actual application where I ran into this I actually do get 10 bytes in the response, but its always 10 bytes regardless of how much data is written to the response.

togakangaroo commented 1 week ago

As an aside, the link-to-a-repo field was not marked as required, but the button to submit issue was disabled until I filled it