Upgrading from Astro v2 to v4
Update March 19, 2024: I missed some issues with static file endpoints that I needed to fix after the upgrade.
In a recent episode of the ShopTalk podcast,
Chris Coyier and Dave Rupert interviewed Fred K. Schott about
new features in Astro. That reminded me
that I need to upgrade this site from 2.6.1 to 4.5.6.
Here is a log of the process:
-
pnpm install astro@latest -
Peer dependency issues
pnpm install typescript@latestpnpm install @astrojs/mdx@latestpnpm install @astrojs/svelte@latestpnpm install svelte@latest
-
Tried
pnpm run build -
Build error on articles:
localStorage is not defined- Found a reference to
localStoragein a Svelte component in the last page generated by Astro - Remove all direct references to
localStorage - That fixed it
- Read
client:onlydocumentation - Realized that is what I wanted
- Reverted changes to Svelte component
- Added
client:onlyto MDX files that referenced the component - That fixed it
- Found a reference to
-
Tried
pnpm run buildagain -
It worked
-
Netlify build failed:
9:32:21 PM: Node.js v18.12.1 is not supported by Astro! 9:32:21 PM: Please upgrade Node.js to a supported version: ">=18.14.1"- Updated
NODE_VERSIONenvironment variable to LTS20.11.1 - That fixed it
- Updated
-
Done
I get a little nervous reading about the complexity of Astro--especially because I'm using it to build this simple site. But in my experience, all of the upgrades have gone smoothly. (Edit: they even have an upgrade tool)
The Astro team deserves a lot of credit for engineering a tool that is feature-rich while managing the complexity for the developer. I hope they can continue that as they grow.
Sean McPherson works as a software engineer at Khan Academy, and enjoys playing games and watching soccer. He lives with family in Pittsburgh, PA.