2019-04-28 23:20:24 -04:00
---
id: using-plugins
2019-10-26 04:10:48 -04:00
title: Plugins
2019-04-28 23:20:24 -04:00
---
2019-10-26 04:10:48 -04:00
Plugins are the building blocks of features in a Docusaurus 2 site. Each plugin handles its own individual feature. Plugins may work and be distributed as part of bundle via [presets ](presets.md ).
2019-07-03 02:12:22 -04:00
2021-03-19 06:00:41 -04:00
## Available plugins {#available-plugins}
2020-11-30 08:07:08 -05:00
We maintain a [list of official plugins ](./api/plugins/overview.md ), but the community has also created some [unofficial plugins ](/community/resources#community-plugins ).
2021-03-19 06:00:41 -04:00
## Installing a plugin {#installing-a-plugin}
2019-07-26 01:00:40 -04:00
2019-10-26 04:10:48 -04:00
A plugin is usually a npm package, so you install them like other npm packages using npm.
2019-07-26 01:00:40 -04:00
2019-11-23 21:32:26 -05:00
```bash npm2yarn
2019-07-26 01:00:40 -04:00
npm install --save docusaurus-plugin-name
```
Then you add it in your site's `docusaurus.config.js` 's `plugins` option:
2020-03-28 13:08:50 -04:00
```jsx {3} title="docusaurus.config.js"
2019-07-26 01:00:40 -04:00
module.exports = {
2019-10-29 02:59:24 -04:00
// ...
2019-07-26 01:00:40 -04:00
plugins: ['@docusaurus/plugin-content-pages'],
};
```
Docusaurus can also load plugins from your local directory, you can do something like the following:
2020-03-28 13:08:50 -04:00
```jsx {5} title="docusaurus.config.js"
2019-07-26 01:00:40 -04:00
const path = require('path');
module.exports = {
2019-10-29 02:59:24 -04:00
// ...
2019-07-26 01:00:40 -04:00
plugins: [path.resolve(__dirname, '/path/to/docusaurus-local-plugin')],
};
```
2021-03-19 06:00:41 -04:00
## Configuring plugins {#configuring-plugins}
2019-07-03 02:12:22 -04:00
2019-10-26 04:10:48 -04:00
For the most basic usage of plugins, you can provide just the plugin name or the absolute path to the plugin.
2019-07-03 02:12:22 -04:00
2019-10-26 04:10:48 -04:00
However, plugins can have options specified by wrapping the name and an options object in an array inside your config. This style is usually called `Babel Style` .
2019-07-03 02:12:22 -04:00
2020-03-28 13:08:50 -04:00
```js {4-9} title="docusaurus.config.js"
2019-07-03 02:12:22 -04:00
module.exports = {
2019-10-29 02:59:24 -04:00
// ...
2019-07-03 02:12:22 -04:00
plugins: [
2019-07-08 02:14:49 -04:00
[
2019-10-26 04:10:48 -04:00
'@docusaurus/plugin-xxx',
2019-07-08 02:14:49 -04:00
{
2019-10-26 04:10:48 -04:00
/* options */
2019-07-08 02:14:49 -04:00
},
],
2019-07-03 02:12:22 -04:00
],
};
```
2019-10-26 04:10:48 -04:00
Example:
2019-07-03 02:12:22 -04:00
2020-03-28 13:08:50 -04:00
```js title="docusaurus.config.js"
2019-07-03 02:12:22 -04:00
module.exports = {
2019-10-26 04:10:48 -04:00
plugins: [
// Basic usage.
'@docusaurus/plugin-google-analytics',
// With options object (babel style)
2019-07-03 02:12:22 -04:00
[
2019-10-26 04:10:48 -04:00
'@docusaurus/plugin-sitemap',
2019-07-03 02:12:22 -04:00
{
2021-02-18 09:12:42 -05:00
changefreq: 'weekly',
2019-07-03 02:12:22 -04:00
},
],
],
};
```
2019-11-25 13:31:57 -05:00
2021-03-19 06:00:41 -04:00
## Multi-instance plugins and plugin ids {#multi-instance-plugins-and-plugin-ids}
2020-07-21 05:16:08 -04:00
2020-12-30 13:41:42 -05:00
All Docusaurus content plugins can support multiple plugin instances.
2020-07-21 05:16:08 -04:00
2020-12-30 13:41:42 -05:00
The Docs plugin has [additional multi-instance documentation ](./guides/docs/docs-multi-instance.mdx )
It is required to assign a unique id to each plugin instance.
2020-07-21 05:16:08 -04:00
By default, the plugin id is `default` .
```js {6,13} title="docusaurus.config.js"
module.exports = {
plugins: [
[
'@docusaurus/plugin-xxx',
{
id: 'plugin-xxx-1',
// other options
},
],
[
'@docusaurus/plugin-xxx',
{
id: 'plugin-xxx-2',
// other options
},
],
],
};
```
2020-12-30 13:41:42 -05:00
:::note
At most one plugin instance can be the "default plugin instance", by omitting the `id` attribute, or using `id: 'default'` .
:::
2021-03-19 06:00:41 -04:00
## Plugins design {#plugins-design}
2019-11-25 13:31:57 -05:00
Docusaurus' implementation of the plugins system provides us with a convenient way to hook into the website's lifecycle to modify what goes on during development/build, which involves (but not limited to) extending the webpack config, modifying the data being loaded and creating new components to be used in a page.
2021-03-19 06:00:41 -04:00
## Creating plugins {#creating-plugins}
2019-11-25 13:31:57 -05:00
2021-12-22 13:10:49 -05:00
A plugin is a function that takes two parameters: `context` and `options` . It returns a plugin instance object (or a promise). You can create plugins as functions or modules. For more information, refer to the [plugin method references section ](./api/plugin-methods/README.md ).
2019-11-25 13:31:57 -05:00
2021-05-15 13:33:05 -04:00
### Functional definition {#functional-definition}
2019-11-25 13:31:57 -05:00
2021-12-21 01:54:04 -05:00
You can use a plugin as a function directly included in the Docusaurus config file:
2020-06-19 09:32:36 -04:00
2020-06-24 14:13:32 -04:00
```js title="docusaurus.config.js"
module.exports = {
// ...
2021-05-15 13:33:05 -04:00
plugins: [
2021-10-20 09:38:25 -04:00
// highlight-start
2021-12-22 13:10:49 -05:00
async function myPlugin(context, options) {
2021-05-15 13:33:05 -04:00
// ...
return {
name: 'my-plugin',
async loadContent() {
// ...
},
async contentLoaded({content, actions}) {
// ...
},
/* other lifecycle API */
};
},
// highlight-end
],
};
```
### Module definition {#module-definition}
2021-12-21 01:54:04 -05:00
You can use a plugin as a module path referencing a separate file or NPM package:
2021-05-15 13:33:05 -04:00
```js title="docusaurus.config.js"
module.exports = {
// ...
plugins: [
// without options:
'./my-plugin',
// or with options:
['./my-plugin', options],
],
2020-06-24 14:13:32 -04:00
};
```
2020-06-19 09:32:36 -04:00
2021-12-21 01:54:04 -05:00
Then in the folder `my-plugin` you can create an index.js such as this:
2020-06-19 09:32:36 -04:00
2021-05-15 13:33:05 -04:00
```js title="my-plugin.js"
2021-12-22 13:10:49 -05:00
module.exports = async function myPlugin(context, options) {
2019-11-25 13:31:57 -05:00
// ...
return {
2021-05-15 13:33:05 -04:00
name: 'my-plugin',
2020-07-01 13:03:59 -04:00
async loadContent() {
/* ... */
},
async contentLoaded({content, actions}) {
/* ... */
},
2020-05-17 23:37:53 -04:00
/* other lifecycle API */
2019-11-25 13:31:57 -05:00
};
};
```