mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
Closes: https://linear.app/vercel/issue/DOC-4686/clean-up-routing-section Redirects: https://github.com/vercel/front/pull/47151 --------- Co-authored-by: Joseph <[email protected]> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
46 lines
1.8 KiB
Plaintext
46 lines
1.8 KiB
Plaintext
---
|
|
title: public Folder
|
|
nav_title: public
|
|
description: Next.js allows you to serve static files, like images, in the public directory. You can learn how it works here.
|
|
---
|
|
|
|
{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
|
|
|
|
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:
|
|
|
|
```jsx filename="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" />
|
|
}
|
|
```
|
|
|
|
## Caching
|
|
|
|
Next.js cannot safely cache assets in the `public` folder because they may change. The default caching headers applied are:
|
|
|
|
```jsx
|
|
Cache-Control: public, max-age=0
|
|
```
|
|
|
|
## Robots, Favicons, and others
|
|
|
|
<PagesOnly>
|
|
|
|
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](/docs/messages/conflicting-public-file-page).
|
|
|
|
</PagesOnly>
|
|
|
|
<AppOnly>
|
|
|
|
For static metadata files, such as `robots.txt`, `favicon.ico`, etc, you should use [special metadata files](/docs/app/api-reference/file-conventions/metadata) inside the `app` folder.
|
|
|
|
</AppOnly>
|