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

[unhandledRejection] Cannot set headers after they are sent to the client #24

Closed
chz opened this issue Nov 4, 2021 · 16 comments
Closed
Labels
bridge bug Something isn't working needs reproduction

Comments

@chz
Copy link

chz commented Nov 4, 2021

Environment

- Operating System: `Darwin`
- Node Version:     `v14.18.1`
- Nuxt Version:     `2.16.0-27266415.3ab12f9f`
- Package Manager:  `yarn@1.22.15`
- Bundler:          `Webpack`
- User Config:      `head`, `css`, `plugins`, `components`, `buildModules`, `modules`, `build`
- Runtime Modules:  `-`
- Build Modules:    `@nuxt/bridge@3.0.0-27266907.929a634`

Describe the bug

 ERROR  [unhandledRejection] Cannot set headers after they are sent to the client                                                                                                                     14:23:45

  at new NodeError (internal/errors.js:322:7)
  at ServerResponse.setHeader (_http_outgoing.js:561:11)
  at send (node_modules/webpack-dev-middleware/dist/utils/compatibleAPI.js:68:7)
  at processRequest (node_modules/webpack-dev-middleware/dist/middleware.js:186:31)
  at ready (node_modules/webpack-dev-middleware/dist/utils/ready.js:11:12)
  at Object.middleware (node_modules/webpack-dev-middleware/dist/middleware.js:44:24)
  at node_modules/pify/index.js:33:11
  at new Promise (<anonymous>)
  at Object.<anonymous> (node_modules/pify/index.js:6:9)
  at Object.apply (node_modules/pify/index.js:80:20)
  at WebpackBundler.middleware (node_modules/@nuxt/webpack-edge/dist/webpack.js:1909:37)
  at middleware (node_modules/@nuxt/nitro/dist/index.mjs:1809:50)
  at node_modules/h3/dist/index.mjs:111:24
  at new Promise (<anonymous>)
  at callHandle (node_modules/h3/dist/index.mjs:108:10)
  at Object.handle (node_modules/h3/dist/index.mjs:104:12)

Reproduction

Fresh install nuxt 2 and migrate to bridge following: https://v3.nuxtjs.org/getting-started/bridge

Additional context

No response

Logs

No response

@chz chz added bridge bug Something isn't working labels Nov 4, 2021
@Dinibewerbig
Copy link

Same problem here.

@Luferov
Copy link
Contributor

Luferov commented Jan 11, 2022

I'm gotting this error when use apollo subscription. Maybe h3 have problem with sockets?

@Luferov
Copy link
Contributor

Luferov commented Jan 12, 2022

When i use my MacBook i got this error, but if i use ubuntu os it's work fine!
I think this error in nodejs base macOS.
Because of, do reproduction almost impossible.

@misaon
Copy link

misaon commented Jan 25, 2022

