vercel / vercel/next.js

Static GET App route throws TypeError when returning cached 204

Open
#49,005 13 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information

From next info:

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.16.0
      npm: 8.11.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.4.20-canary.12
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5
    Next.js Config:
      output: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://github.com/jmurty/next-issue-204-static

To Reproduce
  • Implement a static (non-dynamic) app route endpoint with a GET function that returns a HTTP 204 No Content response
  • Run project with a production build, either self-hosted (e.g. yarn build && yarn start) or deployed to Vercel
  • Project builds correctly, including generation of static page for the 204 app route
  • Send a GET request to the 204 app route – e.g. curl -v https://next-issue-204-static.vercel.app/api/get-204-static
  • Receive a 500 Server Error response, and get a server-side a stack trace similar to the one below
  • Note that nearly identical static app routes that return other 2xx HTTP response codes work as expected – e.g. curl -v https://next-issue-204-static.vercel.app/api/get-201-static
TypeError: Response constructor: Invalid response status code 204
    at webidl.errors.exception (node:internal/deps/undici/undici:1265:14)
    at initializeResponse (node:internal/deps/undici/undici:6845:31)
    at new Response (node:internal/deps/undici/undici:6654:9)
    at NextNodeServer.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/server/base-server.js:1221:61)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async NextNodeServer.renderPageComponent (/var/task/node_modules/next/dist/server/base-server.js:1288:24)
    at async NextNodeServer.renderToResponseImpl (/var/task/node_modules/next/dist/server/base-server.js:1319:32)
    at async NextNodeServer.pipeImpl (/var/task/node_modules/next/dist/server/base-server.js:630:25)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:1144:21)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:315:32) {
  page: '/api/get-204-static'
}

Note that the linked reproduction project is also deployed to the Vercel project next-issue-204-static and the static app route that returns 204 is available at https://next-issue-204-static.vercel.app/api/get-204-static

Describe the Bug

Static app routes do not correctly cache and return HTTP 204 No Content responses.

At build time the static app route is generated without error, but when a request arrives at the endpoint the cached result is not returned. Instead, it seems like the server re-runs the app route and in doing so triggers the server error TypeError: Response constructor: Invalid response status code 204

Expected Behavior

An app route that returns a HTTP 204 No Content response should have the result cached at build time, and the 204 response returned upon request.

Only the 204 HTTP response status code seems to be affected. As well as showing the problem 204 case, the example project (and Vercel deployment) includes nearly identical static app routes that return other 2xx status codes. All of the routes work as expected except the one that returns 204:

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

next start & Vercel

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked next-issue-204-static reproduction, build it in production mode, run it with next start, and request the static 204 app route with curl. Trace the static app-route caching and response path for that endpoint; done means the cached request returns HTTP 204 without a server error, while the listed 200–203 routes continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.