Consider a Svelte component that subscribes to a writable store. What is the immediate effect on the component when the store's value is invalidated using invalidate()?
import { writable } from 'svelte/store'; const count = writable(0); // Somewhere in the component count.set(1); // invalidate('count'); // <-- invalidate() is not a method on writable stores
Think about what invalidate() triggers in Svelte's reactive system.
Calling invalidate() marks the store as needing update, causing components that depend on it to re-run reactive code and update the DOM accordingly.
Given a SvelteKit page with a load function that fetches data, what will be the displayed value after calling invalidate('path')?
export async function load({ fetch }) { const res = await fetch('/api/data'); const data = await res.json(); return { props: { data } }; } // In the component <script> import { invalidate } from '$app/navigation'; let count = 0; function refresh() { invalidate('/api/data'); count += 1; } </script> <button on:click={refresh}>Refresh {count}</button> <p>{JSON.stringify(data)}</p>
Invalidate triggers a reload of the data for the specified path.
Calling invalidate('/api/data') tells SvelteKit to re-run the load function for that path, fetching fresh data and updating the component.
Choose the correct way to invalidate the data of the current page in SvelteKit.
Think about how to specify the current page path dynamically.
To invalidate the current page data, you must pass the current path as a string. Using window.location.pathname dynamically gets the current path.
Given this code snippet, why does calling invalidate('/api/data') not cause the page to reload fresh data?
import { invalidate } from '$app/navigation'; async function refresh() { await invalidate('/api/data'); } // load function export async function load({ fetch }) { const res = await fetch('/api/data'); const data = await res.json(); return { props: { data } }; }
Consider what paths invalidate expects to reload.
Invalidate triggers reloading of page routes, not API endpoints. Since '/api/data' is an API route, invalidating it does not cause the page's load function to run again.
In a SvelteKit app with nested layouts, what happens when you call invalidate() on a parent layout's path?
Think about how SvelteKit cascades data loading in nested routes.
Invalidating a parent layout path causes SvelteKit to re-run load functions for that layout and all nested child layouts and pages, updating their data accordingly.