From 7cbb815b5817ec9b3bb1fe47b94ddc2bdae289c9 Mon Sep 17 00:00:00 2001 From: maia arson crimew Date: Fri, 27 Oct 2023 21:08:25 +0200 Subject: [PATCH] add build time cache bouncing --- .eleventy.js | 2 ++ src/_includes/components/bounce.js | 22 ++++++++++++++++++++++ src/_includes/layouts/default.njk | 4 ++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/_includes/components/bounce.js diff --git a/.eleventy.js b/.eleventy.js index ca16bd0..92e1087 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -8,6 +8,7 @@ const markdownIt = require("markdown-it"); const markdownItAnchor = require("markdown-it-anchor"); const figure = require('./src/_includes/components/figure.js'); +const bounce = require('./src/_includes/components/bounce.js'); module.exports = function (eleventyConfig) { const parseDate = (str) => { @@ -35,6 +36,7 @@ module.exports = function (eleventyConfig) { eleventyConfig.addPlugin(eleventySass); eleventyConfig.addShortcode('figure', figure(md)); + eleventyConfig.addShortcode('bounce', bounce); eleventyConfig.addPassthroughCopy({ "src/static": "/" }); diff --git a/src/_includes/components/bounce.js b/src/_includes/components/bounce.js new file mode 100644 index 0000000..b876fe2 --- /dev/null +++ b/src/_includes/components/bounce.js @@ -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 `\n`; + } else if (type == "js") { + return `\n`; + } else { + throw new Error('undefined bounce type'); + } +} \ No newline at end of file diff --git a/src/_includes/layouts/default.njk b/src/_includes/layouts/default.njk index 2492e19..eaa2f92 100644 --- a/src/_includes/layouts/default.njk +++ b/src/_includes/layouts/default.njk @@ -5,7 +5,7 @@ {{ title if title else site.title }} - + {% bounce { path: '/css/style.css', check_path: 'src/css' } %} @@ -42,5 +42,5 @@ {% include "components/footer.njk" %} - + {% bounce { path: '/oneko.js', type: 'js' } %}