Nuxt fundamentals

Intermediate · 41 min

What Nuxt adds to Vue and how Nuxt 4 conventions work, from the app/ directory and file-based routing to layouts, auto-imports, Nitro routes, and route rules.

Before thisSSR and hydration in VueVue Router fundamentals

Syntax · 41 min

Nuxt fundamentals

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by SyntaxReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Nuxt is the production version of the SSR setup from the previous lesson: it runs the client and server builds, creates the app per request, transfers state, and deploys the result, so you write Vue components instead of plumbing.
  • In Nuxt 4 your Vue code lives under `app/` (pages, components, layouts, composables), server code under `server/`, and code shared by both under `shared/`.
  • Files in `app/pages/` become Vue Router routes: `pages/posts/[id].vue` is `/posts/:id`, and `<NuxtPage>` and `<NuxtLink>` take the place of `<RouterView>` and `<RouterLink>`.
  • Components, composables, utilities, and Vue's own APIs are auto-imported, which keeps files short but means a name's origin is set by folder conventions rather than an import line.
  • Anything in `server/api/` is an HTTP endpoint run by Nitro, Nuxt's server engine, and the same project can be built for a Node server, serverless functions, edge runtimes, or static hosting.
  • Pages are server-rendered then hydrated by default, and `routeRules` in `nuxt.config.ts` switch individual routes to prerendered, cached (`swr`/`isr`), or client-only rendering.

The plumbing you would otherwise build

The previous lesson ended with a list of chores that come with server-rendering Vue yourself: a client build and a server build of the same code, a fresh app and router per request, server data serialized into the page so the client doesn’t fetch it again, the right <script> and <link> tags for each page, and a server to deploy. None of it is hard to understand, but all of it is work, and it is the same work in every app.

Nuxt is that work done once, as a framework. You write Vue components and Nuxt handles the rest. It plays the same role for Vue that Next.js plays for React, with one practical difference: Nuxt renders ordinary Vue components on the server and hydrates them, rather than splitting components into server-only and client kinds the way React Server Components do.

A note on versions, as of November 2026. Nuxt 4.0 shipped in July 2025, and the current release line is 4.6.x. Nuxt 5 has not been released yet. The team has said it will move the server layer to Nitro v3 and h3 v2, and Nuxt 4.6 already lets you opt into most Nuxt 5 defaults early with future.compatibilityVersion: 5. Everything below is Nuxt 4. Many tutorials, including the video for this lesson, were recorded on Nuxt 3, where the folders described next sat at the project root instead of under app/.

The shape of a Nuxt 4 project

A new project from npm create nuxt@latest grows into roughly this:

my-app/
├─ app/
│  ├─ app.vue            # root component
│  ├─ pages/             # file-based routes
│  ├─ layouts/           # page shells
│  ├─ components/        # auto-imported components
│  ├─ composables/       # auto-imported composables
│  └─ middleware/        # route middleware
├─ server/
│  ├─ api/               # API endpoints (Nitro)
│  └─ routes/            # non-/api server routes
├─ shared/               # code usable on both sides
├─ public/               # static files served as-is
└─ nuxt.config.ts

Nuxt 4 introduced the app/ directory to draw a clear line. app/ holds the Vue application, server/ holds code that only ever runs on the server, and shared/ holds types and helpers both can import. That separation also lets the TypeScript setup give each side the right globals.

File-based routing

Instead of writing a routes array, you create files. Nuxt turns app/pages/ into Vue Router routes:

app/pages/
├─ index.vue            →  /
├─ about.vue            →  /about
└─ posts/
   ├─ index.vue         →  /posts
   └─ [id].vue          →  /posts/:id

Square brackets mark a dynamic segment, and [...slug].vue catches everything below a path. Under the hood these are normal Vue Router routes, so everything from Vue Router fundamentals still applies:

<!-- app/pages/posts/[id].vue -->
<script setup lang="ts">
const route = useRoute()
const { data: post } = await useFetch(`/api/posts/${route.params.id}`)
</script>

<template>
  <article v-if="post">
    <h1>{{ post.title }}</h1>
    <NuxtLink to="/posts">All posts</NuxtLink>
  </article>
</template>

<NuxtPage> is Nuxt’s <RouterView>, and <NuxtLink> is its <RouterLink>, with extras such as prefetching the code for links that scroll into view. Per-page settings such as which layout to use or which middleware to run go in definePageMeta().

Layouts

Most sites wrap every page in the same header and footer. A layout is a component with a <slot /> where the page goes:

<!-- app/layouts/default.vue -->
<template>
  <div>
    <SiteHeader />
    <main><slot /></main>
    <SiteFooter />
  </div>
</template>
<!-- app/app.vue -->
<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

default.vue applies automatically. A page that needs a different shell, such as an admin dashboard, names it with definePageMeta({ layout: 'admin' }), and Nuxt uses app/layouts/admin.vue instead.

Auto-imports

Notice that the page above never imports useRoute, useFetch, NuxtLink, or SiteHeader. Nuxt scans known folders and makes their exports available everywhere:

  • Components in app/components/. Nested folders become prefixes, so components/base/Button.vue is used as <BaseButton>. Prefix any component with Lazy (<LazyBaseButton>) to load its code only when it renders.
  • Composables and utilities in app/composables/ and app/utils/.
  • Vue’s APIs such as ref, computed, and watch, plus Nuxt’s own composables.

