Migrating from Next.js
هذا المحتوى غير متوفر بلغتك بعد.
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 Next.js and Astro
Section titled Key Similarities between Next.js and AstroNext.js 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 projects can also be SSG or SSR with page-level prerendering.
- Astro uses file-based routing, and allows specially named pages to create dynamic routes.
- Astro is component-based, and your markup structure will be similar before and after your migration.
- Astro has official integrations for React, Preact, and Solid so you can use your existing JSX components. Note that in Astro, these 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.
Key Differences between Next.js and Astro
Section titled Key Differences between Next.js and AstroWhen you rebuild your Next.js site in Astro, you will notice some important differences:
-
Next.js is a React single-page app, and uses
index.js
as your project’s root. Astro is a multi-page site, 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. -
content-driven: Astro was designed to showcase your content and to allow you to opt-in to interactivity only as needed. An existing Next.js app might be built for high client-side interactivity and may require advanced Astro techniques to include items that are more challenging to replicate using
.astro
components, such as dashboards.
Convert your Next.js Project
Section titled Convert your Next.js ProjectEach project migration will look different, but there are some common actions you will perform when converting from Next.js 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 Next project files over to your new Astro project in 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 Next 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 Next project, or to use MDX in your new Astro site.
Put your source code in src
Section titled Put your source code in srcFollowing Astro’s project structure:
-
Keep Next’s
public/
folder untouched.Astro uses the
public/
directory for static assets, just like Next. There is no change needed to this folder, nor its contents. -
Copy or Move Next’s other files and folders (e.g.
pages
,styles
etc.) into Astro’ssrc/
folder as you rebuild your site, following Astro’s project structure.Like Next, Astro’s
src/pages/
folder is a special folder used for file-based routing. All other folders are optional, and you can organize the contents of yoursrc/
folder any way you like. Other common folders in Astro projects includesrc/layouts/
,src/components
,src/styles
,src/scripts
.
The Astro config file
Section titled The Astro config fileAstro has a configuration file at the root of your project called astro.config.mjs
. This is used only for configuring your Astro project and any installed integrations, including SSR adapters.
Tips: Convert JSX files to .astro
files
Section titled Tips: Convert JSX files to .astro filesHere are some tips for converting a Next .js
component into a .astro
component:
-
Use the returned JSX of the existing Next.js component function as the basis for your HTML template.
-
Change any Next or JSX syntax to Astro or to HTML web standards. This includes
<Link>
,<Script>
,{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 Next function. -
Decide whether any imported components also need to be converted to Astro. With the official integration installed, you can use existing React components in your Astro file. But, you may want to convert them to
.astro
components, especially if they do not need to be interactive! -
Replace
getStaticProps()
with import statements orimport.meta.glob()
to query your local files. Usefetch()
to fetch external data.
See an example of a Next .js
file converted step-by-step.
Compare: JSX vs Astro
Section titled Compare: JSX vs AstroCompare the following Next component and a corresponding Astro component:
import Header from "./header";import Footer from "./footer";import "./layout.css";
export async function getStaticProps() { const res = await fetch("https://api.github.com/repos/withastro/astro"); const json = await res.json(); return { props: { message: json.message, stars: json.stargazers_count || 0 }, }}
const Component = ({ stars, message }) => {
return ( <> <Header /> <p style={{ backgroundColor: `#f4f4f4`, padding: `1em 1.5em`, textAlign: `center`, marginBottom: `1em` }}>Astro has {stars} 🧑🚀</p> <Footer /> </> )}
export default Component;
---import Header from "./header";import Footer from "./footer";import "./layout.css";
const res = await fetch("https://api.github.com/repos/withastro/astro");const json = await res.json();const message = json.message;const stars = json.stargazers_count || 0;---<Header /><p class="banner">Astro has {stars} 🧑🚀</p><Footer />
<style> .banner { background-color: #f4f4f4; padding: 1em 1.5em; text-align: center; margin-bottom: 1em; }<style>
Migrating Layout Files
Section titled Migrating Layout FilesYou may find it helpful to start by converting your Next.js layouts and templates into Astro layout components.
Next has two different methods for creating layout files, each of which handles layouts differently than Astro:
-
The
pages
directory
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 />
for page content, with no import statement required. 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" /> <meta name="generator" content={Astro.generator} /> <title>Astro</title> </head> <body> <!-- Wrap the slot element with your existing layout templating --> <slot /> </body></html>