Skip to Content
Nuxt
快速开始

图片优化

了解如何使用 Nuxt Image 在你的 Nuxt 应用中优化图片。

Nuxt 有一个核心模块(@nuxt/image),用于在你的应用中调整尺寸、转换并投递经过优化的图片:

  • 无缝替换: 在你原本书写 <img> 和 <picture> 的地方改用 <NuxtImg> 和 <NuxtPicture>。
  • 内置尺寸调整器: 使用默认的 provider IPX 优化本地和远程图片,无需任何配置。
  • 支持 40+ 个 provider: 改用你的 CDN 或 CMS 提供的图片服务,或者编写你自己的 provider,没有厂商锁定(vendor lock-in)。
  • 适配响应式: 为每种屏幕尺寸和设备像素密度投递调整过尺寸的图片。
  • 现代格式: 面向支持它们的浏览器把图片转换为 WebP 或 AVIF。
  • 更快的页面加载: 借助原生懒加载,只在图片进入视口时才加载它们,并在加载过程中显示占位图。
  • 视觉稳定性: 使用 width 和 height 为图片预留空间,从而避免布局偏移(layout shift)。

安装(Installation)

将 Nuxt Image 模块添加到你的项目:

npx nuxt add image

这条命令会安装 @nuxt/image 并将其添加到你的 nuxt.config 的 modules 中。

如果你在开发环境中使用了 <NuxtImg> 或 <NuxtPicture> 却没有安装该模块,Nuxt 会检测到它并主动询问是否为你安装 @nuxt/image。

用法(Usage)

用 <NuxtImg> 代替 <img>,用 <NuxtPicture> 代替 <picture>。你不需要导入它们:

<template>
  <NuxtImg
    src="/nuxt.png"
    alt="Nuxt logo"
    width="300"
    height="80"
  />
</template>
<template>
  <NuxtPicture
    src="/nuxt.png"
    alt="Nuxt logo"
    width="300"
    height="80"
    format="avif,webp"
  />
</template>

src 属性可以是本地图片或远程图片。

本地图片(Local Images)

将图片存放在 public/ 目录中,并从根 URL(/)引用它们。例如,public/profile.png 可以通过 /profile.png 访问:

<template>
  <NuxtImg
    src="/profile.png"
    alt="Picture of the author"
    width="500"
    height="500"
  />
</template>

Nuxt Image 会把图片调整为给定的 width 和 height。浏览器也会使用这些值在图片加载完成之前为其预留空间,从而避免布局偏移。Nuxt Image 不会读取你文件的固有尺寸,因此对于那些不由 CSS 控制尺寸的图片,请始终设置 width 和 height。

默认情况下,Nuxt Image 使用 IPX,一个运行在你的 Nuxt 服务器上的自托管优化器。当你使用 nuxt generate 预渲染站点时,Nuxt Image 改为在构建时优化图片。

预渲染只会生成服务端渲染出的 HTML 中出现的图片尺寸。只在浏览器中渲染的图片(例如弹窗或仅客户端组件中的图片)不会生成对应文件,因此它的 URL 会返回 404 错误。请将这类图片列在 nitro.prerender.routes 中,或者使用image provider。更多细节请参阅静态图片。

IPX 无法在 worker 运行时中运行。当你部署到这样的运行时(例如 Cloudflare Workers)时,Nuxt Image 会直接投递你的原始图片而不做优化。请配置一个image provider(例如 cloudflare)来优化它们。

Nuxt Image 不会处理 app/assets/ 目录中的图片,因为你的构建工具会管理它们。要想让源图片不放在 public/ 中,请设置 image.dir 选项。

响应式图片(Responsive Images)

使用 sizes prop 生成多个图片宽度,并让浏览器为当前屏幕挑选合适的那个。每个值从某个屏幕尺寸开始向上生效。屏幕尺寸默认与 Tailwind CSS 断点 保持一致:

<template>
  <NuxtImg
    src="/hero.jpg"
    alt="Mountains at sunrise"
    width="1600"
    height="900"
    sizes="100vw md:50vw xl:800px"
  />
</template>

在这个示例中,图片在小屏幕上占满屏幕宽度,从 md 断点开始占据一半,从 xl 断点开始宽度为 800 像素。Nuxt Image 会生成一个 srcset,其中每个尺寸各有一张图片,并且同时提供 1x 和 2x 两种像素密度。sizes prop 不会设置图片的显示宽度,因此请使用 CSS 让图片具备这些宽度。

