跳到内容

public 文件夹

Next.js 可以将静态文件(如图片)放在根目录下的一个名为 public 的文件夹中。public 文件夹中的文件可以从基础 URL (/) 开始被您的代码引用。

例如,文件 public/avatars/me.png 可以通过访问 /avatars/me.png 路径来查看。显示该图片的代码可能如下所示

avatar.js
import Image from 'next/image'
 
export function Avatar({ id, alt }) {
  return <Image src={`/avatars/${id}.png`} alt={alt} width="64" height="64" />
}
 
export function AvatarOfMe() {
  return <Avatar id="me" alt="A portrait of me" />
}

缓存

Next.js 无法安全地缓存 public 文件夹中的资源,因为它们可能会更改。默认应用的缓存头是

Cache-Control: public, max-age=0

Robots、Favicons 及其他

对于静态元数据文件,例如 robots.txtfavicon.ico 等,您应该在 app 文件夹中使用特殊的元数据文件