-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateServiceWorkerCache.js
65 lines (57 loc) · 1.66 KB
/
generateServiceWorkerCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import crypto from "crypto";
import fs from "fs";
// As Zola does not support custom output formats,
// we generate the `serviceworker-cache.json` via Node.
function getChecksum(path) {
return new Promise(function (resolve, reject) {
const hash = crypto.createHash("sha256");
const input = fs.createReadStream(path);
input.on("error", reject);
input.on("data", function (chunk) {
hash.update(chunk);
});
input.on("close", function () {
resolve(hash.digest("hex"));
});
});
}
/**
* @param {publicPath} publicPath - Resulting path to be used
* @param {localPath} localPath - Path to where the file for checksum resides
* @example
*
* hash("/js/main.js", "static/js/main.js");
*
*/
async function hash(publicPath, localPath) {
const checkSum = await getChecksum(localPath);
return `${publicPath}?h=${checkSum}`;
}
const cache = {
data: {
pages: ["/", "/work/", "/journal/"],
files: [
"/favicon.ico",
"manifest.json",
await hash("/js/main.mjs", "./static/js/main.mjs"),
await hash("/css/main.css", "./public/css/main.css"),
"/fonts/CircularStd-Book.woff2",
"/fonts/CircularStd-Bold.woff2",
"/fonts/Larsseit-Bold.woff2",
"/fonts/FiraCode-Regular.woff2",
"/img/footer__icons.svg",
"/img/icons-36.png",
"/img/icons-48.png",
"/img/icons-60.png",
"/img/icons-72.png",
"/img/icons-76.png",
"/img/icons-96.png",
"/img/icons-120.png",
"/img/icons-152.png",
"/img/icons-180.png",
"/img/icons-192.png",
"/img/icons-512.png",
],
},
};
fs.writeFileSync("public/serviceworker-cache.json", JSON.stringify(cache));