add build time cache bouncing

This commit is contained in:
maia arson crimew 2023-10-27 21:08:25 +02:00
parent 70d74e19b9
commit 7cbb815b58
3 changed files with 26 additions and 2 deletions

View file

@ -8,6 +8,7 @@ const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor"); const markdownItAnchor = require("markdown-it-anchor");
const figure = require('./src/_includes/components/figure.js'); const figure = require('./src/_includes/components/figure.js');
const bounce = require('./src/_includes/components/bounce.js');
module.exports = function (eleventyConfig) { module.exports = function (eleventyConfig) {
const parseDate = (str) => { const parseDate = (str) => {
@ -35,6 +36,7 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventySass); eleventyConfig.addPlugin(eleventySass);
eleventyConfig.addShortcode('figure', figure(md)); eleventyConfig.addShortcode('figure', figure(md));
eleventyConfig.addShortcode('bounce', bounce);
eleventyConfig.addPassthroughCopy({ "src/static": "/" }); eleventyConfig.addPassthroughCopy({ "src/static": "/" });

View file

@ -0,0 +1,22 @@
const cache = new Map();
module.exports = ({ path, check_path = 'src/static/' + path, type = "css" }) => {
// caching hashes significantly speeds up build time
var hash = undefined;
if (cache.get(check_path)) {
hash = cache.get(check_path);
} else {
hash = revision = require('child_process')
.execSync(`git rev-list HEAD -1 -- ${check_path}`)
.toString().trim().slice(0, 7);
cache.set(check_path, hash);
}
if (type == "css") {
return `<link rel="stylesheet" href="${path}?h=${hash}"/>\n`;
} else if (type == "js") {
return `<script src="${path}?h=${hash}"></script>\n`;
} else {
throw new Error('undefined bounce type');
}
}

View file

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title if title else site.title }}</title> <title>{{ title if title else site.title }}</title>
<link rel="stylesheet" href="/css/style.css?v=3"/> {% bounce { path: '/css/style.css', check_path: 'src/css' } %}
<link rel="stylesheet" href="/prism.css"/> <link rel="stylesheet" href="/prism.css"/>
<meta name="description" content="{{ description if description else site.description }}" /> <meta name="description" content="{{ description if description else site.description }}" />
@ -42,5 +42,5 @@
</div> </div>
{% include "components/footer.njk" %} {% include "components/footer.njk" %}
</body> </body>
<script src="/oneko.js"></script> {% bounce { path: '/oneko.js', type: 'js' } %}
</html> </html>