图片优化
了解如何在 Next.js 中优化图片
Next.js <Image> 组件扩展了 HTML <img> 元素,提供以下功能:
- 尺寸优化: 自动为每台设备提供尺寸正确的图片,并使用 WebP 等现代图片格式。
- 视觉稳定性: 在图片加载时自动防止布局偏移。
- 更快的页面加载: 使用浏览器原生懒加载,仅在图片进入视口时才加载,并可选提供模糊占位图。
- 资源灵活性: 按需调整图片尺寸,即使是存储在远程服务器上的图片。
要开始使用 <Image>,请从 next/image 导入它,并在组件中渲染。
TypeScript
import Image from 'next/image'
export default function Page() {
return <Image src="" alt="" />
}
JavaScript
import Image from 'next/image'
export default function Page() {
return <Image src="" alt="" />
}
🎥 观看:
了解关于如何使用
next/image的更多信息 → YouTube(9 分钟)。
本地图片
你可以将图片和字体等静态文件存储在根目录下名为 public 的文件夹中。然后,代码可以从基础 URL(/)开始引用 public 内的文件。

TypeScript
import Image from 'next/image'
export default function Page() {
return (
<Image
src="/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}
JavaScript
import Image from 'next/image'
export default function Page() {
return (
<Image
src="/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}
如果图片是静态导入的,Next.js 会自动确定其固有width和height。这些值用于确定图片宽高比,并在图片加载时防止累积布局偏移。
TypeScript
import Image from 'next/image'
import ProfileImage from './profile.png'
export default function Page() {
return (
<Image
src={ProfileImage}
alt="Picture of the author"
// width={500} automatically provided
// height={500} automatically provided
// blurDataURL="data:..." automatically provided
// placeholder="blur" // Optional blur-up while loading
/>
)
}
JavaScript
import Image from 'next/image'
import ProfileImage from './profile.png'
export default function Page() {
return (
<Image
src={ProfileImage}
alt="Picture of the author"
// width={500} automatically provided
// height={500} automatically provided
// blurDataURL="data:..." automatically provided
// placeholder="blur" // Optional blur-up while loading
/>
)
}
没有静态导入的图片
如果你无法为图片使用静态 import,可以在服务器组件中使用动态 import(),仍可获得自动的 width、height 和 blurDataURL:
TypeScript
import Image from 'next/image'
async function PostImage({
imageFilename,
alt,
}: {
imageFilename: string
alt: string
}) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image contains width, height, and blurDataURL
return <Image src={image} alt={alt} />
}
JavaScript
import Image from 'next/image'
async function PostImage({ imageFilename, alt }) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image contains width, height, and blurDataURL
return <Image src={image} alt={alt} />
}
如果你配置了路径别名(例如 @/),就可以使用它来代替相对路径:
const { default: image } = await import(
`@/content/blog/images/${imageFilename}`
)
路径必须包含静态前缀(例如 ../content/blog/images/)。请尽可能具体,因为所有匹配该前缀的文件都会被打包。只有你指定目录中的文件会被包含,因此外部输入无法访问该目录之外的内容。
远程图片
要使用远程图片,可以为 src 属性提供 URL 字符串。
TypeScript
import Image from 'next/image'
export default function Page() {
return (
<Image
src="https://s3.amazonaws.com/my-bucket/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}
JavaScript
import Image from 'next/image'
export default function Page() {
return (
<Image
src="https://s3.amazonaws.com/my-bucket/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}
由于 Next.js 在构建过程中无法访问远程文件,你需要手动提供 width、height 和可选的 blurDataURL 属性。width 和 height 用于推断图片的正确宽高比,并避免图片加载时产生布局偏移。或者,你可以使用 fill 属性 让图片填满父元素的大小。
为了安全地允许来自远程服务器的图片,你需要在 next.config.js 中定义一组受支持的 URL 模式。请尽可能具体,以防止恶意使用。例如,以下配置只允许来自特定 AWS S3 存储桶的图片:
TypeScript
import type { NextConfig } from 'next'
const config: NextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 's3.amazonaws.com',
port: '',
pathname: '/my-bucket/**',
search: '',
},
],
},
}
export default config
JavaScript
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 's3.amazonaws.com',
port: '',
pathname: '/my-bucket/**',
search: '',
},
],
},
}
API 参考
请参阅 API 参考,了解 Next.js Image 的完整功能集。
- 图片组件:使用内置的 `next/image` 组件优化 Next.js 应用中的图片。