DEV Community

A0mineTV
A0mineTV

Posted on

Optimizing Images in Nuxt 3: A Complete Guide to Better Performance

Images are often the largest assets on modern websites, making optimization crucial for performance. Nuxt 3 provides powerful built-in tools for image optimization through the @nuxt/image module. In this guide, we'll explore how to leverage these features for responsive, efficient images.

Setting Up Image Optimization

First, install the Nuxt Image module:

npm install @nuxt/image
Enter fullscreen mode Exit fullscreen mode

Add it to your nuxt.config.ts:

export default defineNuxtConfig({
  modules: [
    '@nuxt/image'
  ]
})
Enter fullscreen mode Exit fullscreen mode

NuxtPicture vs NuxtImg: When to Use What

Nuxt provides two main components for image optimization:

NuxtPicture - For Maximum Optimization

NuxtPicture generates the <picture> element with multiple formats and sizes:

<template>
  <NuxtPicture
    src="your-image.jpg"
    sizes="sm:100vw md:900px lg:1200px 2xl:1600px"
    :img-attrs="{ alt: 'Hero mountains at sunrise', class: 'hero' }"
    densities="1,2"
    :widths="[320,640,960,1200,1600]"
    format="avif,webp,jpeg"
    placeholder="blur"
  />
</template>
Enter fullscreen mode Exit fullscreen mode

This generates:

  • Multiple formats: AVIF (best compression), WebP (good support), JPEG (fallback)
  • Responsive images: Different sizes for different screen widths
  • Retina support: 1x and 2x pixel densitiesw
  • Blur placeholder: Shows while loading

NuxtImg - For Simpler Use Cases

NuxtImg is perfect when you need basic optimization:

<template>
  <NuxtImg
    src="your-image.jpg"
    sizes="sm:100vw md:800px lg:1200px"
    :modifiers="{ fit: 'cover', width: 1200, height: 800, quality: 80 }"
    alt="Sample image"
    placeholder="blur"
    class="responsive-img"
  />
</template>
Enter fullscreen mode Exit fullscreen mode

Key Features Explained

1. Format Optimization

format="avif,webp,jpeg"
Enter fullscreen mode Exit fullscreen mode
  • AVIF: Next-gen format with 50% better compression than JPEG
  • WebP: 25-35% smaller than JPEG with wide browser support
  • JPEG: Universal fallback

2. Responsive Breakpoints

sizes="sm:100vw md:900px lg:1200px 2xl:1600px"
:widths="[320,640,960,1200,1600]"
Enter fullscreen mode Exit fullscreen mode

This creates optimized images for each screen size, reducing bandwidth on mobile devices.

3. Pixel Density Support

densities="1,2"
Enter fullscreen mode Exit fullscreen mode

Serves high-DPI images for retina displays while keeping standard resolution for regular screens.

4. Smart Placeholders

placeholder="blur"
Enter fullscreen mode Exit fullscreen mode

Shows a blurred version while the actual image loads, improving perceived performance.

Performance Benefits

Using Nuxt's image optimization provides:

  • Faster Load Times: Smaller file sizes mean quicker downloads
  • Better Core Web Vitals: Optimized images improve LCP (Largest Contentful Paint)
  • Reduced Bandwidth: Especially important for mobile users
  • Automatic Format Selection: Browsers automatically choose the best supported format

Best Practices

  1. Always specify alt text for accessibility
  2. Use appropriate sizes - don't serve desktop images to mobile
  3. Choose the right component:
    • NuxtPicture for hero images and important visuals
    • NuxtImg for content images and simpler use cases
  4. Set explicit dimensions when possible to prevent layout shift
  5. Use placeholders for better user experience

Styling Your Images

.hero {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 12px;
}

.responsive-img {
  max-width: 100%;
  height: auto;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Nuxt 3's image optimization features make it easy to serve performant, responsive images without complex setup. By using NuxtPicture for critical images and NuxtImg for content, you can significantly improve your site's performance and user experience.

The automatic format selection, responsive breakpoints, and smart loading features help create fast, modern web applications that work well across all devices and connection speeds.

Top comments (0)