Index of every page on this site: llms.txt
All skills
spences10 avatar

/sveltekit-data-flow

@5fe4989
Run it yourself

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().

Use this Skill: https://skilld.dev/gh/spences10/svelte-skills-kit/sveltekit-data-flow

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesclient-auth-invalidation.md

≈764 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Client-Side Auth Invalidation

When using client-side auth libraries (Better Auth, Firebase, Supabase client), layout server data doesn't auto-refresh after login/logout.

The Problem

// signin/+page.svelte - BROKEN
async function handle_signin() {
  await auth_client.signIn.email({ email, password });
  goto('/');  // Layout still shows "logged out"
}

Why? Client-side navigation (goto()) doesn't re-run server load functions. The session cookie is set, but +layout.server.ts data is stale.

Solution A: Inline Invalidation (Simple)

Call invalidateAll() after auth state change:

// signin/+page.svelte
import { goto, invalidateAll } from '$app/navigation';

async function handle_signin() {
  const result = await auth_client.signIn.email({ email, password });
  if (result.error) return;

  await invalidateAll();  // Re-runs ALL load functions
  goto('/');
}

// signout handler
async function handle_signout() {
  await auth_client.signOut();
  await invalidateAll();
}

Use when: Single auth entry point, simple apps.

Solution B: Auth State Listener (Robust)

Set up a listener in root layout that auto-invalidates on auth changes:

<!-- +layout.svelte -->
<script>
  import { invalidateAll } from '$app/navigation';
  import { onMount } from 'svelte';
  import { auth_client } from '$lib/auth-client';

  onMount(() => {
    // Listen for auth state changes
    const unsubscribe = auth_client.onAuthStateChange(() => {
      invalidateAll();
    });

    return unsubscribe;
  });
</script>

Use when: Multiple auth flows (OAuth, magic links, etc.), complex apps.

Common Mistakes

Not awaiting invalidateAll()

// WRONG - race condition
invalidateAll();
goto('/');

// RIGHT - wait for data refresh
await invalidateAll();
goto('/');

Destructuring layout data

<!-- WRONG - static snapshot -->
<script>
  let { data } = $props();
  const { user } = data;  // Never updates after invalidateAll()
</script>

<!-- RIGHT - reactive access -->
<script>
  let { data } = $props();
</script>

{data.user?.email}

When invalidateAll() Runs

Per Svelte docs:

invalidateAll() is the nuclear option - it re-runs ALL load functions for the current page, regardless of dependencies.

This includes:

  • +layout.server.ts (all levels)
  • +layout.ts (all levels)
  • +page.server.ts
  • +page.ts

Comparison with Server-Side Auth

Approach Auth Location Invalidation
Form actions Server (+page.server.ts) Automatic (page reload)
Client auth Browser (auth_client) Manual (invalidateAll())

Form actions with throw redirect() cause a full navigation, which naturally re-runs load functions. Client-side auth with goto() does not.

See Also

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo5 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides standard documentation and guidance for SvelteKit data flow, including load functions, form actions, and serialization. It promotes secure development practices and contains no malicious patterns.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer9mo

    7/7 files flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 5fe4989. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago

README badge

README badge for spences10/svelte-skills-kit/sveltekit-data-flow