provide a POST route which responds with an "application/zip" content type which results in the download of a zip file.
The zip file could be 10MB or 50MB.
I cannot allow the end-user in any way to understand what the true URL of the zip file is, so we're acting as a proxy.
We do not want to download the file to the local file-system.
Challenges: * I'm not sure what the max Vercel |(or Next.js) would allow a response to take * Proxying a req will pump up memory usage * Not sure what the cost implication may be for bandwidth * Once file download has completed ideally I'd persist a record to DB/Supabase
Has anyone had to do anything similar? Has anyone got any suggestions on how to do this?
Any help would be greatly appreciated!
https://stackoverflow.com/questions/68490546/how-to-download-a-file-on-next-js-using-an-api-route (frontrunner, see first answer) https://stackoverflow.com/questions/20351637/how-to-create-a-simple-http-proxy-in-node-js
To address your requirements, I'll create a POST route that acts as a proxy to download and stream a zip file without exposing the original URL or storing it on the local file system. I'll use Next.js API Routes with the App Router, which is the latest recommended approach 1.
Here's an implementation that should work for your use case:
The code returns the following error:
Download failed
at handleDownload (/app/page)
Revise the code to address the error.
I understand that you're encountering an error when trying to download the file. Let's revise the code to address this issue.
Let's revise both the API route and the client-side code to better handle and log errors:
These changes will provide more detailed error information, which should help in diagnosing the issue. Here's a summary of the modifications:
In the client-side code, we've added more robust error handling:
With these changes, you should get more information about why the download is failing. Some possible reasons could be:
If you're still encountering issues, the additional error logging should provide more context about what's going wrong. You might want to check the server logs as well for any errors that occur during the file fetch process.
make the POST route stream the file without fetching it