Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR: Cannot set headers after they are sent to the client : Cannot set headers after they are sent to the client #21

Closed
ChangJoo-Park opened this issue Mar 16, 2021 · 8 comments

Comments

@ChangJoo-Park
Copy link
Contributor

ChangJoo-Park commented Mar 16, 2021

Hello I made nuxt-nitro demo clone without nitro. URL is here. https://nuxt-without-nitro-demo.vercel.app/

I try with /api/hello made with h3, It failed after deploy. When I test on local, It make error.

 ERROR  Cannot set headers after they are sent to the client                                                                                                              20:39:12

  at ServerResponse.setHeader (_http_outgoing.js:518:11)
  at sendError (node_modules/h3/dist/index.js:502:7)
  at node_modules/h3/dist/index.js:514:14

My server middleware is https://github.com/ChangJoo-Park/nuxt-without-nitro-demo/blob/main/server/hello.js#L4-L8.
I tested with remove res.setHeader('Access-Control-Allow-Origin', '*') . but it fails too.

@ChangJoo-Park
Copy link
Contributor Author

This issue caused by ky-universal in nuxt/http module. not h3's fault.

@ChangJoo-Park
Copy link
Contributor Author

Reopen, It failed with axios with h3. but axios with express deploy success on vercel.
Here is full error logs.

2021-03-16T12:30:43.457Z	65954691-80e8-4140-ab8c-c93745ae5689	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client","reason":{"errorType":"Error","errorMessage":"Cannot set headers after they are sent to the client","code":"ERR_HTTP_HEADERS_SENT","stack":["Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client","    at ServerResponse.setHeader (_http_outgoing.js:558:11)","    at sendError (/var/task/node_modules/h3/dist/index.js:502:7)","    at /var/task/node_modules/h3/dist/index.js:514:14"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client","    at process.<anonymous> (/var/runtime/index.js:35:15)","    at process.emit (events.js:327:22)","    at processEmit [as emit] (/var/task/node_modules/signal-exit/index.js:161:32)","    at processPromiseRejections (internal/process/promises.js:245:33)","    at processTicksAndRejections (internal/process/task_queues.js:94:32)"]}
Unknown application error occurred

@danielroe danielroe reopened this Mar 16, 2021
@danielroe
Copy link
Member

@ChangJoo-Park Thank you so much for your contributions - PRs and issues! ❤️

Could you create a reproduction without Nuxt? (Or alternatively, raise this as an issue in the nuxt repo if it's specific to Nuxt?)

@ChangJoo-Park
Copy link
Contributor Author

ChangJoo-Park commented Mar 16, 2021

Thank you for reply, @danielroe !
I'll make repo without Nuxt.

Here you are. https://github.com/ChangJoo-Park/h3-issue-21/

It is not happen. It seems to have occurred because of h3 with Nuxt.

I met this error https://github.com/ChangJoo-Park/nuxt-without-nitro-demo/blob/65786ca495f8a24939451923ddaa0f7ebf54f125/server/hello.js#L1-L8 . Using h3 with Nuxt

@Zac-Zajdel
Copy link

Same issue as well

@lewebsimple
Copy link

Also getting this error systematically when trying to implement server-sent events in a fresh Nuxt3 project.

@tobychidi-zz
Copy link

Just started getting his error too. Fresh project.

ERROR [unhandledRejection] Cannot set headers after they are sent to the client 21:36:35

at new NodeError (node:internal/errors:372:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at sendError (/C:/Users/tobychidi/Documents/GitHub/CodeKing/codeking-nuxt/node_modules/.pnpm/h3@0.7.13/node_modules/h3/dist/index.mjs:233:13)
at nodeHandler (/C:/Users/tobychidi/Documents/GitHub/CodeKing/codeking-nuxt/node_modules/.pnpm/h3@0.7.13/node_modules/h3/dist/index.mjs:396:15)

@pi0
Copy link
Member

pi0 commented Oct 18, 2022

There might be several usage issues that call a utility from h3 after headers are sent. If you think it might be a bug with h3, please share a reproduction 🙏🏼

@pi0 pi0 closed this as completed Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants