sveltekit-data-flow

SvelteKit data flow guidance. Use for load functions, form actions, and server/client data. Covers +page.server.ts vs +page.ts, serialization, fail(), redirect(), error().

⭐6stars🔀0forks📁View on GitHub🕐Updated Dec 30, 2025

When & Why to Use This Skill

This Claude skill provides expert guidance on SvelteKit data flow management, helping developers navigate the complexities of load functions, form actions, and the critical distinctions between server-side and client-side data handling. It ensures the implementation of best practices for data serialization, error handling, and redirects to build robust SvelteKit applications.

Use Cases

  • Architectural Decision Making: Determining whether to use +page.server.ts or +page.ts based on the need for server-only resources, environment secrets, or client-side browser APIs.
  • Form Handling Optimization: Implementing secure and efficient form actions with standardized error responses using fail() and seamless navigation via redirect().
  • Data Integrity & Serialization: Ensuring data passed from server-side load functions to the client is JSON-serializable and troubleshooting common serialization errors.
  • Error Management: Implementing consistent error-handling patterns across the application using SvelteKit's built-in error() and fail() utilities.
namesveltekit-data-flow
# IMPORTANTKeep description on ONE line for Claude Code compatibility
descriptionSvelteKit data flow guidance. Use for load functions, form actions, and server/client data. Covers +page.server.ts vs +page.ts, serialization, fail(), redirect(), error().

SvelteKit Data Flow

Quick Start

Which file? Server-only (DB/secrets): +page.server.ts | Universal (runs both): +page.ts | API: +server.ts

Load decision: Need server resources? → server load | Need client APIs? → universal load

Form actions: Always +page.server.ts. Return fail() for errors, throw redirect() to navigate, throw error() for failures.

Example

// +page.server.ts
import { fail, redirect } from '@sveltejs/kit';

export const load = async ({ locals }) => {
	const user = await db.users.get(locals.userId);
	return { user }; // Must be JSON-serializable
};

export const actions = {
	default: async ({ request }) => {
		const data = await request.formData();
		const email = data.get('email');

		if (!email) return fail(400, { email, missing: true });

		await updateEmail(email);
		throw redirect(303, '/success');
	},
};

Reference Files

Notes

  • Server load output is automatically passed to universal load as data parameter
  • ALWAYS rethrow redirects/errors: throw redirect(), throw error()
  • Don't return class instances or functions from server load (not serializable)
  • Last verified: 2025-01-11