The official SvelteKit documentation includes a discussion on shared state:
In many server environments, a single instance of your app will serve multiple users. As a result, per-request state should not be saved in shared variables outside load functions but should instead be stored in event.locals. Similarly, per-user state should not rely on global variables, but should utilize $page.data or leverage Svelte's context feature to maintain scoped state.
After going through the documentation, it appears that the mention of event.locals
is scarce, with only another brief hint that it exists in src/app.d.ts
.
This leads me to wonder: when would it be appropriate to store data globally versus locally? Is there a particular scenario where using event.locals
is preferred? It seems like there might be a crucial distinction or guideline missing from the documentation that needs more clarity.