图片优化
使用内置的 next/image 组件优化你的图片。
Next.js 的 <Image> 组件扩展了 HTML <img> 元素,提供以下功能:
- 尺寸优化: 使用 WebP 等现代图片格式,自动为每种设备提供尺寸正确的图片。
- 视觉稳定性: 在图片加载时自动防止布局偏移。
- 加载更快: 使用浏览器原生懒加载,仅在图片进入视口时加载,并可选用模糊占位符。
- 资源灵活性: 按需调整图片大小,即使是存储在远程服务器上的图片。
要开始使用 <Image>,请从 next/image 导入它并在组件中渲染。
import Image from 'next/image'
export default function Page() {
return <Image src="" alt="" />
}import Image from 'next/image'
export default function Page() {
return <Image src="" alt="" />
}🎥 观看: 了解更多关于如何使用
next/image的信息 → YouTube(9 分钟)。
本地图片#
您可以在根目录中名为 public 的文件夹下存储静态文件(如图片和字体)。然后,您的代码可以从基础 URL(/)开始引用 public 中的文件。
import Image from 'next/image'
export default function Page() {
return (
<Image
src="/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}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。这些值用于确定图片比例,并在图片加载时防止累计布局偏移。
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} 自动提供
// height={500} 自动提供
// blurDataURL="data:..." 自动提供
// placeholder="blur" // 加载时可选的模糊效果
/>
)
}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} 自动提供
// height={500} 自动提供
// blurDataURL="data:..." 自动提供
// placeholder="blur" // 加载时可选的模糊效果
/>
)
}不使用静态导入的图片#
如果您无法对图片使用静态 import,可以在 Server Component 中使用动态 import(),仍然获得自动的 width、height 和 blurDataURL:
import Image from 'next/image'
async function PostImage({
imageFilename,
alt,
}: {
imageFilename: string
alt: string
}) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image 包含 width、height 和 blurDataURL
return <Image src={image} alt={alt} />
}import Image from 'next/image'
async function PostImage({ imageFilename, alt }) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image 包含 width、height 和 blurDataURL
return <Image src={image} alt={alt} />
}如果您配置了路径别名(例如 @/),可以使用它代替相对路径:
const { default: image } = await import(
`@/content/blog/images/${imageFilename}`
)路径必须包含静态前缀(如 ../content/blog/images/)。应尽可能具体,因为与该前缀匹配的所有文件都会被打包。只有指定目录中的文件才会被包含,因此外部输入无法访问该目录之外的内容。
远程图片#
要使用远程图片,可以为 src 属性提供 URL 字符串。
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}
/>
)
}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 存储桶的图片:
import type { NextConfig } from 'next'
const config: NextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 's3.amazonaws.com',
port: '',
pathname: '/my-bucket/**',
search: '',
},
],
},
}
export default configmodule.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 's3.amazonaws.com',
port: '',
pathname: '/my-bucket/**',
search: '',
},
],
},
}