h3 icon indicating copy to clipboard operation
h3 copied to clipboard

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

Open ChangJoo-Park opened this issue 3 years ago • 7 comments

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 avatar Mar 16 '21 11:03 ChangJoo-Park

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

ChangJoo-Park avatar Mar 16 '21 11:03 ChangJoo-Park

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

ChangJoo-Park avatar Mar 16 '21 12:03 ChangJoo-Park

@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?)

danielroe avatar Mar 16 '21 12:03 danielroe

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

ChangJoo-Park avatar Mar 16 '21 12:03 ChangJoo-Park

Same issue as well

Zac-Zajdel avatar Mar 24 '22 02:03 Zac-Zajdel

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

lewebsimple avatar Mar 30 '22 01:03 lewebsimple

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/[email protected]/node_modules/h3/dist/index.mjs:233:13) at nodeHandler (/C:/Users/tobychidi/Documents/GitHub/CodeKing/codeking-nuxt/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:396:15)

tobychidi-zz avatar Aug 02 '22 20:08 tobychidi-zz

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 avatar Oct 18 '22 09:10 pi0