Svelte · Senior · Question 25 of 75
Why should load functions be free of side effects?
// +page.js
import { user } from "./state.svelte.js";
export async function load({ fetch }) {
user.current = await (await fetch("/api/user")).json();
}- ✓Correct answer: Writing to shared state from
loadleaks data between users on the server and makes data flow unpredictable; return data instead - 2Load functions can't assign variables
- 3It slows down the build
Explanation
Return the data and let components read it via data or pass it into context.