Add transition:persist to an element or island and Astro’s client router moves the existing DOM node, with its state, into the next page instead of replacing it. A playing audio player keeps playing, a React cart keeps its items, and a video doesn’t restart. It only works with <ClientRouter /> enabled, and the next page must render a matching element, identified by the same transition:name or transition:persist value.
Key Takeaways
transition:persistkeeps the same DOM element, and any island inside it, alive across navigation.- The next page must render the element too; Astro matches it by name.
- By default a persisted island gets the new page’s props;
transition:persist-propskeeps the old ones.- Persist only what needs to survive; everything else should render fresh.
Basic usage
Enable the router in your base layout:
---
// src/layouts/BaseLayout.astro
import { ClientRouter } from 'astro:transitions';
---
<html lang="en">
<head><ClientRouter /></head>
<body><slot /></body>
</html>
Then persist an island:
---
// src/components/SiteHeader.astro
import CartButton from './CartButton.tsx';
---
<header>
<a href="/">Home</a>
<CartButton client:load transition:persist />
</header>
When a visitor navigates, the router finds the cart button on the new page and keeps the old one, with its React state, instead of mounting a fresh copy.
Persisting plain elements
It works on HTML elements as well, which is the classic case for media:
<audio controls src="/audio/episode-12.mp3" transition:persist="player"></audio>
The value is the transition name. Give persisted elements explicit names when the same component appears in different places on different pages, so Astro matches the right ones.
Props: refresh or keep
When both pages render the island with different props, Astro updates the persisted island with the new page’s props by default. That’s usually right for something like a header that highlights the current section. To keep the original props instead, add transition:persist-props:
<Player client:load transition:persist transition:persist-props track={track} />
Now the player keeps the props it was first given, which matters when a prop like track would otherwise reset playback on every page.
Where it helps
- Media players that must not stop on navigation.
- Cart or wishlist islands in the header, so counts don’t flash to zero.
- Chat widgets and support panels the visitor has open.
- Expensive islands, such as a map, that you don’t want to re-initialise.
Pitfalls
- The element must exist on both pages. If the next page doesn’t render it, it’s removed like anything else. Put persisted islands in a shared layout.
- Persisted content doesn’t update. A persisted header with a “current page” link won’t change its markup on its own. Persist the island that needs state, not the whole header.
- Scripts in persisted elements don’t re-run. That’s usually what you want. For non-persisted parts, use astro:page-load.
- Full reloads still reset everything. Opening a link in a new tab or reloading the page starts fresh. Use Nano Stores with persistent storage for state that must survive a reload.
- Forms with
data-astro-reloador links to other sites trigger normal navigation, so nothing persists.
persist versus a shared store
| Need | Use |
|---|---|
| The same DOM element keeps running (audio, video, map) | transition:persist |
| Several islands show the same data | A shared store |
| Data survives a full reload | A store with localStorage |
They combine well: a persisted cart island that reads from a store keeps its DOM across navigation and its data across reloads.
The view transitions guide covers transition:name and animations, and Astro islands architecture explains how islands hydrate. The config docs show where the router is enabled in a Komal theme, and our Astro themes keep their persisted elements in the base layout for this reason.
FAQ
What does transition:persist do in Astro?
It tells the client router to keep an element, and any island inside it, from the old page instead of replacing it with the new page’s copy. State, playback and focus inside it survive navigation.
Why isn’t my island persisting between pages?
Check that <ClientRouter /> is in the layout, that the island is rendered on both pages, and that both use the same transition:persist or transition:name value.
Does transition:persist keep island props?
Not by default; the persisted island receives the new page’s props. Add transition:persist-props to keep the props it was first rendered with.