version fix

This commit is contained in:
Andrey Kondratev
2025-11-10 16:27:35 +05:00
parent f6b696a5f8
commit beb2d19019
2 changed files with 18 additions and 10 deletions

View File

@@ -44,7 +44,7 @@ app.use((req: Request, res: Response, next) => {
// Optimized caching strategy
app.use(express.static('public', {
maxAge: '365d', // Cache static assets for 1 year by default
maxAge: 0, // Don't cache by default, set specific headers below
etag: true,
lastModified: true,
setHeaders: (res: Response, filePath: string) => {
@@ -52,13 +52,21 @@ app.use(express.static('public', {
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 for 1 year
// Cache CSS and JS with version string for 1 year (they have ?v= in URL)
else if (filePath.match(/\.(css|js)$/)) {
res.set('Cache-Control', 'public, max-age=31536000, immutable'); // 1 year
res.set('Cache-Control', 'public, max-age=31536000, immutable');
}
// HTML files - short cache with revalidation
// HTML files - NO CACHE
else if (filePath.match(/\.html$/)) {
res.set('Cache-Control', 'public, max-age=0, must-revalidate');
res.set('Cache-Control', 'no-cache, no-store, must-revalidate');
res.set('Pragma', 'no-cache');
res.set('Expires', '0');
}
// JSON files (version.json) - NO CACHE
else if (filePath.match(/\.json$/)) {
res.set('Cache-Control', 'no-cache, no-store, must-revalidate');
res.set('Pragma', 'no-cache');
res.set('Expires', '0');
}
}
}));
@@ -99,8 +107,8 @@ app.get('/', (req: Request, res: Response) => {
// Read HTML and inject version
try {
let html = fs.readFileSync(indexPath, 'utf8');
// Replace version placeholders with actual version
html = html.replace(/\?v=\d+/g, `?v=${appVersion}`);
// Replace all version placeholders with actual version
html = html.replace(/\?v=(VERSION|\d+)/g, `?v=${appVersion}`);
res.send(html);
} catch (error) {
logger.error('Error serving HTML:', error);