Replies: 1 comment 5 replies
-
I was getting @Timer @timneutkens why |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, first thanks for creating an awesome framework! We are using
serverless-next.js
to deploy our Next.js app to AWS Lambda / CloudFront since we are already heavily using AWS infrastructure.This is a problem/question/request, so I don't think it fits completely in one category.
Problem
I was helping take a look at an issue where when using
experimental-serverless-trace
target, the cold start request duration seems to have increased. For my production app, it went from ~1.5-2 seconds to 4-5 seconds. We have ~15 SSG and ~2 SSR pages.Upon investigation, we found that
experimental-serverless-trace
outputs doesn't optimizerequires
for each page JS, so when you include all dependencies as anode_modules
folder, then you may include components that are not needed.For example, using a popular UI framework like MaterialUI, if you import just one component e.g Typography from "@material/core" then the page JS will
require
the entire contents of @material/core, which if you just include all dependencies as-is, you will pull in ~129 components, most of which would be unused:Here's a test repro: https://github.com/dphang/nextjs-repros, with a very simple app with 3 SSR pages (which
getInitialProps
doesn't call any external APIs, just returns some fixed data) which showcases the above.If I import Typography:
index.js
will require entire@material-ui/core
which has 129 components:Max response times on
serverless-next.js
went from ~700 ms to ~1.5 seconds (based on sample of ~400 requests benchmark each using k6.io and around 20 concurrent requests for 30 seconds) as a result of Lambda having to cold start and load into memory unnecessary code. When this plugin includednode_modules
, it includes unnecessary code due to unoptimizedrequires
in the pages, e.g it takes up 22 MB uncompressed vs. 2.9 MB if bundled & optimized in a single page JS (which willrequire
just@material-ui/core/Typography
).I believe the same problem might be happening on Vercel. I also see the same ~1.4-1.5 second response times at high percentiles (~400 requests in 30 seconds). Obviously I don't know the underlying infrastructure, but AFAIK Vercel uses
experimental-serverless-trace
for its deployments too, so it may be suffering from a similar issue. But I'm not sure if this is a problem there too?App link: https://nextjs-repros.vercel.app/. I'll also try to test our production app's SSR perf on Vercel, but I suspect the same perf issues.
Request
Perhaps
experimental-serverless-trace
target needs to optimize its imports by tree-shaking/rewriting therequires
for each of the page outputs? So then any kind of downstream code (serverless-next.js, Vercel, etc.) that needs to consume these outputs to produce a bundle will not include unnecessary code in dependency code e.gnode_modules
. They will not need to do their own optimizations apart from just including the dependency code somewhere.Thanks in advance!
Notes
All Lambda tests are using 1024 MB memory, Node.js 12.x runtime.
Also, the related issue is here, which has more details if you need them (like performance benchmarks): serverless-nextjs/serverless-next.js#547.
Beta Was this translation helpful? Give feedback.
All reactions