Loading...
Loading...
SvelteKit and Svelte 5 done right. Runes, load functions, form actions, SSR patterns, and modern Svelte.
npx skill4agent add ofershap/sveltekit-best-practices sveltekit-best-practices<script>
import { writable, derived } from 'svelte/store';
let count = writable(0);
$: doubled = $count * 2;
$: if (count > 5) alert('too high');
</script>
<p>{$count}</p><script>
let count = $state(0);
let doubled = $derived(count * 2);
$effect(() => {
if (count > 5) alert('too high');
});
</script>
<p>{count}</p><script>
let count = 0;
count = count + 1;
</script><script>
let count = $state(0);
count = count + 1;
</script><script>
let firstName = $state('John');
let lastName = $state('Doe');
$: fullName = `${firstName} ${lastName}`;
</script><script>
let firstName = $state('John');
let lastName = $state('Doe');
let fullName = $derived(`${firstName} ${lastName}`);
</script><script>
let count = $state(0);
$: if (count > 5) console.log('count is high');
</script><script>
let count = $state(0);
$effect(() => {
if (count > 5) console.log('count is high');
});
</script><script>
export let title = 'Default';
export let count;
</script>
<h1>{title}</h1><script>
let { title = 'Default', count } = $props();
</script>
<h1>{title}</h1><script>
let { value } = $props();
</script>
<input bind:value={value} /><script>
let { value = $bindable() } = $props();
</script>
<input bind:value={value} /><script>
import { onMount } from 'svelte';
let data = $state(null);
onMount(async () => {
data = await fetch('/api/users').then(r => r.json());
});
</script>
{#if data}{data.name}{/if}// +page.server.ts
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ fetch }) => ({
data: await fetch("/api/users").then((r) => r.json()),
});<!-- +page.svelte -->
<script>
let { data } = $props();
</script>
{#if data}{data.name}{/if}<form on:submit={async (e) => {
e.preventDefault();
await fetch('/api/login', { method: 'POST', body: new FormData(e.target) });
goto('/dashboard');
}}>// +page.server.ts
import type { Actions } from "./$types";
export const actions = {
default: async ({ request, cookies }) => {
const data = await request.formData();
// validate, authenticate, set cookie
return { type: "redirect", location: "/dashboard" };
},
};<form method="POST" use:enhance><!-- Multiple pages each fetch user -->
<script>
let user = $state(null);
onMount(() => fetchUser().then(u => user = u));
</script>// +layout.server.ts
export const load = async ({ locals }) => ({
user: locals.user,
});<!-- +error.svelte -->
<script>
let { status, message } = $props();
</script>
<h1>{status}</h1>
<p>{message}</p>// hooks.server.ts
export const handle = async ({ event, resolve }) => {
event.locals.user = await getUser(event);
if (!event.locals.user && event.url.pathname.startsWith("/dashboard")) {
return redirect(302, "/login");
}
return resolve(event);
};<script>
export let slots;
</script>
{#if slots.header}<slot name="header" />{/if}<script>
let { header = @render(() => {}) } = $props();
</script>
{@render header()}datamethod="POST"use:enhance$app/forms{#await data.promise}import type { PageServerLoad, PageProps } from './$types'