cache versions

This commit is contained in:
Andrey Kondratev
2025-11-10 14:27:58 +05:00
parent 712c25a881
commit f6b696a5f8
7 changed files with 242 additions and 5 deletions

View File

@@ -69,6 +69,19 @@ if (!fs.existsSync(downloadsDir)) {
fs.mkdirSync(downloadsDir, { recursive: true });
}
// Load version for cache busting
let appVersion = Date.now().toString();
try {
const versionPath = path.join(__dirname, '../public/version.json');
if (fs.existsSync(versionPath)) {
const versionData = JSON.parse(fs.readFileSync(versionPath, 'utf8'));
appVersion = versionData.version || appVersion;
logger.info(`App version loaded: ${appVersion}`);
}
} catch (error) {
logger.warn('Could not load version file, using timestamp');
}
// Routes
app.get('/', (req: Request, res: Response) => {
// Use minified HTML in production
@@ -76,12 +89,23 @@ app.get('/', (req: Request, res: Response) => {
const htmlFile = isProduction ? 'index.min.html' : 'index.html';
const indexPath = path.join(__dirname, '../public', htmlFile);
// Set cache headers for HTML (short cache)
// Set cache headers for HTML (no cache for HTML itself)
res.set({
'Cache-Control': 'public, max-age=3600, must-revalidate', // 1 hour, revalidate
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.sendFile(indexPath);
// 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}`);
res.send(html);
} catch (error) {
logger.error('Error serving HTML:', error);
res.sendFile(indexPath);
}
});
// Search videos
@@ -288,6 +312,22 @@ app.get('/health', (req: Request, res: Response) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Version endpoint for client-side cache busting
app.get('/api/version', (req: Request, res: Response) => {
res.set('Cache-Control', 'no-cache, no-store, must-revalidate');
try {
const versionPath = path.join(__dirname, '../public/version.json');
if (fs.existsSync(versionPath)) {
const versionData = fs.readFileSync(versionPath, 'utf8');
res.json(JSON.parse(versionData));
} else {
res.json({ version: appVersion, timestamp: Date.now() });
}
} catch (error) {
res.json({ version: appVersion, timestamp: Date.now() });
}
});
// Error handler
app.use((err: Error, _req: Request, res: Response, _next: any) => {
logger.error(err.stack || err.message);