vercel / next.js

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

Cannot read properties of undefined (reading 'headers') when a route handler doesn't return a response #51130

Closed joulev closed 1 year ago

joulev commented 1 year ago

Verify canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:43 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T8112
    Binaries:
      Node: 20.0.0
      npm: 9.6.4
      Yarn: 1.22.19
      pnpm: 8.6.1
    Relevant packages:
      next: 13.4.5
      eslint-config-next: N/A
      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)

App directory (appDir: true)

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

https://github.com/joulev/debug/tree/nextjs-route-handler-not-return-error-bug

To Reproduce

Add a route handler that doesn't return a response. For example

export function POST() {}

Run that route handler. For example in the reproduction repository, make a POST request to /api.

Describe the Bug

The following error is thrown:

- error TypeError: Cannot read properties of undefined (reading 'headers')
    at /Users/joulev/dev/www/debug/.next/server/chunks/322.js:3106:61
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

It is very unclear and has confused many people in the Next.js Discord server. Of course the error can be fixed by returning a response, but the error is too unclear for developers to know what's wrong.

Expected Behavior

I think it would be good if this either returns a default 200 response (new Response(null) for example) (I prefer this), or throws a clearer error, or both.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

github-actions[bot] commented 12 months ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.