Next cookies() not being set on Mutation in App Dir (T3 Turbo)
Trying to set cookies inside TRPC using Nextjs App Router, using the T3 Turbo setup.
Setting cookies works fine inside Queries, but cookies are not set inside Mutations. Maybe I'm doing something wrong, not very up to date with TRPC + App Dir, or it could be a bug in next, not sure.
After calling both the query and the mutation on frontend you only get the cookie from the Query.
But this gets logged to console.
(this is after calling the query first then the mutation.)
9 Replies
Sorry for thing ping but I feel like I may need some more expert help on this @marminge
I though that it may be something to do with not being able to set cookies after streaming starts, but then it doesn’t make sense how cookies work in query but not mutation
At least it doesn’t make sense to me
I’m also struggling with the same issue in app router. I found that it’s actually the .input() that breaks cookies from settings correctly. Both query or mutation sets cookies properly with a hardcoded value and no .input() but as soon as you add the .input() (even if you still use a hardcoded value for the cookie) it doesn’t get set
Really hoping to find a solution, but don't really know where to go from here 😢
Is there on open issue for this?
I haven't had too much time to look into it, but not that I could find
Probably a good idea to open one about it
It seems like unstable_httpBatchStreamLink has this problem, but httpBatchLink does not
If you're using unstable_httpBatchStreamLink, headers are returned at the beginning of the request (since the response is a streamed response).
This means you can't set any headers or cookies inside procedures. httpBatchLink does not have this issue due to the response not being streamed.
Ahhh i see
makes sense
is there a way you could make it so that certain mutations are never streamed? Like use a regular http link for certain routes
https://trpc.io/docs/client/links/splitLink , i'm going to have a look at using split links, see if that works
Split Link | tRPC
splitLink is a link that allows you to branch your link chain's execution depending on a given condition. Both the true and false branches are required. You can provide just one link, or multiple links per branch via an array.
Managed to get it working, basically the answer is to avoid batchStreamLink on any routes/paths that need to set cookies.