Nuxt.js: Cannot set headers after they are sent to the client

Created on 23 May 2018  路  7Comments  路  Source: nuxt/nuxt.js

Version

v1.4.0

Reproduction link

https://pastebin.com/njiXmD6L

Steps to reproduce

For a custom middleware:

// nuxt.config.js
serverMiddleware: [{
    path: '/sitemap.xml',
    handler: async (req, res, next) => {
      const sitemap = await require('./modules/sitemap')
      res.setHeader('Content-Type', 'application/xml')
      res.end(sitemap.toString())
      next()
   }
}]

Will lead to:

 OPEN  http://localhost:8080

  nuxt:render Rendering url /sitemap.xml +0ms
{ Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:471:11)
    at Renderer.nuxtMiddleware (/Users/d/Desktop/jobs/client/node_modules/nuxt/lib/core/middleware/nuxt.js:99:9)
    at process._tickCallback (internal/process/next_tick.js:68:7) statusCode: 500, name: 'Error [ERR_HTTP_HEADERS_SENT]' }

 ERROR 

  Error: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

  - _http_outgoing.js:471 ServerResponse.setHeader
    _http_outgoing.js:471:11

  - error.js:20 sendResponse
    [client]/[nuxt]/lib/core/middleware/error.js:20:9

  - error.js:65 youch.toHTML.then.html
    [client]/[nuxt]/lib/core/middleware/error.js:65:7

For some reason the logic in the sitemap module is the same but works fine.

Similar to #3059

This bug report is available on Nuxt.js community (#c7130)
bug-report

All 7 comments

The reason maybe because that serverMiddleware is a connect middleware(https://github.com/senchalabs/connect) and it seemed not support asynchronous way.

You may need to integrate a custom server such as koa to expose the sitemap.xml api.

I see. Ok if async is the issue then I will use the next upon a promise or callback completion. Will report more here 馃憤

Ok found the issue. Docs:
Middleware are added as a "stack" where incoming requests will execute each middleware one-by-one until a middleware does not call next() within it.

Since I want my middleware be the last one I simply should not call next(). And actually it doesn't matter if it's a promise or async function.

Indeed @Diolor

Because you call res.end(), you should not call next() since you don't want any other middleware to call res.end() again.

@Atinux Yes. Just for the sake of completion here, writeBody below (and maybe other request's
functions too) would also cause the error if you combine it with next.

res.setHeader('Content-Type', 'application/xml')
res.writeBody(sitemap.toString())
next()

the same issue,
Does the nuxt.render returns promise锛烮'v returned undefined.
I found the nuxt.render is based on connect,no promise yet.
https://github.com/nuxt/nuxt.js/blob/dev/lib/core/renderer.js#L37
https://github.com/senchalabs/connect#apphandlereq-res-out

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nassimbenkirane picture nassimbenkirane  路  3Comments

vadimsg picture vadimsg  路  3Comments

maicong picture maicong  路  3Comments

msudgh picture msudgh  路  3Comments

mattdharmon picture mattdharmon  路  3Comments