Astro 3.2: View Transitions improvements

By
Matthew Phillips
Martin Trapp

Astro 3.2 is out and brings several new improvements for view transitions:

To take advantage of the latest view transitions features, make sure you’re running the latest version of Astro. You can upgrade to Astro 3.2 by running the upgrade command for your package manager of choice:

npm install astro@latest
pnpm upgrade astro --latest
yarn upgrade astro --latest

You can now configure individual links to prevent a new history entry from being added to the browser stack on navigation. Add the data-astro-history="replace" attribute to any <a> tag and Astro will instead use the underlying history.replaceState API. Use this for fewer back button clicks, or to avoid sending readers back to pages like form submission confirmations.

<a href="/toggle" data-astro-history="replace">Toggle me</a>

JavaScript Navigation API

You can now trigger navigations from your client-side JavaScript via the new navigate() API. Previously transitions only occurred when the user clicked anchor links. With the new navigation API you have complete control over when navigation occurs.

import { navigate } from 'astro:transitions/client';

// Navigate to the selected option automatically.
document.querySelector('select').onchange = (ev) => {
  let href = ev.target.value;
  navigate(href);
};

Additionally, you have control over the history stack with this method via the history option, which works just like the data-astro-history attribute:

import { navigate } from 'astro:transitions/client';

navigate(href, {
  history: 'replace'
});

Route Announcer

The View Transitions router now does route announcements. When transitioning between pages with a traditional MPA approach, assistive technologies will announce the page title when the page finishes loading. This does not automatically happen during client-side routing, so visitors relying on these technologies to announce routes are not aware when a page has changed.

The view transitions route announcer runs after the astro:page-load event, looking for the page <title> to announce. If one cannot be found, the announcer falls back to the first <h1> it finds or otherwise announces the pathname. We recommend you always include a <title> on each page for accessibility.

See the View Transitions docs for more on how accessibility is handled.

More

Additional bug fixes and integration features are included in this release. Check out the release notes to learn more.