public
Next.js 可以在根目录中名为 public
的文件夹下提供静态文件,例如图片。然后,你的代码可以从基本 URL (/
) 开始引用 public
内的文件。
¥Next.js can serve static files, like images, under a folder called public
in the root directory. Files inside public
can then be referenced by your code starting from the base URL (/
).
例如,访问 /avatars/me.png
路径可以查看文件 public/avatars/me.png
。显示该图片的代码可能如下所示:
¥For example, the file public/avatars/me.png
can be viewed by visiting the /avatars/me.png
path. The code to display that image might look like:
缓存
¥Caching
Next.js 无法安全地将资源缓存在 public
文件夹中,因为它们可能会发生变化。应用的默认缓存标头是:
¥Next.js cannot safely cache assets in the public
folder because they may change. The default caching headers applied are:
机器人、网站图标等
¥Robots, Favicons, and others
该文件夹也适用于 robots.txt
、favicon.ico
、Google Site Verification 以及任何其他静态文件(包括 .html
)。但请确保静态文件不要与 pages/
目录中的文件同名,因为这会导致错误。阅读更多。
¥The folder is also useful for robots.txt
, favicon.ico
, Google Site Verification, and any other static files (including .html
). But make sure to not have a static file with the same name as a file in the pages/
directory, as this will result in an error. Read more.