Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Created September 18, 2019 19:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrowe/5ee12eaf662f2fd5e44b0f7b04dea22f to your computer and use it in GitHub Desktop.
Save tcrowe/5ee12eaf662f2fd5e44b0f7b04dea22f to your computer and use it in GitHub Desktop.
express cache busting - disable the default caching
/*
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");
@tcrowe
Copy link
Author

tcrowe commented Sep 26, 2019

More about tracing a cache issue:

  • What are your headers in the browser developer console? Windows & Linux: ctrl+shift+i Mac: cmd+alt+i → Network
  • Hard Refresh ctrl+f5, cmd+shift+r
  • Does your static server middleware have caching options? Can you disable or change it?
  • Are you sure your static files path is going to the correct directory? __dirname or path.join(?, ?, ?)
  • Do any browser extensions keep a cache to increase speed?
  • In the developer tools there might be a setting for "Disable cache (While DevTools open)."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment