This commit is contained in:
Andrey Kondratev
2025-11-07 21:12:49 +05:00
parent 5d7c6b2a09
commit ca27a2b3f0
3 changed files with 62 additions and 11 deletions

View File

@@ -43,21 +43,21 @@ app.use((req: Request, res: Response, next) => {
// Optimized caching strategy
app.use(express.static('public', {
maxAge: '1d', // Cache static assets for 1 day
maxAge: '365d', // Cache static assets for 1 year by default
etag: true,
lastModified: true,
setHeaders: (res: Response, filePath: string) => {
// Cache images, fonts, etc. longer
if (filePath.match(/\.(jpg|jpeg|png|gif|ico|woff|woff2|ttf|eot)$/)) {
// Cache images, fonts, etc. with immutable flag
if (filePath.match(/\.(jpg|jpeg|png|gif|ico|woff|woff2|ttf|eot|svg)$/)) {
res.set('Cache-Control', 'public, max-age=31536000, immutable');
}
// Cache CSS and JS with version string
// Cache CSS and JS with version string for 1 year
else if (filePath.match(/\.(css|js)$/)) {
res.set('Cache-Control', 'public, max-age=86400'); // 1 day
res.set('Cache-Control', 'public, max-age=31536000, immutable'); // 1 year
}
// HTML files - short cache
// HTML files - short cache with revalidation
else if (filePath.match(/\.html$/)) {
res.set('Cache-Control', 'public, max-age=3600'); // 1 hour
res.set('Cache-Control', 'public, max-age=0, must-revalidate');
}
}
}));