Migrating from Gatsby
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Here are some key concepts and migration strategies to help you get started. Use the rest of our docs and our Discord community to keep going!
Key Similarities between Gatsby and Astro
Section titled Key Similarities between Gatsby and AstroGatsby and Astro share some similarities that will help you migrate your project:
-
The syntax of
.astro
files is similar to JSX. Writing Astro should feel familiar. -
Astro has built-in support for Markdown and an integration for using MDX files. Also, you can configure and continue to use many of your existing Markdown plugins.
-
Astro also has an official integration for using React components. Note that in Astro, React files must have a
.jsx
or.tsx
extension. -
Astro has support for installing NPM packages, including React libraries. Many of your existing dependencies will work in Astro.
-
Like Gatsby, Astro projects can be SSG or SSR with page-level prerendering.
Key Differences between Gatsby and Astro
Section titled Key Differences between Gatsby and AstroWhen you rebuild your Gatsby site in Astro, you will notice some important differences:
-
Gatsby projects are React single-page apps and use
index.js
as your project’s root. Astro projects are multi-page sites, andindex.astro
is your home page. -
Astro components are not written as exported functions that return page templating. Instead, you’ll split your code into a “code fence” for your JavaScript and a body exclusively for the HTML you generate.
-
Local file data: Gatsby uses GraphQL to retrieve data from your project files. Astro uses ESM imports and top-level await functions (e.g.
import.meta.glob()
,getCollection()
) to import data from your project files. You can manually add GraphQL to your Astro project but it is not included by default.
Convert your Gatsby Project
Section titled Convert your Gatsby ProjectEach project migration will look different, but there are some common actions you will perform when converting from Gatsby to Astro.
Create a new Astro project
Section titled Create a new Astro projectUse the create astro
command for your package manager to launch Astro’s CLI wizard or choose a community theme from the Astro Theme Showcase.
You can pass a --template
argument to the create astro
command to start a new Astro project with one of our official starters (e.g. docs
, blog
, portfolio
). Or, you can start a new project from any existing Astro repository on GitHub.
# launch the Astro CLI Wizardnpm create astro@latest
# create a new project with an official examplenpm create astro@latest -- --template <example-name>
# launch the Astro CLI Wizardpnpm create astro@latest
# create a new project with an official examplepnpm create astro@latest --template <example-name>
# launch the Astro CLI Wizardyarn create astro@latest
# create a new project with an official exampleyarn create astro@latest --template <example-name>
Then, copy your existing Gatsby project files over to your new Astro project into a separate folder outside of src
.
Visit https://astro.new for the full list of official starter templates, and links for opening a new project in IDX, StackBlitz, CodeSandbox, or Gitpod.
Install integrations (optional)
Section titled Install integrations (optional)You may find it useful to install some of Astro’s optional integrations to use while converting your Gatsby project to Astro:
-
@astrojs/react: to reuse some existing React UI components in your new Astro site or keep writing with React components.
-
@astrojs/mdx: to bring existing MDX files from your Gatsby project, or to use MDX in your new Astro site.
Put your code in src
Section titled Put your code in srcFollowing Astro’s project structure:
-
Delete Gatsby’s
public/
folder.Gatsby uses the
public/
directory for its build output, so you can safely discard this folder. You will no longer need a built version of your Gatsby site. (Astro usesdist/
by default for the build output.) -
Rename Gatsby’s
static/
folder topublic/
, and use it as Astro’spublic/
folder.Astro uses a folder called
public/
for static assets. You can alternatively copy the contents ofstatic/
into your existing Astropublic/
folder. -
Copy or Move Gatsby’s other files and folders (e.g.
components
,pages
, etc.) as needed into your Astrosrc/
folder as you rebuild your site, following Astro’s project structure.Astro’s
src/pages/
folder is a special folder used for file-based routing to create your site’s pages and posts from.astro
,.md
and.mdx
files. You will not have to configure any routing behavior for your Astro, Markdown, and MDX files.All other folders are optional, and you can organize the contents of your
src/
folder any way you like. Other common folders in Astro projects includesrc/layouts/
,src/components
,src/styles
, andsrc/scripts
.
Tips: Convert JSX files to .astro
files
Section titled Tips: Convert JSX files to .astro filesHere are some tips for converting a Gatsby .js
component into a .astro
component:
-
Use only the
return()
of the existing Gatsby component function as your HTML template. -
Change any Gatsby or JSX syntax to Astro syntax or to HTML web standards. This includes
<Link to="">
,{children}
, andclassName
, for example. -
Move any necessary JavaScript, including import statements, into a “code fence” (
---
). Note: JavaScript to conditionally render content is often written inside the HTML template directly in Astro. -
Use
Astro.props
to access any additional props that were previously passed to your Gatsby function. -
Decide whether any imported components also need to be converted to Astro. With the official React integration installed, you can use existing React components in your Astro files. But, you may want to convert them to
.astro
components, especially if they do not need to be interactive! -
Remove any GraphQL queries. Instead, use import and
import.meta.glob()
statements to query your local files.
See an example from Gatsby’s Blog starter template converted step-by-step
Compare: .jsx
vs .astro
Section titled Compare: .jsx vs .astroCompare the following Gatsby component and a corresponding Astro component:
import * as React from "react"import { useStaticQuery, graphql } from "gatsby"import Header from "./header"import Footer from "./footer"import "./layout.css"
const Component = ({ message, children }) => { const data = useStaticQuery(graphql` query SiteTitleQuery { site { siteMetadata { title } } } `) return ( <> <Header siteTitle={data.site.siteMetadata.title} /> <div style={{ margin: `0`, maxWidth: `960`}}>{message}</div> <main>{children}</main> <Footer siteTitle={data.site.siteMetadata} /> </> )}
export default Component
---import Header from "./Header.astro"import Footer from "./Footer.astro"import "../styles/stylesheet.css"import { site } from "../data/siteMetaData.js"const { message } = Astro.props---<Header siteTitle={site.title} /> <div style="margin: 0; max-width: 960;">{message}</div> <main> <slot /> </main><Footer siteTitle={site.title} />
Migrating Layout Files
Section titled Migrating Layout FilesYou may find it helpful to start by converting your Gatsby layouts and templates into Astro layout components.
Each Astro page explicitly requires <html>
, <head>
, and <body>
tags to be present, so it is common to reuse a layout file across pages. Astro uses a <slot />
instead of React’s {children}
prop for page content, with no import statement required. Your Gatsby layout.js
and templates will not include these.
Note the standard HTML templating, and direct access to <head>
:
<html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" type="image/svg+xml" href="/favicon.svg" /> <meta name="viewport" content="width=device-width" /> <title>Astro</title> </head> <body> <!-- Wrap the slot element with your existing layout templating --> <slot /> </body></html>