2️⃣ Implementing Algolia Search in Docusaurus
Algolia DocSearch is a powerful search solution that Docusaurus officially supports. Follow this tutorial to integrate Algolia DocSearch into your Docusaurus site.
Step 1: Apply for Algolia DocSearch
- Visit the DocSearch application page and apply for the program.
- Ensure your website is developer documentation or a technical blog, as the service is free for these types of content.
- Once approved, you will receive the following credentials:
- Application ID (
appId
) - Public API Key (
apiKey
) - Index Name
- Application ID (
Step 2: Configure Algolia in Docusaurus
If you are using @docusaurus/preset-classic
, Algolia integration is built-in. Simply add the following to your docusaurus.config.js
file:
export default {
// Other configurations...
themeConfig: {
algolia: {
appId: 'YOUR_APP_ID', // Provided by Algolia
apiKey: 'YOUR_SEARCH_API_KEY', // Public API Key (safe to commit)
indexName: 'YOUR_INDEX_NAME', // Name of your Algolia index
// Optional settings
contextualSearch: true, // Enables contextual search (default)
searchPagePath: 'search', // Adds a full search page at /search
},
},
};
If Not Using the Classic Preset
If your project doesn’t use @docusaurus/preset-classic
, you need to install the required package manually:
npm install @docusaurus/theme-search-algolia
Then, add the @docusaurus/theme-search-algolia
to your presets
or themes
configuration in docusaurus.config.js
.
Once your site is deployed and the sitemap.xml
is accessible, Algolia will automatically trigger the first crawl.
Step 3: Styling Your Search
You can customize the DocSearch UI by modifying the /src/css/custom.css
file. For example:
[data-theme='light'] .DocSearch {
--docsearch-modal-background: var(--ifm-color-secondary-lighter);
}
[data-theme='dark'] .DocSearch {
--docsearch-modal-background: var(--ifm-background-color);
}
Options
Contextual Search
Contextual search is enabled by default, ensuring search results are filtered by language and version. To disable it, update your configuration:
export default {
themeConfig: {
algolia: {
contextualSearch: false, // Disable contextual search
searchParameters: {
facetFilters: ['language:en', ['filter1', 'filter2'], 'filter3'],
},
},
},
};
Search Page
Enable a dedicated search page by setting searchPagePath in your configuration:
export default {
themeConfig: {
algolia: {
searchPagePath: 'search',
},
},
};
This will create a full search experience at /search
.
Troubleshooting
If no search results appear:
- Force a new crawl using the Algolia dashboard.
- Check the Algolia dashboard for index issues.
- Ensure your site’s content is crawlable via the sitemap.xml.
- Use the recommended Algolia crawler configuration.
For additional help, refer to the Algolia support page.
Congratulations! Your Docusaurus site now has a robust search functionality powered by Algolia DocSearch.