Efficiently Managing Supabase Sessions and Context Sharing in SolidStart

I am developing an application using Supabase and SolidStart, and I’ve encountered some challenges related to session management and sharing a Supabase instance. My Scenario 1. Session management after user login: • After a user logs in, Supabase returns sessionData (including access tokens, etc.). • I plan to use h3’s session to store this sessionData in the user’s cookies. 2. Handling subsequent requests: • For each user request, I read the sessionData from the cookie, set it as Supabase’s session, and then use Supabase. • However, setting the Supabase session on every request introduces overhead and feels redundant. 3. Optimization goal: • I want to share a configured Supabase instance within the server lifecycle to avoid resetting it repeatedly and only update the session when necessary. • I am considering using Context to achieve this shared instance. My Questions 1. Can Context only be created on the client-side? 2. Is it possible to share Context between the client and the server? 3. Can Router use Context, enabling the preload attribute to leverage the shared Supabase instance for data fetching? Any insights or suggestions would be greatly appreciated!:start: 🎉
1 Reply
peerreynders
peerreynders2w ago
Is it possible to share Context between the client and the server?
No. For the purpose of SSR context is created server side but after hydration, context only exists on the client.
I want to share a configured Supabase instance within the server lifecycle to avoid resetting it repeatedly and only update the session when necessary.
That sounds like a "stateful server". SolidStart's server aims for stateless operation, storing any client associated back end state in an external data store. The other confounding factor is that the server uses separate workers for different capabilities. So even if you used a global Map to keep multiple supabase sessions alive for various clients across their requests it would only be accessible in the worker that created the Map.
MDN Web Docs
Map - JavaScript | MDN
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

Did you find this page helpful?