From 4abdf9c367bcf8f0e873deab5de1f1b2b1be54c4 Mon Sep 17 00:00:00 2001 From: TheMaddax Date: Tue, 28 Oct 2025 08:16:14 -0600 Subject: [PATCH] Fix PDF download: Handle binary file responses in API proxy --- frontend/src/app/api/[...path]/route.ts | 33 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/api/[...path]/route.ts b/frontend/src/app/api/[...path]/route.ts index 27912005..e6e2cda6 100644 --- a/frontend/src/app/api/[...path]/route.ts +++ b/frontend/src/app/api/[...path]/route.ts @@ -71,14 +71,41 @@ async function proxyToBackend(request: NextRequest, pathSegments: string[]) { 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(); - // Return response with same status and headers return new NextResponse(data, { status: response.status, headers: { - 'Content-Type': response.headers.get('content-type') || 'application/json', + 'Content-Type': contentType || 'application/json', }, }); } catch (error) {