🪛 Clean up and fix og-image code (#820)

This commit is contained in:
Luke Vella 2023-08-18 12:32:12 +01:00 committed by GitHub
parent e7a69ffe1d
commit 6ff907004c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 25 deletions

View file

@ -23,7 +23,6 @@ export default async function handler(req: NextRequest) {
const { searchParams } = req.nextUrl; const { searchParams } = req.nextUrl;
const title = searchParams.get("title"); const title = searchParams.get("title");
const excerpt = searchParams.get("excerpt"); const excerpt = searchParams.get("excerpt");
const type = searchParams.get("type");
return new ImageResponse( return new ImageResponse(
( (
@ -47,7 +46,7 @@ export default async function handler(req: NextRequest) {
height={64} height={64}
/> />
<div tw="flex text-gray-800 text-3xl tracking-tight font-bold"> <div tw="flex text-gray-800 text-3xl tracking-tight font-bold">
<span tw="bg-gray-200 px-6 py-3 rounded-full">{type}</span> <span tw="bg-gray-200 px-6 py-3 rounded-full">Blog</span>
</div> </div>
</div> </div>
</div> </div>

View file

@ -36,19 +36,13 @@ const Page: NextPageWithLayout<Props> = ({ post }) => {
openGraph={{ openGraph={{
title: post.title, title: post.title,
description: post.excerpt, description: post.excerpt,
url: absoluteUrl(`/blog/${post.slug}`),
images: [ images: [
{ {
url: url: absoluteUrl("/api/og-image", {
`${absoluteUrl()}/_next/image?w=1200&q=100&url=${encodeURIComponent( title: post.title,
`/api/og-image`, excerpt: post.excerpt,
)}` + }),
encodeURIComponent(
`?type=${encodeURIComponent(
"Blog",
)}&title=${encodeURIComponent(
post.title,
)}&excerpt=${encodeURIComponent(post.excerpt)}`,
),
width: 1200, width: 1200,
height: 630, height: 630,
alt: post.title, alt: post.title,

View file

@ -86,11 +86,12 @@ const GoToApp = () => {
}; };
type PageProps = { type PageProps = {
id: string;
title: string; title: string;
user: string | null; user: string | null;
}; };
const Page = ({ title, user }: PageProps) => { const Page = ({ id, title, user }: PageProps) => {
const { t } = useTranslation(); const { t } = useTranslation();
const name = user ?? t("guest"); const name = user ?? t("guest");
return ( return (
@ -99,17 +100,13 @@ const Page = ({ title, user }: PageProps) => {
openGraph={{ openGraph={{
title, title,
description: `By ${name}`, description: `By ${name}`,
url: absoluteUrl(`/invite/${id}`),
images: [ images: [
{ {
url: url: `${absoluteUrl("/api/og-image-poll", {
`${absoluteUrl()}/_next/image?w=1200&q=100&url=${encodeURIComponent(
`/api/og-image-poll`,
)}` +
encodeURIComponent(
`?title=${encodeURIComponent(
title, title,
)}&author=${encodeURIComponent(name)}`, author: name,
), })}`,
width: 1200, width: 1200,
height: 630, height: 630,
alt: title, alt: title,
@ -192,6 +189,7 @@ export const getStaticProps: GetStaticProps = async (ctx) => {
id: ctx.params?.urlId as string, id: ctx.params?.urlId as string,
}, },
select: { select: {
id: true,
title: true, title: true,
user: { user: {
select: { select: {
@ -207,6 +205,7 @@ export const getStaticProps: GetStaticProps = async (ctx) => {
return { return {
props: { props: {
...res.props, ...res.props,
id: poll.id,
title: poll.title, title: poll.title,
user: poll.user?.name ?? null, user: poll.user?.name ?? null,
}, },

View file

@ -15,13 +15,18 @@ function joinPath(baseUrl: string, subpath = "") {
return baseUrl; return baseUrl;
} }
export function absoluteUrl(subpath = "") { export function absoluteUrl(subpath = "", query?: Record<string, string>) {
const queryString = query
? `?${Object.entries(query)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join("&")}`
: "";
const baseUrl = const baseUrl =
process.env.NEXT_PUBLIC_BASE_URL ?? process.env.NEXT_PUBLIC_BASE_URL ??
getVercelUrl() ?? getVercelUrl() ??
`http://localhost:${port}`; `http://localhost:${port}`;
return joinPath(baseUrl, subpath); return joinPath(baseUrl, subpath) + queryString;
} }
export function shortUrl(subpath = "") { export function shortUrl(subpath = "") {