commit b1825da5c7cd207cc09628794bead4facd774409 parent 9e907864317757b9c221ef8992917c0b5b100dc2 Author: Michael Neuper <michael@michaelneuper.com> Date: Wed, 28 Oct 2026 10:24:32 +0200 fingerprint font files for caching Diffstat:
9 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/README.md b/README.md @@ -32,7 +32,8 @@ npm run preview # serves dist/ at http://localhost:4321 | `src/scripts/emacs/` | M-x, the minibuffer, which-key, the modeline and load-theme | | `src/scripts/home/`, `post/`, `demos/` | what each component and demo does in the browser | | `src/styles/` | the styles, laid out like the components; `themes.css` has the colours | -| `public/` | files served as they are: fonts, favicon, GPG key, `robots.txt`, post videos | +| `src/fonts/` | JetBrains Mono and Computer Modern, built into `/_astro/` with a hash in each name so browsers can cache them for a year | +| `public/` | files served as they are: favicon, GPG key, `robots.txt`, post videos | Each component imports its own styles and starts its own script, so a section's markup, CSS and JavaScript are named after it in `components/`, `styles/` and `scripts/`. @@ -70,5 +71,5 @@ The site is static: `npm run build` writes it to `dist/`. - **Cloudflare**: build command `npm run build`, deploy command `npx wrangler deploy`. `wrangler.jsonc` serves `dist/`, and its `name` has to match the Worker's name in the dashboard. - **Netlify**: `netlify.toml` runs the build and publishes `dist/`. -Both read the response headers from `public/_headers`. The `/giscus/*` and `/fonts/*` headers there let the comments load their theme, so keep them on any host. +Both read the response headers from `public/_headers`. The `/giscus/*` and `/_astro/*` headers there let the comments load their theme and fonts, so keep them on any host. diff --git a/public/_headers b/public/_headers @@ -4,13 +4,12 @@ X-Content-Type-Options: nosniff # Astro puts a content hash in these file names, so browsers can keep them for good. +# The fonts are here too, and the comments' frame on giscus.app loads them. /_astro/* Cache-Control: public, max-age=31536000, immutable + Access-Control-Allow-Origin: https://giscus.app -# The comments' frame on giscus.app loads its stylesheet and our fonts from here. +# The comments' frame on giscus.app loads its stylesheet from here. # The stylesheets are open to any origin, so `npm run dev` can use them too. /giscus/* Access-Control-Allow-Origin: * - -/fonts/* - Access-Control-Allow-Origin: https://giscus.app diff --git a/public/fonts/cm-italic.woff2 b/src/fonts/cm-italic.woff2 Binary files differ. diff --git a/public/fonts/cm-regular.woff2 b/src/fonts/cm-regular.woff2 Binary files differ. diff --git a/public/fonts/jetbrains-mono-regular.woff2 b/src/fonts/jetbrains-mono-regular.woff2 Binary files differ. diff --git a/public/fonts/jetbrains-mono-semibold.woff2 b/src/fonts/jetbrains-mono-semibold.woff2 Binary files differ. diff --git a/src/layouts/Base.astro b/src/layouts/Base.astro @@ -9,6 +9,9 @@ import Analytics from "../components/frame/Analytics.astro"; import "../styles/fonts.css"; import "../styles/themes.css"; import "../styles/base.css"; +// The same hashed URLs fonts.css gets, so the preloads and the @font-face rules fetch one file. +import monoFont from "../fonts/jetbrains-mono-regular.woff2?url"; +import serifFont from "../fonts/cm-regular.woff2?url"; interface Props { title?: string; @@ -40,8 +43,8 @@ const { <meta name="description" content={description}> <link rel="icon" href="/favicon.svg" type="image/svg+xml"> <link rel="alternate" type="application/rss+xml" title="Michael Neuper's posts" href="/index.xml"> -<link rel="preload" href="/fonts/jetbrains-mono-regular.woff2" as="font" type="font/woff2" crossorigin> -<link rel="preload" href="/fonts/cm-regular.woff2" as="font" type="font/woff2" crossorigin> +<link rel="preload" href={monoFont} as="font" type="font/woff2" crossorigin> +<link rel="preload" href={serifFont} as="font" type="font/woff2" crossorigin> {/* is:inline: Astro ships this script as written instead of bundling it, so it still runs before first paint. */} <script is:inline> // Pick the theme before first paint so there is no flash. Keep this list in sync with src/scripts/themes.js. diff --git a/src/pages/giscus/[theme].css.js b/src/pages/giscus/[theme].css.js @@ -5,6 +5,8 @@ // public/_headers allows giscus.app to fetch both. import styles from "../../styles/themes.css?raw"; +import monoFont from "../../fonts/jetbrains-mono-regular.woff2?url"; +import monoBoldFont from "../../fonts/jetbrains-mono-semibold.woff2?url"; import { THEMES, DEFAULT_THEME } from "../../scripts/themes.js"; export function getStaticPaths() { @@ -33,14 +35,14 @@ const css = (tokens, light) => `/* giscus theme for michaelneuper.com, made from @font-face { font-family: "JetBrains Mono"; - src: url("/fonts/jetbrains-mono-regular.woff2") format("woff2"); + src: url("${monoFont}") format("woff2"); font-weight: 400; font-display: swap; } @font-face { font-family: "JetBrains Mono"; - src: url("/fonts/jetbrains-mono-semibold.woff2") format("woff2"); + src: url("${monoBoldFont}") format("woff2"); font-weight: 600; font-display: swap; } diff --git a/src/styles/fonts.css b/src/styles/fonts.css @@ -2,14 +2,14 @@ @font-face { font-family: "JetBrains Mono"; - src: url("/fonts/jetbrains-mono-regular.woff2") format("woff2"); + src: url("../fonts/jetbrains-mono-regular.woff2") format("woff2"); font-weight: 400; font-display: swap; } @font-face { font-family: "JetBrains Mono"; - src: url("/fonts/jetbrains-mono-semibold.woff2") format("woff2"); + src: url("../fonts/jetbrains-mono-semibold.woff2") format("woff2"); font-weight: 600; font-display: swap; } @@ -17,7 +17,7 @@ /* Computer Modern, via KaTeX's build of it: the same family as Latin Modern */ @font-face { font-family: "Computer Modern"; - src: url("/fonts/cm-regular.woff2") format("woff2"); + src: url("../fonts/cm-regular.woff2") format("woff2"); font-weight: 400; font-style: normal; font-display: swap; @@ -25,7 +25,7 @@ @font-face { font-family: "Computer Modern"; - src: url("/fonts/cm-italic.woff2") format("woff2"); + src: url("../fonts/cm-italic.woff2") format("woff2"); font-weight: 400; font-style: italic; font-display: swap;