@@ -19,6 +19,7 @@ import {
19
19
CONTRIBUTOR_SPOTLIGHT_ROOT ,
20
20
BUILD_OUT_ROOT ,
21
21
DEV_MODE ,
22
+ BASE_URL ,
22
23
} from "../libs/env/index.js" ;
23
24
import { isValidLocale } from "../libs/locale-utils/index.js" ;
24
25
import { DocFrontmatter , DocParent , NewsItem } from "../libs/types/document.js" ;
@@ -75,7 +76,10 @@ async function buildContributorSpotlight(
75
76
} ;
76
77
const context = { hyData } ;
77
78
78
- const html = renderHTML ( `/${ locale } /${ prefix } /${ contributor } ` , context ) ;
79
+ const html = renderCanonicalHTML (
80
+ `/${ locale } /${ prefix } /${ contributor } ` ,
81
+ context
82
+ ) ;
79
83
const outPath = path . join (
80
84
BUILD_OUT_ROOT ,
81
85
locale . toLowerCase ( ) ,
@@ -111,13 +115,11 @@ export async function buildSPAs(options: {
111
115
let buildCount = 0 ;
112
116
113
117
// The URL isn't very important as long as it triggers the right route in the <App/>
114
- const url = `/${ DEFAULT_LOCALE } /404.html` ;
115
- const html = renderHTML ( url , { pageNotFound : true } ) ;
116
- const outPath = path . join (
117
- BUILD_OUT_ROOT ,
118
- DEFAULT_LOCALE . toLowerCase ( ) ,
119
- "_spas"
120
- ) ;
118
+ const locale = DEFAULT_LOCALE ;
119
+ const url = `/${ locale } /404.html` ;
120
+ let html = renderHTML ( url , { pageNotFound : true } ) ;
121
+ html = setCanonical ( html , null ) ;
122
+ const outPath = path . join ( BUILD_OUT_ROOT , locale . toLowerCase ( ) , "_spas" ) ;
121
123
fs . mkdirSync ( outPath , { recursive : true } ) ;
122
124
fs . writeFileSync ( path . join ( outPath , path . basename ( url ) ) , html ) ;
123
125
buildCount ++ ;
@@ -185,7 +187,7 @@ export async function buildSPAs(options: {
185
187
noIndexing,
186
188
} ;
187
189
188
- const html = renderHTML ( url , context ) ;
190
+ const html = renderCanonicalHTML ( url , context ) ;
189
191
const outPath = path . join ( BUILD_OUT_ROOT , pathLocale , prefix ) ;
190
192
fs . mkdirSync ( outPath , { recursive : true } ) ;
191
193
const filePath = path . join ( outPath , "index.html" ) ;
@@ -222,7 +224,8 @@ export async function buildSPAs(options: {
222
224
const file = filepath . replace ( dirpath , "" ) ;
223
225
const page = file . split ( "." ) [ 0 ] ;
224
226
225
- const locale = DEFAULT_LOCALE . toLowerCase ( ) ;
227
+ const locale = DEFAULT_LOCALE ;
228
+ const pathLocale = locale . toLowerCase ( ) ;
226
229
const markdown = fs . readFileSync ( filepath , "utf-8" ) ;
227
230
228
231
const frontMatter = frontmatter < DocFrontmatter > ( markdown ) ;
@@ -242,10 +245,10 @@ export async function buildSPAs(options: {
242
245
pageTitle : `${ frontMatter . attributes . title || "" } | ${ title } ` ,
243
246
} ;
244
247
245
- const html = renderHTML ( url , context ) ;
248
+ const html = renderCanonicalHTML ( url , context ) ;
246
249
const outPath = path . join (
247
250
BUILD_OUT_ROOT ,
248
- locale ,
251
+ pathLocale ,
249
252
...slug . split ( "/" ) ,
250
253
page
251
254
) ;
@@ -342,7 +345,7 @@ export async function buildSPAs(options: {
342
345
featuredArticles,
343
346
} ;
344
347
const context = { hyData } ;
345
- const html = renderHTML ( url , context ) ;
348
+ const html = renderCanonicalHTML ( url , context ) ;
346
349
const outPath = path . join ( BUILD_OUT_ROOT , localeLC ) ;
347
350
fs . mkdirSync ( outPath , { recursive : true } ) ;
348
351
const filePath = path . join ( outPath , "index.html" ) ;
@@ -458,3 +461,24 @@ async function fetchLatestNews() {
458
461
items,
459
462
} ;
460
463
}
464
+
465
+ function renderCanonicalHTML ( url : string , context : any ) {
466
+ let html = renderHTML ( url , context ) ;
467
+ html = setCanonical ( html , url ) ;
468
+ return html ;
469
+ }
470
+
471
+ function setCanonical ( html : string , url : string | null ) {
472
+ html = html . replace (
473
+ `<link rel="canonical" href="${ BASE_URL } "/>` ,
474
+ url ? `<link rel="canonical" href="${ BASE_URL } ${ url } "/>` : ""
475
+ ) ;
476
+ // Better safe than sorry.
477
+ html = html . replace (
478
+ `<link rel="canonical" href="https://developer.mozilla.org"/>` ,
479
+ url
480
+ ? `<link rel="canonical" href="https://developer.mozilla.org${ url } "/>`
481
+ : ""
482
+ ) ;
483
+ return html ;
484
+ }
0 commit comments