Skip to content

Commit 0315afa

Browse files
committed
added path library for reliability
1 parent 4a349ed commit 0315afa

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/assets/js/cache-busting.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
module.exports.register = function(Handlebars, options) {
22
Handlebars.registerHelper('versionedAsset', function(options) {
3-
// requiring crypto library for hashing
3+
// node libraries
44
const crypto = require('crypto');
5-
// retrieving full path to the asset from the handlebar helper context
6-
const path = options.fn(this);
7-
const hash = crypto.createHash('sha256');
5+
const path = require('path');
86
const fs = require('fs');
9-
const ext = __dirname.substr(0, __dirname.length - 9).replace("/src", "/dist");
7+
const filePath = options.fn(this);
8+
const hash = crypto.createHash('sha256');
9+
// creating full path from the root dir to the file
10+
const fullPath = path.join(__dirname, "../../..", "dist", filePath);
1011
let hashCode = "";
1112
hash.on('readable', () => {
1213
const data = hash.read();
1314
if (data) hashCode = data.toString('hex');
1415
})
15-
hash.write(fs.readFileSync(ext + path, function (err) { if (err) return 0 }).toString());
16+
hash.write(fs.readFileSync(fullPath, function (err) { if (err) return 0 }).toString());
1617
hash.end();
17-
hashCode = "/" + path + "?v=" + hashCode.substr(0, 6);
18+
// creating a version string from first 6 hash characters
19+
hashCode = filePath + "?v=" + hashCode.substr(0, 6);
1820
return hashCode;
1921
});
2022
};

0 commit comments

Comments
 (0)