S
SolidJS15mo ago
nlivyk

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.
4 Replies
peerreynders
peerreynders15mo 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
nlivykOP15mo 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!
fpalla
fpalla7mo ago
some months later seconding that thank you as this was what I was looking for ❤️
caseybaggz
caseybaggz3mo ago
Had this problem too. Using deferStream did help, but it still triggers on child [slug] routes which is interesting? 🤔 Would using getRequestEvent in combination with useSession help with this? I'm honestly less knowledable about these two APIs since the docs are pretty high level so I have no idea? 🤷‍♀️

Did you find this page helpful?