-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serverless, treeshaking #8956
Comments
Note that you shouldn't zip all pages into one lambda. You should create separate lambdas for every page. Pages are standalone lambda functions. |
@timneutkens Cool, we are using a third party lib to help us with the deployment process but it doesn't split the deployments, there's an issue there serverless-nextjs/serverless-next.js#49 open for this. We should be able to deploy our app after the treeshaking but we'll come up with a better solution, hopefully we'll send a PR there to get things done in the right way. Thanks for the quick comment, ❤️ next.js, cheers. |
I confirm this. Loading font-awesome is making my serverless files huge. If I do deep imports to avoid tree shaking it helps:
@timneutkens If I add these settings from this article, https://wanago.io/2018/08/13/webpack-4-course-part-seven-decreasing-the-bundle-size-with-tree-shaking, to my webpack config I get quite a bit smaller serverless file sizes. Is there something unsafe about them or could whatever Next is missing be added to the defaults so everyone could get smaller sizes?
|
These don't do anything, they're enabled by default.
Minification is disabled by default for serverless bundles as it significantly increases build times and doesn't give any significant benefits when deploying serverless bundles. |
For me, the serverless files generated by Next go from about 5.5m to 3.4m with minimize=true. When having tons of routes and then sourcemaps this could add up. Though it seems like treeshaking not working with serverless build is the real issue. |
I'm also running into this issue too. Only setting |
@sdornan that would mean you're deploying wrong (all in one lambda I guess). A page is generally under 5MB by itself and when using the serverless target every page should be a separate lambda. |
Closing this as we're currently not investigating this and no PR was opened. |
@timneutkens Your feedback about why you intentionally don't minimize serverless files was very helpful. Is there any background you could provide on why Next.js doesn't feel it is worth the effort to investigate turning on treeshaking? The bloated code from huge libraries and font packs in everyones builds must be impacting every single next.js serverless users performance and most users probably don't even realize they lost treeshaking when moving to serverless so I am curious if this is just an oversight or if there is some big known issue. Some pointers on how one might attempt to fix could make it more likely for a PR to be opened. |
Hey Adam, based on how I'm reading your message it sounds like you're confusing client-side tree shaking with serverless / what we disabled (actually we didn't disable anything new). Maybe you're not confusing it, but either way it might be useful to type this out: Next.js has 2 compilations, client and server. A lot of the "tree shaking" is actually just minification (webpack inlines constants, terser removes the code block).
The thing with the serverless target is that you have to bundle every page as a separate serverless function. If you compare this to having to ship the full node_modules + all pages in a container you'll get that the size differences are tremendous. One example is how zeit.co had a 300-400Mb docker image. Now it has page render functions that are always around 500Kb or less zipped (more so because we have a lot of custom code in them, not because of Next.js). Anyway, to recap. Next.js does do tree shaking, just not on the server compilation, which is fine. |
So I am using several huge font packs and when doing a next.js non-serverless build, I load them like this I guess when in serverless mode, I expected each page to be built individually and only have the externals it needed because each page is essentially a unique independent build run through the same processes (minus terser) that non-serverless is. |
On ZEIT Now Next.js already uses this target instead of the current serverless one: #8246 trace has a better result for the case that you're describing but you have even more implementation steps (all taken care of automatically on ZEIT Now). |
Anyone with any solution? My pages are like 10MB each, and I have a lot of pages. |
I have same issue and my out dir size had 150MB!!! Try this yarn add -D rimraf
or
npm i rimraf --save-dev
add this command to your package.json "preexport": "rimraf out .next" this snippet run before your "export" command and delete all cached builds |
We've had serverless function cold boots from about 3-10s on Vercel with serverless nextjs.. In the meantime figured out a bunch of ways to optimise and reduce bundle sizes etc. So we're happy with setting Perhaps minification should be auto-enabled after certain sizes, or we should at least have a prominent report of these sizes and their impacts, as much as the current browser payload informations and warnings at |
Hey @patroza, would you mind including a reproduction of this cold boot? Happy to help assist and look into it further. 3-10s is definitely unexpected and not what we normally see. |
hey @leerob thanks for the response! It's a private app hosted on Vercel. I'm not sure what would be the easiest way for reproduction, debug access, some dump? |
In that case, please contact Vercel support 🙏 |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
Currently, if the next build is targeted to serverless, the resulting files don't go through tree shaking.
To Reproduce
Checkout this repo: https://github.com/fernandobandeira/next-serverless-treeshakingissue
Just install and build the project, if you inspect the resulting index.js file, you'll see that it has 1mb and contains all of the icons from font-awesome
Expected behavior
The only icon that should appear there is the one I've imported, it shouldn't include unused code.
Additional context
For me, this is resulting in each page of my app having 16mb, more than 500mb for the entire app and 120mb zipped, which makes it impossible to deploy on aws lambda.
If you remove the target to serverless, the compiled server file only includes the cofee icon, as it should.
The text was updated successfully, but these errors were encountered: