From f41dd4c2be76761c1e43fa5577061f5e461661f0 Mon Sep 17 00:00:00 2001 From: moshiur01 Date: Thu, 19 Nov 2026 11:10:23 +0600 Subject: [PATCH] fix(docs): prerender Open Graph images at build time Rendering an OG image on request runs past the free Workers CPU limit, so /og failed with Cloudflare error 1102 and link previews showed no image. Each page's image is now prerendered by next build at /og/.png and served as a static asset. Also drop the leftover .vercel ignore entries. Co-authored-by: Claude Opus 5.5 --- .gitignore | 4 --- .prettierignore | 1 - apps/docs/app/[[...slug]]/page.tsx | 7 ++-- apps/docs/app/og/[...slug]/route.ts | 23 ++++++++++++ apps/docs/app/og/route.tsx | 6 ---- apps/docs/config/site.ts | 2 +- apps/docs/lib/metadata.ts | 24 +++++++++++-- apps/docs/lib/og-image/og-image-pages.ts | 44 +++++++++++++++++++++++ apps/docs/lib/og-image/og-image-params.ts | 34 ++++++++++-------- apps/docs/tests/metadata.test.ts | 13 +++++-- apps/docs/tests/og-image.test.ts | 36 +++++++++---------- 11 files changed, 140 insertions(+), 54 deletions(-) create mode 100644 apps/docs/app/og/[...slug]/route.ts delete mode 100644 apps/docs/app/og/route.tsx create mode 100644 apps/docs/lib/og-image/og-image-pages.ts diff --git a/.gitignore b/.gitignore index 6a913d1..2ed8613 100644 --- a/.gitignore +++ b/.gitignore @@ -34,9 +34,6 @@ yarn-error.log* .env*.local !.env.example -# vercel -.vercel - # typescript *.tsbuildinfo next-env.d.ts @@ -45,7 +42,6 @@ next-env.d.ts # turbo cache .turbo -.vercel # cloudflare .open-next diff --git a/.prettierignore b/.prettierignore index cb2aff6..d23b21e 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,7 +3,6 @@ node_modules out dist .turbo -.vercel .open-next .wrangler pnpm-lock.yaml diff --git a/apps/docs/app/[[...slug]]/page.tsx b/apps/docs/app/[[...slug]]/page.tsx index 73cc0ac..85a7ae5 100644 --- a/apps/docs/app/[[...slug]]/page.tsx +++ b/apps/docs/app/[[...slug]]/page.tsx @@ -1,12 +1,13 @@ import type { Metadata } from 'next'; import { notFound } from 'next/navigation'; -import { compileMdxFile, createPageMetadata, DocsPage, DocsPager, LandingLayout } from 'docora'; +import { compileMdxFile, DocsPage, DocsPager, LandingLayout } from 'docora'; import { LandingPage } from '@/components/landing/landing-page'; import { DocsShell } from '@/components/layout/docs-shell'; import { mdxComponents } from '@/components/mdx/registry-components'; import docsConfig from '@/docs.config'; import { fetchStarCount } from '@/lib/github'; import { + docPageMetadata, registryEntryForSlug, registryJsonLd, registryPageMetadata, @@ -37,9 +38,7 @@ export async function generateMetadata({ params }: PageProps): Promise if (!page) return {}; const entry = registryEntryForSlug(slug); - return entry - ? registryPageMetadata(entry, page) - : createPageMetadata({ config: docsConfig, page }); + return entry ? registryPageMetadata(entry, page) : docPageMetadata(page); } const Page = async ({ params }: PageProps) => { diff --git a/apps/docs/app/og/[...slug]/route.ts b/apps/docs/app/og/[...slug]/route.ts new file mode 100644 index 0000000..7f46c63 --- /dev/null +++ b/apps/docs/app/og/[...slug]/route.ts @@ -0,0 +1,23 @@ +import { listOgImagePages } from '@/lib/og-image/og-image-pages'; +import { ogImagePath } from '@/lib/og-image/og-image-params'; +import { renderOgImage } from '@/lib/og-image/render-og-image'; + +type OgImageContext = Readonly<{ + params: Promise<{ slug: string[] }>; +}>; + +export const dynamic = 'force-static'; +export const dynamicParams = false; + +export async function generateStaticParams() { + const pages = await listOgImagePages(); + return pages.map((page) => ({ slug: ogImagePath(page.path).split('/').slice(2) })); +} + +export async function GET(_request: Request, { params }: OgImageContext) { + const imagePath = `/og/${(await params).slug.join('/')}`; + const pages = await listOgImagePages(); + const page = pages.find((candidate) => ogImagePath(candidate.path) === imagePath); + if (!page) return new Response('Not found', { status: 404 }); + return renderOgImage(page.params); +} diff --git a/apps/docs/app/og/route.tsx b/apps/docs/app/og/route.tsx deleted file mode 100644 index 8a72ae0..0000000 --- a/apps/docs/app/og/route.tsx +++ /dev/null @@ -1,6 +0,0 @@ -import { parseOgImageParams } from '@/lib/og-image/og-image-params'; -import { renderOgImage } from '@/lib/og-image/render-og-image'; - -export async function GET(request: Request) { - return renderOgImage(parseOgImageParams(new URL(request.url).searchParams)); -} diff --git a/apps/docs/config/site.ts b/apps/docs/config/site.ts index d21e559..461adb2 100644 --- a/apps/docs/config/site.ts +++ b/apps/docs/config/site.ts @@ -1,7 +1,7 @@ export const siteConfig = { name: 'Tween UI', url: 'https://tween-ui.staticmania.com', - ogImage: 'https://tween-ui.staticmania.com/og', + ogImage: 'https://tween-ui.staticmania.com/og/index.png', description: 'GSAP & CSS animated components for React. Copy the source, own the animation.', links: { github: 'https://github.com/StaticMania/tween-ui', diff --git a/apps/docs/lib/metadata.ts b/apps/docs/lib/metadata.ts index 0a8389b..51a8bcd 100644 --- a/apps/docs/lib/metadata.ts +++ b/apps/docs/lib/metadata.ts @@ -3,6 +3,7 @@ import { createPageMetadata, createRootMetadata } from 'docora'; import { siteConfig } from '@/config/site'; import docsConfig from '@/docs.config'; import { buildOgImageUrl } from '@/lib/og-image/og-image-params'; +import { OG_IMAGE_SIZE } from '@/lib/og-image/og-theme'; import { getEntry, hrefFor } from '@/registry/index'; import type { RegistryEntry } from '@/registry/schema'; @@ -68,8 +69,13 @@ export function registryKeywords(entry: RegistryEntry): string[] { /** Site-wide defaults for the root layout: docora's base plus icons and crawl rules. */ export function rootMetadata(): Metadata { + const base = createRootMetadata(docsConfig); + const ogImage = buildOgImageUrl('/'); + return { - ...createRootMetadata(docsConfig), + ...base, + openGraph: { ...base.openGraph, images: [{ url: ogImage, ...OG_IMAGE_SIZE }] }, + twitter: { ...base.twitter, images: [ogImage] }, applicationName: siteConfig.name, keywords: [...siteConfig.keywords], authors: [{ name: siteConfig.name, url: siteConfig.links.github }], @@ -114,7 +120,7 @@ export function registryPageMetadata( const base = createPageMetadata({ config: docsConfig, page }); const description = clampDescription(entry.description); const title = `${entry.title} — Animated React ${kindLabel(entry)}`; - const ogImage = buildOgImageUrl({ title: entry.title, description, kind: entry.type }); + const ogImage = buildOgImageUrl(hrefFor(entry)); return { ...base, @@ -127,7 +133,7 @@ export function registryPageMetadata( title, description, url: absolute(hrefFor(entry)), - images: [{ url: ogImage, width: 1200, height: 630, alt: title }], + images: [{ url: ogImage, ...OG_IMAGE_SIZE, alt: title }], }, twitter: { ...base.twitter, @@ -138,6 +144,18 @@ export function registryPageMetadata( }; } +/** Metadata for a plain docs page, pointing its share image at the prebuilt PNG. */ +export function docPageMetadata(page: Parameters[0]['page']): Metadata { + const base = createPageMetadata({ config: docsConfig, page }); + const ogImage = buildOgImageUrl(page?.path ?? '/'); + + return { + ...base, + openGraph: { ...base.openGraph, images: [{ url: ogImage, ...OG_IMAGE_SIZE }] }, + twitter: { ...base.twitter, images: [ogImage] }, + }; +} + /** * The bare full-screen preview. It renders the same block as the doc page, so * point search engines at the doc page and keep this one out of the index. diff --git a/apps/docs/lib/og-image/og-image-pages.ts b/apps/docs/lib/og-image/og-image-pages.ts new file mode 100644 index 0000000..33cbe33 --- /dev/null +++ b/apps/docs/lib/og-image/og-image-pages.ts @@ -0,0 +1,44 @@ +import docsConfig from '@/docs.config'; +import { clampDescription, registryEntryForSlug } from '@/lib/metadata'; +import { toOgImageParams, type OgImageParams } from '@/lib/og-image/og-image-params'; +import { source } from '@/lib/source'; +import { hrefFor } from '@/registry/index'; + +export type OgImagePage = Readonly<{ + path: string; + params: OgImageParams; +}>; + +export async function listOgImagePages(): Promise { + const pages: OgImagePage[] = [{ path: '/', params: toOgImageParams({}) }]; + + for (const { slug } of await source.getStaticParams()) { + const entry = registryEntryForSlug(slug); + if (entry) { + pages.push({ + path: hrefFor(entry), + params: toOgImageParams({ + title: entry.title, + description: clampDescription(entry.description), + kind: entry.type, + }), + }); + continue; + } + + const page = await source.getPage(slug); + if (!page) continue; + pages.push({ + path: page.path, + params: toOgImageParams({ + title: page.title, + description: + page.frontmatter?.description ?? + docsConfig.seo?.description ?? + docsConfig.site.description, + }), + }); + } + + return pages; +} diff --git a/apps/docs/lib/og-image/og-image-params.ts b/apps/docs/lib/og-image/og-image-params.ts index cfa0c64..13d7de9 100644 --- a/apps/docs/lib/og-image/og-image-params.ts +++ b/apps/docs/lib/og-image/og-image-params.ts @@ -10,25 +10,31 @@ export type OgImageParams = Readonly<{ const TITLE_LIMIT = 60; const DESCRIPTION_LIMIT = 130; -const OG_IMAGE_PATH = '/og'; - -export function parseOgImageParams(searchParams: URLSearchParams): OgImageParams { - const kind = searchParams.get('kind'); - +const OG_IMAGE_DIR = '/og'; + +export function toOgImageParams({ + title, + description, + kind, +}: { + title?: string; + description?: string; + kind?: string; +}): OgImageParams { return { - title: clampText(searchParams.get('title') ?? siteConfig.name, TITLE_LIMIT), - description: clampText( - searchParams.get('description') ?? siteConfig.description, - DESCRIPTION_LIMIT - ), + title: clampText(title ?? siteConfig.name, TITLE_LIMIT), + description: clampText(description ?? siteConfig.description, DESCRIPTION_LIMIT), kind: kind === 'component' || kind === 'block' ? kind : undefined, }; } -export function buildOgImageUrl({ title, description, kind }: OgImageParams): string { - const query = new URLSearchParams({ title, description }); - if (kind) query.set('kind', kind); - return new URL(`${OG_IMAGE_PATH}?${query.toString()}`, siteConfig.url).toString(); +export function ogImagePath(pagePath: string): string { + const slug = pagePath.replace(/^\/+|\/+$/g, '') || 'index'; + return `${OG_IMAGE_DIR}/${slug}.png`; +} + +export function buildOgImageUrl(pagePath: string): string { + return new URL(ogImagePath(pagePath), siteConfig.url).toString(); } export function isSiteTitle(title: string): boolean { diff --git a/apps/docs/tests/metadata.test.ts b/apps/docs/tests/metadata.test.ts index b00f889..203e176 100644 --- a/apps/docs/tests/metadata.test.ts +++ b/apps/docs/tests/metadata.test.ts @@ -69,9 +69,11 @@ describe('registryPageMetadata', () => { }); expect(metadata.twitter).toMatchObject({ card: 'summary_large_image' }); expect(JSON.stringify(metadata.openGraph?.images)).toContain( - 'https://tween-ui.staticmania.com/og?title=Grid+Cascade' + 'https://tween-ui.staticmania.com/og/block/grid-cascade.png' ); - expect(JSON.stringify(metadata.openGraph?.images)).toContain('kind=block'); + expect(metadata.twitter).toMatchObject({ + images: ['https://tween-ui.staticmania.com/og/block/grid-cascade.png'], + }); expect(metadata.alternates?.canonical).toBe( 'https://tween-ui.staticmania.com/block/grid-cascade' ); @@ -97,6 +99,13 @@ describe('previewMetadata', () => { }); describe('rootMetadata', () => { + it('points the share image at the prebuilt home PNG', () => { + const metadata = rootMetadata(); + expect(JSON.stringify(metadata.openGraph?.images)).toContain( + 'https://tween-ui.staticmania.com/og/index.png' + ); + }); + it('declares the icon set and manifest', () => { const metadata = rootMetadata(); expect(metadata.manifest).toBe('/site.webmanifest'); diff --git a/apps/docs/tests/og-image.test.ts b/apps/docs/tests/og-image.test.ts index 52dad63..6784e4c 100644 --- a/apps/docs/tests/og-image.test.ts +++ b/apps/docs/tests/og-image.test.ts @@ -1,10 +1,15 @@ import { describe, expect, it } from 'vitest'; import { siteConfig } from '@/config/site'; -import { buildOgImageUrl, isSiteTitle, parseOgImageParams } from '@/lib/og-image/og-image-params'; +import { + buildOgImageUrl, + isSiteTitle, + ogImagePath, + toOgImageParams, +} from '@/lib/og-image/og-image-params'; -describe('parseOgImageParams', () => { +describe('toOgImageParams', () => { it('falls back to the site name and description', () => { - const params = parseOgImageParams(new URLSearchParams()); + const params = toOgImageParams({}); expect(params).toEqual({ title: siteConfig.name, description: siteConfig.description, @@ -14,15 +19,13 @@ describe('parseOgImageParams', () => { }); it('accepts only known kinds', () => { - expect(parseOgImageParams(new URLSearchParams('kind=block')).kind).toBe('block'); - expect(parseOgImageParams(new URLSearchParams('kind=component')).kind).toBe('component'); - expect(parseOgImageParams(new URLSearchParams('kind=