How to get around Nextjs 4.5 mb payload size limit #2095
-
|
Vercel has the maximum payload size for the request body or the response body of a Serverless Function is 4.5 MB. So anytime this function is serving a file larger than 4.5 MB, I get 413: FUNCTION_PAYLOAD_TOO_LARGE error. Is there any way to work around this using import got from 'got'
const handler = async (req, res) => {
if (req.method.toUpperCase() !== 'GET') return res.status(500).json({});
const content_id = req.query.id;
const file = await myDataBase.getFile(content_id)
const { url, original_filename, original_file_url } = file.meta;
try {
got.stream(original_file_url).pipe(res);
} catch (error) {
return res.status(500).send(null);
}
};
export default handler; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
The limit is a Vercel serverless function limitation that applies to both requests and responses. Unfortunately, using your options: Redirect to the file directly (simplest): Instead of proxying the file through your serverless function, redirect the client to the original URL: const handler = async (req, res) => {
const content_id = req.query.id;
const file = await myDataBase.getFile(content_id);
const {original_file_url} = file.meta;
// Redirect to the original file
res.redirect(302, original_file_url);
};Use pre-signed URLs: If you need access control, generate a pre-signed URL from your storage provider (S3, Vercel Blob, etc.) and redirect to it: const handler = async (req, res) => {
const content_id = req.query.id;
const file = await myDataBase.getFile(content_id);
// Generate pre-signed URL with expiration
const signedUrl = await generatePresignedUrl(file.url, { expiresIn: 3600 });
res.redirect(302, signedUrl);
}; |
Beta Was this translation helpful? Give feedback.
The limit is a Vercel serverless function limitation that applies to both requests and responses. Unfortunately, using
got.stream().pipe(res)doesn't bypass this limit on Vercel, even though you're streaming.your options:
Redirect to the file directly (simplest):
Instead of proxying the file through your serverless function, redirect the client to the original URL:
Use pre-signed URLs:
If you need access control, generate a pre-signed URL from your s…