diff --git a/app/[page]/opengraph-image.tsx b/app/[page]/opengraph-image.tsx new file mode 100644 index 000000000..2fd59281e --- /dev/null +++ b/app/[page]/opengraph-image.tsx @@ -0,0 +1,11 @@ +import OpengraphImage from 'components/opengraph-image'; +import { getPage } from 'lib/shopify'; + +export const runtime = 'edge'; + +export default async function Image({ params }: { params: { page: string } }) { + const page = await getPage(params.page); + const title = page.seo?.title || page.title; + + return await OpengraphImage({ title }); +} diff --git a/app/api/og/Inter-Regular.ttf b/app/api/og/Inter-Regular.ttf deleted file mode 100644 index 8d4eebf20..000000000 Binary files a/app/api/og/Inter-Regular.ttf and /dev/null differ diff --git a/app/api/og/route.tsx b/app/api/og/route.tsx deleted file mode 100644 index e1d70e35f..000000000 --- a/app/api/og/route.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import { ImageResponse } from '@vercel/og'; -import { NextRequest } from 'next/server'; - -export const runtime = 'edge'; - -const interRegular = fetch(new URL('./Inter-Regular.ttf', import.meta.url)).then((res) => - res.arrayBuffer() -); - -const interBold = fetch(new URL('./Inter-Bold.ttf', import.meta.url)).then((res) => - res.arrayBuffer() -); - -export async function GET(req: NextRequest): Promise { - try { - const [regularFont, boldFont] = await Promise.all([interRegular, interBold]); - - const { searchParams } = new URL(req.url); - - const title = searchParams.has('title') - ? searchParams.get('title')?.slice(0, 100) - : process.env.SITE_NAME; - - return new ImageResponse( - ( -
- - - - - -
{title}
-
- ), - { - width: 1200, - height: 630, - fonts: [ - { - name: 'Inter', - data: regularFont, - style: 'normal', - weight: 400 - }, - { - name: 'Inter', - data: boldFont, - style: 'normal', - weight: 700 - } - ] - } - ); - } catch (e) { - if (!(e instanceof Error)) throw e; - - console.log(e.message); - return new Response(`Failed to generate the image`, { - status: 500 - }); - } -} diff --git a/app/opengraph-image.tsx b/app/opengraph-image.tsx new file mode 100644 index 000000000..23762cbdd --- /dev/null +++ b/app/opengraph-image.tsx @@ -0,0 +1,7 @@ +import OpengraphImage from 'components/opengraph-image'; + +export const runtime = 'edge'; + +export default async function Image() { + return await OpengraphImage(); +} diff --git a/app/search/[collection]/opengraph-image.tsx b/app/search/[collection]/opengraph-image.tsx new file mode 100644 index 000000000..9eb9c47f7 --- /dev/null +++ b/app/search/[collection]/opengraph-image.tsx @@ -0,0 +1,11 @@ +import OpengraphImage from 'components/opengraph-image'; +import { getCollection } from 'lib/shopify'; + +export const runtime = 'edge'; + +export default async function Image({ params }: { params: { collection: string } }) { + const collection = await getCollection(params.collection); + const title = collection?.seo?.title || collection?.title; + + return await OpengraphImage({ title }); +} diff --git a/app/search/[collection]/page.tsx b/app/search/[collection]/page.tsx index 873701bf7..4ec62f2f4 100644 --- a/app/search/[collection]/page.tsx +++ b/app/search/[collection]/page.tsx @@ -20,16 +20,7 @@ export async function generateMetadata({ return { title: collection.seo?.title || collection.title, description: - collection.seo?.description || collection.description || `${collection.title} products`, - openGraph: { - images: [ - { - url: `/api/og?title=${encodeURIComponent(collection.title)}`, - width: 1200, - height: 630 - } - ] - } + collection.seo?.description || collection.description || `${collection.title} products` }; } diff --git a/components/opengraph-image.tsx b/components/opengraph-image.tsx new file mode 100644 index 000000000..93bb8b655 --- /dev/null +++ b/components/opengraph-image.tsx @@ -0,0 +1,45 @@ +import { ImageResponse } from '@vercel/og'; + +export type Props = { + title?: string; +}; + +export default async function OpengraphImage(props?: Props): Promise { + const { title } = { + ...{ + title: process.env.SITE_NAME + }, + ...props + }; + + return new ImageResponse( + ( +
+ + + + +

{title}

+
+ ), + { + width: 1200, + height: 630, + fonts: [ + { + name: 'Inter', + data: await fetch(new URL('../fonts/Inter-Bold.ttf', import.meta.url)).then((res) => + res.arrayBuffer() + ), + style: 'normal', + weight: 700 + } + ] + } + ); +} diff --git a/app/api/og/Inter-Bold.ttf b/fonts/Inter-Bold.ttf similarity index 100% rename from app/api/og/Inter-Bold.ttf rename to fonts/Inter-Bold.ttf diff --git a/lib/constants.tsx b/lib/constants.ts similarity index 100% rename from lib/constants.tsx rename to lib/constants.ts