Next.js Discord

Discord Forum

serverless functions tree-shaking

Unanswered
American Bobtail posted this in #help-forum
Open in Discord
Avatar
American BobtailOP
Hi 👋

i was wondering why each serverless function deployed on vercel would have the same size, even though @vercel/nft gives indications of the deps of each handler.
as i understand it, each handler uses the same global package.json which can be quite heavy and take a toll on lambda cold starts.

(in my screenshot example, i import the sharp library in only one of my 2 handlers, yet both of them end up with a 15.5MB size.)

is there a reason for that or could we think of a way to change the nextjs build script to include a specific package.json for each handler with only the dependencies it needs itself?
Image

1 Reply

Avatar
American BobtailOP
bump + a little more context:
over the weekend i looked at the source code of nextjs and vercel/next builder and ended up understanding that the lambas are grouped, and a layer is created with all the dependencies, so that any handler has access to anything it might need.
so my new question is, shouldn't the functions size ignore the layer size ?

let's say i have a dependency A that weighs 30MB, and dependency B that also weighs 30MB.
if i try to deploy handler1 with dep A and handler2 with dep B i'll end up with a layer of 60MB (aws limit is 250MB for a layer) but vercel will tell me that each of my functions weight 60MB (aws limit is 50MB for a lambda)
i'm guessing vercel won't even try to uplaod to aws since it'll detect 60MB > 50MB even though each of my function should only weigh 30MB.