4meta5

sveltekit-data-flow

0
0
# Install this skill:
npx skills add 4meta5/skills --skill "sveltekit-data-flow"

Install specific skill from multi-skill repository

# Description

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

# SKILL.md


name: sveltekit-data-flow

IMPORTANT: Keep description on ONE line for Claude Code compatibility

prettier-ignore

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

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 → universal load via data param | ALWAYS
    throw redirect()/error()
  • No class instances/functions from server load (not serializable)
  • Last verified: 2025-01-11

# Supported AI Coding Agents

This skill is compatible with the SKILL.md standard and works with all major AI coding agents:

Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.