Commit 28377a78 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add static directory deprecation warning (#8636)

* Add static directory warning

* Simplify migration to public directory

* Update static-dir-deprecated.md
parent e2d713f9
# 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)
......@@ -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
......@@ -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
......
......@@ -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)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment