Skip to content

Commit 2bb4945

Browse files
committed
feat(docs): add static generation for faster page loads
- Add generateStaticParams to pre-build all doc pages at build time - Convert doc page to async server component - Move 'use client' directive to mdx-components where hooks are used
1 parent a310c7e commit 2bb4945

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

web/src/app/docs/[category]/[slug]/page.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use client'
2-
31
import { env } from '@codebuff/common/env'
42
import dynamic from 'next/dynamic'
53
import NextLink from 'next/link'
6-
import { notFound, useParams } from 'next/navigation'
4+
import { notFound } from 'next/navigation'
75
import React from 'react'
86

97
import type { Doc } from '@/types/docs'
@@ -12,6 +10,16 @@ import { Mdx } from '@/components/docs/mdx/mdx-components'
1210
import { getDocsByCategory } from '@/lib/docs'
1311
import { allDocs } from '.contentlayer/generated'
1412

13+
// Generate static params for all doc pages at build time
14+
export function generateStaticParams(): Array<{ category: string; slug: string }> {
15+
return allDocs
16+
.filter((doc) => !doc.slug.startsWith('_'))
17+
.map((doc) => ({
18+
category: doc.category,
19+
slug: doc.slug,
20+
}))
21+
}
22+
1523
// FAQ structured data for SEO - parsed from the FAQ MDX content
1624
const FAQ_ITEMS = [
1725
{
@@ -192,10 +200,8 @@ const DocNavigation = ({
192200
)
193201
}
194202

195-
export default function DocPage() {
196-
const params = useParams<{ category: string; slug: string }>()
197-
const category = params?.category ?? ''
198-
const slug = params?.slug ?? ''
203+
export default async function DocPage({ params }: { params: Promise<{ category: string; slug: string }> }) {
204+
const { category, slug } = await params
199205
const docs = getDocsByCategory(category)
200206
const doc = docs.find((d: Doc) => d.slug === slug)
201207

web/src/app/docs/[category]/page.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { redirect } from 'next/navigation'
22

33
import { getDocsByCategory } from '@/lib/docs'
4+
import { allDocs } from '.contentlayer/generated'
5+
6+
// Generate static params for all category pages at build time
7+
export function generateStaticParams(): Array<{ category: string }> {
8+
const categories = new Set(
9+
allDocs
10+
.filter((doc) => !doc.slug.startsWith('_'))
11+
.map((doc) => doc.category)
12+
)
13+
return Array.from(categories).map((category) => ({ category }))
14+
}
415

516
interface CategoryPageProps {
617
params: Promise<{ category: string }>

web/src/components/docs/mdx/mdx-components.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client'
2+
13
import { Check, Link } from 'lucide-react'
24
import Image from 'next/image'
35
import { useMDXComponent } from 'next-contentlayer2/hooks'

0 commit comments

Comments
 (0)