Skip to content

Commit 036d74b

Browse files
🐛 fix(meta): show the site title once when a page is titled after the site
1 parent a6d6536 commit 036d74b

7 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎site/app/routes/document.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ vi.mock('../content/registry', () => ({
2020
return [{ categories: [{ documents: [document], title: 'General' }], title: 'Components' }];
2121
},
2222
},
23-
findDocument: (pathname: string) => (pathname === '/components/button' ? document : undefined),
23+
findDocument: (pathname: string) => {
24+
if (pathname === '/components/button') return document;
25+
if (pathname === '/guide/overview') {
26+
return { ...document, pathname, source: 'docs/guide/overview.mdx', title: 'Lobe Docs Kit' };
27+
}
28+
return undefined;
29+
},
2430
loadDocument: vi.fn(),
2531
}));
2632

@@ -60,6 +66,17 @@ it('emits absolute canonical, Open Graph, and Twitter metadata for a document',
6066
);
6167
});
6268

69+
it('shows the site title once when a page is titled after the site', () => {
70+
const descriptors = readDescriptors(meta({ location: { pathname: '/guide/overview' } } as never));
71+
72+
expect(descriptors).toEqual(
73+
expect.arrayContaining([
74+
{ title: 'Lobe Docs Kit' },
75+
{ content: 'Lobe Docs Kit', property: 'og:title' },
76+
]),
77+
);
78+
});
79+
6380
it('marks the prerendered not-found document as noindex', () => {
6481
const descriptors = readDescriptors(notFoundMeta({} as never));
6582

‎site/app/routes/document.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import siteConfig from 'virtual:lobedocs/site-config';
66
import { DocsLayout } from '../../components/DocsLayout/DocsLayout';
77
import { overviewMeta } from '../../components/SectionOverview/meta';
88
import { SectionOverview } from '../../components/SectionOverview/SectionOverview';
9+
import { formatPageTitle } from '../../content/pageTitle';
910
import { findOverview } from '../../content/sectionOverview';
1011
import { contentManifest, findDocument, loadDocument } from '../content/registry';
1112
import NotFound from './not-found';
@@ -22,7 +23,7 @@ export const meta: MetaFunction = ({ location }) => {
2223
];
2324
}
2425

25-
const title = `${document.title} - ${siteConfig.title}`;
26+
const title = formatPageTitle(document.title, siteConfig.title);
2627
const canonicalUrl = new URL(document.pathname, siteConfig.siteUrl).href;
2728
const openGraphImage = siteConfig.themeConfig?.metadata?.openGraph?.image ?? '';
2829

‎site/app/routes/home.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import siteConfig from 'virtual:lobedocs/site-config';
55

66
import { styles } from '../../components/Home/homeStyle';
77
import { sectionLandingPathname } from '../../content/pageChrome';
8+
import { formatPageTitle } from '../../content/pageTitle';
89
import { contentManifest, findDocument } from '../content/registry';
910

1011
export const meta: MetaFunction = () => {
1112
const document = findDocument('/');
12-
const title = document ? `${document.title} - ${siteConfig.title}` : siteConfig.title;
13+
const title = document ? formatPageTitle(document.title, siteConfig.title) : siteConfig.title;
1314
const description = document?.description ?? siteConfig.description;
1415
const canonicalUrl = new URL('/', siteConfig.siteUrl).href;
1516
const openGraphImage = siteConfig.themeConfig?.metadata?.openGraph?.image ?? '';

‎site/compiler/audit/artifactAudit.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path';
44
import { type CheerioAPI, load } from 'cheerio';
55
import ts from 'typescript';
66

7+
import { formatPageTitle } from '../../content/pageTitle';
78
import { siteMetadata } from '../../content/siteMetadata';
89
import type { DocumentManifestEntry } from '../../types/content';
910
import { agentPagePathname, listAgentPagePathnames } from '../agent/assembleAgentDocs';
@@ -186,7 +187,7 @@ const auditDocumentMetadata = (
186187
diagnostics: string[],
187188
): void => {
188189
const label = path.relative(path.dirname(path.dirname(file)), file).replaceAll(path.sep, '/');
189-
const expectedTitle = `${document.title} - ${siteMetadata.name}`;
190+
const expectedTitle = formatPageTitle(document.title, siteMetadata.name);
190191
const expectedCanonical = new URL(document.pathname, siteMetadata.origin).href;
191192
const title = $('title').first().text().trim();
192193
if (title !== expectedTitle) {

‎site/components/SectionOverview/meta.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { MetaDescriptor } from 'react-router';
22
import siteConfig from 'virtual:lobedocs/site-config';
33

4+
import { formatPageTitle } from '../../content/pageTitle';
45
import type { OverviewMatch } from '../../content/sectionOverview';
56

67
export function overviewMeta(
78
{ category, section }: OverviewMatch,
89
pathname: string,
910
): MetaDescriptor[] {
1011
const heading = category ? `${category.title} - ${section.title}` : section.title;
11-
const title = `${heading} - ${siteConfig.title}`;
12+
const title = formatPageTitle(heading, siteConfig.title);
1213
const description = `Browse ${heading} documentation in ${siteConfig.title}.`;
1314
const canonicalUrl = new URL(pathname.replace(/\/+$/, ''), siteConfig.siteUrl).href;
1415

‎site/content/pageTitle.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { formatPageTitle } from './pageTitle';
2+
3+
it('appends the site title to a page title', () => {
4+
expect(formatPageTitle('Button', 'Lobe UI')).toBe('Button - Lobe UI');
5+
expect(formatPageTitle('LobeHub UI Kit', 'Lobe UI')).toBe('LobeHub UI Kit - Lobe UI');
6+
});
7+
8+
it('shows the site title once when the page is titled after the site', () => {
9+
expect(formatPageTitle('Streamdown', 'Streamdown')).toBe('Streamdown');
10+
expect(formatPageTitle(' Lobe Docs Kit ', 'Lobe Docs Kit')).toBe('Lobe Docs Kit');
11+
});

‎site/content/pageTitle.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Joins a page title and the site title for `<title>` and `og:title`. When the
3+
* page is titled after the site (typically the home page), the site title is
4+
* shown once instead of `Site - Site`.
5+
*/
6+
export const formatPageTitle = (pageTitle: string, siteTitle: string): string =>
7+
pageTitle.trim() === siteTitle.trim() ? siteTitle : `${pageTitle} - ${siteTitle}`;

0 commit comments

Comments
 (0)