Server Load Functions in Svelte

From the SvelteKit cheat sheet · Data Loading · verified Nov 2026

Server Load Functions

Server-only load functions with direct database access

javascript
// +page.server.js
import { error } from '@sveltejs/kit';
import { db } from '#lib/server/database.js';

export async function load({ params }) {
  const post = await db.post.findUnique({
    where: { slug: params.slug }
  });

  if (!post) error(404, 'Post not found');

  return {
    post
  };
}
🔒 Runs only on server with access to secrets
🗄️ Direct database access without API calls
🍪 Full access to cookies and headers
⚡ Return a promise without await to stream it

Continue with SvelteKit

Save the full cheat sheet or work through every related task.

More Svelte tasks

Back to the full SvelteKit cheat sheet