You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a route handler that doesn't return a response. For example
exportfunctionPOST(){}
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
The text was updated successfully, but these errors were encountered:
… TS plugin) and at runtime (#51394)
### What?
Fixes#51130. Before this PR, the package assumes that route handlers return a `Response` which is not necessarily the case.
The linked issue specified three suggestions to resolve this
1. Return a default 200 response
2. Throw a better error message
3. or both
~~In this issue I implemented (3), except that it is a warning and not an error. Do tell if the team wants to follow a different approach, as it is not too hard to change this.~~
This PR implements (2).
### How?
The returned value of the handler is checked at runtime to ensure it is actually a `Response` instance.
The return type `AppRouteHandlerFn` is also modified to `unknown` to avoid similar assumptions elsewhere.
The TS plugin is also modified to check for the return type during build time.
Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
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.
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
Run that route handler. For example in the reproduction repository, make a
POST
request to/api
.Describe the Bug
The following error is thrown:
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
The text was updated successfully, but these errors were encountered: