Add search to Docusaurus and deploy to GitHub Pages
7 minute read
The goal is to add the full-text search to your Docusaurus site. Use these instructions to add lunr.js Docusaurus search plugin to your site. Deploy your Docusaurus site to GitHub Pages to see the search functionality working.
What’s Lunr.js
The lunr.js search plugin for the Docusaurus site is a node.js package. Use it to add the full-text search functionality to your Docusaurus site.
Prerequisites
To add the lunr.js search plugin, you must have installed:
-
Node.js. Run
node --version
in your Command Prompt to see if it’s installed. If you don’t see the version, download the installer here: https://nodejs.org/en. -
Docusaurus. Run
npx docusaurus --version
in the folder with your Docusaurus project. If you don’t see the version of Docusaurus, install it using these instructions.
How to install lunr.js
To install lunr.js search for your Docusaurus site:
-
Run
npm i docusaurus-lunr-search --save
in your Docusaurus site folder. For example, when you open Command Prompt, you have this path:C:\Users\Ivan_Cheban>
whereIvan Cheban
is your user name. To go to the folder where your Docusaurus is installed:cd test-website
wheretest-website
is the folder with your Docusaurus project.
-
Add the docusaurus-lunr-search plugin to your
docusaurus.config.js
file:Your
docusaurus.config.js
file will look like this:
Note
Remember that your Lunr.js search works only in production. It means that you can’t check the search locally, you need to deploy your website, for example to GitLab Pages or Netlify to see how it works. To deploy your site to GitLab Pages, read this article.Deploy your site to GitHub Pages
To deploy your Docusaurus site online using GitHub Pages:
-
In Visual Studio Code, open the Docusaurus project folder.
-
Go to the Source Control tab.
-
Click Publish to GitHub.
-
Select Publish to GitHub public repository.
-
Click Open on GitHub to open the browser GitHub page with the created repository.
-
In your
docusaurus.config.js
file, change the values for the following fields:-
url
:https://ivancheban.github.io
-
baseUrl
:/test-website/
-
organizationName
:ivancheban
-
projectName
:test-website
Your
docusaurus.config.js
file would look like this: -
-
Create a folder and file in the root of your Docusaurus project folder:
.github/workflows/deploy.yml
: -
Add a new branch called
gh-pages
. -
In your GitHub website, go to Settings > Pages and select a branch from which you want to deploy your Docusaurus site. Type:
gh-pages
. Click Save. -
Commit and push your changes to the remote repository.
-
Go to Actions in your web repository.
You should see the pipeline and the site being built.
You can check the settings in the repository and the site here:
-
repository: https://github.com/ivancheban/test-website
-
site: https://ivancheban.github.io/test-website/
Your Docusaurus site text is indexed and full-text search works.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.