next.js: Next.js server returns `connection: close` header for all routes

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103
    Binaries:
      Node: 20.3.0
      npm: 9.6.7
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.7-canary.2
      eslint-config-next: 13.4.6
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

Which area(s) of Next.js are affected? (leave empty if unsure)

Middleware / Edge (API routes, runtime), Standalone mode (output: “standalone”)

Link to the code that reproduces this issue or a replay of the bug

npx create-next-app

To Reproduce

  1. Create a new app using npx create-next-app@latest
  2. Run the app using npx next dev
  3. Launch a browser and open the Network tab in the developer tools
  4. Navigate to http://localhost:3000
  5. In the Network tab, select the request for localhost and note that in the Response headers connection: close is listed. Select other requests and note the same header is sent for all responses.
  6. Back in the terminal, Ctrl+C to stop the dev server
  7. Run npx next build to build the app, then run npx next start to run it
  8. Back in the browser, refresh the page and observe that the same behavior is present once the app is built

Describe the Bug

The nodejs server created by Next.js, whether it be in dev mode, production mode after building, or production mode when standalone deployment is configured, returns a connection: close response header to clients when rendering routes. This instructs the client to close the underlying TCP connection, rather than returning connection: keep-alive which allows the client to reuse the TCP connection when making subsequent requests, greatly reducing latency. This behavior is leading to high latencies for server-rendered Next.js routes.

I reproduced this behavior on multiple machines, including OSX and Windows 11, on multiple versions of nodejs, including latest LTS and Current releases, using next 13.4.x and canary, with multiple browsers including Edge and Firefox.

Expected Behavior

The nodejs server created by Next.js should utilize HTTP 1.1 keep-alive to allow clients to keep TCP connections open.

Which browser are you using? (if relevant)

Edge, Firefox

How are you deploying your application? (if relevant)

next start

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 17 (2 by maintainers)

Commits related to this issue

Most upvoted comments

@PassionPenguin You can use:

Response.redirect(`${request.nextUrl.origin}/path`, 303);

to solve your issue. I bet you were using redirect() in a POST route handler? If that’s the case then you can use the workaround of Response.redirect.

The Next.js team will have to fix redirect() probably. Repository: https://github.com/vvo/next-redirect-connection-closed Live demo: https://next-redirect-connection-closed.vercel.app/ (error codes are 405 on vercel and 500 in dev)

It turns out any <form method="POST"> to a non-existent path will also result in 500 (probably because POST to the error page is the same as POST to a page).