Only what you use is bundled, and Nuxt generates type declarations so editors still autocomplete and type-check. The trade-off is that a file no longer tells you where a name comes from. You trace it through folder conventions and tooling instead of an import line. If you prefer explicitness, everything can still be imported from #imports, and auto-imports can be turned off in config.

Server routes with Nitro

The server/ directory is a backend in the same project. Nuxt’s server engine is Nitro, which sits on the small HTTP framework h3. A file in server/api/ becomes an endpoint, and a method suffix restricts it to one HTTP verb:

// server/api/posts/[id].get.ts  →  GET /api/posts/:id
export default defineEventHandler(async (event) => {
  const id = getRouterParam(event, 'id')
  const post = await db.posts.find(id) // your data layer
  if (!post) {
    throw createError({ statusCode: 404, statusMessage: 'Post not found' })
  }
  return post // serialized to JSON
})

Two things make this more than “Express in a folder”. When a page calls useFetch('/api/posts/1') during server rendering, Nitro handles it in the same process with no network hop, and the response type flows through to the page’s data. Nitro also builds the server for whatever you deploy to, using presets for Node, serverless platforms, edge workers, and more.

Nitro is also why the upcoming Nuxt 5 matters for server code. Its move to Nitro v3 and h3 v2 may change some of these server utilities, and Nuxt 4.6 added opt-in flags to help code written today carry over.

Rendering modes and route rules

By default Nuxt does universal rendering. The first request for any page is rendered on the server and hydrated, and after that the app behaves like a single-page app, rendering later pages in the browser. You can set ssr: false in nuxt.config.ts for a pure client-rendered app, or run nuxt generate to prerender every route to static HTML.

The interesting part is mixing modes per route with route rules:

// nuxt.config.ts
export default defineNuxtConfig({
  routeRules: {
    '/': { prerender: true },              // built once, served as static HTML
    '/products/**': { swr: 3600 },         // cached, regenerated in the background
    '/blog/**': { isr: true },             // cached on the CDN until next deploy
    '/admin/**': { ssr: false },           // client-rendered only
    '/old-page': { redirect: '/new-page' },
  },
})

Nitro applies these at the server level, so one deployment can serve static, cached, server-rendered, and client-only pages side by side. isr behaves like swr but stores the result in the CDN cache, which only works on hosts that support it, such as Netlify and Vercel.

Modules

Much of Nuxt’s ecosystem comes as modules, packages that hook into the build and add features through configuration: image optimization, content from Markdown files, UI libraries, auth, and more. Adding one is usually an install plus a line in the modules array of nuxt.config.ts. Treat them as optional. The conventions above are the core you need first.

What the video covers

CJ Reynolds’ Syntax crash course builds a small task app and touches every convention in this lesson, with chapter marks to jump around. It starts with a tour of the docs and modules, then covers layouts (10:27), pages (11:32), auto-imported components with folder prefixes (12:13), NuxtLink (13:26), how the first load is server-rendered and later navigation is client-side (14:41), Nitro API routes and the h3/Nitro/Nuxt layering (15:11), useFetch and $fetch, and route params. The first ten minutes are editor and lint setup, which you can skip. It was recorded on Nuxt 3, so pages/ and components/ sit at the project root rather than under app/, and route rules get only a mention. The first “go deeper” video covers what changed in Nuxt 4, and the second is a full walkthrough of route rules.

Where this fits

The task app in the video already fetches data with useFetch. Why Nuxt needs its own fetching composables instead of plain fetch, and how they move server data into the client, is the subject of data fetching in Nuxt.

Check yourself

  1. 01In a Nuxt 4 project, what URL does `app/pages/blog/[slug].vue` serve, and how does the component read the slug?

    It serves `/blog/:slug`, so `/blog/hello-world` matches with `slug` equal to `hello-world`. The component reads it with the auto-imported `useRoute()`, as `useRoute().params.slug`, because Nuxt's file-based routing generates ordinary Vue Router routes.

  2. 02What is the role of Nitro in a Nuxt app?

    Nitro is the server engine Nuxt is built on. It runs the server-side rendering of pages, serves the endpoints you write in `server/api/` and `server/routes/`, applies route rules such as caching, and packages the whole server for a deployment target (a Node server, serverless functions, edge workers, or static output) through presets.

  3. 03Your marketing homepage rarely changes, your product pages change hourly, and your admin area is only used by logged-in staff. How could `routeRules` express that?

    Prerender the homepage at build time with `'/': { prerender: true }`, cache product pages and regenerate them in the background with something like `'/products/**': { swr: 3600 }` (or `isr` on hosts that support CDN caching), and turn off server rendering for the admin area with `'/admin/**': { ssr: false }`, since it needs no SEO and benefits little from SSR.

  4. 04What is the trade-off of Nuxt's auto-imports?

    They remove repetitive import lines for components, composables, and Vue APIs, and only what you use ends up in the bundle. The cost is that a file no longer shows where a name comes from, so you rely on folder conventions, editor tooling, and the generated type declarations to trace it, and a name collision between two folders or modules can be confusing.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark