Skip to content

Commit

Permalink
fix(spas): set canonical url
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Apr 29, 2024
1 parent 7a24a17 commit 1638ca6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions build/spas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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 +113,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 +189,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 +246,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 +346,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 All @@ -368,6 +372,15 @@ export async function buildSPAs(options: {
}
}

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

async function fetchGitHubPRs(repo, count = 5) {
const twoDaysAgo = new Date(Date.now() - 48 * 60 * 60 * 1000);
const pullRequestsQuery = [
Expand Down

0 comments on commit 1638ca6

Please sign in to comment.