sveltekit-data-flow

Manage SvelteKit server-client data flow with load functions and form actions.

6|Updated Jul 7, 2017
One-click install
npx skills add https://github.com/Seeker1911/dotfiles --skill sveltekit-data-flow-seeker1911
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sveltekit-data-flow
Source: https://github.com/Seeker1911/dotfiles/tree/main/agents/skills/sveltekit-data-flow
Command: npx skills add https://github.com/Seeker1911/dotfiles --skill sveltekit-data-flow-seeker1911

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Managing data flow in SvelteKit, especially between server and client, through load functions, form actions, and serialization, can be complex and error-prone. This Skill provides clear, actionable guidance to ensure robust and efficient data handling, letting you focus on building features.

Core Features & Use Cases

  • Load Functions Mastery: Understand the differences and best use cases for +page.server.ts (server-only) and +page.ts (universal) load functions to fetch data efficiently.
  • Form Action Handling: Get expert guidance on implementing form actions in +page.server.ts, including how to use fail() for validation errors, redirect() for navigation, and error() for fatal issues.
  • Data Serialization: Learn what data types can and cannot be safely passed between server and client, preventing common serialization errors and ensuring data integrity.
  • Use Case: When you're building a SvelteKit application and need to fetch data for a page, handle user form submissions, or ensure that data passed between server and client is correctly formatted and secure, this Skill simplifies the process.

Quick Start

I need to create a login form in SvelteKit. Show me how to set up a form action in +page.server.ts that validates user input, returns fail() on error, and redirects() to the dashboard on success.

Frequently Asked Questions about sveltekit-data-flow

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle data flow between server and client in SvelteKit?

Data flow in SvelteKit occurs through load functions and form actions. Use +page.server.ts for server-only logic and +page.ts for universal code. Data must be JSON-serializable when passed between server and client to avoid serialization errors.

What's the difference between +page.server.ts and +page.ts load functions?

+page.server.ts load functions run only on the server and can access secrets and databases directly. +page.ts load functions are universal and run on both server and client, so they must not expose sensitive data and must handle serialization constraints.

How do I validate form submissions and handle errors in SvelteKit form actions?

Implement form actions in +page.server.ts using fail() to return validation errors with a 400 status, redirect() to navigate on success, and error() to handle fatal issues. This ensures progressive enhancement and proper HTTP semantics.

What data types can safely pass between SvelteKit server and client?

Only JSON-serializable data types—strings, numbers, booleans, arrays, and plain objects—safely transfer between server and client. Dates, functions, Map, Set, and custom class instances require serialization or will cause transfer failures.

When should I use fail() versus redirect() in a SvelteKit form action?

Use fail() to return validation errors and keep the user on the form with error messages displayed. Use redirect() after successful form submission to navigate to a new page, following POST-redirect-GET patterns.

How do I prevent serialization errors when returning data from SvelteKit load functions?

Return only JSON-serializable objects from load functions. Convert non-serializable types like Date to strings before returning, and parse them on the client if needed. Ensure no functions, class instances, or circular references are included.