|
| 1 | +import { writeFile } from "node:fs/promises"; |
1 | 2 | import { defineConfig } from "astro/config"; |
2 | 3 | import tailwindcss from "@tailwindcss/vite"; |
3 | 4 | import nimbus, { defineConfig as defineNimbusConfig } from "@cloudflare/nimbus-docs"; |
@@ -39,6 +40,31 @@ const nimbusConfig = defineNimbusConfig({ |
39 | 40 | }, |
40 | 41 | }); |
41 | 42 |
|
| 43 | +/** |
| 44 | + * Cloudflare Pages reads `_redirects` from the build root and serves real |
| 45 | + * 301s from it at the edge. Written here rather than committed to public/ |
| 46 | + * so the stable version comes from src/lib/site.mjs and cannot go stale. |
| 47 | + * |
| 48 | + * These two routes were Cloudflare zone rules, which intercept before Pages |
| 49 | + * ever sees the request. Zone rules therefore win over this file: the rules |
| 50 | + * have to be deactivated for these lines to take effect, and this file has |
| 51 | + * to be deployed before they are, or /latest/* has nothing to fall back on. |
| 52 | + */ |
| 53 | +const cloudflareRedirects = { |
| 54 | + name: "cloudflare-redirects", |
| 55 | + hooks: { |
| 56 | + "astro:build:done": async ({ dir, logger }: { dir: URL; logger: { info: (message: string) => void } }) => { |
| 57 | + const body = [ |
| 58 | + `/ /${STABLE_VERSION}/introduction 301`, |
| 59 | + `/latest/* /${STABLE_VERSION}/:splat 301`, |
| 60 | + ].join("\n"); |
| 61 | + |
| 62 | + await writeFile(new URL("_redirects", dir), `${body}\n`, "utf8"); |
| 63 | + logger.info(`_redirects written, /latest -> /${STABLE_VERSION}`); |
| 64 | + }, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
42 | 68 | export default defineConfig({ |
43 | 69 | output: "static", |
44 | 70 | // Slashless URLs. Pages still emit as `x/index.html` (the `directory` |
@@ -72,6 +98,7 @@ export default defineConfig({ |
72 | 98 | defaultStrategy: "hover", |
73 | 99 | }, |
74 | 100 | integrations: [ |
| 101 | + cloudflareRedirects, |
75 | 102 | nimbus(nimbusConfig, { |
76 | 103 | // Authoring rules are opt-in by design — your repo, your taste. The |
77 | 104 | // two below are the load-bearing pair: frontmatter has to validate |
|
0 commit comments