|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + getAllBlogPosts, |
| 5 | + getBlogPostBySlug, |
| 6 | + getLatestPostsForNav, |
| 7 | + getPaginatedPosts, |
| 8 | + getPostsPerPage, |
| 9 | +} from "@/lib/blog"; |
| 10 | +import { compileMdxContent, extractHeadings } from "@/lib/mdx"; |
| 11 | +import { createMetadata } from "@/lib/metadata"; |
| 12 | +import { BlogPostPage } from "@/lib/blog-post-page"; |
| 13 | +import { BlogListPage } from "@/lib/blog-list-page"; |
| 14 | +import { notFound } from "next/navigation"; |
| 15 | + |
| 16 | +interface PageProps { |
| 17 | + params: Promise<{ slug: string[] }>; |
| 18 | +} |
| 19 | + |
| 20 | +export async function generateStaticParams() { |
| 21 | + const posts = getAllBlogPosts(); |
| 22 | + const postsPerPage = getPostsPerPage(); |
| 23 | + const totalPages = Math.ceil(posts.length / postsPerPage); |
| 24 | + |
| 25 | + const params: { slug: string[] }[] = []; |
| 26 | + |
| 27 | + // Pagination pages (page 2+) |
| 28 | + for (let i = 2; i <= totalPages; i++) { |
| 29 | + params.push({ slug: [String(i)] }); |
| 30 | + } |
| 31 | + |
| 32 | + // Blog post pages (year/month/day/slug) |
| 33 | + for (const post of posts) { |
| 34 | + const parts = post.slug.replace(/^\/blog\//, "").split("/"); |
| 35 | + params.push({ slug: parts }); |
| 36 | + } |
| 37 | + |
| 38 | + return params; |
| 39 | +} |
| 40 | + |
| 41 | +export async function generateMetadata({ params }: PageProps) { |
| 42 | + const { slug } = await params; |
| 43 | + |
| 44 | + // Pagination page |
| 45 | + if (slug.length === 1 && /^\d+$/.test(slug[0])) { |
| 46 | + return createMetadata({ title: `Blog - Page ${slug[0]}` }); |
| 47 | + } |
| 48 | + |
| 49 | + // Blog post (year/month/day/slug) |
| 50 | + if (slug.length === 4) { |
| 51 | + const postSlug = `/blog/${slug.join("/")}`; |
| 52 | + const post = getBlogPostBySlug(postSlug); |
| 53 | + |
| 54 | + return createMetadata({ |
| 55 | + title: post?.title || "Blog Post", |
| 56 | + description: post?.description, |
| 57 | + isArticle: true, |
| 58 | + imageUrl: post?.featuredImage, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + return createMetadata({ title: "Blog" }); |
| 63 | +} |
| 64 | + |
| 65 | +export default async function BlogCatchAllPage({ params }: PageProps) { |
| 66 | + const { slug } = await params; |
| 67 | + |
| 68 | + // Pagination page (e.g., /blog/2, /blog/3) |
| 69 | + if (slug.length === 1 && /^\d+$/.test(slug[0])) { |
| 70 | + const pageNum = parseInt(slug[0], 10); |
| 71 | + const { posts, totalPages } = getPaginatedPosts(pageNum); |
| 72 | + |
| 73 | + return ( |
| 74 | + <BlogListPage |
| 75 | + posts={posts} |
| 76 | + currentPage={pageNum} |
| 77 | + totalPages={totalPages} |
| 78 | + linkPrefix="/blog" |
| 79 | + /> |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + // Blog post page (e.g., /blog/2024/01/15/my-post) |
| 84 | + if (slug.length === 4) { |
| 85 | + const postSlug = `/blog/${slug.join("/")}`; |
| 86 | + const post = getBlogPostBySlug(postSlug); |
| 87 | + |
| 88 | + if (!post) { |
| 89 | + return notFound(); |
| 90 | + } |
| 91 | + |
| 92 | + const { mdxSource } = await compileMdxContent(post.content); |
| 93 | + const headings = extractHeadings(post.content); |
| 94 | + const latestPosts = getLatestPostsForNav(); |
| 95 | + |
| 96 | + return ( |
| 97 | + <BlogPostPage |
| 98 | + post={post} |
| 99 | + mdxSource={mdxSource} |
| 100 | + headings={headings} |
| 101 | + latestPosts={latestPosts} |
| 102 | + /> |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return notFound(); |
| 107 | +} |
0 commit comments