Search code examples
aws-lambdanext.jsserver-side-renderingvercel

Next.js on Vercel, is server code on single Lambda?


I found this quote on the Vercel website:

When using Next.js, Vercel optimizes Serverless Function output for server-rendered pages and API Routes. All functions will be created inside the same bundle (or in multiple chunks if greater than 50mb). This helps reduce cold starts since the bundled function is more likely warm

But is this also true for getServerSideProps?

I have a small project with an API and another page that loads the data with getServerSideProps. When the first API is done. I would except the next page with the getServerSideProps would be fast, but that also seems to have a cold boot. The second time everything is fast.


Solution

  • Based on the two following comments from Vercel in related GitHub discussions:

    One thing to note — in Vercel (a recent update), Next.js SSR pages and pages/api routes get bundled into two separate Lambdas (λ), so you should not have to worry about the Serverless Function limit.

    Source: https://github.com/vercel/vercel/discussions/5093#discussioncomment-57628

    API Routes will all be bundled, each SSR page will also be bundled - both of these will be creating another function should they exceed maximum capacity (50MB) although this is handled a little before the limit to avoid it being too close.

    Source: https://github.com/vercel/vercel/discussions/5458#discussioncomment-614662

    My understanding is that in your scenario the API route (or any other API routes you may have) will be bundled into one function, and getServerSideProps into another function. If either exceeds the 50MB size limit then an additional function would be created.

    What you're experiencing seems to be expected. The API route and getServerSideProps would be a different functions, thus having separate cold boots.