没有 sizes 的图片(例如图标或头像)默认会获得 1x 和 2x 两个版本,从而在高密度屏幕上保持清晰。若要修改像素密度,可以对单张图片使用 densities prop,或对全部图片使用 image.densities 选项。

图片格式(Image Formats)

使用 format prop 将图片转换为现代格式:

<template>
  <NuxtImg
    src="/hero.jpg"
    alt="Mountains at sunrise"
    format="webp"
  />
</template>

若要为每个浏览器投递它所支持的最佳格式,请使用 <NuxtPicture>。它会渲染一个 <picture> 元素,其中每种格式对应一个 source,并提供一个使用 JPEG 或 PNG 等传统格式的兜底图片:

<template>
  <NuxtPicture
    src="/hero.jpg"
    alt="Mountains at sunrise"
    format="avif,webp"
  />
</template>

加载优先级(Loading Priority)

首屏之下的图片可以等到用户滚动到它们附近时再加载。设置 loading="lazy" 可以让图片只在进入视口时才加载:

<template>
  <NuxtImg
    src="/gallery/sunset.jpg"
    alt="Sunset over the sea"
    width="400"
    height="300"
    loading="lazy"
  />
</template>

作为页面最大内容绘制(Largest Contentful Paint,LCP)的图片(例如主视觉图)必须尽早加载。使用 preload prop 可以在页面 head 中添加一个带有高抓取优先级(fetch priority)的 <link rel="preload"> 标签:

<template>
  <NuxtImg
    src="/hero.jpg"
    alt="Mountains at sunrise"
    width="1600"
    height="900"
    :preload="{ fetchPriority: 'high' }"
  />
</template>

占位图(Placeholders)

若要在完整图片加载期间显示模糊的低质量版本,请添加 placeholder prop:

<template>
  <NuxtImg
    src="/hero.jpg"
    alt="Mountains at sunrise"
    width="1600"
    height="900"
    placeholder
  />
</template>

<NuxtImg> 会在浏览器中用完整图片替换占位图,因此占位图需要 JavaScript。如果没有 JavaScript,页面会一直显示占位图。

你也可以传入自己的占位图,或设置它的大小、质量和模糊程度。全部选项请参阅 placeholder prop。

远程图片(Remote Images)

要使用远程图片,请在 src 属性中提供它的 URL:

<template>
  <NuxtImg
    src="https://images.unsplash.com/photo-1501785888041-af3ef285b470"
    alt="Lake surrounded by mountains"
    width="500"
    height="500"
  />
</template>

为了防止其他网站利用你的服务器去优化互联网上的任意图片,Nuxt Image 只会优化你允许的域名下的远程图片。请将它们添加到 nuxt.config 的 image.domains 选项中:

export default defineNuxtConfig({
  modules: ['@nuxt/image'],
  image: {
    domains: ['images.unsplash.com'],
  },
})

在默认的 IPX provider 下,Nuxt Image 会用原始 URL 渲染来自其他域名的图片,不做优化。其他provider对这些图片的处理方式可能不同。

图片 Provider(Image Providers)

如果你已经把图片托管在图片 CDN 或 headless CMS 上,Nuxt Image 可以使用它们的图片转换服务来代替 IPX。Nuxt Image 支持超过 40 个 provider,例如 Cloudinary、Imgix、Sanity 和 Storyblok。当你部署到 Vercel、Netlify 或 AWS Amplify 时,Nuxt Image 会自动检测并使用该平台自带的图片服务。

要使用某个 provider,请把它设置为默认的 provider 并在你的 nuxt.config 中配置它:

export default defineNuxtConfig({
  modules: ['@nuxt/image'],
  image: {
    provider: 'cloudinary',
    cloudinary: {
      baseURL: 'https://res.cloudinary.com/<your-cloud-name>/image/upload',
    },
  },
})

这样你的图片 src 就是该 provider 上的一个路径:

<template>
  <NuxtImg
    src="/blog/cover.jpg"
    alt="Blog post cover"
    width="800"
    height="450"
  />
</template>

你也可以通过 provider prop 为单张图片指定 provider。

下一步(Next Steps)

请查看 Nuxt Image 文档以了解它的全部功能:

  • <NuxtImg>:图片组件的所有 props。
  • <NuxtPicture>:投递多种图片格式。
  • useImage():在你的代码中生成优化后的图片 URL,例如用于 CSS 背景。
  • 配置:设置质量、格式、屏幕尺寸和预设(presets)等默认值。

On this page