Lifecycle APIs
caution
This section is a work in progress.
Lifecycle APIs are shared by Themes and Plugins.
validateOptions({options,validate})
Return validated and normalized options for the plugin. This method is called before the plugin is initialized.You must return options since the returned options will be passed to plugin during intialization.
options
validateOptions
is called with options
passed to plugin for validation and normalization.
validate
validateOptions
is called with validate
function which takes a Joi schema and options as argument, returns validated and normalized options. validate
will automatically handle error and validation config.
tip
Joi is recommended for validation and normalization of options.
If you don't use Joi for validation you can throw an Error in case of invalid options and return options in case of success.
You can also use ES modules style exports.
validateThemeConfig({themeConfig,validate})
Return validated and normalized configuration for the theme.
themeConfig
validateThemeConfig
is called with themeConfig
provided in docusaurus.config.js
for validation and normalization.
validate
validateThemeConfig
is called with validate
function which takes a Joi schema and themeConfig
as argument, returns validated and normalized options. validate
will automatically handle error and validation config.
tip
Joi is recommended for validation and normalization of theme config.
If you don't use Joi for validation you can throw an Error in case of invalid options.
You can also use ES modules style exports.
getPathsToWatch()
Specifies the paths to watch for plugins and themes. The paths are watched by the dev server so that the plugin lifecycles are reloaded when contents in the watched paths change. Note that the plugins and themes modules are initially called with context
and options
from Node, which you may use to find the necessary directory information about the site.
Example:
async loadContent()
Plugins should use this lifecycle to fetch from data sources (filesystem, remote API, headless CMS, etc) or doing some server processing.
For example, this plugin below return a random integer between 1 to 10 as content;
async contentLoaded({content, actions})
Plugins should use the data loaded in loadContent
and construct the pages/routes that consume the loaded data (optional).
content
contentLoaded
will be called after loadContent
is done, the return value of loadContent()
will be passed to contentLoaded
as content
.
actions
actions
contain two functions:
addRoute(config: RouteConfig): void
Create a route to add to the website.
createData(name: string, data: any): Promise<string>
A function to help you create static data (generally json or string), that you can provide to your routes as props.
For example, this plugin below create a /friends
page which display Your friends are: Yangshun, Sebastien
:
setGlobalData(data: any): void
This function permits to create some global plugin data, that can be read from any page, including the pages created by other plugins, and your theme layout.
This data become accessible to your client-side/theme code, through the useGlobalData
and usePluginData
One this data is created, you can access it with the global data hooks APIs
caution
Global data is... global: its size affects the loading time of all pages of your site, so try to keep it small.
Prefer createData
and page-specific data whenever possible.
For example, this plugin below create a /friends
page which display Your friends are: Yangshun, Sebastien
:
configureWebpack(config, isServer, utils)
Modifies the internal webpack config. If the return value is a JavaScript object, it will be merged into the final config using webpack-merge
. If it is a function, it will be called and receive config
as the first argument and an isServer
flag as the argument argument.
config
configureWebpack
is called with config
generated according to client/server build. You may treat this as the base config to be merged with.
isServer
configureWebpack
will be called both in server build and in client build. The server build receives true
and the client build receives false
as isServer
.
utils
The initial call to configureWebpack
also receives a util object consists of three functions:
getStyleLoaders(isServer: boolean, cssOptions: {[key: string]: any}): Loader[]
getCacheLoader(isServer: boolean, cacheOptions?: {}): Loader | null
getBabelLoader(isServer: boolean, babelOptions?: {}): Loader
You may use them to return your webpack configures conditionally.
For example, this plugin below modify the webpack config to transpile .foo
file.
Merge strategy
We merge the Webpack configuration parts of plugins into the global Webpack config using webpack-merge.
It is possible to specify the merge strategy. For example, if you want a webpack rule to be prepended instead of appended:
Read the webpack-merge strategy doc for more details.
postBuild(props)
Called when a (production) build finishes.
Example:
extendCli(cli)
Register an extra command to enhance the CLI of docusaurus. cli
is commander object.
Example:
injectHtmlTags()
Inject head and/or body HTML tags to Docusaurus generated HTML.
Example:
getThemePath()
Returns the path to the directory where the theme components can be found. When your users calls swizzle
, getThemePath
is called and its returned path is used to find your theme components.
If you use the folder directory above, your getThemePath
can be:
getTypeScriptThemePath()
Similar to getThemePath()
, it should return the path to the directory where the source code of TypeScript theme components can be found. Theme components under this path will not be resolved by Webpack. Therefore, it is not a replacement of getThemePath()
. Instead, this path is purely for swizzling TypeScript theme components.
If you want to support TypeScript component swizzling for your theme, you can make the path returned by getTypeScriptThemePath()
be your source directory, and make path returned by getThemePath()
be the compiled JavaScript output.
Example:
getClientModules()
Returns an array of paths to the modules that are to be imported in the client bundle. These modules are imported globally before React even renders the initial UI.
As an example, to make your theme load a customCss
object from options
passed in by the user:
Example
Here's a mind model for a presumptuous plugin implementation.