Skip to content

Commit d790d00

Browse files
author
Troy
committed
feat: cache files for longer
Cache js forever (contents hash in filename), cache resources for a few days, cache styles only for 10 hours
1 parent 4ea8923 commit d790d00

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"watch:styles": "nodemon -e scss -x \"npm run build:styles\"",
1515
"build:js": "cd client && webpack --progress --profile --colors",
1616
"watch:js": "cd client && webpack --progress --profile --colors --watch",
17-
"postbuild": "mkdir -p webpage/ && cp -r public/* webpage/",
17+
"postbuild": "mkdir -p webpage/ && cp -r public/* webpage/ && node postbuild.mjs",
1818
"lint": "npx standard ./client",
1919
"lint:fix": "npx standard --fix ./client",
2020
"test": "babel-tape-runner client/test/*.js | tap-spec"
@@ -34,6 +34,7 @@
3434
"redux-thunk": "^2.1.0"
3535
},
3636
"devDependencies": {
37+
"@types/node": "^25.0.3",
3738
"babel-loader": "^6.2.4",
3839
"babel-preset-es2015": "^6.9.0",
3940
"babel-preset-react": "^6.11.1",

postbuild.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** This file: improves the caching behaviour of the JavaScript bundle file */
2+
import fs from 'fs'
3+
import crypto from 'crypto'
4+
5+
// 1. rename webpage/bundle.js to "bundle.{hash}.js"
6+
const folderToDeploy = 'webpage';
7+
const bundlePath = `${folderToDeploy}/bundle.js`
8+
9+
const bundleContents = fs.readFileSync(bundlePath, { encoding: 'utf8'})
10+
11+
const shaHash = crypto.hash("SHA-1", bundleContents)
12+
const shortHash = shaHash.slice(0, 12);
13+
14+
console.log('Adding short SHA hash to the js filename:', shortHash);
15+
16+
const bundleNameWithHash = `bundle.${shortHash}.js`
17+
18+
fs.renameSync(bundlePath, `${folderToDeploy}/${bundleNameWithHash}`)
19+
console.log(`Renamed ${bundlePath} to ${folderToDeploy}/${bundleNameWithHash}`);
20+
21+
// 2. in webpage/index.html, replace "bundle.js" with the new name
22+
const indexPath = `${folderToDeploy}/index.html`
23+
let indexContents = fs.readFileSync(indexPath, { encoding: 'utf8'})
24+
25+
indexContents = indexContents.replace('bundle.js', bundleNameWithHash)
26+
27+
fs.writeFileSync(indexPath, indexContents, { encoding: 'utf8'})
28+
console.log(`Updated ${indexPath} to reference ${bundleNameWithHash}`);

public/_headers

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Prevent other origins from iframeing our app
2+
/*.html
3+
X-Frame-Options: DENY
4+
5+
# Cache hashed asset pathsand sounds (not styles) for ~72 hours
6+
/bundle.*.js
7+
Cache-Control: public, max-age=31536000, immutable
8+
9+
# Cache images and sounds (not styles) for ~72 hours
10+
/resources/sound/*
11+
Cache-Control: public, max-age=260000, must-revalidate
12+
13+
/resources/images/*
14+
Cache-Control: public, max-age=260000, must-revalidate
15+
16+
/resources/styles/*
17+
Cache-Control: public, max-age=36000, must-revalidate

0 commit comments

Comments
 (0)