π Introduction
Docusaurus is a modern static site generator that's perfect for creating documentation websites. Hosting your Docusaurus site on GitHub Pages is a popular choice because itβs free, easy to set up, and integrates directly with your GitHub repository.
This article will guide you through:
- Creating a Docusaurus project
- Building your documentation site
- Deploying it seamlessly to GitHub Pages
β Prerequisites
Before we start, ensure you have:
- Node.js (version 16.14+ recommended)
- Git
- A GitHub account
- Basic terminal knowledge
π οΈ Step 1: Create a New Docusaurus Project
Run the following command to scaffold a new Docusaurus project:
npx create-docusaurus@latest my-website classic
-
my-website
is your project folder name. -
classic
is the template (the most commonly used one).
Move into your project directory:
cd my-website
Test the site locally:
npm run start
βοΈ Step 2: Configure GitHub Pages Deployment
- Initialize a Git repository (if not already done):
git init
- Push your project to GitHub:
git remote add origin https://github.com/yourusername/your-repo-name.git
git branch -M main
git add .
git commit -m "Initial commit"
git push -u origin main
- Install GitHub Pages Package:
npm install --save gh-pages
-
Update
docusaurus.config.js
:
module.exports = {
// Your existing config...
url: 'https://yourusername.github.io', // Your GitHub Pages URL
baseUrl: '/your-repo-name/', // The name of your GitHub repo
// If deploying to https://<USERNAME>.github.io/, set baseUrl to '/'
// If deploying to a custom domain, set baseUrl to '/' and set the 'cname' file in static folder
};
βοΈ Step 3: Add Deployment Scripts
In your package.json
, add the following scripts:
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
ποΈ Step 4: Build the Project
Run:
npm run build
This will generate a build
folder that contains your static site.
π Step 5: Deploy to GitHub Pages
Run:
npm run deploy
This will:
- Build the project
- Push the
build
folder to thegh-pages
branch automatically
π Step 6: Configure GitHub Pages
- Go to your GitHub repository.
- Click on Settings > Pages.
- Under Source, select the
gh-pages
branch and the/ (root)
directory. - Click Save.
Your site will typically be live at:
https://yourusername.github.io/your-repo-name/
π οΈ Optional: Custom Domain Setup
If you want to use a custom domain:
- Add your domain in
docusaurus.config.js
:
url: 'https://yourdomain.com',
baseUrl: '/',
- Create a file named
CNAME
in thestatic
folder with the content:
yourdomain.com
- Set up your DNS to point to GitHub Pages IPs.
π Troubleshooting
-
404 Errors?
- Ensure your
baseUrl
indocusaurus.config.js
is correctly set.
- Ensure your
-
Build Errors?
- Run
npm install
to make sure all dependencies are installed.
- Run
-
Deployment Not Updating?
- Try deleting the local
node_modules
and reinstalling:
rm -rf node_modules npm install
- Try deleting the local
π― Summary Checklist
Task | Status |
---|---|
Create Docusaurus project | β |
Push to GitHub | β |
Configure docusaurus.config.js
|
β |
Add deploy scripts | β |
Build the project | β |
Deploy to GitHub Pages | β |
Set GitHub Pages source | β |
Top comments (0)