Use this file to discover all available pages before exploring further.
Docusaurus plugins expose a set of lifecycle methods that are called at specific points during the build and development server processes. Each plugin is a function that receives a context and options argument and returns an object implementing whichever lifecycle methods it needs. Methods are called in parallel across plugins where possible, so avoid assuming a particular ordering between plugins unless Docusaurus documents one explicitly.
A plugin module exports a constructor function as its default export. The function receives the site-wide LoadContext and any plugin-specific options, then returns a plain object containing lifecycle method implementations.
import type {LoadContext, Plugin} from '@docusaurus/types';export default function myPlugin( context: LoadContext, options: {path: string},): Plugin { return { name: 'my-plugin', // lifecycle methods go here };}
The context object is identical for every plugin on a given site. It exposes site-level configuration and paths that plugins frequently need.
type LoadContext = { /** Absolute path to the site root directory. */ siteDir: string; /** Absolute path where generated files are written. */ generatedFilesDir: string; /** Absolute path to the production output directory. */ outDir: string; /** Resolved docusaurus.config.js contents. */ siteConfig: DocusaurusConfig; /** The config file path on disk. */ siteConfigPath: string; /** Resolved base URL (includes locale prefix when applicable). */ baseUrl: string; /** i18n context for the current build. */ i18n: I18n; /** Pre-loaded code translations for the current locale. */ codeTranslations: CodeTranslations; /** Current bundler details (Webpack or Rspack). */ currentBundler: CurrentBundler;};
The i18n object on context contains currentLocale, defaultLocale, locales, and per-locale configuration. Use it in getPathsToWatch or loadContent to load locale-specific data files.
loadContent is the first lifecycle hook called during a build. Use it to fetch data from external APIs, read from the filesystem, query a CMS, or perform any other async server-side work. Whatever value you return becomes the content argument passed to later lifecycle methods.
loadContent?: () => Promise<Content> | Content;
Return value: Any serializable value, or a Promise that resolves to one. The type parameter Content flows through to contentLoaded, injectHtmlTags, configureWebpack, and the translation lifecycles.
import type {LoadContext, Plugin} from '@docusaurus/types';import {readFile} from 'fs/promises';import path from 'path';type MyContent = {items: string[]};export default function myPlugin( context: LoadContext, options: {dataFile: string},): Plugin<MyContent> { return { name: 'my-plugin', async loadContent(): Promise<MyContent> { const dataPath = path.resolve(context.siteDir, options.dataFile); const raw = await readFile(dataPath, 'utf8'); return {items: JSON.parse(raw)}; }, };}
contentLoaded is called after loadContent completes. It receives the value returned by loadContent and an actions object with three functions for registering routes and data with Docusaurus.
Registers a page at the given URL path. The component field must be a path that the bundler can resolve with require.
type RouteConfig = { /** URL path with a leading slash. Trailing slash is normalized by config. */ path: string; /** Bundler-resolvable path to the React component that renders this route. */ component: string; /** * Prop modules: each key maps a prop name to a JSON file path created * with createData(). The data is loaded lazily on the client. */ modules?: RouteModules; /** Context data made available via useRouteContext(). */ context?: RouteModules; /** Nested child routes for layout routes. */ routes?: RouteConfig[]; /** When true, only matches the exact path (no sub-paths). */ exact?: boolean; /** When true, the trailing slash is significant. */ strict?: boolean; /** Higher values are matched first. */ priority?: number; /** Server-only metadata (e.g. lastmod for sitemap). */ metadata?: RouteMetadata;};
Writes a static JSON (or string) file to the generated files directory and returns its absolute path. Pass the returned path as a value inside modules when calling addRoute to make the data available as a React prop on the client.
createData writes files to disk. Call it once per data file and reuse the returned path — do not call it inside a loop for the same data.
Called once after the production build completes. Use it to generate sitemaps, send notifications, post-process output files, or run any other cleanup tasks that require the full set of built routes.
Injects arbitrary HTML tags into every page’s <head>, the opening <body>, or before the closing </body>. Tags can be raw HTML strings or structured HtmlTagObject values.
injectHtmlTags?: (args: {content: Content}) => { headTags?: HtmlTags; preBodyTags?: HtmlTags; postBodyTags?: HtmlTags;};type HtmlTags = string | HtmlTagObject | (string | HtmlTagObject)[];type HtmlTagObject = { /** E.g. `'link'`, `'script'`, `'meta'` */ tagName: string; /** E.g. `{ rel: 'preconnect', href: 'https://fonts.googleapis.com' }` */ attributes?: {[key: string]: string | boolean}; /** Inner HTML content of the tag. */ innerHTML?: string; /** Set to true for custom HTML elements. */ customElement?: boolean;};
Tag placement rules:
headTags — inserted before the closing </head> tag, after script tags added by Docusaurus config.
preBodyTags — inserted immediately after the opening <body> tag.
postBodyTags — inserted immediately before the closing </body> tag.
Returns an array of glob patterns or absolute file paths that the Docusaurus development server should watch. When any watched file changes, the plugin’s loadContent lifecycle is re-executed and the site hot-reloads.
getPathsToWatch?: () => string[];
Only use getPathsToWatch for files consumed on the server side (i.e. in loadContent). Theme component files under the path returned by getThemePath() are watched automatically by the bundler’s dev server.
import type {LoadContext, Plugin} from '@docusaurus/types';import path from 'path';export default function myPlugin( context: LoadContext, options: {contentDir: string},): Plugin { return { name: 'my-plugin', getPathsToWatch(): string[] { const contentPath = path.resolve(context.siteDir, options.contentDir); return [`${contentPath}/**/*.{json,yaml,md}`]; }, };}
Returns the path to the directory containing custom theme components. When a user runs docusaurus swizzle, Docusaurus calls getThemePath() on every theme to locate available components. Paths are resolved relative to the plugin’s entry point file.
getThemePath?: () => string;
import type {LoadContext, Plugin} from '@docusaurus/types';export default function myTheme(context: LoadContext): Plugin { return { name: 'my-theme', getThemePath(): string { // Compiled JS output — used by webpack at runtime return '../lib/theme'; }, getTypeScriptThemePath(): string { // TypeScript source — used by swizzle for type-safe overrides return '../src/theme'; }, };}
If you provide TypeScript theme components, implement both getThemePath() (pointing to compiled JS) and getTypeScriptThemePath() (pointing to the .tsx source). This lets Docusaurus serve the compiled output while offering typed swizzle sources for users.
Returns an array of module paths that are imported globally in the client bundle, before React renders the initial UI. Use this to inject global CSS, polyfills, or side-effect modules.
getClientModules?: () => string[];
import type {LoadContext, Plugin} from '@docusaurus/types';export default function myTheme( context: LoadContext, options: {customCss?: string; customJs?: string},): Plugin { return { name: 'my-theme', getClientModules(): string[] { return [options.customCss, options.customJs].filter( (p): p is string => Boolean(p), ); }, };}
configureWebpack is called twice per build: once for the server bundle and once for the client bundle. Use the isServer flag to apply rules selectively.
Always mutate and return the existing postcssOptions object rather than returning a fresh one. Returning a fresh object discards plugins added by other plugins or Docusaurus itself.