Static Assets in `public`
Next.js allows you to serve static files, like images, in the public directory. You can learn how it works here.
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 (/
).
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 cannot safely cache assets in the public
folder because they may change. The default caching headers applied are:
Robots, Favicons, and others
For static metadata files, such as robots.txt
, favicon.ico
, etc, you should use special metadata files inside the app
folder.
Good to know:
- The directory must be named
public
. The name cannot be changed and it's the only directory used to serve static assets. - Only assets that are in the
public
directory at build time will be served by Next.js. Files added at request time won't be available. We recommend using a third-party service like Vercel Blob for persistent file storage.