next.js

How to override the 4mb API Routes body size limit?


I have tried several ways to override the API Routes body size limit.

https://nextjs.org/docs/messages/api-routes-body-size-limit

export const config = {
  api: {
    bodyParser: {
      sizeLimit: '10mb',
    },
  },
}

I have some db responses that are over 7mb in size and am looking to be able to support responses this big. I am deployed to AWS and not Vercel, and I am not using serverless functions.

What am I missing to accomplish this so I don't keep getting that exceeds 4MB. This will cause the request to fail in a future version error message on the backend?


Solution

  • It's not because of Vercel / AWS. It's by design of Next.js, unfortunately.

    From the docs:

    Why This Error Occurred

    API Routes are meant to respond quickly and are not intended to support responding with large amounts of data. The maximum size of responses is 4 MB.

    Possible Ways to Fix It

    Limit your API Route responses to less than 4 MB. If you need to support sending large files to the client, you should consider using a dedicated media host for those assets. See link below for suggestions.

    So basically you're stuck with that 4 megabyte limit, even if you set it otherwise.

    You can try to use this tip to avoid the 5 MB limit.

    A tip from me, maybe you can try to build your own API if you really need to support responses that big. Express is a good choice. You can get fine-grained control over your own back-end architecture.