Migrating from NuxtJS
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!
This guide is referring to Nuxt 2, not the newer Nuxt 3. While some of the concepts are similar, Nuxt 3 is a newer version of the framework and may require different strategies for parts of your migration.
Key Similarities between Nuxt and Astro
Section titled Key Similarities between Nuxt and AstroNuxt and Astro share some similarities that will help you migrate your project:
- 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 an official integration for using Vue components.
- Astro has support for installing NPM packages, including Vue libraries. You may be able to keep some or all of your existing Vue components and dependencies.
Key Differences between Nuxt and Astro
Section titled Key Differences between Nuxt and AstroWhen you rebuild your Nuxt site in Astro, you will notice some important differences:
-
Nuxt is a Vue-based SPA (single-page application). Astro sites are multi-page apps built using
.astro
components, but can also support React, Preact, Vue.js, Svelte, SolidJS, AlpineJS, and raw HTML templating. -
Page Routing: Nuxt uses
vue-router
for SPA routing, andvue-meta
for managing<head>
. In Astro, you will create separate HTML page routes and control your page<head>
directly, or in a layout component. -
content-driven: Astro was designed to showcase your content and to allow you to opt-in to interactivity only as needed. An existing Nuxt app may be built for high client-side interactivity. Astro has built-in capabilities for working with your content, such as page generation, but may require advanced Astro techniques to include items that are more challenging to replicate using
.astro
components, such as dashboards.
Convert your NuxtJS Project
Section titled Convert your NuxtJS ProjectEach project migration will look different, but there are some common actions you will perform when converting from Nuxt 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 Nuxt 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 Nuxt project to Astro:
-
@astrojs/vue: to reuse some existing Vue UI components in your new Astro site, or keep writing with Vue components.
-
@astrojs/mdx: to bring existing MDX files from your Nuxt project, or to use MDX in your new Astro site.
Put your source code in src
Section titled Put your source code in src-
Move the contents of Nuxt’s
static/
folder intopublic/
.Astro uses the
public/
directory for static assets, similar to Nuxt’sstatic/
folder. -
Copy or Move Nuxt’s other files and folders (e.g.
pages
,layouts
etc.) into Astro’ssrc/
folder.Like Nuxt, 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
.
Convert Vue SFC pages to .astro
files
Section titled Convert Vue SFC pages to .astro filesHere are some tips for converting a Nuxt .vue
component into a .astro
component:
-
Use the
<template>
of the existing NuxtJS component function as the basis for your HTML template. -
Change any Nuxt or Vue syntax to Astro or to HTML web standards. This includes
<NuxtLink>
,:class
,{{variable}}
, andv-if
, for example. -
Move
<script>
JavaScript, into a “code fence” (---
). Convert your component’s data-fetching properties to server-side JavaScript - see Nuxt data fetching to Astro. -
Use
Astro.props
to access any additional props that were previously passed to your Vue component. -
Decide whether any imported components also need to be converted to Astro. With the official integration installed, you can use existing Vue components in your Astro file. But, you may want to convert them to Astro, especially if they do not need to be interactive!
See an example from a Nuxt app converted step-by-step.
Compare: Vue vs Astro
Section titled Compare: Vue vs AstroCompare the following Nuxt component and a corresponding Astro component:
<template> <div> <p v-if="message === 'Not found'"> The repository you're looking up doesn't exist </p> <div v-else> <Header/> <p class="banner">Astro has {{stars}} 🧑🚀</p> <Footer /> </div> </div></template>
<script>import Vue from 'vue'
export default Vue.extend({ name: 'IndexPage', async asyncData() { const res = await fetch('https://api.github.com/repos/withastro/astro') const json = await res.json(); return { message: json.message, stars: json.stargazers_count || 0, }; }});</script>
<style scoped>.banner { background-color: #f4f4f4; padding: 1em 1.5em; text-align: center; margin-bottom: 1em;}<style>
---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;---
{message === "Not Found" ? <p>The repository you're looking up doesn't exist</p> : <> <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 Nuxt layouts and templates into Astro layout components.
Each Astro page explicitly requires <html>
, <head>
, and <body>
tags to be present. Your Nuxt layout.vue
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>