Skip to content

Commit 4bdb87b

Browse files
authored
Try to fix Google titles for directive pages (#6393)
https://twitter.com/sophiebits/status/1718042539650150609
1 parent b8acf01 commit 4bdb87b

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

src/components/Layout/Page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ interface PageProps {
2828
children: React.ReactNode;
2929
toc: Array<TocItem>;
3030
routeTree: RouteItem;
31-
meta: {title?: string; canary?: boolean; description?: string};
31+
meta: {
32+
title?: string;
33+
titleForTitleTag?: string;
34+
canary?: boolean;
35+
description?: string;
36+
};
3237
section: 'learn' | 'reference' | 'community' | 'blog' | 'home' | 'unknown';
3338
}
3439

@@ -107,6 +112,7 @@ export function Page({children, toc, routeTree, meta, section}: PageProps) {
107112
<>
108113
<Seo
109114
title={title}
115+
titleForTitleTag={meta.titleForTitleTag}
110116
isHomePage={isHomePage}
111117
image={`/images/og-` + section + '.png'}
112118
searchOrder={searchOrder}

src/components/Seo.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {siteConfig} from '../siteConfig';
99

1010
export interface SeoProps {
1111
title: string;
12+
titleForTitleTag: undefined | string;
1213
description?: string;
1314
image?: string;
1415
// jsonld?: JsonLDType | Array<JsonLDType>;
@@ -36,6 +37,7 @@ function getDomain(languageCode: string): string {
3637
export const Seo = withRouter(
3738
({
3839
title,
40+
titleForTitleTag,
3941
image = '/images/og-default.png',
4042
router,
4143
children,
@@ -46,7 +48,9 @@ export const Seo = withRouter(
4648
const canonicalUrl = `https://${siteDomain}${
4749
router.asPath.split(/[\?\#]/)[0]
4850
}`;
49-
const pageTitle = isHomePage ? title : title + ' – React';
51+
// Allow setting a different title for Google results
52+
const pageTitle =
53+
(titleForTitleTag ?? title) + (isHomePage ? '' : ' – React');
5054
// Twitter's meta parser is not very good.
5155
const twitterTitle = pageTitle.replace(/[<>]/g, '');
5256
let description = isHomePage

src/content/reference/react/use-client.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: "'use client'"
3+
titleForTitleTag: "'use client' directive"
34
canary: true
45
---
56

src/content/reference/react/use-server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: "'use server'"
3+
titleForTitleTag: "'use server' directive"
34
canary: true
45
---
56

0 commit comments

Comments
 (0)