Skip to content
This repository has been archived by the owner on Apr 25, 2021. It is now read-only.

kachkaev/hanging-response-in-next-via-redirect-plus-compression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MWE for hanging http responses in Next.js when using res.redirect and compression

TL;DR;

Using

res.redirect("/pathname");
res.end();

instead of

res.redirect();

Causes issues with subsequent page / api requests when:

  • node version is >= 14.0.0
  • next.config.jscompress is enabled (default)
  • compressed body size of a follow-up response is greater than zlib.Z_DEFAULT_CHUNK (=16384 bytes)

Remedy

Option 1: remove res.end()

Option 2: Instead of Next.js res.redirect() use:

res.writeHead(302, { Location: "/pathname" });
res.end();

Local reproduction

Needs nvm

## OK
NODE_VERSION=v12.20.0
NODE_VERSION=v13.14.0

## BROKEN
NODE_VERSION=v14.0.0
NODE_VERSION=v14.15.3
NODE_VERSION=v15.5.0
nvm install $NODE_VERSION && nvm use $NODE_VERSION && npm i -g yarn && yarn --ignore-engines && yarn --ignore-engines dev