Invalidation and reloading help keep your app's data fresh by updating it when something changes.
Invalidation and reloading in Svelte
import { invalidate } from '$app/navigation'; // To reload data for a route await invalidate('route-or-url');
invalidate is an async function that tells SvelteKit to reload data for a given route or URL.
You can call invalidate() without arguments to reload the current page's data.
import { invalidate } from '$app/navigation'; // Reload current page data await invalidate();
/products page, even if you are on a different page.import { invalidate } from '$app/navigation'; // Reload data for a specific route await invalidate('/products');
import { invalidate } from '$app/navigation'; // Reload data for a specific URL with query await invalidate('/search?query=svelte');
This simple Svelte component shows a button that, when clicked, increases a count and reloads the current page's data using invalidate(). The count updates immediately, simulating fresh data.
<script> import { invalidate } from '$app/navigation'; let count = 0; async function refreshData() { // Simulate data change count += 1; // Reload current page data await invalidate(); } </script> <button on:click={refreshData} aria-label="Refresh data">Refresh Data</button> <p>Data refresh count: {count}</p>
Use invalidate() to keep your app's data up to date without a full page reload.
Invalidation works well with SvelteKit's load functions that fetch data.
Remember to use await with invalidate() to wait for data to reload before continuing.
Invalidation reloads data for a route or page to keep information fresh.
Use invalidate() from $app/navigation to trigger reloads.
This helps your app show the latest data after changes or navigation.