Skip to content

Commit dfbd63f

Browse files
feat(frontend): prerender canonical URL for subpages (#2035)
1 parent edf35b8 commit dfbd63f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"build:subnets": "node scripts/cmc.subnets.mjs && npm run format",
1818
"build:passkey:aaguids": "node scripts/passkey.aaguids.mjs && npm run format",
1919
"build:csp": "node scripts/build.csp.mjs",
20-
"build:post-process": "npm run build:csp",
20+
"build:seo": "node scripts/build.seo.mjs",
21+
"build:post-process": "npm run build:seo && npm run build:csp",
2122
"dev": "npm run i18n && vite dev",
2223
"dev:skylab": "npm run i18n && vite dev --mode skylab",
2324
"build": "npm run build:frontend",

scripts/build.seo.mjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { readFileSync, writeFileSync } from 'node:fs';
2+
import { dirname, join, relative } from 'node:path';
3+
import { findHtmlFiles } from './build.utils.mjs';
4+
5+
const OUTPUT_DIR = join(process.cwd(), 'build');
6+
const SITE_ROOT_CANONICAL = 'https://console.juno.build';
7+
8+
const updateCanonical = (htmlFilePath) => {
9+
// 1. We determine the route based on the output
10+
const routePath = dirname(relative(OUTPUT_DIR, htmlFilePath));
11+
12+
// 2. Build the effective canonical route
13+
const canonicalPath = `${SITE_ROOT_CANONICAL}/${routePath}/`;
14+
15+
// 2. Read content
16+
let html = readFileSync(htmlFilePath, 'utf-8');
17+
18+
// 3. Update canonical
19+
html = html.replace(
20+
`<link href="${SITE_ROOT_CANONICAL}" rel="canonical" />`,
21+
`<link href="${canonicalPath}" rel="canonical" />`
22+
);
23+
24+
// 4. Update og:url to reflect the canonical
25+
html = html.replace(
26+
`<meta content="${SITE_ROOT_CANONICAL}" property="og:url" />`,
27+
`<meta content="${canonicalPath}" property="og:url" />`
28+
);
29+
30+
// 5. Save the content with the updated canonical URL
31+
writeFileSync(htmlFilePath, html);
32+
};
33+
34+
// Do not replace canonical for root and 404 pages
35+
const filterSubPages = (htmlFile) => dirname(htmlFile) !== OUTPUT_DIR;
36+
37+
const htmlFiles = findHtmlFiles().filter(filterSubPages);
38+
htmlFiles.forEach((htmlFile) => updateCanonical(htmlFile));

0 commit comments

Comments
 (0)