Under The Hood
frontend svelte SvelteKit 2.x

+page.ts (Universal) vs +page.server.ts (Server-Only) Load

Last updated
Prerequisites:
SvelteKit routing basics
Understanding of SSR and client-side navigation
  • sveltekit
  • load
  • universal
  • server
  • ssr
  • data-fetching

Read at your depth

The practical view

+page.server.ts runs only on the server (during SSR and when the route is loaded on the server). +page.ts (universal load) runs on both the server and the client. Use +page.server.ts when you need direct DB access, secrets, or server-only APIs. Use +page.ts for data that can be fetched from public APIs on either side, or when you need to run code in the browser. The universal load's returned data is available in the page's $props; the server load's data is merged into the same props. Server load runs before universal load on the server (universal receives the server data as input).

The same idea in other frameworks

react equivalent
Next.js RSC server components + client data fetching map to server load vs universal load.

Legacy vs modern

Fetching in a component vs server load

Fetching inside a component couples data logic to the UI and lacks SSR serialization; server load runs before render on the server and ships the data with the page.

before → after
Fetch in onMount (client-only)
onMount(async () => {
  data = await fetch('/api/users').then(r => r.json()); // ❌ OLD: client-only
});
Server load
export const load = async ({ fetch }) => {
  const res = await fetch('/api/users'); // ✅ NEW: runs on the server during SSR
  return { users: await res.json() };
};

Interview gotchas

Context

This tests the data-fetching architecture decision — interviewers want the security and capability boundary, not just 'server runs first'.

The mechanical answer

Use +page.server.ts when the load needs server-only capabilities: direct database access, private environment variables/secrets, cookies (reading AND writing), request headers, and server-side mutation logic. These must not be exposed to the client. Universal +page.ts runs in both environments, so it can use browser APIs and run during client-side navigation without a server round trip, but it cannot access secrets or read/write cookies. The guidance: default to server load for data that originates server-side (DB, authenticated data, CMS), and use universal load for public API data, browser-dependent computation, or when you need the same load to run during client navigation. Both serialize their results to the page; server data is merged and available to the universal load via the parent argument.

Trap

The naive answer is 'use universal load to be faster'. Universal load re-runs on every client navigation and cannot access secrets — putting sensitive logic there leaks it. Another trap: assuming server load runs on every client-side navigation — it does not; only universal load runs client-side, so data needing re-fetch must be handled by universal load or invalidation. Candidates who map capabilities to the two files show real architectural understanding.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.