Introduction: All pros and cons of the static site generators and factors you should consider while choosing if they are right for you or not.
Static site generators are new buzz word on the web. They are getting popular day by day because of their advantages. There are serious talks going on weather static site generators are good or not.
Here in this post, I will try to cover all possible things to consider when deciding to use static generators or not.
The traditional tech stack that we normally use for our CMS is a server which gets the request from the user. Then it fetches the required data from the database to fill the view templates and send them to the user.
The problem here is that our data in the database remains the same most of the times (say a blog post remains the same until we update it after a certain time).
So here was the problem that with each request the server needs to work to generate the same content over and over again. This is the reason static site generators came into existence.
What are Static Site Generators?
Static site generators take data from various sources (including CMS API's, local markdown files) and generate static web pages with all the data and assets.
If the data changes on the server (say a new post is added) then a new build is triggered to regenerate the static pages.
Now when a user requests for a webpage the static page is sent without hitting the server.
Pros of Static Site Generators
There are many benefits to using static site generators. Here are some of the most important ones for which they are popular.
What can anyone hack in a static HTML page?
Static site generators generate static HTML pages which get served to the end-users. We can easily host these static files on any web server or directly to CDN's without the need of any backend.
With no servers involved in this process, it is very secure.
Most of the times hackers target servers and database vulnerability to get into the system. But in this case, both are out of reach from end-users.
Speed is one of the most important SEO factors according to google. Static sites have a speed that traditional CMS users can only dream.
Here is a flow of request and response in traditional CMS and static sites.
This is the reason why static site generators are the fastest ones.
Static sites are easy and cheap to scale.
In case of the backend, we need more RAM, CPU, Bandwidth, Storage, CDN's to scale up which is very costly and inefficient.
To scale static sites we only need more Bandwidth and Storage which are relatively cheaper and easily available by CDN's.
No need of a backend server
There is no need for the active backend for static generators. They are needed only to source data during the build which we can also put on our local system (generate static pages and push to GitHub).
For extended functionality like users management and dynamic content, we can definitely use backend but again it creates additional security risks.
You can host static sites for free on Github pages and Netlify CMS.
Even on free hosting these static sites have page load speed around 300ms when optimized well. We can also use Netlify CMS to manage content which uses GitHub as backend.
These free hosting can easily handle more then 50k traffic per month without any problem. Above that traffic, I will recommend switching to paid plans for bandwidth.
Cons of Static Site Generators
Steep learning curve
Static site generators are not like plug and play. You need some coding knowledge to customize and maintain static site generators.
Learning to customize static site generators is not easy and requires you to invest some time learning about them.
No user management
There is no inbuilt user management in static site generators.
User management means new users can register to site and create new content. This is possible through custom CMS on the back of your static site. But it opens a new headache to maintain security.
It is possible to manage users by netlify CMS but with limited functionalities.
The biggest disadvantage of static site generators is the build time.
Build time means total time static site generators take to build all the static pages.
Build time depends on the source, the greater number of posts and pages means more build time.
This is the reason that you should avoid static site generators if you are running a site which publishes content too often or has a greater number of posts.
Incremental build by Gatsby.js and Next.js comes to rescue to this situation, but they are experimental feature.
Files and assets management is a painful task for static site generators.
You need to manually interact with the file system (if possible) to operate on files.
If you are using headless CMS which supports file management then this might not be a problem :).
You might notice that I mentioned cons with their fixes so I am biased in favour of static site generators.
Yes, I am.
The web is very dynamic and it changes over the years for improvements in performance, user experience and security.
Static site generators are also a part of modern change.
I won't say that other CMS or tech stacks are bad. They are perfect for dynamic content like a news site, or blogs with thousands of authors.
If you have a simple website which serves static content most of the times (like a blog with few posts published per day) then you can choose static site generators.
Static site generators are improving day by day like the incremental build is a revolutionary feature which will eliminate the build time problem.
I am also prototyping with Gatsby.js and Next.js for HolyCoders, both seem fine until now. I will share my experience and updates soon.
If you are just getting started with web development then try to avoid static site generators as it requires some serious knowledge for workflow, customizations and tweaks.