Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import Button from "@/components/ui/Button";
import Skeleton from "@/components/ui/Skeleton";
import { getTOC, headingComponents, remarkCustomAlerts } from "@/lib/markdown";
import { metadata as siteMetadata } from "@/lib/metadata";
import staticData from "@/lib/staticdata";
import type { Metadata } from "next";
import Image from "next/image";
import Link from "next/link";
import { notFound } from "next/navigation";
Expand Down Expand Up @@ -237,6 +239,58 @@ async function BlogPageContent({ slug }: { slug: string }) {
);
}

export async function generateStaticParams() {
return (await getBlogs()).map((blog) => ({ slug: blog.slug }));
}

export async function generateMetadata({
params,
}: {
params: Promise<{ slug: string }>;
}): Promise<Metadata> {
const { slug } = await params;
const blog = await getBlogBySlug(slug);

if (!blog) return {};

const { title, description, date, authors, tags } = blog.metadata;
const parsed = date ? new Date(date) : undefined;
// front matter dates have no timezone, keep the calendar day in UTC
const published =
parsed && !isNaN(parsed.getTime())
? new Date(
Date.UTC(parsed.getFullYear(), parsed.getMonth(), parsed.getDate()),
).toISOString()
: undefined;
const url = `/blog/${slug}`;

return {
title,
description,
authors: authors?.map((name) => ({ name })),
keywords: tags,
alternates: { canonical: url },
// openGraph and twitter replace the root ones, so keep the site defaults
openGraph: {
siteName: siteMetadata.openGraph?.siteName,
locale: siteMetadata.openGraph?.locale,
images: siteMetadata.openGraph?.images,
type: "article",
url,
title,
description,
publishedTime: published,
authors,
tags,
},
twitter: {
...siteMetadata.twitter,
title,
description,
},
};
}

export default async function Blog({
params,
}: {
Expand All @@ -246,6 +300,9 @@ export default async function Blog({
}) {
const { slug } = await params;

// notFound() inside the suspense boundary would still answer with a 200
if (!(await getBlogBySlug(slug))) notFound();

return (
<div className="w-full flex justify-center py-md">
<div className="max-w-6xl w-full flex justify-center flex-col sm:flex-row gap-md px-lg">
Expand Down
41 changes: 41 additions & 0 deletions app/pages/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Button from "@/components/ui/Button";
import ShinyText from "@/components/ui/ShinyText";
import Skeleton from "@/components/ui/Skeleton";
import { headingComponents, remarkCustomAlerts } from "@/lib/markdown";
import { metadata as siteMetadata } from "@/lib/metadata";
import staticData from "@/lib/staticdata";
import type { Metadata } from "next";
import Link from "next/link";
import { notFound } from "next/navigation";
import { Suspense } from "react";
Expand Down Expand Up @@ -152,6 +154,42 @@ function PageSkeleton() {
);
}

export async function generateStaticParams() {
return (await getPages()).map((page) => ({ slug: page.slug }));
}

export async function generateMetadata({
params,
}: {
params: Promise<{ slug: string }>;
}): Promise<Metadata> {
const { slug } = await params;
const page = await getPageBySlug(slug);

if (!page) return {};

const { title, description } = page.metadata;
const url = `/pages/${slug}`;

return {
title,
description,
alternates: { canonical: url },
// openGraph and twitter replace the root ones, so keep the site defaults
openGraph: {
...siteMetadata.openGraph,
url,
title,
description,
},
twitter: {
...siteMetadata.twitter,
title,
description,
},
};
}

export default async function Page({
params,
}: {
Expand All @@ -161,6 +199,9 @@ export default async function Page({
}) {
const { slug } = await params;

// notFound() inside the suspense boundary would still answer with a 200
if (!(await getPageBySlug(slug))) notFound();

return (
<div className="w-full flex justify-center py-md">
<div className="max-w-6xl w-full flex justify-center flex-col sm:flex-row gap-md px-lg">
Expand Down