Using session causes "Cannot set headers after they are sent to the client"

Hi! I'm facing a weird behavior when trying to use 'useSession' or 'getSession' in a "use server" function. If I clear the cookies on the page, I get the error "Cannot set headers after they are sent to the client". To reproduce: clone and run https://github.com/livyk/solid-bug (it's a starter with the tailwind template + the sessions example from the docs). Navigate to /users. Clean cookies on the page and you are supposed to get the error. Am I doing something wrong, or it's a potential bug? Thanks!
GitHub
GitHub - livyk/solid-bug
Contribute to livyk/solid-bug development by creating an account on GitHub.
2 Replies
peerreynders
peerreynders7d ago
- useSession() will always create a session if there isn't one already. Use getCookie() instead. - That error typically appears when you try to modify cookies (session, headers) when Start has already started streaming the response (which is the default). Either the session is being modified in the wrong place or you need to add { deferStream: true } to the createAsync() involved.
Advanced - h3
More utilities
nlivyk
nlivyk5d ago
Thank you for the answer! Then I'll try to solve it using deferStream or just make a custom session using getCookie. Adding { deferStream: true } helped! The session example should be updated with { deferStream: true }
Thank you again!