Skip to content

Commit

Permalink
Add OpenGraph and other metadata to blog posts [#134]
Browse files Browse the repository at this point in the history
  • Loading branch information
genehack committed Nov 8, 2024
1 parent 881b56a commit 9e5c3bb
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions static-site/app/blog/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Metadata } from "next";
import { redirect } from "next/navigation";
import React from "react";

import { BigSpacer } from "../../../components/spacers";
import {
siteLogo,
siteTitle,
siteTitleAlt,
siteUrl,
} from "../../../data/BaseConfig";

import { getBlogPosts, markdownToHtml } from "../utils";

Expand All @@ -20,6 +27,48 @@ export function generateStaticParams(): BlogPostParams[] {
});
}

// generate opengraph and other metadata tags
export async function generateMetadata({
params,
}: {
params: BlogPostParams;
}): Promise<Metadata> {
const { id } = params;

// set up some defaults that are independent of the specific blog post
const baseUrl = new URL(siteUrl);
const metadata: Metadata = {
metadataBase: baseUrl,
openGraph: {
description: siteTitleAlt,
images: [
{
url: `${siteUrl}${siteLogo}`,
},
],
siteName: siteTitle,
title: siteTitle,
type: "website",
url: baseUrl,
},
};

// this is the specific post we're rendering
const blogPost = getBlogPosts().find((post) => post.blogUrlName === id);

if (blogPost) {
const description = `Nextstrain blog post from ${blogPost.date}; author(s): ${blogPost.author}`;

metadata.title = blogPost.title;
metadata.description = description;
metadata.openGraph!.description = description;
metadata.openGraph!.title = `${siteTitle}: ${blogPost.title}`;
metadata.openGraph!.url = `/blog/${blogPost.blogUrlName}`;
}

return metadata;
}

export default async function BlogPost({
params,
}: {
Expand Down

0 comments on commit 9e5c3bb

Please sign in to comment.