Upgrading from Astro 2 to Astro 4
Astro is building fast. Right on the heels of their version 3 launch on August 30th, Astro version 4 launched on December 6th, 2023. They've built so fast, that I didn't even have a chance to try 3 before 4 came out! But the short time between versions makes sense, because the two releases are very complementary. Many Astro features introduced in version 3 as experimental are made stable by version 4. If, like me, you're looking at a two-version upgrade, here's what you need to know about Astro 3 and 4 combined.
View Transitions
Astro makes it easy to include animated transitions between routes and components with the <ViewTransitions />
component. You can add it to the <head>
of specific pages, or to your site-wide <head>
to be enabled across the entire site. No configuration is required, but you'll probably still want to configure it.
Adding <ViewTransitions />
between pages effectively turns your site into a single-page application, animating in and out content on route change rather than downloading a new statically generated HTML document. You can further customize how specific components animate in and out with the transition:animate
property. If you don't want client side routing for a specific link, you can opt out for that link with the data-astro-reload
property.
Image Optimization
The way Astro works with images has changed a lot from version 2. If you were using @astrojs/image
, then updating how you handle images is probably going to be the most time-consuming part of your Astro migration.
Astro's <Image />
and <Picture />
components have had API changes, which will require you to make changes in your usage of them. You should definitely check out the full image migration guide in the Astro docs for all of the details. But some of the details you should be aware of:
@astrojs/image
is out,astro:assets
is in- You get image optimization when you import images inside
/src
. This can change your entire imagesrc
referencing strategy. Optimization only works for images imported from inside/src
, so you might want to relocate images you've been keeping inside the/public
directory. - Importing an image file no longer returns the path as a
string
, and anImageMetadata
object withsrc
,width
,height
, andformat
properties. If you need the previous behavior, add?url
to the import path. - Markdown documents can reference image paths inside
/src
for automatic image optimization, no need to use<Image />
in MDX nor reference the root relative paths of images in the/public
directory. - The
<Image />
and<Picture />
components have changes to properties. For example,aspectRatio
is no longer a valid property because it is inferred from thewidth
andheight
of images. A newpictureAttributes
property lets you do things like add a CSSstyle
string. - You can use a helper
image
schema validator in your content collection schemas.
Dev Toolbar
The Dev Toolbar is a new local development feature to help developers work with their interactive islands and to integrate with other tools. The Inspect option highlights what parts of the page are interactive, and lets you examine them. You can view their props and even open them directly in your editor. Audit checks for accessibility issues, such as missing alt
attributes in images. And the Menu lets you use specific integrations. Currently only Storyblok and spotlight are available, but you can expect more integrations in the future. And if you don't want to wait, you can also extend the Dev Toolbar yourself with their API.
If you don't like the Dev Toolbar, you can turn it off in the CLI with astro preferences disable devToolbar
.
Conclusion
Astro has added a lot of cool features in 2 major back-to-back releases, and you should absolutely consider upgrading if you're still on version 2. Be prepared to modify how you've handled images, but also get excited to play with view transitions!