koajs / session

Simple session middleware for koa
MIT License
902 stars 113 forks source link

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

Open vjpr opened 6 years ago

vjpr commented 6 years ago

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

galvez commented 6 years ago

@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.

chrisregnier commented 5 years ago

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?