|
| 1 | +# Docusaurus Configuration - Detailed Guide |
| 2 | + |
| 3 | +## Configuration File Structure |
| 4 | + |
| 5 | +Docusaurus configuration can be in multiple formats: |
| 6 | + |
| 7 | +### TypeScript (Recommended) |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { Config } from "@docusaurus/types"; |
| 11 | +import { themes as prismThemes } from "prism-react-renderer"; |
| 12 | + |
| 13 | +const config: Config = { |
| 14 | + // Configuration here |
| 15 | +}; |
| 16 | + |
| 17 | +export default config; |
| 18 | +``` |
| 19 | + |
| 20 | +### JavaScript (ESM) |
| 21 | + |
| 22 | +```javascript |
| 23 | +export default { |
| 24 | + // Configuration here |
| 25 | +}; |
| 26 | +``` |
| 27 | + |
| 28 | +### JavaScript (CommonJS) |
| 29 | + |
| 30 | +```javascript |
| 31 | +module.exports = { |
| 32 | + // Configuration here |
| 33 | +}; |
| 34 | +``` |
| 35 | + |
| 36 | +### Async Configuration |
| 37 | + |
| 38 | +```typescript |
| 39 | +export default async function createConfig(): Promise<Config> { |
| 40 | + // Import ESM-only packages |
| 41 | + const mdxMermaid = await import("mdx-mermaid"); |
| 42 | + |
| 43 | + return { |
| 44 | + // Configuration here |
| 45 | + }; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Required Fields |
| 50 | + |
| 51 | +### title |
| 52 | + |
| 53 | +- Type: `string` |
| 54 | +- Required: Yes |
| 55 | +- Description: Site title for metadata and browser tabs |
| 56 | + |
| 57 | +### url |
| 58 | + |
| 59 | +- Type: `string` |
| 60 | +- Required: Yes |
| 61 | +- Format: Must not end with trailing slash |
| 62 | +- Example: `'https://example.com'` |
| 63 | +- Description: Full URL where site will be hosted |
| 64 | + |
| 65 | +### baseUrl |
| 66 | + |
| 67 | +- Type: `string` |
| 68 | +- Required: Yes |
| 69 | +- Format: Must start and end with `/` |
| 70 | +- Example: `'/'` or `'/docs/'` |
| 71 | +- Description: Path where site is served from |
| 72 | + |
| 73 | +## Common Optional Fields |
| 74 | + |
| 75 | +### Site Metadata |
| 76 | + |
| 77 | +- `tagline`: Short description of your site |
| 78 | +- `favicon`: Path to favicon (relative to static folder) |
| 79 | +- `organizationName`: GitHub org/user name (for deployment) |
| 80 | +- `projectName`: GitHub repo name (for deployment) |
| 81 | + |
| 82 | +### Deployment |
| 83 | + |
| 84 | +- `deploymentBranch`: Branch for deployment (default: 'gh-pages') |
| 85 | +- `trailingSlash`: Boolean or undefined for trailing slash handling |
| 86 | + |
| 87 | +### Internationalization |
| 88 | + |
| 89 | +```typescript |
| 90 | +i18n: { |
| 91 | + defaultLocale: 'en', |
| 92 | + locales: ['en', 'fr', 'es'], |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Themes Configuration |
| 97 | + |
| 98 | +### Using Presets (Recommended) |
| 99 | + |
| 100 | +```typescript |
| 101 | +presets: [ |
| 102 | + [ |
| 103 | + '@docusaurus/preset-classic', |
| 104 | + { |
| 105 | + docs: { |
| 106 | + sidebarPath: './sidebars.ts', |
| 107 | + editUrl: 'https://github.com/user/repo/tree/main/', |
| 108 | + }, |
| 109 | + blog: { |
| 110 | + showReadingTime: true, |
| 111 | + }, |
| 112 | + theme: { |
| 113 | + customCss: './src/css/custom.css', |
| 114 | + }, |
| 115 | + }, |
| 116 | + ], |
| 117 | +], |
| 118 | +``` |
| 119 | + |
| 120 | +### Direct Theme Configuration |
| 121 | + |
| 122 | +```typescript |
| 123 | +themes: ['@docusaurus/theme-classic'], |
| 124 | +themeConfig: { |
| 125 | + navbar: { |
| 126 | + title: 'My Site', |
| 127 | + logo: { |
| 128 | + alt: 'My Site Logo', |
| 129 | + src: 'img/logo.svg', |
| 130 | + }, |
| 131 | + items: [ |
| 132 | + {to: '/docs/intro', label: 'Docs', position: 'left'}, |
| 133 | + ], |
| 134 | + }, |
| 135 | + footer: { |
| 136 | + style: 'dark', |
| 137 | + links: [], |
| 138 | + copyright: `Copyright © ${new Date().getFullYear()}`, |
| 139 | + }, |
| 140 | +}, |
| 141 | +``` |
| 142 | + |
| 143 | +## Plugins |
| 144 | + |
| 145 | +### Adding Plugins |
| 146 | + |
| 147 | +String format (no options): |
| 148 | + |
| 149 | +```typescript |
| 150 | +plugins: ['@docusaurus/plugin-debug'], |
| 151 | +``` |
| 152 | + |
| 153 | +Array format (with options): |
| 154 | + |
| 155 | +```typescript |
| 156 | +plugins: [ |
| 157 | + [ |
| 158 | + '@docusaurus/plugin-content-docs', |
| 159 | + { |
| 160 | + id: 'community', |
| 161 | + path: 'community', |
| 162 | + routeBasePath: 'community', |
| 163 | + }, |
| 164 | + ], |
| 165 | +], |
| 166 | +``` |
| 167 | + |
| 168 | +### Shorthand Notation |
| 169 | + |
| 170 | +Official Docusaurus packages can use shorthand: |
| 171 | + |
| 172 | +- `'classic'` → `'@docusaurus/preset-classic'` |
| 173 | +- `'plugin-debug'` → `'@docusaurus/plugin-debug'` |
| 174 | + |
| 175 | +## Custom Fields |
| 176 | + |
| 177 | +Unknown fields cause validation errors. Use `customFields` for custom data: |
| 178 | + |
| 179 | +```typescript |
| 180 | +customFields: { |
| 181 | + apiKey: process.env.API_KEY, |
| 182 | + customValue: 'my-value', |
| 183 | + complexData: { |
| 184 | + nested: true, |
| 185 | + }, |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +Access in components: |
| 190 | + |
| 191 | +```typescript |
| 192 | +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
| 193 | + |
| 194 | +function MyComponent() { |
| 195 | + const {siteConfig} = useDocusaurusContext(); |
| 196 | + const apiKey = siteConfig.customFields.apiKey; |
| 197 | + |
| 198 | + return <div>{apiKey}</div>; |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## Validation Checklist |
| 203 | + |
| 204 | +When modifying config, verify: |
| 205 | + |
| 206 | +1. **Required fields present**: |
| 207 | + |
| 208 | + - ✅ `title` exists |
| 209 | + - ✅ `url` exists and has no trailing slash |
| 210 | + - ✅ `baseUrl` exists and starts/ends with `/` |
| 211 | + |
| 212 | +2. **Plugins and themes**: |
| 213 | + |
| 214 | + - ✅ Use proper package names or shorthand |
| 215 | + - ✅ Options passed as second array element |
| 216 | + - ✅ No duplicate plugins |
| 217 | + |
| 218 | +3. **Custom data**: |
| 219 | + |
| 220 | + - ✅ Unknown fields in `customFields` object |
| 221 | + - ✅ No direct custom properties at root level |
| 222 | + |
| 223 | +4. **File format**: |
| 224 | + |
| 225 | + - ✅ Valid JS/TS syntax |
| 226 | + - ✅ Proper export (ESM or CommonJS) |
| 227 | + - ✅ TypeScript types imported if using TS |
| 228 | + |
| 229 | +5. **Testing**: |
| 230 | + - ✅ Restart dev server after changes |
| 231 | + - ✅ Build succeeds: `npm run build` |
| 232 | + - ✅ No console errors |
| 233 | + |
| 234 | +## Common Patterns |
| 235 | + |
| 236 | +### Multi-Instance Docs |
| 237 | + |
| 238 | +```typescript |
| 239 | +plugins: [ |
| 240 | + [ |
| 241 | + '@docusaurus/plugin-content-docs', |
| 242 | + { |
| 243 | + id: 'product', |
| 244 | + path: 'product', |
| 245 | + routeBasePath: 'product', |
| 246 | + }, |
| 247 | + ], |
| 248 | + [ |
| 249 | + '@docusaurus/plugin-content-docs', |
| 250 | + { |
| 251 | + id: 'community', |
| 252 | + path: 'community', |
| 253 | + routeBasePath: 'community', |
| 254 | + }, |
| 255 | + ], |
| 256 | +], |
| 257 | +``` |
| 258 | + |
| 259 | +### Environment Variables |
| 260 | + |
| 261 | +```typescript |
| 262 | +const config: Config = { |
| 263 | + url: process.env.SITE_URL || "https://localhost:3000", |
| 264 | + customFields: { |
| 265 | + apiEndpoint: process.env.API_ENDPOINT, |
| 266 | + }, |
| 267 | +}; |
| 268 | +``` |
| 269 | + |
| 270 | +### Babel Customization |
| 271 | + |
| 272 | +Create `babel.config.js`: |
| 273 | + |
| 274 | +```javascript |
| 275 | +module.exports = { |
| 276 | + presets: [require.resolve("@docusaurus/babel/preset")], |
| 277 | + plugins: [ |
| 278 | + // Your custom Babel plugins |
| 279 | + ], |
| 280 | +}; |
| 281 | +``` |
| 282 | + |
| 283 | +## Troubleshooting |
| 284 | + |
| 285 | +### Common Errors |
| 286 | + |
| 287 | +**"url must not have a trailing slash"** |
| 288 | + |
| 289 | +- Fix: Change `url: 'https://example.com/'` to `url: 'https://example.com'` |
| 290 | + |
| 291 | +**"baseUrl must start and end with /"** |
| 292 | + |
| 293 | +- Fix: Change `baseUrl: 'docs'` to `baseUrl: '/docs/'` |
| 294 | + |
| 295 | +**"Unknown field 'myField'"** |
| 296 | + |
| 297 | +- Fix: Move to `customFields: { myField: 'value' }` |
| 298 | + |
| 299 | +**"Cannot find module '@docusaurus/types'"** |
| 300 | + |
| 301 | +- Fix: Run `npm install --save-dev @docusaurus/types` |
| 302 | + |
| 303 | +### Best Practices |
| 304 | + |
| 305 | +1. Use TypeScript for better autocomplete and type checking |
| 306 | +2. Always read existing config before modifying |
| 307 | +3. Preserve file format (don't convert ESM to CommonJS) |
| 308 | +4. Test locally before deploying |
| 309 | +5. Use environment variables for deployment-specific values |
| 310 | +6. Document custom fields with comments |
| 311 | +7. Keep config organized with clear sections |
| 312 | + |
| 313 | +## Additional Resources |
| 314 | + |
| 315 | +- [Official Configuration API](https://docusaurus.io/docs/api/docusaurus-config) |
| 316 | +- [Plugin Configuration](https://docusaurus.io/docs/using-plugins) |
| 317 | +- [Theme Configuration](https://docusaurus.io/docs/using-themes) |
| 318 | +- [Deployment Guide](https://docusaurus.io/docs/deployment) |
0 commit comments