Replies: 2 comments 2 replies
-
It's been a while since I did PHP directly, but the main difference with Node is that Node has one global scope. So you can't do something like
Global on the server: I'd keep the global stuff to a minimum. I have a database connection pool and a Redis client. The environment is also global. "Locally" on the server means "scoped to the request in On the client SvelteKit exposes a set of "readable stores" that are shared across client-side router navigations. |
Beta Was this translation helpful? Give feedback.
-
@cdcarson is correct, and I can add to you need to be aware of your deployment target as well. If you plan to run Sveltekit as a long running process, be very careful about keeping things scoped to the current request. (aggressively so!) Since Node resolves each dependency once and then serves it from a cache, each dependent is using the same object in memory to work against, i.e. shared state is easy to do - and easy to share by accident if you aren't thinking about it. The good news is that SK has mechanisms to get around this using prescribed methodology in the docs, so that should be your preferred approach. Even if you plan to run SK in an edge function or serverless environment, this can still be an issue and worth considering, though less so. Edge tech will keep the endpoint "hot" for a short time period and it's conceivable that a request could come in from a different user to a hot function and state could be shared. This would of course be less of an issue than in a long running situation, but you get the idea. When in doubt, the best practice is to build up a context for each request through the handle hook and |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I come from a PHP/Symfony background, so using a non-MVC TS framework for both back end and front end development is completely out of my wheelhouse. Apologies in advance if this question is answered elsewhere. What follows is a question I asked on Stack Overflow (here) which has yet to receive an answer:
The official SvelteKit docs have this little blurb regarding shared state:
From what I've seen of the docs, it's one of two places where
event.locals
is even mentioned, with the other brief mention being simply that its interface exists insrc/app.d.ts
.So, my question is: what would constitute data I would want to store globally versus locally? Is there a common use case where
event.locals
is the way to go? It seems like there's a missing, yet important, distinction/rule-of-thumb that should be explicitly illustrated in the docs.Beta Was this translation helpful? Give feedback.
All reactions