Cannot set headers after they are sent to the client
I have used login system from this repo https://github.com/itsrakeshhq/finance-tracker/tree/main/backend/src/modules/auth but reworked it to tRPC v11 (11.0.0-rc.824). I changed nothing regarding logic, only stuff regarding new version of tRPC and also i am using tRPC standalone not express. Now I have issue when i try to set cookies in authRouter.login
Which leads to:
Setting cookies there throws this error.
How is that possible? Nothing is sent to client before return from this function right?
If I remove cookies code, it works fine. But it is quite important to set cookies for login endpoint ๐
I am stuck on this for hours and I tried everything! Please help ๐
Node v22.12.0
npm v11.0.0
GitHub
finance-tracker/backend/src/modules/auth at main ยท itsrakeshhq/fina...
[TUTORIAL]. Contribute to itsrakeshhq/finance-tracker development by creating an account on GitHub.
3 Replies
I tried to narrow it down. It seems to be problem with createContext. Even if i change it to this very simple implementation, it returns same error.
It seem like, that I can set cookies in createContext like this:
But if I return method from
createContext
(first example), or even if I return res
and then call res.appendHeader(...)
in my route, it always return this error ๐ค Seems like res
in mutable only in createContext
but not in routes (authRouter.login
etc.)I wonder if it could be related in some way to the batch link
while you're not using the streaming batch link tRPC batching could interact with setting headers in part of the batch
I wouldn't recommend trying to interact with headers directly but I understand some apps might need it
Thats it! I have been using
unstable_httpBatchStreamLink
so I switched to httpBatchLink
and it fixed the issue. Thank you mate, you just saved my ass.