Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 32 ยท Quality

Performance and Core Web Vitals

Make a Nuxt app fast: lazy components and hydration, smaller payloads, image and font optimization, route rule caching and bundle analysis, with runnable models.

A fast site keeps visitors, ranks better and costs less to run. Search engines measure it with the Core Web Vitals: how quickly the main content appears (Largest Contentful Paint, LCP), how quickly the page reacts when someone taps or types (Interaction to Next Paint, INP), and how much the layout jumps around while loading (Cumulative Layout Shift, CLS). Nuxt gives you a good start, since pages arrive as ready-to-read HTML, but a slow app is still easy to build: giant JavaScript bundles, huge images, data you do not show, and pages rendered again for every visitor.

This lesson walks through the levers that matter most in Nuxt: loading components lazily and hydrating them late, shrinking the payload, serving the right images and fonts, caching pages with route rules, and measuring the bundle. Nuxt itself cannot run on this site, so its configuration and components are read-only. The runnable examples are JavaScript models that measure the effect of each idea with real numbers: payload bytes, and a page cache with a fake clock.

Where the time goes

HTML The server renders it. Fast when cached.
JavaScript Downloaded and run to make the page interactive.
Images and fonts Often the biggest files on the page.
Data API calls, and the payload sent along with the HTML.

Send less, send it later, send it from a cache.

1 / 2

Measure first

Guessing wastes time. Measure with Lighthouse in the browser's developer tools (or PageSpeed Insights), and look at real-user data when you have traffic. Test the production build, never nuxt dev, because the dev server ships unminified code and extra tooling. Then fix the biggest item on the report. Most problems fall into the categories in the rest of this lesson.

Lazy components and delayed hydration

A Nuxt page is rendered to HTML on the server, then the browser downloads JavaScript and hydrates it, attaching event handlers so the page becomes interactive (see Rendering modes). Every component that is part of the first load adds to the JavaScript the browser must download and run.

Putting the prefix Lazy in front of a component name loads it in a separate chunk, only when it is rendered:

VUE
<script setup lang="ts">
// read-only
const showChart = ref(false);
</script>

<template>
  <button @click="showChart = true">Show statistics</button>
  <LazyStatsChart v-if="showChart" />
</template>

Until the button is clicked, the chart code is not even downloaded. This pays off for big, conditional pieces such as modals, charts, editors and maps. (How the auto-import prefix works is covered in Auto-imports.)

Newer Nuxt versions go one step further with lazy hydration: the component is still rendered on the server as HTML, so visitors and search engines see it at once, but its JavaScript is only loaded and attached when a condition is met:

VUE
<template>
  <!-- read-only: hydrate when it scrolls into view -->
  <LazyReviewList hydrate-on-visible />

  <!-- hydrate when the browser is idle -->
  <LazyNewsletterForm hydrate-on-idle />

  <!-- hydrate on first interaction with the component -->
  <LazyShareButtons hydrate-on-interaction />
</template>

The exact set of hydration strategies depends on your Nuxt version, so check the documentation of the version you use. The idea is stable: above-the-fold content hydrates normally, and everything below the fold or rarely used waits. Do not delay hydration of anything the visitor will interact with right away, or INP gets worse.

Keep the payload small

When a page fetches data on the server (see Data fetching with useFetch and useAsyncData), Nuxt embeds the result in the HTML as a JSON payload so the browser does not fetch it again. That payload is part of every page download, so everything you fetch but do not display is waste. APIs often return far more than a page needs. Trim it with pick or transform:

VUE
<script setup lang="ts">
// read-only
const { data: products } = await useFetch("/api/products", {
  // keep only the fields this page shows
  pick: ["id", "name", "priceCents"],
});
</script>

For deeper reshaping (for example, only the first 12 items, or a computed label), use transform to return the smaller structure. Better still, make the server route return only what pages need, since that also shrinks the data sent over the network.

Measuring a payload

The code below builds a catalog of 40 products with realistic extra fields (long descriptions, specs and a list of related ids) and measures the JSON size of three strategies: everything, only the fields a listing card needs, and a listing capped to the first 12 items. Nuxt's real payload format is more compact than plain JSON in some cases, so the numbers are a model of the effect, not of Nuxt's exact bytes.

JS How much does the payload shrink?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Run it and you will see the full payload at about 66 times the size of the smallest variant (roughly 42 KB against 0.6 KB). The point is not the exact numbers but the proportion: most of the weight was never shown to anyone.

Compression (gzip or Brotli, usually done by your host or CDN) shrinks text well, but it cannot remove data that should not have been sent. Trim first, compress second.

Images and fonts

Images are usually the heaviest files and often the LCP element. The @nuxt/image module provides <NuxtImg> and <NuxtPicture>, which resize images, serve modern formats such as WebP or AVIF when supported, and generate responsive srcset values:

VUE
<template>
  <!-- read-only -->
  <NuxtImg
    src="/hero.jpg"
    width="1200"
    height="600"
    sizes="100vw sm:50vw"
    format="webp"
    loading="eager"
    fetchpriority="high"
    alt="A desk lamp on a wooden table"
  />
</template>

