import type { InferGetStaticPropsType } from "next"; import { useRouter } from "next/router"; import ErrorPage from "next/error"; import Comment from "../../components/comment"; import Container from "../../components/container"; import distanceToNow from "../../lib/dateRelative"; import { getAllPosts, getPostBySlug } from "../../lib/getPost"; import markdownToHtml from "../../lib/markdownToHtml"; import Head from "next/head"; export default function PostPage({ post, }: InferGetStaticPropsType) { const router = useRouter(); if (!router.isFallback && !post?.slug) { return ; } return ( {post.title} | My awesome blog {router.isFallback ? (
Loading…
) : (

{post.title}

{post.excerpt ? (

{post.excerpt}

) : null}
)}
); } type Params = { params: { slug: string; }; }; export async function getStaticProps({ params }: Params) { const post = getPostBySlug(params.slug, [ "slug", "title", "excerpt", "date", "content", ]); const content = await markdownToHtml(post.content || ""); return { props: { post: { ...post, content, }, }, }; } export async function getStaticPaths() { const posts = getAllPosts(["slug"]); return { paths: posts.map(({ slug }) => { return { params: { slug, }, }; }), fallback: false, }; }