Skip to content

Commit 8aa0d99

Browse files
committed
feat: add hash versioning for client side main.js
1 parent 31fea30 commit 8aa0d99

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Module dependencies.
33
*/
44
const path = require('node:path');
5+
const crypto = require('node:crypto');
6+
const fs = require('node:fs');
57
const express = require('express');
68
const compression = require('compression');
79
const session = require('express-session');
@@ -207,6 +209,12 @@ app.use('/js/lib', express.static(path.join(__dirname, 'node_modules/@simpleweba
207209
app.use('/webfonts', express.static(path.join(__dirname, 'node_modules/@fortawesome/fontawesome-free/webfonts'), { maxAge: 31557600000 }));
208210
app.use('/image-cache', express.static(path.join(__dirname, 'tmp/image-cache'), { maxAge: 31557600000 }));
209211

212+
app.locals.mainJsHash = crypto
213+
.createHash('sha256')
214+
.update(fs.readFileSync(path.join(__dirname, 'public', 'js', 'main.js')))
215+
.digest('hex')
216+
.slice(0, 8);
217+
210218
/**
211219
* Analytics IDs needed thru layout.pug; set as express local so we don't have to pass them with each render call
212220
*/

views/layout.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ html.h-100(lang='en')
2727

2828
script(src='/js/lib/jquery.min.js')
2929
script(src='/js/lib/bootstrap.min.js')
30-
script(src='/js/main.js')
30+
script(src='/js/main.js?v=' + mainJsHash)
3131
script(src='https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js')
3232

3333
script(type='text/javascript').

0 commit comments

Comments
 (0)