Introduction
In this guide, you will learn how to add a comments system to your Next.js blog or website using Giscus. (If you prefer storing comments directly in your CMS without requiring readers to sign in with GitHub, check out my custom Next.js + Sanity commenting system .) By the end of this tutorial, you'll be able to:
Enable GitHub Discussions on your repository
Install and configure the Giscus app
Implement a comments component in your Next.js application
Handle light/dark mode theming
Troubleshoot common issues
Giscus is a comments system powered by GitHub Discussions, which means all comments are stored as GitHub Discussion comments. This approach has several advantages:
No database required : Comments are stored in GitHub
Free hosting : No additional costs for comment storage
Markdown support : Full markdown formatting in comments
GitHub authentication : Users sign in with their GitHub accounts
Moderation tools : Moderate comments directly through GitHub
Diagram
Prerequisites
A GitHub repository for your project
A Next.js application
Basic knowledge of React and TypeScript
Step 1: Enable GitHub Discussions
Before you can use Giscus, you need to enable GitHub Discussions on your repository:
Go to your GitHub repository
Click on the "Settings" tab
Scroll down to the "Features" section
Check the box next to "Discussions"
Click "Set up discussions"
Configure the discussion categories (make sure to have an "Announcements" category)
GitHub Discussions Setup
Step 2: Install the Giscus App
Next, you need to install the Giscus app on your repository:
Visit https://github.com/apps/giscus
Click on "Install" or "Configure"
Choose the repository where you want to enable comments
Complete the installation process
Step 3: Get Your Giscus Configuration
After installing the app, you need to get your configuration parameters:
Go to https://giscus.app/
Enter your repository name in the format username/repo
Choose the "Discussion Category" (typically "Announcements")
Choose "Page <-> Discussion Mapping" (typically "pathname")
Enable reactions if desired
Choose the theme that matches your site
Copy the generated configuration
You'll need the following values from the generated script:
data-repo
data-repo-id
data-category
data-category-id
data-mapping
Step 4: Install Giscus in Your Next.js Project
Install the Giscus React component:
npm install @giscus/react
yarn add @giscus/react
Create a new file called Comments.tsx in your components directory:
'use client'
import React , { useEffect, useState } from 'react'
import Giscus from '@giscus/react'
import { useTheme } from 'next-themes'
interface CommentsProps {
slug : string
}
export function Comments ({ slug }: CommentsProps ) {
const { resolvedTheme } = useTheme ()
const [mounted, setMounted] = useState (false )
const [theme, setTheme] = useState ('light' )
const [error, setError] = useState<string | null >(null )
useEffect (() => {
setMounted (true )
setTheme (resolvedTheme === 'dark' ? 'dark' : 'light' )
}, [resolvedTheme])
useEffect (() => {
const handleGiscusError = (event : MessageEvent ) => {
if (
event.origin === 'https://giscus.app' &&
event.data .giscus ?.error
) {
setError (event.data .giscus .error );
}
};
window .addEventListener ('message' , handleGiscusError);
return () => window .removeEventListener ('message' , handleGiscusError);
}, []);
if (!mounted) return null
return (
<div className ="mt-10 pt-10 border-t border-gray-200 dark:border-gray-800" >
<h2 className ="text-2xl font-bold mb-8" > Comments</h2 >
{error ? (
<div className ="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-md p-4 mb-6" >
<h3 className ="text-red-800 dark:text-red-300 font-medium mb-2" > Error loading comments</h3 >
<p className ="text-red-700 dark:text-red-400 text-sm" > {error}</p >
</div >
) : null}
<Giscus
id ="comments"
repo ="YOUR_USERNAME/YOUR_REPO"
repoId ="YOUR_REPO_ID"
category ="YOUR_CATEGORY"
categoryId ="YOUR_CATEGORY_ID"
mapping ="pathname"
term ={slug}
reactionsEnabled ="1"
emitMetadata ="1"
inputPosition ="top"
theme ={theme}
lang ="en"
loading ="lazy"
/>
</div >
)
}
Replace the placeholder values with your actual configuration from giscus.app.
Add the Comments component to your blog post or article page:
import { Comments } from '@/components/Comments'
export default function BlogPostPage ({ params } ) {
const { slug } = params
return (
<div >
<h1 > My Blog Post</h1 >
<article >
{/* Your blog post content */}
</article >
{/* Add the Comments component */}
<Comments slug ={slug} />
</div >
)
}
Step 7: Install next-themes for Dark Mode Support (Optional)
If you want to support dark mode, install next-themes:
npm install next-themes
yarn add next-themes
Then wrap your application with the ThemeProvider in your layout:
'use client'
import { ThemeProvider } from 'next-themes'
export default function RootLayout ({
children,
}: {
children: React.ReactNode
} ) {
return (
<html lang ="en" suppressHydrationWarning >
<body >
<ThemeProvider attribute ="class" >
{children}
</ThemeProvider >
</body >
</html >
)
}
Alternative Configuration: Using a Specific Discussion
If you want all comments to go to a single discussion instead of creating one per page:
Create a discussion in your GitHub repository
Note the discussion number (e.g., 1 for the first discussion)
Update your Giscus configuration:
<Giscus
id="comments"
repo="YOUR_USERNAME/YOUR_REPO"
repoId="YOUR_REPO_ID"
mapping="number"
term="1"
reactionsEnabled="1"
emitMetadata="1"
inputPosition="top"
theme={theme}
lang="en"
loading="lazy"
/>
Troubleshooting
"giscus is not installed on this repository"
This error occurs when the Giscus app is not installed on your repository. To fix it:
Go to https://github.com/apps/giscus
Click "Install" or "Configure"
Make sure your repository is selected
Complete the installation process
"Discussion not found"
This error occurs when Giscus can't find a matching discussion. This is normal when you first set up Giscus with pathname mapping. To fix it:
Sign in with GitHub in the comments section
Post a comment
Giscus will automatically create the discussion
Repository Visibility Issues
Giscus works best with public repositories. If your repository is private:
Make sure the Giscus app has access to your private repository
Consider using a different comments system for private repositories
Advanced Configuration
Custom Styling
You can customize the appearance of Giscus by wrapping it in a styled container:
<div className="my-custom-comments-container" >
<Giscus
// Your configuration
/>
</div>
Lazy Loading
Giscus supports lazy loading to improve page performance:
<Giscus
loading="lazy"
/>
Multiple Languages
Giscus supports multiple languages:
Conclusion
You now have a fully functional comments system powered by GitHub Discussions! Your users can:
Sign in with GitHub to leave comments
Use Markdown formatting
React with emojis
Engage in threaded discussions
All comments are stored in GitHub Discussions, making them easy to manage and moderate.
Resources