$app/navigation module provides functions for programmatic navigation and managing the navigation lifecycle.
goto
Navigate programmatically to a given route.Navigate to a URL with optional configuration.Parameters:
url(string | URL) - Where to navigate tooptions(object) - Navigation options
Options
If
true, will replace the current history entry rather than creating a new one with pushStateIf
true, the browser will maintain its scroll position rather than scrolling to the topIf
true, the currently focused element will retain focus after navigationIf
true, all load functions of the page will be rerunCauses load functions to re-run if they depend on one of the URLs
An optional object that will be available as
page.stateUsage
invalidate
Causesload functions to re-run if they depend on the URL.
Invalidate data dependencies.Parameters:
resource(string | URL | ((url: URL) => boolean)) - The URL or predicate to invalidate
invalidateAll
Causes allload and query functions belonging to the currently active page to re-run.
Invalidates all data on the current page.Returns: Promise<void>
refreshAll
Causes all currently active remote functions to refresh, and allload functions to re-run.
Refresh all remote functions and optionally load functions.Parameters:
options(object) - Configuration options
preloadData
Programmatically preloads a page’s code and data.Preload page data for instant navigation.Parameters:
href(string) - Page URL to preload
preloadCode
Programmatically imports the code for routes that haven’t yet been fetched.Preload route code modules.Parameters:
pathname(string) - Route pathname to preload (e.g.,/aboutor/blog/*)
beforeNavigate
A navigation interceptor that triggers before navigation starts.Register a callback that runs before navigation.Parameters:
callback(function) - Called with navigation details
Callback Parameters
Information about the current page (url, route, params)
Information about the destination page
Navigation type:
'enter', 'leave', 'link', 'goto', or 'popstate'Whether the navigation will cause document unload
Call to prevent navigation (may show browser confirmation for
'leave' type)Number of entries to go back/forward (only for
'popstate' type)afterNavigate
A lifecycle function that runs after navigation completes.Register a callback that runs after navigation.Parameters:
callback(function) - Called with navigation details
onNavigate
A lifecycle function for handling view transitions and post-navigation cleanup.Register a callback that runs during navigation.Parameters:
callback(function) - Called before DOM updates
disableScrollHandling
Disables SvelteKit’s built-in scroll handling.Disable automatic scroll handling for current navigation.Returns: void
pushState
Programmatically create a new history entry with state.Push a new history state.Parameters:
url(string | URL) - The URL (use''for current URL)state(App.PageState) - State object to store
replaceState
Programmatically replace the current history entry with state.Replace the current history state.Parameters:
url(string | URL) - The URL (use''for current URL)state(App.PageState) - State object to store
Use
pushState and replaceState for shallow routing to update state without full navigation.