This is not bug only in Nuxt 2 bridge, this hapnes in Nuxt 3 too, when you use Nuxt under reverse proxy (descripted by @Diizzayy here nuxt/nuxt#12003), Nitro, h3 or some base server logic in Nuxt part not process headers properly. @danielroe @pi0

@danielroe
Copy link
Member

I'm closing as there is no reproduction provided. If you can provide one, I'll happily reopen.

@misaon
Copy link

misaon commented Mar 7, 2022

@danielroe try run Nuxt3 Inside Docker with simple Nginx proxy_pass and you will get this error

@danielroe
Copy link
Member

If you can provide me a reproduction, I'll do so 😊

@acidjazz
Copy link

acidjazz commented Mar 8, 2022

I am getting this at times when I try to fetch a cookie with useCookie

 ERROR  [worker] Cannot set headers after they are sent to the client                                                        14:19:11

  at new NodeError (node:internal/errors:371:5)
  at ServerResponse.setHeader (node:_http_outgoing:576:11)
  at .nuxt/dist/server/server.mjs:2084:34
  at triggerAfterEach (node_modules/vue-router/dist/vue-router.cjs.js:3153:13)
  at node_modules/vue-router/dist/vue-router.cjs.js:3056:13

server.mjs:

  router.beforeEach(async (to, from) => {
    to.meta = __vite_ssr_import_0__.reactive(to.meta);
    nuxtApp._processingMiddleware = true;
    const middlewareEntries = new Set([...__vite_ssr_import_7__.globalMiddleware, ...nuxtApp._middleware.global]);
    for (const component of to.matched) {
      const componentMiddleware = component.meta.middleware;
      if (!componentMiddleware) {
        continue;
      }

line 2048 is the continue after if (!componentMiddleware)

@danielroe
Copy link
Member

What is the content of your middleware?

@acidjazz
Copy link

acidjazz commented Mar 8, 2022

What is the content of your middleware?

import { useNuxtApp } from '#app'

export default defineNuxtRouteMiddleware(to => {
  const { $api } = useNuxtApp()
  if (to.path !== $api.config.redirect.logout && $api.loggedIn.value === false)
    return navigateTo($api.config.redirect.logout)
})

I think I tracked it down to when I do this in my auth library:

  private getToken(): string {
    if (this.config.req) return useCookie(this.config.req, 'token')
    return this.cookies.get('token')
  }

this.config.req is an instance of IncomingRequest from http

@danielroe danielroe reopened this Mar 8, 2022
@Bjunk
Copy link

Bjunk commented Mar 12, 2022

Same issue in Nuxt3 clean installation when I used public folder.

Nuxt CLI v3.0.0-27449998.32cf973

ERROR [unhandledRejection] Cannot set headers after they are sent to the client

at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at sendError (node_modules/@nuxt/nitro/node_modules/h3/dist/index.mjs:217:7)
at node_modules/@nuxt/nitro/node_modules/h3/dist/index.mjs:229:14
at processTicksAndRejections (node:internal/process/task_queues:96:5)

[Vue Router warn]: No match found for location with path "/__webpack_hmr/client"
[Vue Router warn]: No match found for location with path "/__webpack_hmr/client"
[Vue Router warn]: No match found for location with path "/__webpack_hmr/client"
[Vue Router warn]: No match found for location with path "/__webpack_hmr/client"
[Vue Router warn]: No match found for location with path "/_content/ws"
[Vue Router warn]: No match found for location with path "/_content/ws"

@birdlavv
Copy link

Could this be related to unjs/h3#21 & unjs/h3#35?

@Zac-Zajdel
Copy link

Same issue while using Mac OS on Apple Silicon. I do not have the same issue on Ubuntu or Windows.

@acidjazz
Copy link

image

@danielroe danielroe transferred this issue from nuxt/framework Apr 13, 2022
@danielroe
Copy link
Member

I'm closing this as this should be resolved with #324. If you encounter this again, feel free to open a new issue with a reproduction.

@m0rtyn
Copy link

m0rtyn commented Aug 2, 2022

I encountered with this issue. Migrated existing Nuxt2 project with Nuxt Bridge

 ERROR  [uncaughtException] Cannot set headers after they are sent to the client

  at ClientRequest.setHeader (_http_outgoing.js:558:11)
  at ProxyServer.<anonymous> (node_modules/nitropack/dist/chunks/prerender.mjs:2444:16)
  at ProxyServer.emit (node_modules/eventemitter3/index.js:184:35)
  at ClientRequest.<anonymous> (node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:133:16)
  at ClientRequest.emit (events.js:327:22)
  at ClientRequest.EventEmitter.emit (domain.js:467:12)
  at tickOnSocket (_http_client.js:778:7)
  at onSocketNT (_http_client.js:817:5)
  at processTicksAndRejections (internal/process/task_queues.js:81:21)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bridge bug Something isn't working needs reproduction
Projects
None yet
Development

No branches or pull requests

10 participants