Avoid shared state on the server
Browsers are stateful — state is stored in memory as the user interacts with the application. Servers, on the other hand, are stateless — the content of the response is determined entirely by the content of the request.The problem
Consider this buggy code:Why is this bad?
Why is this bad?
The
user variable is shared by everyone who connects to this server. If Alice submitted an embarrassing secret, and Bob visited the page after her, Bob would know Alice’s secret!The solution
Instead, you should authenticate the user using cookies and persist the data to a database:No side-effects in load
For the same reason, yourload functions should be pure — no side-effects (except maybe the occasional console.log(...)).
Bad: Writing to global state
Good: Returning data
Using state and stores with context
You might wonder how we’re able to usepage.data and other app state if we can’t use global state. The answer is that app state on the server uses Svelte’s context API.
Creating context-based state
We’re passing a function into
setContext to keep reactivity across boundaries. Read more about this in the Svelte documentation on passing state into functions.Reactivity considerations
Component and page state is preserved
When you navigate around your application, SvelteKit reuses existing layout and page components.The problem
Consider this buggy code:Why is this buggy?
Why is this buggy?
Navigating from
/blog/my-short-post to /blog/my-long-post won’t cause the component to be destroyed and recreated. The data prop will update, but wordCount and estimatedReadingTime won’t be recalculated.The solution
Make the values reactive using$derived:
Force component recreation
If you need to completely destroy and remount a component on navigation:If your code in
onMount and onDestroy needs to run again after navigation, use afterNavigate and beforeNavigate respectively.Storing state in the URL
If you have state that should survive a reload and/or affect SSR, such as filters or sorting rules on a table, URL search parameters are a good place to put them.Using search params
- In links
- In forms
- Programmatically
Accessing in load functions
Accessing in components
Storing ephemeral state in snapshots
Some UI state, such as ‘is the accordion open?’, is disposable — if the user navigates away or refreshes the page, it doesn’t matter if the state is lost. In some cases, you do want the data to persist if the user navigates to a different page and comes back. For this, SvelteKit provides snapshots:Snapshots let you associate component state with a history entry without storing it in the URL or database.
State management strategies
For different types of state, use different strategies:Authentication state
Use cookies and
event.localsPage data
Use
load functions and return dataFilters and sorting
Use URL search parameters
UI state
Use Svelte state and context
Ephemeral history
Use snapshots
Global client state
Use context or stores (only if not using SSR)