Article · topic 02 · 7 min

Module-level state in SvelteKit leaks between requests (594 of 600)

Keep the signed-in user in module-level state on the server and every request shares it. One request at a time, nothing goes wrong. With ten in flight, almost every page carried another user's name. Here is the measurement, why it happens, and where per-request data belongs.

Measured 28 September 2026 on node build (adapter-node), SvelteKit 3.0.1 · Svelte 5.57.1 · 9 Oct 2026 (and 2.70.3), Node 26.3, Windows 11, i9-13900KF. Text updated 3 October 2026. Author: Lean Pilar.

The pattern

A module is evaluated once per server process. A $state object in a .svelte.ts file, a store, or a plain let is therefore one object for every request that process handles. Write the current user into it during a request, and the next request can render with that user. It looks like idiomatic Svelte 5, and the SvelteKit docs do warn about it: "it's important not to store data in shared variables". It still ships, because every test you run alone passes.

The common pattern
// src/lib/unsafe/session.svelte.ts
export const session = $state<{ name: string | null }>({ name: null });

// src/routes/leak/module-state/+layout.server.ts
// ANTI-PATTERN: "remember the signed-in user globally, so any component can read it".
export const load = async ({ locals }) => {
	session.name = locals.user?.name ?? null;
	return {};
};
demos/02-ssr-state-leak · /leak/module-state
The safe pattern
// src/routes/safe/props/+layout.server.ts
// SAFE: per-request data leaves the load function as data, never through a module.
export const load = async ({ locals }) => ({ name: locals.user?.name ?? null });
demos/02-ssr-state-leak · /safe/props

How it was measured

A small SvelteKit app written for this topic, with 15 routes: the ways of keeping the user in shared state that leak ($state, let, store, hook, layout component), and seven safe ways. Production build, served by node build. Each request comes from a different simulated user and the data call waits 20–80 ms, like a database would. A script keeps N requests in flight (3 rounds of 200 = 600 requests; 150 for one at a time) and counts every page whose HTML shows a name that is not the requester's. JavaScript stays off: this is the server HTML, the one that caches, link previews and slow phones get.

Pages that showed another user's name

Where the user is kept1 at a time10 in flight50 in flight200 in flight
module $state0 / 150594 / 600597 / 600597 / 600
module let0 / 150594 / 600597 / 600597 / 600
module store0 / 150592 / 600595 / 600597 / 600
set in hook, read by helper0 / 150595 / 600596 / 600597 / 600
set in layout component, read after await (async SSR)0 / 150594 / 600597 / 600597 / 600
set in layout component (sync render)0000
all 7 safe routes0000

"N in flight" is a stress test and is labelled as one. The plain let and the hook-set variable leak the same way as $state: this is how JavaScript modules work on a server, not a Svelte bug.

Requests arriving at a realistic rate

/leak/module-state, open loop (Poisson arrivals)Another user's name
1 request/s for 180 s6 / 180 (3.3%)
10 requests/s for 30 s123 / 300 (41%)
100 requests/s for 10 s978 / 1,000 (97.8%)
safe route (props), 100 requests/s0 / 1,000

At one request per second, 6 of 180 pages (3.3 %) showed another user. The rate depends on your traffic and on how long your data calls take; it is not zero on a small site.

What a browser shows

In 10 of 10 browser trials with 20 other users loading the same dashboard, the server HTML showed another user's name for the test user who made the request (10 of 10). After hydration the page said "Not signed in" (10 of 10), so the wrong name is in the HTML, and the browser then overwrites it. A public page that only reads the global greeted 20 of 20 anonymous visitors with a previous user's name, one request at a time.

Where per-request data belongs

The two rules that are free in the demo README, and the only two you need to stop the leak:

  1. Never write per-user or per-request data into module-level state on the server. Not into $state, not into a store, not into a let, not from a hook.
  2. Pass per-request data as data. Return it from load; read it through page.data or props. On the server, keep it in event.locals and reach the current request with getRequestEvent().

Module-level state is fine for values that are the same for every user, and for state that is only written in the browser. The seven safe routes in the demo (props, page.data, context created per request, locals + getRequestEvent(), and others) showed 0 wrong pages at 10, 50 and 200 in flight and at 100 requests per second.

Serverless platforms were not measured here (adapter-node only). Where an instance serves more than one request, the read-only case above applies: it needs no concurrency at all. With experimental async SSR, treat every read of shared state after an await as unsafe: the "set in layout component, read after await" row above is that case.

Rerun it

The demo (MIT) with the measure script, the open-loop script and the 15 tests (T1–T15) goes public before sales open soon; the waitlist gets the link first. Until then every number above is on the topic page with the versions and the date, and the result file is quoted there.

What I haven't measured: how often AI assistants write this pattern. What I measured: what it does once it ships, and which fix holds. The full write-up, with the anonymous-visitor case, async SSR, search patterns for your codebase and a drop-in concurrency test, is the guide for this topic (sales open soon).

Free now · guides soon

Get the SSR state checklist

One page per topic, every check backed by a measurement: section renderer, ssr state, sveltekit defaults and big lists in svelte 5 state.

  1. 1Enter your email. You land on our mailing-list page.
  2. 2Open the confirmation email from hello@measuredfrontend.com and click the link.
  3. 3The confirmation page lists the four PDFs. About one minute in total.

After that: one email when the guides open, and a short note when a new measurement is published. Unsubscribe in one click. No confirmation, no emails: the address is deleted within 30 days.

Double opt-in: nothing is sent until you click the link in that email.