The Docusaurus client is the JavaScript layer that runs in the user’s browser after the static HTML has been delivered. It consists of the React component tree provided by themes, client modules that register global side-effects, and a set of hooks that give any component access to site configuration and routing state. Understanding this layer is essential for customising behaviour — from injecting a global analytics script to readingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/facebook/docusaurus/llms.txt
Use this file to discover all available pages before exploring further.
themeConfig values inside your own components.
Theme aliases and the layered architecture
Docusaurus themes work by exporting components under the@theme Webpack alias. When your code writes import Navbar from '@theme/Navbar', Webpack resolves it through a priority stack:
- User swizzles —
website/src/theme/Navbar.js(highest priority) - Theme package —
@docusaurus/theme-classic/theme/Navbar.js - Core fallbacks — built-in Docusaurus components (rarely needed)
src/theme/ and it shadows the theme package’s version everywhere in the build, without modifying node_modules.
The component stack in detail
When multiple themes or plugins provide the same component, Docusaurus builds a stack:@theme/*
@theme/*
Always points to the topmost component in the stack. When you swizzle
CodeBlock, every component that imports @theme/CodeBlock automatically gets your version.@theme-original/*
@theme-original/*
Points to the topmost non-swizzled component. Use this inside a swizzled component to delegate to the theme’s original implementation — the standard wrapper pattern.
@theme-init/*
@theme-init/*
Points to the bottommost component — typically the one originally provided by
theme-classic. Safe to use in plugin-provided theme enhancements. Only exists when more than one theme provides the same component name.Wrapping example
src/theme/CodeBlock/index.js
Client modules
Client modules are JavaScript (or CSS) files that Docusaurus imports globally, before React renders the initial UI. They are the right place for side-effects that must be present on every page: analytics initialisation, global event listeners, custom fonts, or global CSS.packages/docusaurus/src/client/App.tsx (simplified)
Registering client modules
- Via docusaurus.config.js
- Via a plugin
docusaurus.config.js
Global JavaScript example
src/myGlobalScript.js
Client modules run during SSR as well as in the browser. Always guard browser-only code with
ExecutionEnvironment.canUseDOM (or inside a useEffect) or you will get ReferenceError: window is not defined during the build.Global CSS example
src/myGlobalStyles.css
Route lifecycle hooks
Because Docusaurus is a single-page application,<script> tags execute only once — on the very first page load. If you need to run JavaScript on every navigation (e.g. to send a pageview to an analytics service, or to reset a UI element), export the lifecycle functions onRouteUpdate and onRouteDidUpdate from a client module.
Timing of events
User clicks a link
React Router records the new location.
onRouteUpdate fires with {location, previousLocation}.Docusaurus preloads the next route's assets
The current page remains visible. Any cleanup function returned by
onRouteUpdate is called when preloading completes.JavaScript lifecycle example
src/clientModules/analytics.js
TypeScript lifecycle example
src/clientModules/analytics.ts
useDocusaurusContext
The useDocusaurusContext hook returns the full DocusaurusContext object, which is built from generated files at bundle time and never changes at runtime. It is the canonical way to access site configuration in any theme component.
src/components/SiteTitle.jsx
| Property | Type | Contents |
|---|---|---|
siteConfig | DocusaurusConfig | The full docusaurus.config.js value after serialisation. Includes title, baseUrl, themeConfig, customFields, etc. |
siteMetadata | SiteMetadata | Docusaurus version, plugin versions, Node version. |
globalData | GlobalData | Data published by plugins via setGlobalData, keyed by plugin name and ID. |
i18n | I18n | Current locale, default locale, list of all configured locales. |
codeTranslations | CodeTranslations | Translated strings for built-in UI text. |
useThemeConfig and theme hooks
@docusaurus/theme-common exports a collection of hooks for reading and managing theme state. The most commonly used is useThemeConfig, which returns the themeConfig section of siteConfig with full TypeScript types:
src/theme/Navbar/index.tsx
@docusaurus/theme-common: