2022-08-21

Sveltekit and shared state - can someone give a concrete example of when to use event.locals vs. $page.data?

The official SvelteKit docs have this little blurb regarding shared state:

In many server environments, a single instance of your app will serve multiple users. For that reason, per-request state must not be stored in shared variables outside your load functions, but should instead be stored in event.locals. Similarly, per-user state must not be stored in global variables, but should instead make use of $page.data (which contains the combined data of all load functions) or use Svelte's context feature to create scoped 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 in src/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.



No comments:

Post a Comment