-
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 Next: make next
dev-only dependency, introduce next-server
for smaller builds and faster bootup
#4496
Comments
wou, this is awesome !! :o |
wow... that's great. some questions for
|
@Nishchit14 You wouldn’t add I’m sure |
So what we’re talking about here is extracting the existing server into it’s own package. So it’ll work the same way as before, but instead of importing next, you import next-server. |
This is awesome! I and other people that I know have been running Next.js on top of AWS Lambda using scandium (using this guide) and some of the main problems have been:
Very glad to see this initiative and happy to help out! |
This is a great idea, we have the same with Nuxt.js, we called it |
Following this closely. As a data point for what is possible, www.bustle.com is a SSR preact app on AWS Lambda with <1s cold starts. The entire deployed production zip file is 166kb. That is all the application and library code. Webpack is used for bundling. |
Thanks for sharing @southpolesteve. That's super impressive. #goals |
The user case looks very similar to Why not use the same nomenclature? |
I am messing around with next.js and serverless using this example and curious if there any way to accomplish smaller builds now. Is there a list of node_modules that we absolutely don't need in production and can be excluded with config files in a packager like serverless or repack-zip? |
@Enalmada I'm running next.js with several deps and |
@albinekb I am inspired by southpolesteve bustle.com response above of 166kb and wonder how much of my "45MB" is useless and easy to remove if I just knew what to put in a dist exclude file as a hack until this excellent ticket is finished. |
@albinekb Highly recommend you look at using webpack, parcel, or rollup to bundle your JS for lambda. You'll save on size, but also boot time as hitting the filesystem via normal node require is pretty slow. |
If you're deploying to ZEIT Now and you want to keep your image small for fast cold boots, you can use a tool like Package Phobia to check the size of a npm dependency before you install it (or just check the size of current dependencies to cut the bloat). The readme also has many similar tools to help you fight bloat. Check it out here: https://github.com/styfle/packagephobia |
Wasn't this supposed to be addressed in the Next 7 release? :( |
Damn you antd: https://packagephobia.now.sh/result?p=antd |
@Enalmada it's probably the dependencies of antd that are responsible, not the library itself. I have been looking into that for https://packagephobia.now.sh/result?p=%40material-ui%2Fcore. Most of the weight comes from one or two dependencies. |
To be clear about this, Next.js 7 lays the foundation for Serverless Next.js, we've removed around 5 routes, leaving only 2 really needed for production. |
Has anyone ever gotten next.js to work with rollup? I feel like I got very close...running rollup on my 60m dist file brought the size down to 6m. Unfortunately the dist file wouldn't actually startup and I think it is due to a single circular dependency in next.js code that is a warning during rollup. If someone could weigh in on the possibility of removing a circular dependency in next.js code we might all be very close to much smaller builds and faster bootup: |
@southpolesteve would you be able to share anything around your webpack bundle config? |
@timneutkens Should I move |
@Skaronator if you're implementing using #5927 it's neither, as per the specification it'll output one bundle per page, no dependencies needed. Meaning you can take
This will render the page and finish the response |
That's awesome! I guess we shouldn't need a custom express server anymore, we could just require the serverless file based on the path 🤔 edit
|
Looks like you're confusing the "custom server" with "serverless", their APIs are completely separate, there is no
|
Indeed, and i have no idea why the code above worked (maybe it was just the cache) but neither of mine or your piece of code worked because Also, i don't want to use express/koa/whatever since it will break the whole purpose of this next new feature.. ( I'm out of ideas :/ |
Maybe this will help you @guillaumebreux https://github.com/zeit/now-builders/blob/master/packages/now-node-bridge/bridge.js |
Thanks @timneutkens, I appreciate your help. I'll stop polluting this thread and keep digging and i'll write an example if I ever find a solution 😄 |
I'm a bit unclear. This thread looks like it applies to two scenarios: server-less apps and pre-compiled servers that include all of the necessary server-side npm packages webpack'd together. The gif in the first comment on this thread appears to regard the latter scenario, which is the one I'm interested in. It looks like it uses Is what I've written so far accurate? If so, even though it's in canary, is there any way I can get early access to it? My current solution (which for evident reasons, I'm not using in prod*) is to remove the function from I would love to be able to produce a pre-built server so that I don't need to install 200MB of node_modules and then spend 2 minutes compiling on my poor, little production VM every time I push an update. * "prod" is used loosely, as this project isn't mission critical or all that professional |
Next.js 8 serverless target has a zip size of 42Kb by default 😌 |
That's awesome! Looking forward to this! |
I have exactly the same question as @dfoverdx. I want to make a server build, that also includes all the node_modules needed for running. I am using a custom server with express so I don't expect those dependencies to be included in the package, but now you have to install all dependencies on your server too (react, next, axios, ...). I don't understand how this is not by default? Overwriting the
But react and react-dom are still required on the server. I cannot figure out how to include those as well... |
Unfortunately it's not possible to create custom server with current serverless mode. And if you use normal mode you need to include next and all its dependencies because generated _app.js in .next is depending for example on next/router Why coudn't non-serverless mode also bundle next? |
Note, that since next 8, you can require 'next-server' instead of 'next' in your server.js, and you only losing hot reloading during local development by doing that. In theory it gives you ability to do CI build on intermediate server and not to copy Webpack related dependencies to production instances. But we haven't yet tried it in our project. |
@ElvenMonky waiting for something like this since a year, but could not find anything about this in the docs or examples. @timneutkens could you please verify this? If so, I might experiment with such a setup, and send a PR for the docs/examples. |
Unfortunately this doesn't work. First, running serverless build with server target is actively blocked with following message: "Cannot start server when target is not server. https://err.sh/zeit/next.js/next-start-serverless" Then, if you decide do to do normal build, then build files are referencing things from |
Next is doing that internally as a Babel Plugin as you can see here: |
@sheerun as I mentioned also in #7011 you can eliminate unresolved I've forked and adjusted example for custom express server to illustrate the solution: https://github.com/ElvenMonky/next.js/tree/custom-next-server-express/examples/custom-server-express P.S.: I'm still really excited about #5927 no matter, that my application requires everything listed in TODO, not to mention dynamic routes and serving static content. |
I've been using this advice, but unfortunately can't use Runtime Configuration as that requires I don't know why but Is it possible to move next/config to a different package, like Let me know if acceptable, I'll create a PR. |
Hey everyone! This issue has been implemented since Next.js 8, and is still around in Next.js 9. I'm going to close this as completed. 😌 |
Sorry I got confused with this issue: #7011 See deleted commentSome
But there are a few for which there is no support for such optimization mentioned by OP of this issue.
Not needed as they are inlined even in server (AKAIK)
|
Hi @Timer - Are you referring to the servless target, or the replacing of 'next ' with 'next-server' ? If we replace next with next server, that doesnt change the node_modules folder size unless the packages.json dependencies is also updated. Is there an example of either approach available? with serverless, my use case is deploying to AWS Lambda. |
The approach outlined in the initial issue evolved into the |
@timneutkens I was under impression that you understand this difference and was hoping to see more robust solution someday to the initial issue as it is described by @rauchg |
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. |
The problem: when optimizing for a production build, invoking
next start
or usingrequire('next')
within a customserver.js
involves bringing the entire set ofnext
dependencies, including the ones related exclusively to development, such aswebpack
.Not only is this problematic from a build image standpoint and download time performance when generating production builds, but it also likely hurts bootup time. Note: This is lessened by the fact that we carefully lazily load heavy dependencies such as
webpack
in dev mode.For the performance conscious and those sensitive to cold start times (see for example: https://twitter.com/rauchg/status/990667331205447680), we can introduce a
next-server
package.It would have the same capabilities as
require('next')
minus all development-time settings, plus a very smallnext-server
CLI that can open a port and perform graceful shutdown.What we want to optimize for:
next-server
has to be as small as possibleFurthermore, we should provide an example in
examples/
of how to usenext-server
in combination withpkg
to export your Next.js application as a self-contained ELF binary.The text was updated successfully, but these errors were encountered: