Intermediate LevelExpress JS
How do you implement long-term immutable caching for Express static assets using content hashes?
❓ Interview Prompt: Explain static asset cache-busting, content hashes (app.8f3a1e.js), immutable Cache-Control headers, and maxAge configuration.
Modern build tools (Vite/Webpack) inject unique content hash strings into compiled asset filenames ('app.a8f3d9.js').
Immutable Caching:
Because the filename changes whenever the asset content updates, client browsers can safely cache assets forever without checking the server:
'express.static("dist", { maxAge: "1y", immutable: true })'. Sets 'Cache-Control: public, max-age=31536000, immutable'.
Code Implementation Example
javascript
import express from 'express';
import path from 'path';
const app = express();
// Serve hashed production assets with 1-year immutable caching!
app.use('/assets', express.static(path.join(process.cwd(), 'dist/assets'), {
maxAge: '1y',
immutable: true
}));💡Serving hashed static assets with 1-year immutable caching headers.
🎯 Interview Key Takeaway
Serve hashed static assets with maxAge: "1y" and immutable: true for maximum browser caching efficiency.
#express.static#Cache-Busting#Immutable Caching#Intermediate
Back to all Express JS Questions→