Compatibility
The official guide describes rebuilding the site in a new Astro project rather than converting it in place. Much carries over. .astro syntax is close to JSX, Markdown is built in and MDX comes through an integration, many existing Markdown plugins and npm dependencies keep working, and React components can be reused through the official React integration. Like Gatsby, Astro can produce a static site or render on the server, with prerendering per page.
Before you switch
- Create a project with
npm create astro@latest, and copy the Gatsby files into a folder outsidesrc. Add@astrojs/reactto reuse React components and@astrojs/mdxfor MDX files. - Delete Gatsby’s
public/folder (its build output), and renamestatic/topublic/. Astro builds todist/. - Move components, pages and the rest into
src/. Pages go insrc/pages/, where routing follows the file path. - Convert layouts first. Each Astro page needs its own
<html>,<head>and<body>, so a shared layout carries them, with<slot />in place of{children}. Global CSS is imported in that layout instead ofgatsby-browser.js. - Convert each
.jspage to an.astropage: keep only thereturn()as the template, move imports and logic into the---code fence, and read props fromAstro.props. JSX page files cannot be used as pages. - Replace GraphQL queries with
import.meta.glob(), or withgetCollection()andgetEntry()if you use content collections. - Repurpose the
gatsby-*.jsfiles:siteMetadatafromgatsby-config.jsgoes into a data file such assrc/data/siteMetadata.js, and an SSR setup fromgatsby-ssr.jsbecomes an adapter inastro.config.mjs.
Pitfalls
- GraphQL is not included. You can add it by hand, but the guide’s route is to remove every query.
<Link to="">becomes a plain<a href="">,classNamebecomesclass, and inline style objects becomestyleattribute strings.- CSS-in-JS libraries such as styled-components may need replacing.
<StaticImage />and<GatsbyImage />become Astro’s<Image />, which works in.astroand.mdxfiles only and has attributes that differ from Gatsby’s. Local images in.mdfiles must use Markdown syntax, not<img>, and<img />in React components is not optimized..astrofiles and several other file types must be imported with their full extension.- Markdown and MDX content outside
src/has to move in, unless you use content collections. Existing files may need frontmatter changes, such as thelayoutproperty. - End-to-end tests may pass unchanged only if the new markup matches the old site’s.