Skip to content

Commit d509ca9

Browse files
committed
Use static asset redirects and headers instead of Pages files
Replace the classic meta-refresh fallback pages with _redirects rules, cache fingerprinted Astro assets for a year through _headers, and drop the GitHub Pages CNAME file.
1 parent 0dc4dcd commit d509ca9

6 files changed

Lines changed: 25 additions & 42 deletions

File tree

‎CNAME‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎_headers‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Workers Static Assets response headers.
2+
# Astro fingerprints these files, so their content never changes.
3+
/_astro/*
4+
Cache-Control: public, max-age=31536000, immutable

‎scripts/check-canonical-urls.mjs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,12 @@ for (const route of routes) {
3636
}
3737
}
3838

39+
const redirectRules = new Set((await readFile(path.join(siteRoot, "_redirects"), "utf8")).trim().split("\n"));
40+
if (!redirectRules.has("/index.html / 308")) failures.push("_redirects: expected /index.html / 308");
3941
for (const route of routes.filter((route) => route.file !== "index.html")) {
40-
const classicFile = route.file.replace(/\/index\.html$/, ".html");
41-
const html = await readFile(path.join(siteRoot, classicFile), "utf8");
42-
const destination = `https://www.gecode.dev${route.canonical}`;
43-
if (!html.includes(`<link rel="canonical" href="${destination}">`)) {
44-
failures.push(`${classicFile}: expected redirect canonical ${destination}`);
45-
}
46-
if (!html.includes(`http-equiv="refresh" content="0;url=${route.canonical}"`)) {
47-
failures.push(`${classicFile}: expected redirect to ${route.canonical}`);
42+
const page = route.canonical.slice(0, -1);
43+
for (const rule of [`${page}.html ${route.canonical} 308`, `${page} ${route.canonical} 308`]) {
44+
if (!redirectRules.has(rule)) failures.push(`_redirects: expected ${rule}`);
4845
}
4946
}
5047

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
11
#!/usr/bin/env node
2-
import { mkdir, writeFile } from "node:fs/promises";
2+
import { writeFile } from "node:fs/promises";
33
import path from "node:path";
4-
import { activePageSlugs, site } from "../src/data/site.ts";
4+
import { activePageSlugs } from "../src/data/site.ts";
55
import { getPublications } from "../src/lib/legacy-content.ts";
66

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.
711
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`]),
1419
];
1520

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.`);

‎scripts/dev.mjs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ async function activeDevServer() {
8484
async function staticAssetsReady() {
8585
const required = [
8686
".astro-public/images/gecode-logo.ico",
87-
".astro-public/CNAME",
8887
".astro-public/robots.txt",
8988
".astro-public/users-archive/index.html",
9089
".astro-public/users-archive/archive.css",

‎scripts/prepare-static.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "node:path";
33

44
const root = process.cwd();
55
const target = path.join(root, ".astro-public");
6-
const entries = ["images", "papers", "download", "CNAME", "robots.txt"];
6+
const entries = ["images", "papers", "download", "robots.txt", "_headers"];
77
const incremental = process.argv.includes("--incremental");
88

99
// Production builds must not inherit an archive prepared by the dev server.

0 commit comments

Comments
 (0)