Created
September 18, 2019 19:15
-
-
Save tcrowe/5ee12eaf662f2fd5e44b0f7b04dea22f to your computer and use it in GitHub Desktop.
express cache busting - disable the default caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. by default express is using etags caching which may not be preferable | |
2. add nocache middleware | |
3. disable etags for server | |
4. disable etags and last modified for static files middleware | |
*/ | |
const express = require("express"); | |
const nocache = require("nocache"); | |
const server = express(); | |
server.disable("etag"); | |
server.use(nocache()); | |
// ⚠️ __dirname should be your public path | |
server.use(express.static(__dirname, { | |
etag: false, | |
lastModified: false, | |
setHeaders: res => nocache(null, res, function(){}) | |
}); | |
server.listen(8888, "127.0.0.1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More about tracing a cache issue:
ctrl+shift+i
Mac:cmd+alt+i
→ Networkctrl+f5
,cmd+shift+r
__dirname
orpath.join(?, ?, ?)