upd
This commit is contained in:
@@ -4,13 +4,36 @@ export function registerImages(app, cacheDir) {
|
||||
app.get('/img/cat/:id.avif', (req, res) => {
|
||||
const { id } = req.params;
|
||||
const imagePath = path.join(cacheDir, 'img', 'categories', `${id}.avif`);
|
||||
const resolvedPath = path.resolve(imagePath);
|
||||
|
||||
// Cache images for 1 year (immutable content)
|
||||
res.set('Cache-Control', 'public, max-age=31536000, immutable');
|
||||
|
||||
res.sendFile(path.resolve(imagePath), (err) => {
|
||||
res.sendFile(resolvedPath, {
|
||||
headers: {
|
||||
'Cache-Control': 'public, max-age=31536000, immutable'
|
||||
}
|
||||
}, (err) => {
|
||||
if (err) {
|
||||
res.status(404).send('Image not found');
|
||||
if (!res.headersSent) {
|
||||
res.status(404).send('Image not found');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Product images
|
||||
app.get('/img/prod/:id.avif', (req, res) => {
|
||||
const { id } = req.params;
|
||||
const imagePath = path.join(cacheDir, 'img', 'products', `${id}.avif`);
|
||||
const resolvedPath = path.resolve(imagePath);
|
||||
res.sendFile(resolvedPath, {
|
||||
headers: {
|
||||
'Cache-Control': 'public, max-age=31536000, immutable'
|
||||
}
|
||||
}, (err) => {
|
||||
if (err) {
|
||||
console.error(`❌ Error serving image ${resolvedPath}:`, err);
|
||||
if (!res.headersSent) {
|
||||
res.status(404).send('Image not found');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user