Client-side navigation functions for programmatic routing, data invalidation, and preloading.
/**
* Navigate to a URL programmatically
* @param url - Target URL
* @param opts - Navigation options
*/
function goto(
url: string | URL,
opts?: {
replaceState?: boolean;
noScroll?: boolean;
keepFocus?: boolean;
invalidateAll?: boolean;
invalidate?: Array<string | URL | ((url: URL) => boolean)>;
state?: App.PageState;
}
): Promise<void>;Usage:
import { goto } from '$app/navigation';
await goto('/dashboard');
await goto('/post/123', { replaceState: true });
await goto('/search', { keepFocus: true, state: { query: 'hello' } });/**
* Invalidate load functions that depend on a resource
* @param resource - URL, URL object, or predicate function
*/
function invalidate(
resource: string | URL | ((url: URL) => boolean)
): Promise<void>;
/**
* Invalidate all load functions
*/
function invalidateAll(): Promise<void>;Usage:
import { invalidate, invalidateAll } from '$app/navigation';
// Invalidate specific URL
await invalidate('/api/posts');
// Invalidate by predicate
await invalidate(url => url.pathname === '/api/user');
// Invalidate custom dependency
await invalidate('app:posts');
// Invalidate everything
await invalidateAll();Refresh all remote functions on the current page, and optionally re-run load functions.
/**
* Refresh remote functions and optionally include load functions
* @param opts - Options controlling what gets refreshed
* @since 2.47.0
*/
function refreshAll(opts?: { includeLoadFunctions?: boolean }): Promise<void>;Usage:
import { refreshAll } from '$app/navigation';
// Refresh only remote functions (default)
await refreshAll();
// Refresh remote functions and re-run load functions
await refreshAll({ includeLoadFunctions: true });
// Refresh remote functions without re-running load functions
await refreshAll({ includeLoadFunctions: false });/**
* Preload data for a page
* @param href - URL to preload
*/
function preloadData(href: string): Promise<
| { type: 'loaded'; status: number; data: Record<string, any> }
| { type: 'redirect'; location: string }
>;
/**
* Preload code for a single page
* @param pathname - Path to preload code for
*/
function preloadCode(pathname: string): Promise<void>;Usage:
import { preloadData, preloadCode } from '$app/navigation';
// Preload on hover
<a
href="/slow-page"
on:mouseenter={() => preloadData('/slow-page')}
>
Slow Page
</a>
// Preload code for a route
await preloadCode('/dashboard');
await preloadCode('/settings');/**
* Create a history entry without navigating
* @param url - Target URL
* @param state - State object
*/
function pushState(url: string | URL, state: App.PageState): void;
/**
* Replace current history entry without navigating
* @param url - Target URL
* @param state - State object
*/
function replaceState(url: string | URL, state: App.PageState): void;
/**
* Disable automatic scroll handling for the current navigation
* Must be called during navigation (e.g., in beforeNavigate callback)
*/
function disableScrollHandling(): void;Usage:
import { pushState, replaceState } from '$app/navigation';
// Open modal with history
function openModal() {
pushState('/modal', { modal: true });
}
// Close modal
function closeModal() {
history.back();
}
// Update URL without navigation
replaceState(`/search?q=${query}`, {});
// Disable scroll handling during navigation
import { beforeNavigate, disableScrollHandling } from '$app/navigation';
beforeNavigate(() => {
disableScrollHandling();
});$app/navigationgoto() options:
replaceState: Replace history entry instead of pushnoScroll: Don't scroll to topkeepFocus: Preserve focusinvalidateAll: Rerun all load functionsinvalidate: Array of resources to invalidate (URLs, URL objects, or predicate functions)state: Shallow routing state of type App.PageState (accessible via $page.state)invalidate() causes load functions with matching dependencies to rerunfetch() and custom dependencies from depends()preloadData() fetches data, preloadCode() loads JavaScript only