Skip to content

Commit 01db757

Browse files
authored
fix(spas): set canonical url (#11033)
Previously, we declared `https://developer.example.com` as the canonical URL for all static pages.
1 parent b900918 commit 01db757

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

Diff for: build/spas.ts

+37-13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
CONTRIBUTOR_SPOTLIGHT_ROOT,
2020
BUILD_OUT_ROOT,
2121
DEV_MODE,
22+
BASE_URL,
2223
} from "../libs/env/index.js";
2324
import { isValidLocale } from "../libs/locale-utils/index.js";
2425
import { DocFrontmatter, DocParent, NewsItem } from "../libs/types/document.js";
@@ -75,7 +76,10 @@ async function buildContributorSpotlight(
7576
};
7677
const context = { hyData };
7778

78-
const html = renderHTML(`/${locale}/${prefix}/${contributor}`, context);
79+
const html = renderCanonicalHTML(
80+
`/${locale}/${prefix}/${contributor}`,
81+
context
82+
);
7983
const outPath = path.join(
8084
BUILD_OUT_ROOT,
8185
locale.toLowerCase(),
@@ -111,13 +115,11 @@ export async function buildSPAs(options: {
111115
let buildCount = 0;
112116

113117
// 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");
121123
fs.mkdirSync(outPath, { recursive: true });
122124
fs.writeFileSync(path.join(outPath, path.basename(url)), html);
123125
buildCount++;
@@ -185,7 +187,7 @@ export async function buildSPAs(options: {
185187
noIndexing,
186188
};
187189

188-
const html = renderHTML(url, context);
190+
const html = renderCanonicalHTML(url, context);
189191
const outPath = path.join(BUILD_OUT_ROOT, pathLocale, prefix);
190192
fs.mkdirSync(outPath, { recursive: true });
191193
const filePath = path.join(outPath, "index.html");
@@ -222,7 +224,8 @@ export async function buildSPAs(options: {
222224
const file = filepath.replace(dirpath, "");
223225
const page = file.split(".")[0];
224226

225-
const locale = DEFAULT_LOCALE.toLowerCase();
227+
const locale = DEFAULT_LOCALE;
228+
const pathLocale = locale.toLowerCase();
226229
const markdown = fs.readFileSync(filepath, "utf-8");
227230

228231
const frontMatter = frontmatter<DocFrontmatter>(markdown);
@@ -242,10 +245,10 @@ export async function buildSPAs(options: {
242245
pageTitle: `${frontMatter.attributes.title || ""} | ${title}`,
243246
};
244247

245-
const html = renderHTML(url, context);
248+
const html = renderCanonicalHTML(url, context);
246249
const outPath = path.join(
247250
BUILD_OUT_ROOT,
248-
locale,
251+
pathLocale,
249252
...slug.split("/"),
250253
page
251254
);
@@ -342,7 +345,7 @@ export async function buildSPAs(options: {
342345
featuredArticles,
343346
};
344347
const context = { hyData };
345-
const html = renderHTML(url, context);
348+
const html = renderCanonicalHTML(url, context);
346349
const outPath = path.join(BUILD_OUT_ROOT, localeLC);
347350
fs.mkdirSync(outPath, { recursive: true });
348351
const filePath = path.join(outPath, "index.html");
@@ -458,3 +461,24 @@ async function fetchLatestNews() {
458461
items,
459462
};
460463
}
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

Comments
 (0)