mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-06 21:57:14 +02:00
* [WIP] Implementaion of multiple directory static sourcing * Move default to validation * Update test * Refactor * Port to MDX loader * Fix * Move dogfooding assets * Doc writeup * Restore assets * Support absolute paths * Dogfood absolute path * Fix * More tests * Fix snapshots Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
71 lines
3.2 KiB
Markdown
71 lines
3.2 KiB
Markdown
---
|
|
id: static-assets
|
|
title: Static Assets
|
|
---
|
|
|
|
Every website needs assets: images, stylesheets, favicons etc. By default, you are suggested to put these assets in the `static` folder.
|
|
|
|
Every file you put into **that directory will be copied** into the root of the generated `build` folder with the directory hierarchy preserved. E.g. if you add a file named `sun.jpg` to the static folder, it will be copied to `build/sun.jpg`.
|
|
|
|
This means that:
|
|
|
|
- for site `baseUrl: '/'`, the image `/static/img/docusaurus.png` will be served at `/img/docusaurus.png`.
|
|
- for site `baseUrl: '/subpath/'`, the image `/static/img/docusaurus.png` will be served at `/subpath/img/docusaurus.png`.
|
|
|
|
You can customize the static directory sources in `docusaurus.config.js`. For example, we can add `public` as another possible path:
|
|
|
|
```js title="docusaurus.config.js"
|
|
module.exports = {
|
|
title: 'My site',
|
|
staticDirectories: ['public', 'static'],
|
|
// ...
|
|
};
|
|
```
|
|
|
|
Now, all files in `public` as well as `static` will be copied to the build output.
|
|
|
|
## Referencing your static asset {#referencing-your-static-asset}
|
|
|
|
In JSX, you can reference assets from the `static` folder in your code using absolute paths, but this is not ideal because changing the site `baseUrl` will **break those links**. For the image `<img src="/img/docusaurus.png" />` served at `https://example.com/test`, the browser will try to resolve it from the URL root, i.e. as `https://example.com/img/docusaurus.png`, which will fail because it's actually served at `https://example.com/test/img/docusaurus.png`.
|
|
|
|
You can `import` / `require()` the static asset (recommended), or use the `useBaseUrl` utility function: both prepend the `baseUrl` to paths for you.
|
|
|
|
:::info
|
|
|
|
In Markdown, things are different: you can stick to use absolute paths because Docusaurus actually handles them as `require` calls instead of URLs when parsing the Markdown. See [Markdown static assets](./guides/markdown-features/markdown-features-assets.mdx).
|
|
|
|
:::
|
|
|
|
### Examples {#examples}
|
|
|
|
```jsx title="MyComponent.js"
|
|
import DocusaurusImageUrl from '@site/static/img/docusaurus.png';
|
|
|
|
<img src={DocusaurusImageUrl} />;
|
|
```
|
|
|
|
```jsx title="MyComponent.js"
|
|
<img src={require('@site/static/img/docusaurus.png').default} />
|
|
```
|
|
|
|
```jsx title="MyComponent.js"
|
|
import useBaseUrl from '@docusaurus/useBaseUrl';
|
|
|
|
<img src={useBaseUrl('/img/docusaurus.png')} />;
|
|
```
|
|
|
|
You can also import SVG files: they will be transformed into React components.
|
|
|
|
```jsx title="MyComponent.js"
|
|
import DocusaurusLogoWithKeytar from '@site/static/img/docusaurus_keytar.svg';
|
|
|
|
<DocusaurusLogoWithKeytar title="Docusaurus Logo" className="logo" />;
|
|
```
|
|
|
|
### Caveats {#caveats}
|
|
|
|
Keep in mind that:
|
|
|
|
- By default, none of the files in `static` folder will be post-processed, hashed or minified.
|
|
- Missing files referenced via hardcoded absolute paths will not be detected at compilation time, and will result in a 404 error.
|
|
- By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb.com/). Since Jekyll will discard any files that begin with `_`, it is recommended that you disable Jekyll by adding an empty file named `.nojekyll` file to your `static` directory if you are using GitHub pages for hosting.
|