From 35db64a82f8ff863de2aa2dbf661b61ca325f48a Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 23:22:06 +0000 Subject: [PATCH] Deploy: CDN cache headers for HTML and an nginx smoke test in CI - nginx sends Cache-Control with s-maxage=3600 on HTML, sitemap and robots, so Cloudflare can serve crawlers from the edge once a Cache Rule makes HTML eligible. Browsers still revalidate every time. open_file_cache for the 10k files in the export. - scripts/nginx-smoke.mjs runs in CI against nginx:1.27-alpine with the production config: every sitemap URL answers 200 (no redirect), lowercase and hyphen-less registrations 301 to the canonical page, unknown registrations 404, and cache headers are present. Against the 20dd041 config it fails on every aircraft page with the self-redirect. --- .github/workflows/ci.yml | 10 ++++++++ deploy/nginx.conf | 13 +++++++++++ package.json | 3 ++- scripts/nginx-smoke.mjs | 50 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 scripts/nginx-smoke.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ddc9240..f9c28b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,3 +19,13 @@ jobs: - run: npm run typecheck - run: npm run build - run: npm run seo:check + # Serve the export with the production nginx config and check status codes, redirects and cache headers. + - name: nginx smoke test + run: | + docker run -d --name vt-nginx -p 8080:80 \ + -v "$PWD/deploy/nginx.conf:/etc/nginx/conf.d/default.conf:ro" \ + -v "$PWD/build/redirects.map:/etc/nginx/redirects.map:ro" \ + -v "$PWD/out:/usr/share/nginx/html:ro" \ + nginx:1.27-alpine + for i in $(seq 1 30); do curl -sf -o /dev/null http://127.0.0.1:8080/ && break; sleep 1; done + npm run nginx:smoke -- http://127.0.0.1:8080 || { docker logs vt-nginx | tail -50; exit 1; } diff --git a/deploy/nginx.conf b/deploy/nginx.conf index f96a339..ac936f3 100644 --- a/deploy/nginx.conf +++ b/deploy/nginx.conf @@ -7,6 +7,16 @@ map $uri $vt_canonical { include /etc/nginx/redirects.map; } +# Pages are static, so let the CDN keep HTML for an hour (browsers always revalidate). Cloudflare only stores +# HTML when a Cache Rule makes it eligible; with "respect origin" it then follows s-maxage. Without it every +# crawler fetch reaches the origin (the origin slowed and dropped checks during the 26-27 Oct 2026 recrawl). +map $sent_http_content_type $vt_cache_control { + default ""; + ~^text/html "public, max-age=0, s-maxage=3600, stale-while-revalidate=86400"; + ~^text/xml "public, max-age=0, s-maxage=3600"; + ~^text/plain "public, max-age=0, s-maxage=3600"; +} + server { listen 80; server_name _; @@ -14,6 +24,9 @@ server { index index.html; # Behind Traefik and Cloudflare: keep Location relative so the scheme stays https. absolute_redirect off; + open_file_cache max=20000 inactive=5m; + open_file_cache_valid 5m; + add_header Cache-Control $vt_cache_control; gzip on; gzip_types text/plain text/css application/json application/javascript image/svg+xml; diff --git a/package.json b/package.json index 369caea..d9580c1 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "og": "tsx scripts/og.ts", "pipeline:extras": "tsx pipeline/extras.ts", "indexnow": "node scripts/indexnow.mjs", - "seo:check": "node scripts/seo-check.mjs" + "seo:check": "node scripts/seo-check.mjs", + "nginx:smoke": "node scripts/nginx-smoke.mjs" }, "dependencies": { "@fontsource-variable/inter": "^5.3.0", diff --git a/scripts/nginx-smoke.mjs b/scripts/nginx-smoke.mjs new file mode 100644 index 0000000..e0e3923 --- /dev/null +++ b/scripts/nginx-smoke.mjs @@ -0,0 +1,50 @@ +// Smoke-test the nginx config against a running server (default http://127.0.0.1:8080) that serves out/ +// with build/redirects.map, the way the Docker image does. Catches redirect loops, broken try_files and +// missing cache headers before they reach Googlebot. Run after `npm run build`. +import { readFileSync } from "node:fs"; + +const base = (process.argv[2] ?? "http://127.0.0.1:8080").replace(/\/$/, ""); +const ORIGIN = "https://vtaircrafts.in"; +const paths = [...readFileSync("out/sitemap.xml", "utf8").matchAll(/([^<]+)<\/loc>/g)].map((m) => m[1].slice(ORIGIN.length) || "/"); +const regs = paths.filter((p) => p.startsWith("/aircraft/")).map((p) => p.slice("/aircraft/".length)); +const problems = []; +let checked = 0; + +async function expect(path, want) { + const res = await fetch(base + path, { redirect: "manual" }); + checked++; + const got = { status: res.status, location: res.headers.get("location"), cache: res.headers.get("cache-control") ?? "" }; + await res.arrayBuffer(); + if (got.status !== want.status) problems.push(`${path}: ${got.status}, want ${want.status}${got.location ? ` (Location ${got.location})` : ""}`); + else if (want.location && got.location !== want.location) problems.push(`${path}: Location ${got.location}, want ${want.location}`); + else if (want.cache && !got.cache.includes(want.cache)) problems.push(`${path}: Cache-Control "${got.cache}", want ${want.cache}`); +} + +async function run(jobs, width = 32) { + const queue = [...jobs]; + await Promise.all(Array.from({ length: width }, async () => { while (queue.length) await queue.shift()(); })); +} + +// Every sitemap URL answers 200 itself, never a redirect (a self-redirect here is the 25 Oct 2026 loop). +const jobs = paths.map((p) => () => expect(p, { status: 200, cache: "s-maxage" })); +// Other spellings of a registration 301 to the canonical page with a relative Location. +const step = Math.max(1, Math.floor(regs.length / 300)); +for (const reg of regs.filter((_, i) => i % step === 0)) { + const target = `/aircraft/${reg}`; + jobs.push(() => expect(`/aircraft/${reg.toLowerCase()}`, { status: 301, location: target })); + jobs.push(() => expect(`/aircraft/${reg.replace("-", "")}`, { status: 301, location: target })); + jobs.push(() => expect(`/aircraft/${reg.replace("-", "").toLowerCase()}`, { status: 301, location: target })); +} +jobs.push(() => expect("/aircraft/VT-ZZZZ9", { status: 404 })); +jobs.push(() => expect("/robots.txt", { status: 200 })); +jobs.push(() => expect(`/aircraft/${regs[0]}.txt`, { status: 200 })); + +await run(jobs); +console.log(`nginx smoke: ${checked} requests against ${base}`); +if (problems.length) { + // Wrong status codes and redirects first; missing cache headers matter less. + problems.sort((a, b) => Number(a.includes("Cache-Control")) - Number(b.includes("Cache-Control"))); + console.error(problems.slice(0, 50).join("\n")); + console.error(`${problems.length} problems`); + process.exit(1); +}