Skip to content

Commit

Permalink
Merge branch 'MP-1095-fix-canonical-urls' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Apr 29, 2024
2 parents 5efa01f + 30cfed4 commit 8cf3be2
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions build/spas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
CONTRIBUTOR_SPOTLIGHT_ROOT,
BUILD_OUT_ROOT,
DEV_MODE,
BASE_URL,
} from "../libs/env/index.js";
import { isValidLocale } from "../libs/locale-utils/index.js";
import { DocFrontmatter, DocParent, NewsItem } from "../libs/types/document.js";
Expand Down Expand Up @@ -75,7 +76,7 @@ async function buildContributorSpotlight(
};
const context = { hyData };

const html = renderHTML(`/${locale}/${prefix}/${contributor}`, context);
const html = renderPage(`/${locale}/${prefix}/${contributor}`, context);
const outPath = path.join(
BUILD_OUT_ROOT,
locale.toLowerCase(),
Expand Down Expand Up @@ -113,6 +114,10 @@ export async function buildSPAs(options: {
// The URL isn't very important as long as it triggers the right route in the <App/>
const url = `/${DEFAULT_LOCALE}/404.html`;
const html = renderHTML(url, { pageNotFound: true });
html.replace(
'<link rel="canonical" href="https://developer.mozilla.org"/>',
""
);
const outPath = path.join(
BUILD_OUT_ROOT,
DEFAULT_LOCALE.toLowerCase(),
Expand Down Expand Up @@ -185,7 +190,7 @@ export async function buildSPAs(options: {
noIndexing,
};

const html = renderHTML(url, context);
const html = renderPage(url, context);
const outPath = path.join(BUILD_OUT_ROOT, pathLocale, prefix);
fs.mkdirSync(outPath, { recursive: true });
const filePath = path.join(outPath, "index.html");
Expand Down Expand Up @@ -242,7 +247,7 @@ export async function buildSPAs(options: {
pageTitle: `${frontMatter.attributes.title || ""} | ${title}`,
};

const html = renderHTML(url, context);
const html = renderPage(url, context);
const outPath = path.join(
BUILD_OUT_ROOT,
locale,
Expand Down Expand Up @@ -342,7 +347,7 @@ export async function buildSPAs(options: {
featuredArticles,
};
const context = { hyData };
const html = renderHTML(url, context);
const html = renderPage(url, context);
const outPath = path.join(BUILD_OUT_ROOT, localeLC);
fs.mkdirSync(outPath, { recursive: true });
const filePath = path.join(outPath, "index.html");
Expand Down Expand Up @@ -458,3 +463,17 @@ async function fetchLatestNews() {
items,
};
}

function renderPage(url: string, context: any) {
let html = renderHTML(url, context);
html = html.replace(
`<link rel="canonical" href="${BASE_URL}"/>`,
`<link rel="canonical" href="${BASE_URL}${url}"/>`
);
// Better safe than sorry.
html = html.replace(
`<link rel="canonical" href="https://developer.mozilla.org"/>`,
`<link rel="canonical" href="https://developer.mozilla.org${url}"/>`
);
return html;
}

0 comments on commit 8cf3be2

Please sign in to comment.