Skip to content

Commit

Permalink
Try to fix Google titles for directive pages (#6393)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiebits authored Oct 28, 2023
1 parent b8acf01 commit 4bdb87b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/Layout/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ interface PageProps {
children: React.ReactNode;
toc: Array<TocItem>;
routeTree: RouteItem;
meta: {title?: string; canary?: boolean; description?: string};
meta: {
title?: string;
titleForTitleTag?: string;
canary?: boolean;
description?: string;
};
section: 'learn' | 'reference' | 'community' | 'blog' | 'home' | 'unknown';
}

Expand Down Expand Up @@ -107,6 +112,7 @@ export function Page({children, toc, routeTree, meta, section}: PageProps) {
<>
<Seo
title={title}
titleForTitleTag={meta.titleForTitleTag}
isHomePage={isHomePage}
image={`/images/og-` + section + '.png'}
searchOrder={searchOrder}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Seo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {siteConfig} from '../siteConfig';

export interface SeoProps {
title: string;
titleForTitleTag: undefined | string;
description?: string;
image?: string;
// jsonld?: JsonLDType | Array<JsonLDType>;
Expand Down Expand Up @@ -36,6 +37,7 @@ function getDomain(languageCode: string): string {
export const Seo = withRouter(
({
title,
titleForTitleTag,
image = '/images/og-default.png',
router,
children,
Expand All @@ -46,7 +48,9 @@ export const Seo = withRouter(
const canonicalUrl = `https://${siteDomain}${
router.asPath.split(/[\?\#]/)[0]
}`;
const pageTitle = isHomePage ? title : title + ' – React';
// Allow setting a different title for Google results
const pageTitle =
(titleForTitleTag ?? title) + (isHomePage ? '' : ' – React');
// Twitter's meta parser is not very good.
const twitterTitle = pageTitle.replace(/[<>]/g, '');
let description = isHomePage
Expand Down
1 change: 1 addition & 0 deletions src/content/reference/react/use-client.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: "'use client'"
titleForTitleTag: "'use client' directive"
canary: true
---

Expand Down
1 change: 1 addition & 0 deletions src/content/reference/react/use-server.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: "'use server'"
titleForTitleTag: "'use server' directive"
canary: true
---

Expand Down

0 comments on commit 4bdb87b

Please sign in to comment.