feat(images): enable Next.js image optimization with complete upstream buffer retrieval and explicit MIME types
Deploy Canina / deploy (push) Successful in 2m4s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s

This commit is contained in:
parsa aghaei committed 2026-09-02 16:14:33 +03:30
1 parent 89e59c8427
commit 08ad51152f
4 files changed
+81 -11

No files matched your search

@@ -34,6 +34,24 @@ function getCandidateUrls(filePath: string): string[] {
return Array.from(set);
}
function getMimeType(filePath: string, fallback: string): string {
const ext = filePath.split('.').pop()?.toLowerCase();
switch (ext) {
case 'webp': return 'image/webp';
case 'jpg':
case 'jpeg': return 'image/jpeg';
case 'png': return 'image/png';
case 'gif': return 'image/gif';
case 'svg': return 'image/svg+xml';
case 'avif': return 'image/avif';
case 'mp4': return 'video/mp4';
case 'webm': return 'video/webm';
case 'mp3': return 'audio/mpeg';
case 'pdf': return 'application/pdf';
default: return fallback || 'application/octet-stream';
}
}
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ path: string[] }> }
@@ -44,6 +62,7 @@ export async function GET(
return new NextResponse('Bad Request: Missing file path', { status: 400 });
}
const rawFilePath = path.join('/');
const filePath = path.map(segment => encodeURIComponent(segment)).join('/');
const candidates = getCandidateUrls(filePath);
@@ -75,12 +94,31 @@ export async function GET(
}
}
// If 304 received without body or if not found with conditional headers, retry unconditionally
if (backendRes && backendRes.status === 304 && !backendRes.body) {
for (const targetUrl of candidates) {
try {
const freshRes = await fetch(targetUrl, {
method: 'GET',
cache: 'no-store',
});
if (freshRes.ok) {
backendRes = freshRes;
break;
}
} catch {
// Continue
}
}
}
if (!backendRes) {
return new NextResponse('Media file not found upstream', { status: 404 });
}
const responseHeaders = new Headers();
const contentType = backendRes.headers.get('content-type') || 'application/octet-stream';
const rawContentType = backendRes.headers.get('content-type') || '';
const contentType = getMimeType(rawFilePath, rawContentType);
responseHeaders.set('Content-Type', contentType);
const contentLength = backendRes.headers.get('content-length');