Standalone mode for Gatsby #35532
Unanswered
tlgimenes
asked this question in
Ideas / Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi folks!
I work in a company that uses Gatsby for many of our websites. We are currently deploying it on our own Kubernetes infrastructure using Docker and Nginx. This setup has been working flawlessly for the past 2 years.
After the introduction of Gatsby v4, we decided to give it a try and speedup our build times using DSG and SSR. Using DSG and SSR is great for empowering our developers. This, however, makes it necessary to have a Node.JS/Gatsby capable server on our Dockerized infrastructure.
Our first idea on solving this problem was packing the website's
node_modules
into a docker image and use thegatsby serve
command to generate this DSG and SSR pages lazily.This solution worked, however, we saw a huge jump in our docker image sizes (going from a few 50Mb to 1.7Gb). This made our startup times skyrocket and made "scale to zero" impossible on these node.js pods. By analyzing the source of this increase in size, it became clear
node_modules
was the real villain in town.As a counter measure, we tried to create an empty image containing only
gatsby
as a dependency. Our hopes were that we would only get Gatsby dependencies on this image, which would reducenode_modules
sizes drastically. Turns out that the final node_modules had a size of ~500Mb.Taking a look at how other frameworks solve this problem, I've come across this standalone mode on Next.JS. The idea is that they generate a folder called
standalone
with only the minimal dependencies you need to have a Next.JS server running.By using this idea, and reverse engineering how the
gatsby serve
command works, I created a self containing script using Express and Webpack that outputs a file namedserver.js
. By placing this file at the root of a Gatsby project, it uses thepublic
and.cache
folder to serve a Gatsby website without the need of anynode_modules
folder. According to my tests, the final docker image had 48Mb of files for serving the gatsby site. This was a nice decrease of the initial 1.7Gb to 48Mb.The drawback I see on this solution is, since I had to code a new server, I'll also have to keep updating this script with new Gatsby versions.
I decided then, to share this with you and listen how thoughts about having a
standalone
mode on Gatsby. This could be supported via a Gatsby Plugin, like the Gatsby Fuctions Plugin or even entirely replace the current gatsby serve command.cheers!
Beta Was this translation helpful? Give feedback.
All reactions