Skip to content

Commit 8a1ceaa

Browse files
committed
og-image fix 2
1 parent c104db9 commit 8a1ceaa

File tree

7 files changed

+54
-154
lines changed

7 files changed

+54
-154
lines changed

src/app/(rest)/blog/[slug]/opengraph-image.tsx

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getBlogSocialImageResponse } from '../social-image'
2+
3+
export const runtime = 'nodejs'
4+
export const revalidate = 86400
5+
6+
type Params = {
7+
params: Promise<{ slug: string }>
8+
}
9+
10+
export async function GET(_request: Request, { params }: Params) {
11+
const { slug } = await params
12+
return getBlogSocialImageResponse(slug)
13+
}

src/app/(rest)/blog/[slug]/page.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,25 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
2424
const { slug } = await params
2525

2626
const { metadata } = await getPost(slug)
27-
28-
return createMetadata({
27+
const socialImageAlt = metadata.title
28+
const socialMetadataPath = `/blog/${slug}`
29+
const baseMetadata = createMetadata({
2930
description: metadata.description,
30-
pathname: `/blog/${slug}`,
31+
pathname: socialMetadataPath,
3132
title: `${metadata.title} | ${META.title}`
3233
})
34+
35+
return {
36+
...baseMetadata,
37+
openGraph: {
38+
...(baseMetadata.openGraph ?? {}),
39+
images: [{ alt: socialImageAlt, url: `${socialMetadataPath}/opengraph-image` }]
40+
},
41+
twitter: {
42+
...(baseMetadata.twitter ?? {}),
43+
images: [{ alt: socialImageAlt, url: `${socialMetadataPath}/twitter-image` }]
44+
}
45+
}
3346
}
3447

3548
export default async function Page({ params }: Props) {

src/app/(rest)/blog/[slug]/twitter-image.tsx renamed to src/app/(rest)/blog/[slug]/social-image.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import { readFile } from 'node:fs/promises'
22
import path from 'node:path'
33
import { ImageResponse } from 'next/og'
4-
import { getPost, getPostSlugs } from '~/lib/utils/posts'
4+
import { getPost } from '~/lib/utils/posts'
55
import { Rubric } from '~/ui/logos/rubric'
66

7-
export const runtime = 'nodejs'
8-
export const revalidate = 86400
9-
export const contentType = 'image/png'
10-
export const size = {
7+
export const BLOG_SOCIAL_IMAGE_ALT =
8+
'Applied AI lab helping companies build intelligent applications'
9+
export const BLOG_SOCIAL_IMAGE_SIZE = {
1110
height: 630,
1211
width: 1200
1312
}
1413

1514
const fontDataPromise = readFile(path.join(process.cwd(), 'src/app/fonts/matter-regular.woff'))
1615
const bannerSrcCache = new Map<string, string>()
1716

18-
export async function generateStaticParams() {
19-
const slugs = await getPostSlugs()
20-
return slugs.map(slug => ({ slug }))
21-
}
22-
23-
export const Component = ({
17+
const BlogSocialImage = ({
2418
title,
2519
backgroundImageUrl
2620
}: {
@@ -113,14 +107,12 @@ const getBannerSrc = async (bannerImageUrl: string) => {
113107
return bannerSrc
114108
}
115109

116-
export default async function Response({ params }: { params: Promise<{ slug: string }> }) {
117-
const { slug } = await params
118-
110+
export const getBlogSocialImageResponse = async (slug: string) => {
119111
const [{ metadata }, localFont] = await Promise.all([getPost(slug), fontDataPromise])
120112
const bannerSrc = await getBannerSrc(metadata.bannerImageUrl)
121113

122-
return new ImageResponse(<Component title={metadata.title} backgroundImageUrl={bannerSrc} />, {
123-
...size,
114+
return new ImageResponse(<BlogSocialImage title={metadata.title} backgroundImageUrl={bannerSrc} />, {
115+
...BLOG_SOCIAL_IMAGE_SIZE,
124116
fonts: [
125117
{
126118
data: localFont,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getBlogSocialImageResponse } from '../social-image'
2+
3+
export const runtime = 'nodejs'
4+
export const revalidate = 86400
5+
6+
type Params = {
7+
params: Promise<{ slug: string }>
8+
}
9+
10+
export async function GET(_request: Request, { params }: Params) {
11+
const { slug } = await params
12+
return getBlogSocialImageResponse(slug)
13+
}

src/lib/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ export const env = createEnv({
1313
ROS_API_URL: process.env.ROS_API_URL,
1414
ROS_SECRET: process.env.ROS_SECRET,
1515
URL: process.env.URL,
16+
VERCEL_PROJECT_PRODUCTION_URL: process.env.VERCEL_PROJECT_PRODUCTION_URL,
1617
VERCEL_URL: process.env.VERCEL_URL
1718
},
1819
server: {
1920
NODE_ENV: z.string().min(1),
2021
ROS_API_URL: z.string().min(1),
2122
ROS_SECRET: z.string().min(1),
2223
URL: z.string().min(1),
24+
VERCEL_PROJECT_PRODUCTION_URL: z.string().min(1).optional(),
2325
VERCEL_URL: z.string().min(1).optional()
2426
}
2527
})

src/lib/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { env } from '~/lib/env'
22

33
export const getBaseUrl = () => {
4-
const host = env.VERCEL_URL ?? env.URL
4+
const host = env.URL || env.VERCEL_PROJECT_PRODUCTION_URL || env.VERCEL_URL
5+
if (!host) throw new Error('Missing URL configuration')
56
const protocol = host.includes('localhost') ? 'http' : 'https'
67
return `${protocol}://${host}`
78
}

0 commit comments

Comments
 (0)