Basic Svelte
Introduction
Bindings
Classes and styles
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
SvelteKit makes three readonly state objects available via the $app/state module — page, navigating and updated. The one you’ll use most often is page, which provides information about the current page:
url— the URL of the current pageparams— the current page’s parametersroute— an object with anidproperty representing the current routestatus— the HTTP status code of the current pageerror— the error object of the current page, if any (you’ll learn more about error handling in later exercises)data— the data for the current page, combining the return values of allloadfunctionsform— the data returned from a form action
Each of these properties is reactive, using $state.raw under the hood. Here’s an example using page.url.pathname:
src/routes/+layout
<script>
import { page } 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>
</nav>
{@render children()}<script lang="ts">
import { page } 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>
</nav>
{@render children()}Prior to SvelteKit 2.12, you had to use
$app/storesfor this, which provides a$pagestore with the same information. If you’re currently using$app/stores, we advise you to migrate towards$app/state(requires Svelte 5).
previous next
1
2
3
<h1>home</h1>
<p>this is the home page.</p>