feat: added cache for css

This commit is contained in:
Leyla Becker 2026-02-11 16:08:51 -06:00
parent a7676d06b6
commit 642ee3b45a
3 changed files with 93 additions and 3 deletions

View file

@ -4,8 +4,43 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }} | Volpe</title> <title>{{ title }} | Volpe</title>
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/prism.css"> {# Critical CSS inlined for faster initial render #}
<style>
:root {
--primary-color: #2c3e50;
--secondary-color: #3498db;
--text-color: #333;
--background-color: #fff;
--border-color: #ddd;
--code-background: #f5f5f5;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
line-height: 1.6;
color: var(--text-color);
background-color: var(--background-color);
max-width: 800px;
margin: 0 auto;
padding: 2rem;
}
header { margin-bottom: 2rem; padding-bottom: 1rem; border-bottom: 1px solid var(--border-color); }
header nav a { color: var(--primary-color); text-decoration: none; font-weight: bold; font-size: 1.2rem; }
main { min-height: calc(100vh - 200px); }
h1, h2, h3 { color: var(--primary-color); margin: 1.5rem 0 1rem; }
h1 { font-size: 2rem; }
footer { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border-color); text-align: center; color: #666; font-size: 0.9rem; }
</style>
{# Preload full stylesheet with cache-busted filename #}
<link rel="preload" href="{{ 'style.css' | cssHash }}" as="style">
<link rel="stylesheet" href="{{ 'style.css' | cssHash }}">
{# Defer prism.css - only needed for syntax highlighting #}
<link rel="preload" href="{{ 'prism.css' | cssHash }}" as="style">
<link rel="stylesheet" href="{{ 'prism.css' | cssHash }}" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="{{ 'prism.css' | cssHash }}"></noscript>
</head> </head>
<body> <body>
<header> <header>

View file

@ -4,8 +4,26 @@ const markdownItFootnote = require("markdown-it-footnote");
const markdownItMermaid = require('markdown-it-mermaid').default const markdownItMermaid = require('markdown-it-mermaid').default
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight"); const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const fs = require("fs"); const fs = require("fs");
const crypto = require("crypto");
const path = require("path");
const { DateTime } = require("luxon"); const { DateTime } = require("luxon");
const cssHashCache = {};
const getCssHash = (cssFile) => {
if (cssHashCache[cssFile]) return cssHashCache[cssFile];
const cssPath = path.join(__dirname, "css", cssFile);
try {
const content = fs.readFileSync(cssPath, "utf-8");
const hash = crypto.createHash("md5").update(content).digest("hex").slice(0, 8);
cssHashCache[cssFile] = hash;
return hash;
} catch (e) {
console.warn(`Could not hash CSS file: ${cssFile}`);
return null;
}
};
const extractTags = (content, mdInstance) => { const extractTags = (content, mdInstance) => {
if (!content) return []; if (!content) return [];
@ -220,7 +238,36 @@ module.exports = (eleventyConfig) => {
return tagMap; return tagMap;
}); });
eleventyConfig.addPassthroughCopy("css"); // Cache busting filter: returns hashed CSS filename
eleventyConfig.addFilter("cssHash", (cssFile) => {
const hash = getCssHash(cssFile);
const ext = path.extname(cssFile);
const base = path.basename(cssFile, ext);
return `/css/${base}.${hash}${ext}`;
});
eleventyConfig.on("eleventy.before", async () => {
const cssDir = path.join(__dirname, "css");
const outputCssDir = path.join(__dirname, "_site", "css");
if (!fs.existsSync(outputCssDir)) {
fs.mkdirSync(outputCssDir, { recursive: true });
}
const cssFiles = fs.readdirSync(cssDir).filter(f => f.endsWith(".css"));
for (const cssFile of cssFiles) {
const hash = getCssHash(cssFile);
const ext = path.extname(cssFile);
const base = path.basename(cssFile, ext);
const hashedName = `${base}${hash == null ? '' : `.${hash}`}${ext}`;
fs.copyFileSync(
path.join(cssDir, cssFile),
path.join(outputCssDir, hashedName)
);
}
});
eleventyConfig.addPassthroughCopy("robots.txt"); eleventyConfig.addPassthroughCopy("robots.txt");
eleventyConfig.ignores.add("README.md"); eleventyConfig.ignores.add("README.md");

View file

@ -20,6 +20,14 @@
locations."/" = { locations."/" = {
tryFiles = "$uri $uri/ /index.html"; tryFiles = "$uri $uri/ /index.html";
}; };
# Cache static assets (CSS, JS, images) for 1 year with immutable
locations."~* \\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$" = {
extraConfig = ''
expires 1y;
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
'';
};
}; };
in { in {
options.services.volpe = { options.services.volpe = {