An open source design system that's fully customizable and agent ready
TypeScript
feat(docsite): add per-page SEO metadata across components, docs, and galleries (#3141)
Joey6fc892be3b1a
diff --git a/apps/docsite/src/__tests__/pageMetadata.test.ts b/apps/docsite/src/__tests__/pageMetadata.test.tsnew file mode 100644index 0000000000..66a0bf5bba--- /dev/null+++ b/apps/docsite/src/__tests__/pageMetadata.test.ts@@ -0,0 +1,91 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Tests for the shared page-metadata helper and per-page coverage.+ *+ * Verifies that pageMetadata() produces a plain title (the root layout template+ * appends "· Astryx"), a self-referencing canonical, and branded social cards,+ * and that the generated registries carry the fields the dynamic routes feed+ * into it, so component and doc pages get unique, non-empty metadata instead+ * of the inherited site default.+ *+ * Run: pnpm -F @astryxdesign/docsite test+ */++import {describe, it, expect} from 'vitest';+import {pageMetadata} from '../lib/pageMetadata';+import {SITE_NAME} from '../lib/siteConfig';+import {components} from '../generated/componentRegistry';+import {docTopics} from '../generated/docsRegistry';++const allComponents = Object.values(components).flat();++describe('pageMetadata', () => {+ const meta = pageMetadata({+ title: 'Button',+ description: 'Triggers an action when clicked.',+ path: '/components/Button',+ type: 'article',+ });++ it('passes the plain title through (root template adds the brand)', () => {+ expect(meta.title).toBe('Button');+ });++ it('sets a self-referencing canonical URL', () => {+ expect(meta.alternates?.canonical).toBe('/components/Button');+ });++ it('brands the OpenGraph + Twitter titles to match the tab', () => {+ expect(meta.openGraph?.title).toBe(`Button · ${SITE_NAME}`);+ expect(meta.twitter?.title).toBe(`Button · ${SITE_NAME}`);+ });++ it('carries the OpenGraph url and type', () => {+ expect(meta.openGraph?.url).toBe('/components/Button');+ expect((meta.openGraph as {type?: string}).type).toBe('article');+ });++ it('re-states the branded social image on both cards', () => {+ expect(JSON.stringify(meta.openGraph?.images)).toContain(+ 'astryx-og-banner',+ );+ expect(JSON.stringify(meta.twitter?.images)).toContain('astryx-og-banner');+ });++ it('defaults the OpenGraph type to website', () => {+ const website = pageMetadata({+ title: 'Themes',+ description: 'Browse every theme.',+ path: '/themes',+ });+ expect((website.openGraph as {type?: string}).type).toBe('website');+ });+});++describe('dynamic-route metadata coverage', () => {+ it('every component has a display name to title the page', () => {+ for (const c of allComponents) {+ expect(c.displayName.length).toBeGreaterThan(0);+ }+ });++ it('every doc topic has a non-empty title and description', () => {+ for (const t of docTopics) {+ expect(t.title.length).toBeGreaterThan(0);+ expect(t.description.length).toBeGreaterThan(0);+ }+ });++ it('produces a unique canonical URL per component', () => {+ const canonicals = allComponents.map(+ c =>+ pageMetadata({+ title: c.displayName,+ description: c.description || `The ${c.displayName} component.`,+ path: `/components/${c.name}`,+ }).alternates?.canonical,+ );+ expect(new Set(canonicals).size).toBe(canonicals.length);+ });+});diff --git a/apps/docsite/src/app/(docs)/changelog/page.tsx b/apps/docsite/src/app/(docs)/changelog/page.tsxindex 89946de2d5..cf8c18db93 100644--- a/apps/docsite/src/app/(docs)/changelog/page.tsx+++ b/apps/docsite/src/app/(docs)/changelog/page.tsx@@ -1,8 +1,17 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+import type {Metadata} from 'next';import {ChangelogView} from '../../../components/ChangelogView';import {components} from '../../../generated/componentRegistry';import {packages} from '../../../generated/packageRegistry';+import {pageMetadata} from '../../../lib/pageMetadata';++export const metadata: Metadata = pageMetadata({+ title: 'Changelog',+ description:+ 'Release notes and version history for Astryx packages and components.',+ path: '/changelog',+});export default function ChangelogPage() {const changelogs = packagesdiff --git a/apps/docsite/src/app/(docs)/components/[name]/page.tsx b/apps/docsite/src/app/(docs)/components/[name]/page.tsxindex 74f10ed3b0..016773ea7a 100644--- a/apps/docsite/src/app/(docs)/components/[name]/page.tsx+++ b/apps/docsite/src/app/(docs)/components/[name]/page.tsx@@ -6,12 +6,14 @@* which handles tabs (Overview / Playground) on the client.*/+import type {Metadata} from 'next';import {notFound} from 'next/navigation';import {components} from '../../../../generated/componentRegistry';import {blocks} from '../../../../generated/blockRegistry';import {packages} from '../../../../generated/packageRegistry';import {ComponentDetailClient} from '../../../../components/component-detail/ComponentDetailClient';import {flattenComponentSidebarEntries} from '../../../../components/componentSidebarData';+import {pageMetadata} from '../../../../lib/pageMetadata';const allComponents = Object.values(components).flat();@@ -19,6 +21,26 @@ export function generateStaticParams() {return flattenComponentSidebarEntries().map(c => ({name: c.name}));}+export async function generateMetadata({+ params,+}: {+ params: Promise<{name: string}>;+}): Promise<Metadata> {+ const {name} = await params;+ const comp = allComponents.find(c => c.name === name);+ if (!comp) {+ return {};+ }+ return pageMetadata({+ title: comp.displayName,+ description:+ comp.description ||+ `Props, usage, and live examples for the ${comp.displayName} component.`,+ path: `/components/${comp.name}`,+ type: 'article',+ });+}+export default async function ComponentPage({params,}: {diff --git a/apps/docsite/src/app/(docs)/components/layout.tsx b/apps/docsite/src/app/(docs)/components/layout.tsxnew file mode 100644index 0000000000..bf0d0dce35--- /dev/null+++ b/apps/docsite/src/app/(docs)/components/layout.tsx@@ -0,0 +1,26 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * Supplies metadata for the /components gallery index. That page is a client+ * component ('use client'), which cannot export metadata, so this server layout+ * provides it. The dynamic /components/[name] pages set their own per-component+ * metadata via generateMetadata, which overrides the defaults declared here.+ */++import type {Metadata} from 'next';+import {pageMetadata} from '../../../lib/pageMetadata';++export const metadata: Metadata = pageMetadata({+ title: 'Components',+ description:+ 'Browse every Astryx component with copy-ready examples for each variant, state, and pattern.',+ path: '/components',+});++export default function ComponentsLayout({+ children,+}: {+ children: React.ReactNode;+}) {+ return children;+}diff --git a/apps/docsite/src/app/(docs)/docs/[topic]/page.tsx b/apps/docsite/src/app/(docs)/docs/[topic]/page.tsxindex b5579ac87a..50a0309f15 100644--- a/apps/docsite/src/app/(docs)/docs/[topic]/page.tsx+++ b/apps/docsite/src/app/(docs)/docs/[topic]/page.tsx@@ -10,6 +10,7 @@* Theme packages are NOT served here — they live at /themes/<name>.*/+import type {Metadata} from 'next';import {notFound} from 'next/navigation';import {docTopics} from '../../../../generated/docsRegistry';import {packages} from '../../../../generated/packageRegistry';@@ -17,6 +18,7 @@ import {ReferenceDocView} from '../../../../components/docs/ReferenceDocView';import {TokensDocView} from '../../../../components/docs/TokensDocView';import {PackageStubPage} from '../../../../components/docs/PackageStubPage';import {type InstallStep} from '../../../../components/docs/PackageActions';+import {pageMetadata} from '../../../../lib/pageMetadata';const TOKEN_TOPICS = new Set(['tokens',@@ -65,6 +67,44 @@ export function generateStaticParams() {];}+export async function generateMetadata({+ params,+}: {+ params: Promise<{topic: string}>;+}): Promise<Metadata> {+ const {topic: slug} = await params;++ // Mirror the page's own slug resolution: long-form doc topic first, then a+ // non-theme package reference page.+ const topic = docTopics.find(d => d.topic === slug);+ if (topic) {+ return pageMetadata({+ title: topic.title,+ description:+ topic.description ||+ `${topic.title} documentation for the Astryx design system.`,+ path: `/docs/${slug}`,+ type: 'article',+ });+ }++ const pkg = packages.find(+ p => p.name === slugToPackageName(slug) && !isThemePackage(p.name),+ );+ if (pkg) {+ return pageMetadata({+ title: pkg.displayName,+ description:+ pkg.description ||+ `The ${pkg.name} package for the Astryx design system.`,+ path: `/docs/${slug}`,+ type: 'article',+ });+ }++ return {};+}+export default async function DocPage({params,}: {diff --git a/apps/docsite/src/app/(site)/community/page.tsx b/apps/docsite/src/app/(site)/community/page.tsxindex 159b9db627..0fdba23212 100644--- a/apps/docsite/src/app/(site)/community/page.tsx+++ b/apps/docsite/src/app/(site)/community/page.tsx@@ -21,10 +21,12 @@*/import type {ReactNode} from 'react';+import type {Metadata} from 'next';import {FileText, Scale} from 'lucide-react';import {NavSurfaceMode} from './NavSurfaceMode';import {TemplatesPreview} from '../_landing/TemplatesPreview';import {ThemesPreview} from '../_landing/ThemesPreview';+import {pageMetadata} from '../../../lib/pageMetadata';import * as stylex from '@stylexjs/stylex';import {Card} from '@astryxdesign/core/Card';import {ClickableCard} from '@astryxdesign/core/ClickableCard';@@ -723,6 +725,13 @@ async function fetchContributors(): Promise<Contributor[]> {// Page// =============================================================================+export const metadata: Metadata = pageMetadata({+ title: 'Community',+ description:+ 'Join the Astryx community: contribute on GitHub, follow updates, and meet the people building the design system.',+ path: '/community',+});+export default async function CommunityPage() {const contributors = await fetchContributors();diff --git a/apps/docsite/src/app/(site)/templates/layout.tsx b/apps/docsite/src/app/(site)/templates/layout.tsxnew file mode 100644index 0000000000..bc3be7a7fd--- /dev/null+++ b/apps/docsite/src/app/(site)/templates/layout.tsx@@ -0,0 +1,25 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * Supplies metadata for the /templates gallery index. That page reads+ * `?preview=` via useSearchParams (so it's a client component and cannot export+ * metadata); this server layout provides it without touching that render path.+ */++import type {Metadata} from 'next';+import {pageMetadata} from '../../../lib/pageMetadata';++export const metadata: Metadata = pageMetadata({+ title: 'Templates',+ description:+ 'Ready-to-use page templates to kickstart your project: dashboards, settings, forms, AI chat, and more.',+ path: '/templates',+});++export default function TemplatesLayout({+ children,+}: {+ children: React.ReactNode;+}) {+ return children;+}diff --git a/apps/docsite/src/app/(site)/themes/page.tsx b/apps/docsite/src/app/(site)/themes/page.tsxindex fd52f671b7..ca72aeb181 100644--- a/apps/docsite/src/app/(site)/themes/page.tsx+++ b/apps/docsite/src/app/(site)/themes/page.tsx@@ -15,11 +15,23 @@* than the default seed.*/+import type {Metadata} from 'next';import {notFound} from 'next/navigation';import {Section} from '@astryxdesign/core/Section';import {packages} from '../../../generated/packageRegistry';import {themeObjects} from '../../../generated/themeRegistry';import {ThemePackagePage} from '../../../components/ThemePackagePage';+import {pageMetadata} from '../../../lib/pageMetadata';++// Static canonical metadata for /themes. The page also accepts a `?theme=`+// param to preselect the picker, but every variant is the same surface, so the+// canonical stays the bare /themes path to avoid duplicate-URL dilution.+export const metadata: Metadata = pageMetadata({+ title: 'Themes',+ description:+ 'Browse and preview every Astryx theme and see how design tokens, type, and components restyle across the gallery.',+ path: '/themes',+});// Default seed for the page — the picker opens with this theme// selected on first visit. Neutral is the most restrained / brand-diff --git a/apps/docsite/src/lib/pageMetadata.ts b/apps/docsite/src/lib/pageMetadata.tsnew file mode 100644index 0000000000..68a1eaf865--- /dev/null+++ b/apps/docsite/src/lib/pageMetadata.ts@@ -0,0 +1,67 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file pageMetadata.ts+ *+ * Builds per-page Next.js Metadata with a consistent title, description,+ * self-referencing canonical URL, and social cards. Centralizes the boilerplate+ * every route would otherwise repeat.+ *+ * Two Next.js behaviors this encapsulates:+ * - Metadata is shallow-merged: a route that declares `openGraph`/`twitter`+ * REPLACES the root objects wholesale (the default social image is NOT+ * inherited), so we always re-state the branded banner.+ * - The root layout's title template renders the plain `title` as+ * "<title> · Astryx". That template does NOT apply to OpenGraph/Twitter+ * titles, so we brand those explicitly to match the browser tab.+ */++import type {Metadata} from 'next';+import {SITE_NAME} from './siteConfig';++/** Branded launch banner that matches the root layout + blog fallback card. */+const DEFAULT_OG_IMAGE = '/astryx-og-banner.png';++export interface PageMetadataInput {+ /** Plain page title; rendered as "<title> · Astryx" via the root template. */+ title: string;+ /** Meta description, reused for the social card description. */+ description: string;+ /** Absolute path from the site root, e.g. "/components/Button". */+ path: string;+ /**+ * OpenGraph type: 'article' for content/reference pages (components, docs,+ * blog posts), 'website' for galleries and landing surfaces.+ */+ type?: 'website' | 'article';+ /** Social card image path; defaults to the branded banner. */+ image?: string;+}++export function pageMetadata({+ title,+ description,+ path,+ type = 'website',+ image = DEFAULT_OG_IMAGE,+}: PageMetadataInput): Metadata {+ const socialTitle = `${title} · ${SITE_NAME}`;+ return {+ title,+ description,+ alternates: {canonical: path},+ openGraph: {+ type,+ title: socialTitle,+ description,+ url: path,+ images: [{url: image}],+ },+ twitter: {+ card: 'summary_large_image',+ title: socialTitle,+ description,+ images: [image],+ },+ };+}