|
1 | 1 | #!/usr/bin/env node |
2 | | -import { mkdir, writeFile } from "node:fs/promises"; |
| 2 | +import { writeFile } from "node:fs/promises"; |
3 | 3 | import path from "node:path"; |
4 | | -import { activePageSlugs, site } from "../src/data/site.ts"; |
| 4 | +import { activePageSlugs } from "../src/data/site.ts"; |
5 | 5 | import { getPublications } from "../src/lib/legacy-content.ts"; |
6 | 6 |
|
| 7 | +// Workers Static Assets evaluates _redirects before matching files. The |
| 8 | +// classic URL redirect Worker answers these paths first; these rules keep the |
| 9 | +// redirects working when that Worker fails open, and give the canonical pages |
| 10 | +// a permanent redirect when requested without the trailing slash. |
7 | 11 | const siteRoot = path.resolve(process.argv[2] ?? "_site"); |
8 | | -const redirects = [ |
9 | | - ...activePageSlugs.map((slug) => ({ source: `${slug}.html`, destination: `/${slug}/` })), |
10 | | - ...(await getPublications()).map((publication) => ({ |
11 | | - source: `publications/${publication.slug}.html`, |
12 | | - destination: `/publications/${publication.slug}/`, |
13 | | - })), |
| 12 | +const pages = [ |
| 13 | + ...activePageSlugs.map((slug) => `/${slug}`), |
| 14 | + ...(await getPublications()).map((publication) => `/publications/${publication.slug}`), |
| 15 | +]; |
| 16 | +const rules = [ |
| 17 | + "/index.html / 308", |
| 18 | + ...pages.flatMap((page) => [`${page}.html ${page}/ 308`, `${page} ${page}/ 308`]), |
14 | 19 | ]; |
15 | 20 |
|
16 | | -for (const redirect of redirects) { |
17 | | - const filename = path.join(siteRoot, redirect.source); |
18 | | - const canonical = new URL(redirect.destination, site.url).href; |
19 | | - const html = `<!doctype html> |
20 | | -<html lang="en"> |
21 | | - <head> |
22 | | - <meta charset="utf-8"> |
23 | | - <meta name="robots" content="noindex"> |
24 | | - <meta http-equiv="refresh" content="0;url=${redirect.destination}"> |
25 | | - <link rel="canonical" href="${canonical}"> |
26 | | - <title>Redirecting…</title> |
27 | | - <script>location.replace(${JSON.stringify(redirect.destination)} + location.search + location.hash)</script> |
28 | | - </head> |
29 | | - <body> |
30 | | - <p><a href="${redirect.destination}">Continue to the current Gecode page</a>.</p> |
31 | | - </body> |
32 | | -</html> |
33 | | -`; |
34 | | - await mkdir(path.dirname(filename), { recursive: true }); |
35 | | - await writeFile(filename, html); |
36 | | -} |
37 | | - |
38 | | -console.log(`Generated ${redirects.length} classic URL fallback redirects.`); |
| 21 | +await writeFile(path.join(siteRoot, "_redirects"), `${rules.join("\n")}\n`); |
| 22 | +console.log(`Generated ${rules.length} classic URL redirect rules.`); |
0 commit comments