Skip to content

Commit 82e69e3

Browse files
authored
feat: differentiate versioned pages metadata for search engines (#2478)
1 parent 15a0730 commit 82e69e3

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/layouts/Layout.astro

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ const {
2323
canonicalPathname,
2424
} = Astro.props;
2525
26-
const pageTitle = title
27-
? `${title} · Express.js`
28-
: 'Express.js · Node.js web application framework';
29-
3026
const pathname = Astro.url.pathname.replace(/\/$/, '');
3127
const segments = pathname.split('/').filter(Boolean);
3228
const langSegment = segments[0] || 'en';
@@ -35,6 +31,18 @@ const isApi =
3531
restSegments[0] === 'api' || (restSegments[1] === 'api' && /^\dx$/.test(restSegments[0] ?? ''));
3632
const isBlogOrApi = restSegments[0] === 'blog' || isApi;
3733
34+
// Versioned pages carry the version in title/description so search engines
35+
// don't treat 4x and 5x as duplicates.
36+
const versionSegment = /^\dx$/.test(restSegments[0] ?? '') ? restSegments[0] : undefined;
37+
const versionLabel = versionSegment && `Express.js ${versionSegment[0]}.x`;
38+
39+
const pageTitle = title
40+
? `${title} · ${versionLabel ?? 'Express.js'}`
41+
: 'Express.js · Node.js web application framework';
42+
const pageDescription = versionLabel
43+
? `${description} Documentation for ${versionLabel}.`
44+
: description;
45+
3846
// Blog and API content is not translated, so their canonical is the English
3947
// page and they emit no hreflang alternates.
4048
let canonicalPath = canonicalPathname ?? Astro.url.pathname;
@@ -94,7 +102,7 @@ const lang = getLangFromUrl(Astro.url);
94102
<link rel="icon" href="/favicon.ico" sizes="any" />
95103
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
96104
<link rel="manifest" href="/site.webmanifest" />
97-
<meta name="description" content={description} />
105+
<meta name="description" content={pageDescription} />
98106

99107
<link rel="canonical" href={canonicalUrl} />
100108

@@ -128,15 +136,15 @@ const lang = getLangFromUrl(Astro.url);
128136
<meta property="og:site_name" content="Express.js" />
129137
<meta property="og:url" content={canonicalUrl} />
130138
<meta property="og:title" content={pageTitle} />
131-
<meta property="og:description" content={description} />
139+
<meta property="og:description" content={pageDescription} />
132140
<meta property="og:image" content={imageUrl} />
133141
{authors?.map((author) => <meta property="article:author" content={author.name} />)}
134142

135143
<!-- Twitter Card -->
136144
<meta name="twitter:card" content="summary_large_image" />
137145
<meta name="twitter:site" content="@UseExpressJS" />
138146
<meta name="twitter:title" content={pageTitle} />
139-
<meta name="twitter:description" content={description} />
147+
<meta name="twitter:description" content={pageDescription} />
140148
<meta name="twitter:domain" content="expressjs.com" />
141149
<meta name="twitter:image" content={imageUrl} />
142150

0 commit comments

Comments
 (0)