Basic Svelte
Introduction
Bindings
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
The navigating
object represents the current navigation. When a navigation starts — because of a link click, or a back/forward navigation, or a programmatic goto
— the value of navigating
will become an object with the following properties:
from
andto
— objects withparams
,route
andurl
propertiestype
— the type of navigation, e.g.link
,popstate
orgoto
For complete type information visit the
Navigation
documentation.
It can be used to show a loading indicator for long-running navigations. In this exercise, src/routes/+page.server.js
and src/routes/about/+page.server.js
both have an artificial delay. Inside src/routes/+layout.svelte
, import the navigating
object and add a message to the nav bar:
<script>
import { page, navigating } from '$app/state';
let { children } = $props();
</script>
<nav>
<a href="/" aria-current={page.url.pathname === '/'}>
home
</a>
<a href="/about" aria-current={page.url.pathname === '/about'}>
about
</a>
{#if navigating.to}
navigating to {navigating.to.url.pathname}
{/if}
</nav>
{@render children()}
<script lang="ts">
import { page, navigating } from '$app/state';
let { children } = $props();
</script>
<nav>
<a href="/" aria-current={page.url.pathname === '/'}>
home
</a>
<a href="/about" aria-current={page.url.pathname === '/about'}>
about
</a>
{#if navigating.to}
navigating to {navigating.to.url.pathname}
{/if}
</nav>
{@render children()}
Prior to SvelteKit 2.12, you had to use
$app/stores
for this, which provides a$navigating
store with the same information. If you’re currently using$app/stores
, we advise you to migrate towards$app/state
(requires Svelte 5).
<h1>home</h1>
<p>this is the home page.</p>