Getting Started with Nuxt 3
Learn how to build modern web applications with Nuxt 3, including SSR, static generation, and the new features that make development faster and more enjoyable.
Getting Started with Nuxt 3
Nuxt 3 represents a major evolution in the Vue.js meta-framework landscape. Built from the ground up with TypeScript, it offers improved performance, better developer experience, and leverages the latest Vue 3 features including the Composition API.
What's New in Nuxt 3?
Vue 3 and Composition API
Nuxt 3 is built on Vue 3, which means you get access to all the latest features:
- Composition API: Write more reusable and maintainable code
- Multiple root elements: No more wrapper divs required
- Better TypeScript support: First-class TypeScript integration
Nitro Engine
The new Nitro engine provides:
- Universal deployment: Deploy anywhere with zero configuration
- API routes: Built-in API handling with file-based routing
- Edge-side rendering: Faster response times globally
Auto-imports
Say goodbye to import statements for:
- Vue composition functions (
ref
,reactive
,computed
) - Nuxt composables (
useHead
,useFetch
,useState
) - Your own utilities and components
Installation and Setup
Getting started with Nuxt 3 is straightforward:
npx nuxi@latest init my-nuxt-app
cd my-nuxt-app
npm install
npm run dev
File-based Routing
Nuxt 3 continues the tradition of file-based routing. Create a file in the pages/
directory, and it automatically becomes a route:
pages/
├── index.vue # → /
├── about.vue # → /about
├── blog/
│ ├── index.vue # → /blog
│ └── [slug].vue # → /blog/:slug
└── [...slug].vue # → catch-all route
Data Fetching
Nuxt 3 introduces new composables for data fetching:
<script setup>
// Server-side rendering with caching
const { data: posts } = await useFetch('/api/posts')
// Client-side only
const { data: stats } = await $fetch('/api/stats')
// With refresh capability
const { data: user, refresh } = await useFetch('/api/user')
</script>
SEO and Meta Tags
Managing SEO is easier than ever with the useSeoMeta
composable:
<script setup>
useSeoMeta({
title: 'My Amazing Page',
description: 'This page is truly amazing.',
ogImage: '/og-image.jpg',
twitterCard: 'summary_large_image'
})
</script>
Server API Routes
Create API endpoints by adding files to the server/api/
directory:
// server/api/hello.js
export default defineEventHandler((event) => {
return {
message: 'Hello from Nuxt 3!'
}
})
Deployment
Nuxt 3's universal deployment means you can deploy to various platforms with zero configuration:
- Static hosting:
npm run generate
for static sites - Node.js:
npm run build
for SSR applications - Edge functions: Deploy to Vercel, Netlify, Cloudflare Workers
Conclusion
Nuxt 3 brings significant improvements in performance, developer experience, and deployment flexibility. Whether you're building a static site, a server-rendered application, or a hybrid solution, Nuxt 3 provides the tools you need.
The migration from Nuxt 2 might require some changes, but the benefits in terms of performance and developer experience make it worthwhile. Start your next project with Nuxt 3 and experience the future of Vue.js development.