Fix PDF download: Handle binary file responses in API proxy

This commit is contained in:
TheMaddax 2025-10-28 08:16:14 -06:00
parent d5037b35a9
commit 4abdf9c367

View file

@ -71,14 +71,41 @@ async function proxyToBackend(request: NextRequest, pathSegments: string[]) {
const response = await fetch(url, options); const response = await fetch(url, options);
// Get response body // Check if this is a file download (binary response)
const contentType = response.headers.get('content-type') || '';
const isFileDownload = contentType.includes('application/pdf') ||
contentType.includes('application/octet-stream') ||
response.headers.get('content-disposition');
// Handle binary file downloads differently
if (isFileDownload) {
const blob = await response.blob();
const headers = new Headers();
// Copy important headers for file downloads
if (response.headers.get('content-type')) {
headers.set('Content-Type', response.headers.get('content-type')!);
}
if (response.headers.get('content-disposition')) {
headers.set('Content-Disposition', response.headers.get('content-disposition')!);
}
if (response.headers.get('content-length')) {
headers.set('Content-Length', response.headers.get('content-length')!);
}
return new NextResponse(blob, {
status: response.status,
headers,
});
}
// Handle regular JSON/text responses
const data = await response.text(); const data = await response.text();
// Return response with same status and headers
return new NextResponse(data, { return new NextResponse(data, {
status: response.status, status: response.status,
headers: { headers: {
'Content-Type': response.headers.get('content-type') || 'application/json', 'Content-Type': contentType || 'application/json',
}, },
}); });
} catch (error) { } catch (error) {