koajs / koajs/session

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

Open
#151 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
908
Forks
115
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

Research direction

Start by locating the session middleware's autoCommit path and its tests; the issue does not name files. Reproduce the ctx.respond = false and ctx.res.end() case, then resolve whether completion means suppressing the commit, checking finished/headersSent, or warning, with regression coverage for the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.