Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static directory deprecation warning #8636

Merged
merged 6 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions errors/static-dir-deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Static directory is deprecated

#### Why This Error Occurred

In versions prior to 9.0.6 the `static` directory was used to serve static assets in a Next.js application. This has been deprecated in favor of a `public` directory.

The reason we want to support a `public` directory instead is to not require the `/static` prefix for assets anymore and there is no reason to maintain both paths.

#### Possible Ways to Fix It

You can move your `static` directory inside of the `public` directory and all URLs will remain the same as they were before.

**Before**

```sh
static/
my-image.jpg
pages/
index.js
components/
my-image.js
```

**After**

```sh
public/
static/
my-image.jpg
pages/
index.js
components/
my-image.js
```

### Useful Links

- [Static file serving docs](https://nextjs.org/docs#static-file-serving-eg-images)
10 changes: 4 additions & 6 deletions packages/next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ So far, we get:
- Automatic transpilation and bundling (with webpack and babel)
- Hot code reloading
- Server rendering and indexing of `./pages/`
- Static file serving. `./static/` is mapped to `/static/` (given you [create a `./static/` directory](#static-file-serving-eg-images) inside your project)
- Static file serving. `./public/` is mapped to `/` (given you [create a `./public/` directory](#static-file-serving-eg-images) inside your project)

### Automatic code splitting

Expand Down Expand Up @@ -276,21 +276,19 @@ To support importing `.css`, `.scss`, `.less` or `.styl` files you can use these

### Static file serving (e.g.: images)

Create a folder called `static` in your project root directory. From your code you can then reference those files with `/static/` URLs:
Create a folder called `public` in your project root directory. From your code you can then reference those files starting from the baseURL `/`

```jsx
function MyImage() {
return <img src="/static/my-image.png" alt="my image" />
return <img src="/my-image.png" alt="my image" />
}

export default MyImage
```

<!--
To serve static files from the root directory you can add a folder called `public` and reference those files from the root, e.g: `/robots.txt`.
-->

_Note: Don't name the `static` directory anything else. The name can't be changed and is the only directory that Next.js uses for serving static assets._
_Note: Don't name the `public` directory anything else. The name can't be changed and is the only directory that Next.js uses for serving static assets._

### Dynamic Routing

Expand Down
5 changes: 5 additions & 0 deletions packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export default class DevServer extends Server {
)
})
}
if (fs.existsSync(join(this.dir, 'static'))) {
console.warn(
`The static directory has been deprecated in favor of the public directory. https://err.sh/zeit/next.js/static-dir-deprecated`
)
}
this.pagesDir = findPagesDir(this.dir)
}

Expand Down