Three rules cover most cases:

  • Always give width and height (or an aspect ratio) so the browser reserves the space, which prevents layout shift (CLS).
  • Load the main image eagerly with high priority; everything else lazily (loading="lazy" is the default for images below the fold in most setups).
  • Send the size you display, not the size of the camera original.

For fonts, the @nuxt/fonts module can find the fonts you use in your CSS, self-host them and add matching fallback metrics so text does not jump when the real font loads. Use as few font files as you can (each weight and style is a download), and avoid blocking the first render on a font from another domain.

Cache pages with route rules

Rendering a page costs server time. If a page is the same for everyone, render it once and reuse the result. In Nuxt you decide per route with routeRules in nuxt.config.ts:

TypeScript
// nuxt.config.ts (read-only)
export default defineNuxtConfig({
  routeRules: {
    "/": { prerender: true },                 // built once at build time
    "/blog/**": { swr: 3600 },                 // cache 1 hour, refresh in the background
    "/products/**": { isr: 600 },              // like swr, using the platform's CDN cache where supported
    "/api/prices": { cache: { maxAge: 60 } }, // cache this API response for a minute
    "/account/**": { ssr: false },             // client-rendered, never cached as HTML
    "/admin/**": { headers: { "cache-control": "no-store" } },
  },
});

The names mean different things: prerender builds a static file during the build; swr ("stale-while-revalidate") serves the cached copy and, when it is older than the limit, refreshes it in the background so no visitor waits; isr is a similar idea that relies on the hosting platform's cache; cache stores handler results on the server. Rendering modes explains each rule in detail. Never cache pages that show per-user data to everyone: choose rules by what is public.

A model of stale-while-revalidate

This model has a page cache with a fake clock, so you can see exactly when a visitor pays for a render. A render costs 200 ms in the model; a cache hit costs 1 ms. The cache follows the swr idea: a fresh entry is served directly, a stale entry is served immediately and a refresh is started, and only a missing entry makes the visitor wait. It is a simplification (a real server also deals with concurrent refreshes, storage and headers).

JS Stale-while-revalidate with a fake clock

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Six visits cause only three renders. Only the first visitor waits 200 ms. The visit at t=3700 gets the old HTML instantly (version 1) while the refresh produces version 2, which the next visitor receives. The last visit, a long time later, again sees stale content once before it is refreshed: that is the trade-off of swr, since a visitor after a long quiet period sees old content one time.

Find the heavy parts of the bundle

If JavaScript is the bottleneck, look at what is inside it. Nuxt includes an analysis command (npx nuxt analyze) that builds the project and opens a treemap of every chunk and the modules inside. Look for:

  • A big library used on one page that ended up in the main bundle (import it lazily, or inside a Lazy component).
  • Two libraries doing the same job, such as two date libraries.
  • A whole icon set or utility library imported when you need five functions. Import named functions, or use per-icon imports.
  • Code that only runs in the browser but is loaded for everyone.

Other habits that help: prefer a small, targeted package over a huge one, check what each new dependency adds before you install it, avoid loading third-party scripts (chat widgets, trackers) before the page is usable, and remember that <NuxtLink> prefetches the code of visible links, which makes navigation fast (see File-based routing).

A short checklist

  • Test the production build with Lighthouse; fix the biggest finding first.
  • Lazy-load heavy conditional components; delay hydration for below-the-fold sections.
  • Use pick or a leaner API so the payload holds only what is shown.
  • Give every image dimensions; mark the hero image as high priority; serve modern formats.
  • Self-host a few font files; avoid layout jumps.
  • Use routeRules (prerender, swr) for public pages; keep personal pages uncached.
  • Run nuxt analyze after adding a dependency.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does putting Lazy in front of a component name (for example <LazyStatsChart />) do?

    The Lazy prefix creates a dynamic import, so the component's JavaScript is not part of the initial bundle and is downloaded when the component is first rendered.
  2. 2. Why does data you fetch but do not display still slow the page down?

    Server-fetched data is serialized into the page so the browser can reuse it. Unused fields add to every download, so use pick, transform or a leaner API.
  3. 3. Which image habit helps Cumulative Layout Shift the most?

    When the browser knows the image dimensions it reserves the space in advance, so the content below does not jump when the image arrives.
  4. 4. A route has swr: 3600. A visitor arrives two hours after the page was last rendered. What happens?

    Stale-while-revalidate serves the old copy right away and refreshes it for later visitors, so nobody waits except when no copy exists.
  5. 5. When should you NOT use a public page cache rule?

    A shared cache gives every visitor the same HTML. Pages with per-user content must not be cached that way, or one person could see another's data.

Summary

  • Measure the production build with Lighthouse and tackle the biggest finding first; the Core Web Vitals are LCP, INP and CLS.
  • Lazy components split code out of the first load; lazy hydration strategies delay interactivity for sections that do not need it immediately.
  • Embedded data counts toward page weight: trim it with pick, transform or a leaner API.
  • Use <NuxtImg> with dimensions, the right size and priority for the hero image, and a few self-hosted fonts.
  • routeRules choose per route between prerendering, swr, isr, caching and client-only rendering; never cache per-user pages for everyone.
  • nuxt analyze shows what makes the bundle heavy.

Next: Security and common mistakes