fiddle with cache busting

This commit is contained in:
SArpnt 2024-01-01 15:15:37 -05:00
parent ae08b0a6bb
commit cb93587ccb
Signed by: SArpnt
SSH key fingerprint: SHA256:wyRI40FXp6te9QA9r2OFJf+DtCZQmqZFNTj7EV6aMDA
4 changed files with 115 additions and 178 deletions

View file

@ -1,7 +1,9 @@
const esbuild = require("esbuild");
const eleventyAutoCacheBuster = require("eleventy-auto-cache-buster");
const XXHash = require("xxhash");
const htmlmin = require("html-minifier");
const npath = require("path");
const afs = require("fs/promises");
const minimatch = require("minimatch");
const input = "src";
const output = "_site";
@ -10,12 +12,13 @@ const serve = process.env.ELEVENTY_RUN_MODE === "serve";
// TODO minify html/pug and css and js in html/pug
module.exports = config => {
config.addPlugin(eleventyAutoCacheBuster, {
hashFunction: f => XXHash.hash(f, 0xe0b52cfa, "base64").replaceAll("=", ""),
});
config.on("afterBuild", () =>
esbuild.build({
function getHash(f) {
XXHash.hash(f, 0xe0b52cfa, "base64").replaceAll("=", "");
}
module.exports = eleventyConfig => {
eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
const esbuildResult = await esbuild.build({
entryPoints: [
`${input}/style.css`,
`${input}/bytebeat.ts`,
@ -28,9 +31,81 @@ module.exports = config => {
splitting: true,
minify: !serve,
sourcemap: true,
}),
);
config.addTransform("htmlmin", function (content) {
//write: serve,
});
// proper cachebusting is difficult not doing it right now
if (false && !serve) {
async function getHashOfFile(assetPath, content) {
console.log(`hashing ${assetPath}`);
assetPaths.push({
assetPath: assetPath.replace(dir.output, ""),
assetHash: getHash(content ?? (await afs.readFile(assetPath))),
});
}
const globs = [
`!${dir.output}/library/**`,
`${dir.output}/**/*.{css,js,png,jpg,jpeg,gif,mp4,ico}`,
].map(g => new minimatch.Minimatch(g));
const assetPaths = [];
(await Promise.all(await afs.readdir(dir.output, {}))).forEach(relpath => {
const path = npath.join(dir.output, relpath);
if (globs.every(mg => mg.match(path))) {
getHashOfFile(path);
}
});
const esbuildFiles = [];
esbuildResult.outputFiles.forEach(({ contents, path: apath }) => {
const path = npath.relative("", apath);
console.log(path);
console.log(globs[1].match(path));
if (globs.every(mg => mg.match(path))) {
const string = Buffer.from(contents).toString();
console.log(string.substring(0, 20).replace(/\n/g, "|"));
assetPaths.push({
assetPath: path.replace(dir.output, ""),
assetHash: getHash(contents),
});
esbuildFiles.push({ contents, path });
} else {
afs.writeFile(path, contents);
}
});
results.forEach(({ inputPath, outputPath, url, content }) => {
let outputData = ""; // Assigned later
let outputChanged = false; // Check if any hashes have been added
// Read the output content
fs.readFile(outputPath, (encoding = "UTF-8"), (err, data) => {
if (err) {
logRed(err);
throw err;
}
// Save the output data
outputData = data;
assetPaths.forEach(({ assetPath, assetHash }) => {
if (data.includes(assetPath)) {
// TODO very error prone
outputData = outputData.replace(assetPath, assetPath + "?v=" + assetHash);
outputChanged = true;
}
});
});
});
// TODO replace text in esbuild files
// TODO write esbuild files
}
});
eleventyConfig.addTransform("htmlmin", function (content) {
if (this.page.outputPath && this.page.outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
@ -42,10 +117,10 @@ module.exports = config => {
return content;
});
//config.setServerPassthroughCopyBehavior("passthrough"); // doesn't work
config.addPassthroughCopy({ assets: "/" });
config.addWatchTarget("src/**/*.js");
config.addWatchTarget("src/**/*.ts");
config.addWatchTarget("src/**/*.json");
eleventyConfig.addPassthroughCopy({ assets: "/" });
eleventyConfig.addWatchTarget("src/**/*.js");
eleventyConfig.addWatchTarget("src/**/*.ts");
eleventyConfig.addWatchTarget("src/**/*.json");
// TODO does the css need a watch target?
return {
dir: {

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Denperidge
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,7 +1,7 @@
{
scripts: {
clean: 'rm -rf _site',
build: 'eleventy',
build: 'rm -rf _site && eleventy',
serve: 'eleventy --serve',
debug: 'DEBUG=* eleventy --serve',
},
@ -22,9 +22,9 @@
'@jamshop/eleventy-plugin-esbuild': '^1.0.2',
'@prettier/plugin-pug': '^3.0.0',
//'@stdlib/constants-float32-eps': '^0.1.1',
'eleventy-auto-cache-buster': '^0.3.0',
fflate: '^0.8.1',
'html-minifier': '^4.0.0',
minimatch: '^9.0.3',
'ts-essentials': '^9.4.1',
xxhash: '^0.3.0',
},

View file

@ -50,15 +50,15 @@ dependencies:
'@prettier/plugin-pug':
specifier: ^3.0.0
version: 3.0.0(prettier@3.1.1)
eleventy-auto-cache-buster:
specifier: ^0.3.0
version: 0.3.0
fflate:
specifier: ^0.8.1
version: 0.8.1
html-minifier:
specifier: ^4.0.0
version: 4.0.0
minimatch:
specifier: ^9.0.3
version: 9.0.3
ts-essentials:
specifier: ^9.4.1
version: 9.4.1(typescript@5.3.3)
@ -373,18 +373,6 @@ packages:
resolution: {integrity: sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==}
dev: false
/@isaacs/cliui@8.0.2:
resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==}
engines: {node: '>=12'}
dependencies:
string-width: 5.1.2
string-width-cjs: /string-width@4.2.3
strip-ansi: 7.1.0
strip-ansi-cjs: /strip-ansi@6.0.1
wrap-ansi: 8.1.0
wrap-ansi-cjs: /wrap-ansi@7.0.0
dev: false
/@jamshop/eleventy-plugin-esbuild@1.0.2:
resolution: {integrity: sha512-N0nCxExDVVYslq1jUFqLEweuVOuHPziK352AW1BXVgu5fWXAf/elsdLcfzJLbgFlf4+tU+vJt/pb4fB4A27aqQ==}
dependencies:
@ -428,13 +416,6 @@ packages:
fastq: 1.16.0
dev: false
/@pkgjs/parseargs@0.11.0:
resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==}
engines: {node: '>=14'}
requiresBuild: true
dev: false
optional: true
/@prettier/plugin-pug@3.0.0(prettier@3.1.1):
resolution: {integrity: sha512-ERMMvGSJK/7CTc8OT7W/dtlV43sytyNeiCWckN0DIFepqwXotU0+coKMv5Wx6IWSNj7ZSjdNGBAA1nMPi388xw==}
engines: {node: ^16.13.0 || >=18.0.0, npm: '>=7.10.0'}
@ -475,16 +456,6 @@ packages:
hasBin: true
dev: false
/ansi-regex@5.0.1:
resolution: {integrity: sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==}
engines: {node: '>=8'}
dev: false
/ansi-regex@6.0.1:
resolution: {integrity: sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==}
engines: {node: '>=12'}
dev: false
/ansi-styles@4.3.0:
resolution: {integrity: sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==}
engines: {node: '>=8'}
@ -492,11 +463,6 @@ packages:
color-convert: 2.0.1
dev: false
/ansi-styles@6.2.1:
resolution: {integrity: sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==}
engines: {node: '>=12'}
dev: false
/any-promise@0.1.0:
resolution: {integrity: sha512-lqzY9o+BbeGHRCOyxQkt/Tgvz0IZhTmQiA+LxQW8wSNpcTbj8K+0cZiSEvbpNZZP9/11Gy7dnLO3GNWUXO4d1g==}
dev: false
@ -797,10 +763,6 @@ packages:
domhandler: 4.3.1
dev: false
/eastasianwidth@0.2.0:
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
dev: false
/ee-first@1.1.1:
resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==}
dev: false
@ -813,20 +775,6 @@ packages:
jake: 10.8.7
dev: false
/eleventy-auto-cache-buster@0.3.0:
resolution: {integrity: sha512-zxFKL+9lZ43I9r1bnQXuNlhyTAZnze8B1A3n+6ribe0BZBlYMa6zkVz53X+46AGmg1oQjVd8ZgkcJNG5eSU+MA==}
dependencies:
glob: 10.3.10
dev: false
/emoji-regex@8.0.0:
resolution: {integrity: sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==}
dev: false
/emoji-regex@9.2.2:
resolution: {integrity: sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==}
dev: false
/encodeurl@1.0.2:
resolution: {integrity: sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==}
engines: {node: '>= 0.8'}
@ -930,14 +878,6 @@ packages:
- supports-color
dev: false
/foreground-child@3.1.1:
resolution: {integrity: sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==}
engines: {node: '>=14'}
dependencies:
cross-spawn: 7.0.3
signal-exit: 4.1.0
dev: false
/fs.realpath@1.0.0:
resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==}
dev: false
@ -970,18 +910,6 @@ packages:
is-glob: 4.0.3
dev: false
/glob@10.3.10:
resolution: {integrity: sha512-fa46+tv1Ak0UPK1TOy/pZrIybNNt4HCv7SDzwyfiOZkvZLEbjsZkJBPtDHVshZjbecAoAGSC20MjLDG/qr679g==}
engines: {node: '>=16 || 14 >=14.17'}
hasBin: true
dependencies:
foreground-child: 3.1.1
jackspeak: 2.3.6
minimatch: 9.0.3
minipass: 7.0.4
path-scurry: 1.10.1
dev: false
/glob@7.2.3:
resolution: {integrity: sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==}
dependencies:
@ -1154,11 +1082,6 @@ packages:
engines: {node: '>=0.10.0'}
dev: false
/is-fullwidth-code-point@3.0.0:
resolution: {integrity: sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==}
engines: {node: '>=8'}
dev: false
/is-glob@4.0.3:
resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==}
engines: {node: '>=0.10.0'}
@ -1196,15 +1119,6 @@ packages:
engines: {node: '>=6.0'}
dev: false
/jackspeak@2.3.6:
resolution: {integrity: sha512-N3yCS/NegsOBokc8GAdM8UcmfsKiSS8cipheD/nivzr700H+nsMOxJjQnvwOcRYVuFkdH0wGUvW2WbXGmrZGbQ==}
engines: {node: '>=14'}
dependencies:
'@isaacs/cliui': 8.0.2
optionalDependencies:
'@pkgjs/parseargs': 0.11.0
dev: false
/jake@10.8.7:
resolution: {integrity: sha512-ZDi3aP+fG/LchyBzUM804VjddnwfSfsdeYkwt8NcbKRvo4rFkjhs456iLFn3k2ZUWvNe4i48WACDbza8fhq2+w==}
engines: {node: '>=10'}
@ -1276,11 +1190,6 @@ packages:
resolution: {integrity: sha512-2Fgx1Ycm599x+WGpIYwJOvsjmXFzTSc34IwDWALRA/8AopUKAVPwfJ+h5+f85BCp0PWmmJcWzEpxOpoXycMpdA==}
dev: false
/lru-cache@10.1.0:
resolution: {integrity: sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==}
engines: {node: 14 || >=16.14}
dev: false
/lru-cache@6.0.0:
resolution: {integrity: sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==}
engines: {node: '>=10'}
@ -1368,11 +1277,6 @@ packages:
yallist: 4.0.0
dev: false
/minipass@7.0.4:
resolution: {integrity: sha512-jYofLM5Dam9279rdkWzqHozUo4ybjdZmCsDHePy5V/PbBcVMiSZR97gmAy45aqi8CK1lG2ECd356FU86avfwUQ==}
engines: {node: '>=16 || 14 >=14.17'}
dev: false
/mkdirp@0.5.6:
resolution: {integrity: sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==}
hasBin: true
@ -1494,14 +1398,6 @@ packages:
resolution: {integrity: sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==}
dev: false
/path-scurry@1.10.1:
resolution: {integrity: sha512-MkhCqzzBEpPvxxQ71Md0b1Kk51W01lrYvlMzSUaIzNsODdd7mqhiimSZlr+VegAz5Z6Vzt9Xg2ttE//XBhH3EQ==}
engines: {node: '>=16 || 14 >=14.17'}
dependencies:
lru-cache: 10.1.0
minipass: 7.0.4
dev: false
/path-to-regexp@6.2.1:
resolution: {integrity: sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==}
dev: false
@ -1765,11 +1661,6 @@ packages:
engines: {node: '>=8'}
dev: false
/signal-exit@4.1.0:
resolution: {integrity: sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==}
engines: {node: '>=14'}
dev: false
/slash@1.0.0:
resolution: {integrity: sha512-3TYDR7xWt4dIqV2JauJr+EJeW356RXijHeUlO+8djJ+uBXPn8/2dpzBc8yQhh583sVvc9CvFAeQVgijsH+PNNg==}
engines: {node: '>=0.10.0'}
@ -1801,38 +1692,6 @@ packages:
engines: {node: '>= 0.8'}
dev: false
/string-width@4.2.3:
resolution: {integrity: sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==}
engines: {node: '>=8'}
dependencies:
emoji-regex: 8.0.0
is-fullwidth-code-point: 3.0.0
strip-ansi: 6.0.1
dev: false
/string-width@5.1.2:
resolution: {integrity: sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==}
engines: {node: '>=12'}
dependencies:
eastasianwidth: 0.2.0
emoji-regex: 9.2.2
strip-ansi: 7.1.0
dev: false
/strip-ansi@6.0.1:
resolution: {integrity: sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==}
engines: {node: '>=8'}
dependencies:
ansi-regex: 5.0.1
dev: false
/strip-ansi@7.1.0:
resolution: {integrity: sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==}
engines: {node: '>=12'}
dependencies:
ansi-regex: 6.0.1
dev: false
/strip-bom-string@1.0.0:
resolution: {integrity: sha512-uCC2VHvQRYu+lMh4My/sFNmF2klFymLX1wHJeXnbEJERpV/ZsVuonzerjfrGpIGF7LBVa1O7i9kjiWvJiFck8g==}
engines: {node: '>=0.10.0'}
@ -1937,24 +1796,6 @@ packages:
resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==}
dev: false
/wrap-ansi@7.0.0:
resolution: {integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==}
engines: {node: '>=10'}
dependencies:
ansi-styles: 4.3.0
string-width: 4.2.3
strip-ansi: 6.0.1
dev: false
/wrap-ansi@8.1.0:
resolution: {integrity: sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ==}
engines: {node: '>=12'}
dependencies:
ansi-styles: 6.2.1
string-width: 5.1.2
strip-ansi: 7.1.0
dev: false
/wrappy@1.0.2:
resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}
dev: false