Use this file to discover all available pages before exploring further.
useHistory() returns the history object that the nearest ancestor <Router> is using to track and drive navigation. By default this is svelte5-router’s built-in globalHistory, which wraps the browser’s window.history API (or a memory-based fallback during SSR). If a <Router> was given a custom history prop, useHistory() returns that custom instance instead—making it the correct way to perform imperative navigation inside a component when you need to respect a potentially overridden history. Like the other context hooks, it must be called during Svelte component initialisation.
A read-only snapshot of the current location. This is a plain object—not a store—so it reflects the location at the moment of access. For a reactive store that updates on every navigation, use useLocation() instead.
Subscribes to location changes. The listener is called whenever a navigation occurs, whether triggered by navigate(), a <Link> click, or the browser’s back/forward buttons.Returns an unsubscribe function—call it to remove the listener and prevent memory leaks.
When true, the currently focused element will have .blur() called on it after navigation. Useful for dismissing mobile keyboards or removing focus rings after a form submission.
The top-level navigate() export always uses globalHistory—the default browser history singleton. useHistory() returns whichever history object was passed to the nearest <Router> via its history prop.
navigate() (top-level)
history.navigate() from useHistory()
Usable outside components
✅ Yes
❌ No (requires context)
Respects custom history prop
❌ No
✅ Yes
Good for SSR / testing
❌ No (uses window)
✅ Yes (if a memory history is injected)
// Top-level — always uses the global browser historyimport { navigate } from "svelte5-router";navigate("/dashboard");
<script lang="ts"> // Inside a component — uses the Router's history, which may be custom import { useHistory } from "svelte5-router"; const history = useHistory(); // history.navigate respects any custom history passed to <Router history={...}></script>
useHistory() uses Svelte’s getContext() and must be called during component initialisation, at the top level of a <script> block inside a .svelte file. It cannot be used in plain .ts files or inside callbacks and effects.
For navigation outside Svelte components—such as in utility modules or service files—use the top-level navigate() export instead.
import { navigate } from "svelte5-router";navigate("/login");