From c9829aed72f18653709481b89a0077a5d99e4e72 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust <24361490+mathiazom@users.noreply.github.com> Date: Wed, 11 Sep 2024 06:13:55 +0000 Subject: [PATCH] v3 - open graph image dimensions (#602) * feat(openGraph): ensure 1200x630 with transparent fill * refactor(seo): common open graph image dimensions constant * feat(openGraph): add recommended dimensions in seo image description --- src/app/api/openGraphImage/route.tsx | 5 +++-- src/utils/seo.ts | 26 ++++++++++++++++++++++---- studio/schemas/objects/seo.ts | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/app/api/openGraphImage/route.tsx b/src/app/api/openGraphImage/route.tsx index 8a7dc6e89..cb05e1169 100644 --- a/src/app/api/openGraphImage/route.tsx +++ b/src/app/api/openGraphImage/route.tsx @@ -1,6 +1,7 @@ import { ImageResponse } from "next/og"; import { NextRequest } from "next/server"; import OpenGraphImage from "./OpenGraphImage"; +import { OPEN_GRAPH_IMAGE_DIMENSIONS } from "../../../utils/seo"; export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams; @@ -9,8 +10,8 @@ export async function GET(request: NextRequest) { return new ImageResponse( , { - width: 1200, - height: 630, + width: OPEN_GRAPH_IMAGE_DIMENSIONS.width, + height: OPEN_GRAPH_IMAGE_DIMENSIONS.height, }, ); } diff --git a/src/utils/seo.ts b/src/utils/seo.ts index 7a2fb2113..3420aa9ea 100644 --- a/src/utils/seo.ts +++ b/src/utils/seo.ts @@ -29,6 +29,11 @@ type CompanyInfo = { defaultSEO: SeoData; }; +export const OPEN_GRAPH_IMAGE_DIMENSIONS = { + width: 1200, + height: 630, +}; + export async function fetchSeoData( query: string, variables?: any, @@ -81,8 +86,12 @@ export async function generateMetadataFromSeo( ): Promise { const companyInfo = await fetchCompanyInfo(); - const title = seo?.title || companyInfo?.siteMetadata?.siteName || "Variant"; - const description = seo?.description; + const title = + seo?.title || + companyInfo?.defaultSEO?.title || + companyInfo?.siteMetadata?.siteName || + "Variant"; + const description = seo?.description || companyInfo?.defaultSEO?.description; const keywords = seo?.keywords || ""; const favicon = companyInfo?.brandAssets?.favicon; @@ -96,8 +105,17 @@ export async function generateMetadataFromSeo( title: title, ...(description ? { description: description } : {}), })}`; - const imageUrl = - seo?.imageUrl || companyInfo?.defaultSEO?.imageUrl || fallbackImageUrl; + const sanityImageUrl = seo?.imageUrl || companyInfo?.defaultSEO?.imageUrl; + const sanityImageParams = `?${new URLSearchParams({ + w: OPEN_GRAPH_IMAGE_DIMENSIONS.width.toString(), + h: OPEN_GRAPH_IMAGE_DIMENSIONS.height.toString(), + fit: "fill", + fm: "png", // required for transparent + bg: "00000000", // transparent + })}`; + const imageUrl = sanityImageUrl + ? `${sanityImageUrl}${sanityImageParams}` + : fallbackImageUrl; return { title: title, diff --git a/studio/schemas/objects/seo.ts b/studio/schemas/objects/seo.ts index af7189e7f..8f6bb4603 100644 --- a/studio/schemas/objects/seo.ts +++ b/studio/schemas/objects/seo.ts @@ -56,7 +56,7 @@ const seo = defineField({ title: "Social Media Image", type: "image", description: - "A compelling image for social media can greatly improve conversion rates, even though it doesn't directly affect SEO.", + "A compelling image for social media can greatly improve conversion rates, even though it doesn't directly affect SEO. Recommended dimensions: 1200x630 (landscape).", }), ], });