by imnnos · 3y ago · 0 comments
For instance, Firebase is meant to connect on the client so you don’t need the backend database connection, but then you can’t use SvelteKit’s SSR. Does Supabase work similarly in this aspect? What do you do in this situation?
In Node, a “module” is generally scoped to a file. So you make a db.ts file and export the instance you create and it functions basically as a singleton. One DB connection per user per session. SvelteKit let’s you choose where to put that. The first place you import it is when it instantiates.
Auth in SvelteKit is trickier than it should be, but there’s a really good technique that I recommend you follow: https://www.captaincodeman.com/re-creating-the-sveltekit-ses...
I believe he uses firebase in his example. But don’t just copy paste, try to understand what is actually happening and why he had to do this workaround. Once you understand how SSR actually works, things get a lot easier.
Stores are a way of storing state on the client side, but to do SSR Svelte has to use stores on its “backend”. The key thing to understand is that stores that are used for SSR are server wide and persistent so you can’t store user specific data in them. There’s a special spot for that kind of state called events.locals.
It’s probably the trickiest area of SvelteKit IMO. I hope they make it better.