This commit is contained in:
sebseb7
2025-11-23 12:18:01 +01:00
parent 68cc98cfd4
commit 22a4b88f58
3 changed files with 94 additions and 7 deletions

View File

@@ -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');
}
}
});
});