feat: added socials to bottom of site
This commit is contained in:
parent
1077572131
commit
a330ae2b1c
5 changed files with 83 additions and 23 deletions
|
|
@ -8,18 +8,19 @@ const crypto = require("crypto");
|
|||
const path = require("path");
|
||||
const { DateTime } = require("luxon");
|
||||
|
||||
const cssHashCache = {};
|
||||
const getCssHash = (cssFile) => {
|
||||
if (cssHashCache[cssFile]) return cssHashCache[cssFile];
|
||||
const fileHashCache = {};
|
||||
const getFileHash = (file, dir = "css") => {
|
||||
const cacheKey = `${dir}/${file}`;
|
||||
if (fileHashCache[cacheKey]) return fileHashCache[cacheKey];
|
||||
|
||||
const cssPath = path.join(__dirname, "css", cssFile);
|
||||
const filePath = path.join(__dirname, dir, file);
|
||||
try {
|
||||
const content = fs.readFileSync(cssPath, "utf-8");
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const hash = crypto.createHash("md5").update(content).digest("hex").slice(0, 8);
|
||||
cssHashCache[cssFile] = hash;
|
||||
fileHashCache[cacheKey] = hash;
|
||||
return hash;
|
||||
} catch (e) {
|
||||
console.warn(`Could not hash CSS file: ${cssFile}`);
|
||||
console.warn(`Could not hash file: ${file} in ${dir}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
|
@ -238,15 +239,16 @@ module.exports = (eleventyConfig) => {
|
|||
return tagMap;
|
||||
});
|
||||
|
||||
// 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}`;
|
||||
// Cache busting filter: returns hashed filename
|
||||
eleventyConfig.addFilter("fileHash", (file, dir = "css") => {
|
||||
const hash = getFileHash(file, dir);
|
||||
const ext = path.extname(file);
|
||||
const base = path.basename(file, ext);
|
||||
return `/${dir}/${base}.${hash}${ext}`;
|
||||
});
|
||||
|
||||
eleventyConfig.on("eleventy.before", async () => {
|
||||
// Copy CSS files with hashes
|
||||
const cssDir = path.join(__dirname, "css");
|
||||
const outputCssDir = path.join(__dirname, "_site", "css");
|
||||
|
||||
|
|
@ -256,7 +258,7 @@ module.exports = (eleventyConfig) => {
|
|||
|
||||
const cssFiles = fs.readdirSync(cssDir).filter(f => f.endsWith(".css"));
|
||||
for (const cssFile of cssFiles) {
|
||||
const hash = getCssHash(cssFile);
|
||||
const hash = getFileHash(cssFile, "css");
|
||||
const ext = path.extname(cssFile);
|
||||
const base = path.basename(cssFile, ext);
|
||||
const hashedName = `${base}${hash == null ? '' : `.${hash}`}${ext}`;
|
||||
|
|
@ -266,6 +268,29 @@ module.exports = (eleventyConfig) => {
|
|||
path.join(outputCssDir, hashedName)
|
||||
);
|
||||
}
|
||||
|
||||
// Copy assets files with hashes
|
||||
const assetsDir = path.join(__dirname, "assets");
|
||||
const outputAssetsDir = path.join(__dirname, "_site", "assets");
|
||||
|
||||
if (fs.existsSync(assetsDir)) {
|
||||
if (!fs.existsSync(outputAssetsDir)) {
|
||||
fs.mkdirSync(outputAssetsDir, { recursive: true });
|
||||
}
|
||||
|
||||
const assetFiles = fs.readdirSync(assetsDir);
|
||||
for (const assetFile of assetFiles) {
|
||||
const hash = getFileHash(assetFile, "assets");
|
||||
const ext = path.extname(assetFile);
|
||||
const base = path.basename(assetFile, ext);
|
||||
const hashedName = `${base}${hash == null ? '' : `.${hash}`}${ext}`;
|
||||
|
||||
fs.copyFileSync(
|
||||
path.join(assetsDir, assetFile),
|
||||
path.join(outputAssetsDir, hashedName)
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
eleventyConfig.addPassthroughCopy("robots.txt");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue