session icon indicating copy to clipboard operation
session copied to clipboard

`autoCommit` shouldn't be true when `ctx.respond = false`

Open vjpr opened this issue 5 years ago • 2 comments

If you call ctx.res.end() in your middleware chain, then you will get a Can't set headers after they are sent., because ‘autoCommit’ attempts to set cookie header on the koa middleware return phase.

This may happen if you set ctx.respond = false, because you have some middleware that needs to bypass Koa's response middleware (https://github.com/koajs/koa/blob/master/docs/api/context.md#ctxres) - like Next.js custom server for example.


Maybe we should check ctx.respond and show a warning if autoCommit is true?

And we should check res.finished || res.headersSent to avoid throwing the 'header sent message', and show a detailed warning.

The 'header sent' error is difficult to track down because async stack traces are not complete - so this would save a lot of user frustration.

@galvez @dead-horse

vjpr avatar Nov 07 '18 12:11 vjpr

@vjpr I'll try and take a look at this and #153 over the weekend. It's in my interest to keep the API for these cases solid.

galvez avatar Nov 10 '18 01:11 galvez

I just ran into the same issue and it took me a while to figure out what was going on because of the incomplete stacktraces. Has any progress been made here?

chrisregnier avatar Mar 09 '19 19:03 chrisregnier