Nextra Giscus Comments Integration
This prompt helps you quickly integrate Giscus comment system into your Nextra blog.
Quick Implementation Steps
1. Install Dependencies
bun add @giscus/react2. Create Comments Component src/components/GiscusComments/index.tsx
'use client'
import Giscus from '@giscus/react'
import { useTheme } from 'next-themes'
interface GiscusCommentsProps {
lang: string
}
export function GiscusComments({ lang }: GiscusCommentsProps) {
const { resolvedTheme } = useTheme()
return (
<div className="mt-8">
<Giscus
id="comments"
repo="your-username/your-repo"
repoId="YOUR_REPO_ID"
category="General"
categoryId="YOUR_CATEGORY_ID"
mapping="og:title"
strict="0"
reactionsEnabled="1"
emitMetadata="0"
inputPosition="bottom"
theme={resolvedTheme === 'dark' ? 'dark' : 'light'}
lang={lang === 'zh' ? 'zh-CN' : 'en'}
loading="lazy"
/>
</div>
)
}3. Modify src/app/[lang]/[[...mdxPath]]/page.tsx
import { generateStaticParamsFor, importPage } from 'nextra/pages'
import { useMDXComponents } from '@/mdx-components'
import { GiscusComments } from '@/components/GiscusComments'
export const generateStaticParams = generateStaticParamsFor('mdxPath')
export async function generateMetadata(props: PageProps) {
const params = await props.params
const { metadata } = await importPage(params.mdxPath, params.lang)
return metadata
}
type PageProps = Readonly<{
params: Promise<{
mdxPath: string[]
lang: string
}>
}>
const Wrapper = useMDXComponents().wrapper
export default async function Page(props: PageProps) {
const params = await props.params
const result = await importPage(params.mdxPath, params.lang)
const { default: MDXContent, toc, metadata, sourceCode } = result
const isDocsPage = params.mdxPath && params.mdxPath.length > 0
return (
<Wrapper
toc={toc}
metadata={metadata}
sourceCode={sourceCode}
bottomContent={isDocsPage ? <GiscusComments lang={params.lang} /> : undefined}
>
<MDXContent {...props} params={params} />
</Wrapper>
)
}Key Points
- Use
bottomContentparameter: Use Nextra’swrappercomponent’sbottomContentparameter to render comments below the pagination navigation - Auto theme switching: Use
next-themes’suseThemeto implement automatic dark mode switching - Multi-language support: Automatically switch comment language based on page language
- Conditional rendering: Only show comments on documentation pages, not on homepage
Configuration
Visit https://giscus.app to configure and obtain your:
repo: GitHub repository pathrepoId: Repository IDcategory: Discussion categorycategoryId: Category ID
Last updated on: