more fixes

This commit is contained in:
Andrey Kondratev
2025-11-07 14:37:21 +05:00
parent 53633dd837
commit 5d7c6b2a09
5 changed files with 200 additions and 199 deletions

View File

@@ -1,4 +1,5 @@
import express, { Request, Response } from 'express';
import compression from 'compression';
import path from 'path';
import fs from 'fs';
import ffmpeg from 'fluent-ffmpeg';
@@ -18,6 +19,7 @@ const db = new Database();
const soundcloud = new SoundCloudService();
// Middleware
app.use(compression()); // Enable gzip compression
app.use(express.json());
app.use((req: Request, res: Response, next) => {
res.set('Content-Security-Policy',
@@ -39,17 +41,26 @@ app.use((req: Request, res: Response, next) => {
next();
});
// Cache-busting middleware for iOS Safari
app.use('/dist/*.js', (req: Request, res: Response, next) => {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': '0'
});
next();
});
app.use(express.static('public'));
// Optimized caching strategy
app.use(express.static('public', {
maxAge: '1d', // Cache static assets for 1 day
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)$/)) {
res.set('Cache-Control', 'public, max-age=31536000, immutable');
}
// Cache CSS and JS with version string
else if (filePath.match(/\.(css|js)$/)) {
res.set('Cache-Control', 'public, max-age=86400'); // 1 day
}
// HTML files - short cache
else if (filePath.match(/\.html$/)) {
res.set('Cache-Control', 'public, max-age=3600'); // 1 hour
}
}
}));
// Ensure downloads directory exists
const downloadsDir = path.join(__dirname, '../downloads');
@@ -59,21 +70,14 @@ if (!fs.existsSync(downloadsDir)) {
// Routes
app.get('/', (req: Request, res: Response) => {
// Read and modify index.html to add timestamp for iOS cache busting
const indexPath = path.join(__dirname, '../public/index.html');
let html = fs.readFileSync(indexPath, 'utf8');
// Add timestamp to script URL for cache busting
const timestamp = Date.now();
html = html.replace('dist/script.js?v=2', `dist/script.js?v=${timestamp}`);
// Set cache headers for HTML (short cache)
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
'Cache-Control': 'public, max-age=3600, must-revalidate', // 1 hour, revalidate
});
res.send(html);
res.sendFile(indexPath);
});
// Search videos