Browse Source

Fix <meta> for Twitter

main
Dan Abramov 2 years ago
parent
commit
cbe2a266e2
  1. 21
      src/components/Seo.tsx

21
src/components/Seo.tsx

@ -23,15 +23,17 @@ export const Seo = withRouter(
router, router,
children, children,
isHomePage, isHomePage,
}: SeoProps & {router: Router}) => ( }: SeoProps & {router: Router}) => {
const pageTitle = isHomePage ? 'React' : title + ' – React';
// Twitter's meta parser is not very good.
const twitterTitle = pageTitle.replace(/[<>]/g, '');
return (
<Head> <Head>
{/* DEFAULT */} {/* DEFAULT */}
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
{title != null && ( {title != null && <title key="title">{pageTitle}</title>}
<title key="title">{title + (isHomePage ? '' : ' – React')}</title>
)}
{description != null && ( {description != null && (
<meta name="description" key="description" content={description} /> <meta name="description" key="description" content={description} />
)} )}
@ -46,11 +48,7 @@ export const Seo = withRouter(
content={`https://react.dev${router.asPath.split(/[\?\#]/)[0]}`} content={`https://react.dev${router.asPath.split(/[\?\#]/)[0]}`}
/> />
{title != null && ( {title != null && (
<meta <meta property="og:title" content={pageTitle} key="og:title" />
property="og:title"
content={isHomePage ? 'React' : title}
key="og:title"
/>
)} )}
{description != null && ( {description != null && (
<meta <meta
@ -78,7 +76,7 @@ export const Seo = withRouter(
<meta <meta
name="twitter:title" name="twitter:title"
key="twitter:title" key="twitter:title"
content={isHomePage ? 'React' : title} content={twitterTitle}
/> />
)} )}
{description != null && ( {description != null && (
@ -142,5 +140,6 @@ export const Seo = withRouter(
/> />
{children} {children}
</Head> </Head>
) );
}
); );

Loading…
Cancel
Save