Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/app/og/ai/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const description = 'Get instant answers to your questions about Superwall.';

export const revalidate = false;

export async function GET() {
return renderOgImage({ title, description, site: SITE_NAME });
export async function GET(req: Request) {
const logoUrl = new URL('/docs/resources/logo.svg', req.url).toString();
return renderOgImage({ title, description, site: SITE_NAME, logoUrl });
}
5 changes: 3 additions & 2 deletions src/app/og/docs/[...slug]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DEFAULT_DESCRIPTION, SITE_NAME } from '@/lib/metadata';
export const revalidate = false;

export async function GET(
_req: Request,
req: Request,
context: { params: Promise<{ slug: string[] }> },
) {
const params = await context.params;
Expand All @@ -17,6 +17,7 @@ export async function GET(

const title = page.data.title || SITE_NAME;
const description = page.data.description || DEFAULT_DESCRIPTION;
const logoUrl = new URL('/docs/resources/logo.svg', req.url).toString();

return renderOgImage({ title, description, site: SITE_NAME });
return renderOgImage({ title, description, site: SITE_NAME, logoUrl });
}
5 changes: 3 additions & 2 deletions src/app/og/sdk/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const description = 'Select your SDK to view the specific documentation for this

export const revalidate = false;

export async function GET() {
return renderOgImage({ title, description, site: SITE_NAME });
export async function GET(req: Request) {
const logoUrl = new URL('/docs/resources/logo.svg', req.url).toString();
return renderOgImage({ title, description, site: SITE_NAME, logoUrl });
}
43 changes: 5 additions & 38 deletions src/lib/og.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
import { readFile } from 'fs/promises';
import { ImageResponse } from 'next/og';
import { Buffer } from 'buffer';
import { fileURLToPath } from 'url';
import { DEFAULT_DESCRIPTION, SITE_NAME } from '@/lib/metadata';

const assetPath = (path: string) => fileURLToPath(new URL(path, import.meta.url));

const toArrayBuffer = (buffer: Buffer) => {
const arrayBuffer = new ArrayBuffer(buffer.byteLength);
new Uint8Array(arrayBuffer).set(buffer);
return arrayBuffer;
};

const interRegular = readFile(
assetPath('../assets/fonts/Inter-Regular.ttf'),
).then((buffer) => toArrayBuffer(buffer));
const interSemiBold = readFile(
assetPath('../assets/fonts/Inter-SemiBold.ttf'),
).then((buffer) => toArrayBuffer(buffer));
const interBold = readFile(
assetPath('../assets/fonts/Inter-Bold.ttf'),
).then((buffer) => toArrayBuffer(buffer));
const logoSvg = readFile(
assetPath('../assets/logo.svg'),
'utf-8',
).then((svg) => `data:image/svg+xml;base64,${Buffer.from(svg).toString('base64')}`);

function truncateText(value: string, maxLength: number) {
if (value.length <= maxLength) return value;
const ellipsis = '...';
Expand All @@ -37,26 +12,23 @@ type OgImageOptions = {
title: string;
description?: string;
site?: string;
logoUrl?: string;
};

export async function renderOgImage({
title,
description,
site = SITE_NAME,
logoUrl,
}: OgImageOptions) {
const [regular, semiBold, bold, logo] = await Promise.all([
interRegular,
interSemiBold,
interBold,
logoSvg,
]);

const displayDescription = truncateText(
description?.trim() || DEFAULT_DESCRIPTION,
180,
);
const displayTitle = truncateText(title.trim(), 110);
const titleSize = displayTitle.length > 70 ? 52 : 64;
const logo = logoUrl || 'https://superwall.com/docs/resources/logo.svg';

return new ImageResponse(
(
<div
Expand All @@ -71,7 +43,7 @@ export async function renderOgImage({
backgroundColor: '#13151A',
color: '#F6F8FA',
borderTop: '6px solid #74F8F0',
fontFamily: 'Inter',
fontFamily: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif',
}}
>
<div style={{ display: 'flex', alignItems: 'center', gap: '24px' }}>
Expand Down Expand Up @@ -125,11 +97,6 @@ export async function renderOgImage({
{
width: 1200,
height: 630,
fonts: [
{ name: 'Inter', data: regular, weight: 400, style: 'normal' },
{ name: 'Inter', data: semiBold, weight: 600, style: 'normal' },
{ name: 'Inter', data: bold, weight: 700, style: 'normal' },
],
},
);
}