Fix: set no-cache headers for HTML, short cache for JS/CSS
Prevents Caddy/browser from caching index.html across deployments.
This commit is contained in:
parent
f154adf032
commit
362eaadeb0
1 changed files with 19 additions and 2 deletions
21
server.js
21
server.js
|
|
@ -84,7 +84,19 @@ app.use('/api/auth/forgot-password', rateLimit({
|
|||
}));
|
||||
|
||||
app.use(loggingMiddleware);
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.static(path.join(__dirname, 'public'), {
|
||||
setHeaders: (res, filePath) => {
|
||||
if (filePath.endsWith('.html')) {
|
||||
// Never cache HTML — ensures new deployments are picked up immediately
|
||||
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
||||
res.setHeader('Pragma', 'no-cache');
|
||||
res.setHeader('Expires', '0');
|
||||
} else if (filePath.match(/\.(js|css)$/)) {
|
||||
// Short cache for JS/CSS — 1 hour
|
||||
res.setHeader('Cache-Control', 'public, max-age=3600');
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Routes
|
||||
app.use('/api/auth', require('./src/routes/auth'));
|
||||
|
|
@ -115,7 +127,12 @@ app.get('/api/health', (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
app.get('*', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));
|
||||
app.get('*', (req, res) => {
|
||||
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
||||
res.setHeader('Pragma', 'no-cache');
|
||||
res.setHeader('Expires', '0');
|
||||
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
server.listen(PORT, () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue