Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,43 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(require("./_11ty/apply-csp.js"));
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
const addHashCache = new Map();
eleventyConfig.addNunjucksAsyncFilter(
"addHash",
function (absolutePath, callback) {
const resolvedPath = path.join(".", absolutePath);
if (!fs.existsSync(resolvedPath)) {
// Avoid hard failure for missing assets; keep the original URL.
callback(null, absolutePath);
if (addHashCache.has(resolvedPath)) {
addHashCache
.get(resolvedPath)
.then((res) => callback(null, res))
.catch((err) => callback(err));
return;
}
readFile(resolvedPath, {
encoding: "utf-8",
})
.then((content) => {
return hasha.async(content);
})
.then((hash) => {
callback(null, `${absolutePath}?hash=${hash.substr(0, 10)}`);
const promise = new Promise((resolve, reject) => {
if (!fs.existsSync(resolvedPath)) {
// Avoid hard failure for missing assets; keep the original URL.
resolve(absolutePath);
return;
}
readFile(resolvedPath, {
encoding: "utf-8",
})
.catch((error) => {
callback(
new Error(`Failed to addHash to '${absolutePath}': ${error}`)
);
});
.then((content) => {
return hasha.async(content);
})
.then((hash) => {
resolve(`${absolutePath}?hash=${hash.substr(0, 10)}`);
})
.catch((error) => {
reject(
new Error(`Failed to addHash to '${absolutePath}': ${error}`)
);
});
});
addHashCache.set(resolvedPath, promise);
promise
.then((res) => callback(null, res))
.catch((err) => callback(err));
}
);

Expand Down