Categories
Software Technology

Client and Server Side Rendering Static Site Generators

In the last few years the popularity of Single Page Applications has slightly increased. Before the SPA revolution, the majority of application logic was done back-side with some AJAX additions and the user interface improvements were done by JavaScript. Due to the fact that only a small piece of content was added asynchronously almost the whole content was rendered Server-side and available even while browsing without JavaScript.

When the SPA application appeared, in the era of Backbone JS, Ember and Angular, the first versions of all search engines crawlers were clear. None of them supported javascript rendering. Crawlers were just receiving the content rendered by servers and indexed by web pages which ended up with a terrible SEO so common while relying on javascript too much. Technology is evolving very fast, so a question arises whether the SEO problem is still valid for SPA applications in 2018/2019.

What is SSR? Do we need it?

SSR stands for Server-Side Rendering. It is a way to prerender parts of the application on the server like in a classical back-end language. A browser makes a request to a server, the server runs application pre-rendering, as it goes, and responds with a generated html. On the browser site the JS logic is automatically attached to the rendered state and the application can used as usual.
On the opposite end, we have Client-Side Rendering which is a standard method for all SPA frameworks. Such applications generate whole content in the browser runtime. Typically the client-side applications receive from the server a basic html structure with an empty placeholder “div” where all application components will be rendered.

There is also a third method similar to Server-Side Rendering called Static Site Generation. Although it may be confused with SSR, but the idea is a bit different. With this method during a building phase  html files are generated which will be served from the server. Such html files can be effective at rendering hundreds of components which will take time if done on client-side.The application can be pre-rendered once in a specific state, only to send it to the end user in simple html files in the fastest way possible while using http hosting.

We have 3 options to choose from. So, which one I should use?
Each option is used in specific cases and fulfils different needs. Let’s hope the following paragraphs will help you choose the best one.

Which solution I should use?

Client side rendering, server side rendering, static site generators

The infographic below shows a general idea behind each approach. The final effect is the same: fully functional application. The difference is in a way it is achieved.

At a first glance, all three graphics look almost the same with the same number of steps. The key difference is timing for each action. In client-side rendering, application stays long in the “loading” state, while it is making ajax request for necessary data, compiling views and injecting everything into DOM. Server load will be small even for a considerable number of requests. The browser side is overloaded with work which may even work longer and slower on weak computers or phones.

For server-side rendering most work is on the server-side, which does most of the job. The server executes the application code and generates html based on it in order to create a response.
After that the browser will need to attach all the events to the existing html, create virtual DOM etc. When does the server-side work the most? If the server is not powerful enough, the response time may increase slightly. With the server-side rendering we need to think how much traffic the application will generate and whether the server is able to handle it fast.

The third option is to use Static Site Generator which is the fastest one. The server just sends previously prepared html. On the browser side the framework that is being used is attached to the existing structure, as a result the application is ready to use. Neither the server nor the browser is excessively overloaded.

Let’s make a small comparison table for all the 3 solutions:

ProblemClient-Side renderingServer-Side RenderingStatic Site Generators
Social media sharing⛔??
SEO, search engines❓it’s complicated 🙂??
High traffic?⛔?
Frequently changed dynamic content??❓it depends
Easy to implement?⛔❓it depends

Quick notes about the above points:
In the case of social media sharing like Facebook or Twitter, they don’t execute javascript. With the client-side generated application there is always the same set of OG tags and meta properties, there is no possibility to share a specific page/route in our application/website. If social sharing is a must, then it is better to choose SSR or Static Site Generators.

SEO remains a big mystery. A few years ago the situation was clear because none of the the search engines  understood javascript. So, no content rendered client-side was indexed. The situation changed once Google announced that their crawler would finally render javascript. (The following paragraphs will contain small test to check them out). With SSR and Static Site Generators we are certain  that the content will be indexed, whereas in the case of CSR it’s a bit more complicated issue 🙂

For high traffic websites it is always  better to put as much as possible to the client, and CSR seems to be a reasonable option.

Static Site Generators don’t work well with a dynamic content because all html files are created much too often. Naturally, there can be parts which don’t change, to fully render the server-side and the dynamic ajax content on the client-side, in such a case Static Site Generators will do the job.

The easiest option to implement is a standard SPA app done client-side without any additions. With server-side rendering we have to maintain server part, which may be tricky based on solutions. With Static Site Generators depending on the chosen framework, the implementation will take as much as for client-side but with some additions.

How do crawlers work?

Crawlers are automated scripts which browse www to collect information about web pages and the connections between them. They are connected with search engines and pass on information to them. Based on the knowledge web crawlers/spiders collected search engines decide on the order in which search results appear.

Historically, no crawlers were executing javascript. Client-side rendered application was not indexed at all. The situation changed a little when Google announced that crawlers they were going to use execute javascript. It was great news but let’s remember there is world outside Google. And, there are more search engines on the market such as: Bing, Yahoo, Ask.com, Baidu, Yandex, DuckDuckGo and others.

According to market share calculations Google powered 76% of all searches. It means that 24% of searches around the web are done with different engines! How should we know how they will see our pages?  Here you can find fantastic tests with different frameworks and the most popular search engines. The results of this comparison are clear: only Google and Ask.com understand javascript and are able to index client-side rendered application correctly.

While creating a an application constantly ask yourself a question whether it is fine to be indexed only by Google and Ask.com or maybe it would be a good idea to consider other search engines on the market.

How will Google crawler see my page?

A while ago Google provided a great tool for developers in order to check how Google bots/crawlers can see our web pages. With just a few simple steps we can run a diagnostic of our page and see whether all information that is posted  is correctly rendered by the bot script. Bots may either reject some external scripts that are included or not render something if the execution time is too long. Unfortunately, there are no similar tools for other search engines and the developers learn about their functionality only from experiments like the one linked in the previous chapter.

Let’s test few simple examples using fetch as google tool. In the experiment we will check how Google render client-side rendered React application in different configurations.
Google needs to verify you as the owner of a webpage to be rendered by bot. There are multiple methods to achieve this. The simplest one requires uploading a file to the server or adding a special meta tag to the webpage.

Test 1 – a simple React application with nested components

Test 1 - a simple React application with nested components

The above example simple application with 2 nested components is being rendered by displaying image from an external link and a simple text. Everything is correctly rendered by Google bot. This proves that Google can handle correctly such a simple javascript rendered app.

Test 2 – a React application with a content loaded by slow AJAX response

Test 2 - a React application with a content loaded by slow AJAX response

In the above example there is an extended application from the previous example which adds a third component by requesting content from the server by using ajax request. The example adopted NASA api and it also added a small delay of 5-6 seconds for the content to be available the the application. When the content is ready both the image url and the simple text paragraph are rendered. Looks like again the bot has handled it correctly.

Test 3 – a React application with a very slow AJAX request

Test 3 - a React application with a very slow AJAX request

In the third example the first problem occurs 10 seconds have been added to the loading time for the AJAX response. The component is not rendered at all and it was skipped by Google bot. The example has used a single AJAX request with long loading time which probably would never happen in reality. But what about having a very complex application which has been created from hundreds of components and has relied on multiple external data endpoints? If there are some who require all Promises to be resolved then it is possible to end up with the same situation as the one above, some parts of the content will not be available for Google crawler and will not be indexed. In such a situation the only solution is to pre-render  SSR or Static Site Generators.

Summary

While developing a new Single Page Application we should carefully think over our needs. If we are going to implement an internal application, the admin panel where user needs to log in, we don’t need to worry about SEO, indexing or the social media stuff, client-side rendered application will do it for us.
Taking social media sharing into consideration use specific Open Graph tags for particular pages, having different titles and descriptions for each page etc. requires SRR or Static Site Generators. Social media tools don’t execute javascript so replacing meta information for specific routes will not be possible.

When we want to develop a website which should be indexed by search engines we need to be careful. If we only care about Google we can write client-side rendered application and in order to make sure that everything will be correctly indexed we should frequently check with fetch as google tool. Too long loading/rendering times may result in a part of the content not being indexed at all. If we need to support other search engines, the only solution will be to go for SSR or Static Site Generators solutions, because they are not prepared to oversee javascript applications.

While choosing between SSR and Static Site Generators we should consider how often our data will change. If we implement a highly static website we can choose one of the existing solutions for Static Site generators and develop our website in the technology we love to work with.

There are multiple libraries providing SSR and Static Site Generators solutions for top SPA frameworks. There are solutions for React, Vue or Angular. What are the differences and how to use them. Hope the lecture of my next article will help you choose proper tool for your next application.

See also:

Categories
Software Technology

Front-end history, 2018 trends and Espeo choices

Front-end development has changed slightly over last 10 to 15 years. Javascript has evolved the most among the existing programming languages. It turned away from writing simple logic on websites by using ugly, unstructured code and plugins, and evolved into building completely functional Single Page Applications.

Let’s go on a small journey back in time to see how it used to look in the past and what changes have happened over the last couple of years.

The good old days…

Front-end history, 2018 trends and Espeo choices
We are in 2006/2007. It’s a time when most 30+ developers started their careers. Javascript was weak and was not taken seriously. Most developers chose backed languages as their main technology to master. Web applications were using reloads every time a user interacted with UI. AJAX was still something new and was used only for updating small elements on the pages. The key player was jQuery, it simplified DOM data manipulations and added a nice wrapper for asynchronous code. Thousands of jQuery plugins were created which were responsible for specific UI interactions such as sliders, carousels, tabs and many more.

The term front-end developer didn’t exist then. People involved in javascript stuff were called web developers. Their responsibility was to turn designs into fully working web pages. Designers worked closely together, they styled using pure CSS without any any compilers and added  small portions of JS code to make websites more interactive. Various browsers with different capabilities posed a great challenge as they were implementing different standards. This is the era of many browser tricks and hours spent on Internet Explorer 7 support. Any automation like concatenating and scripts minification were done by back-end technologies and in most cases no one cared about it, ending with a long list of scripts and linked styles in html head section.

JavaScript revolution

A few years later, between 2009-2010 a lot of things happened in the front-end world. Many new key players appeared on the scene. It’s worth mentioning about a few frameworks which made a lot of noise at  that time:

  • Backbone JS It was the first attempt to make a 100% javascript application without any annoying reloads. The idea of Models and Collections behind Backbone made web application development more structured and created with well documented and clear boundaries understandable  for many developers.
  • RequireJSIt was the first solution for module dependency management in JS. It allowed  to split a code into multiple files so it could be downloaded on demand in browsers which was a great step into the future.
  • Sass and Less compilers became mature enough, which meant no more pure CSS, because NodeJs was a toddler compilers compared to other languages like Ruby, Python or .Net.
  • NodeJS It enabled javascript code to run not only in browsers, it  revolutionised front-end development workflow once and for all.
  • AngularJSIt was the first angular version, which gained a lot of popularity, and most developers turned to Angular world quickly.
  • PhoneGap It allowed to write html applications which worked  inside iOS/Android WebView, it was not perfect and frequently ended with performance issues because of Webview boundaries but enabled to create their own mobile application knowing only html and js.

Naturally, there is much, much more. Javascript stopped being just an additional technology. Companies started to hire “JS Developers.”

It’s a little too fast…

Frontend history


Evolution sped up after 2010. In the following years, thousands of new libraries appeared. Finally, JS Devs had package managers for dependencies. Most users started with bower and slowly turned into NPM packages when nodeJS started to be more mature. No more keeping all dependencies in projects repositories.

In the “good old days” any attempts to make some automation like concatenating files, minification or images optimization needed to be done outside of the JS world. But change was coming…

New task runners started to appear. Most developers began to use Grunt or Gulp for simple automation. Many tasks appeared for both frameworks allowing the possibility to check JS quality, to concatenate files or even to watch changes in source files to do browser refresh or inject changes asynchronously without refreshing. Both tools started to lose popularity for the  benefit of npm scripts. Small pieces of nodeJS scripts had the ability to do everything nodeJS allowed.

The biggest changes appeared in javascript SPA applications. Developers were inspired by solutions put forward by Backbone and Angular teams and started to recreate and improve on similar solutions.

It’s when: Ember, Knockout, and React were gaining popularity. The last one made the biggest impact in the front-end world for a long time.

Another aspect of the development which changed slightly during this time was the modular approach to development. In “the good old days”, modules in javascript did not exist. The only way to preserve privacy was to use design patterns such as Module Pattern, which were created from an unnamed and immediately executed javascript function. It was the only way not to pollute global scope with everything we have implemented. Require JS mentioned in the previous chapter made some progress here with AMD approach. Another approach which appeared was Browserify, it used common JS modules like in Node JS but inside a browser and it compiled them to a single file which should have been injected in html. Finally, Webpack appeared which became a standard for bundling and dealing with modules for client-side applications.

A big impact, a way of development, was also caused by UI Frameworks. Bootstrap, Foundation and, later on, Material-UI. Each one included a complete set of components, fonts, icons and utils such as the frequently-used grid system. Each one sped up the development process and helped to achieve consistent UI without any additional styling.

Fast and furious….

Few years ago, it was easy to follow all the news taking place in the front-end world. But the popularity of javascript was increasing. More and more developers started contributing to open source projects.
There are now millions of libraries/frameworks/scripts in javascript. Let’s take a simple test:

  • Think of any name of an item not connected with programing, e.g. focus on any home-related ones such as: table, home, knife, doors, bed….
  • Try to google it with “js” suffix
  • Almost every name is a name of a library, npm script or github repository…, you should have 100% match in the first 5 words which popped to your head.
Vue, React, Angular

Almost every aspect of the front-end development has changed. In the case of styling, for a few years, SASS was unquestionably a leader. We have adopted nice architectural solutions for sass such as: BEM or SMACSS . Then there was an idea that styles could be created directly in js tied up to the components with the proper scopes. Currently, there are large numbers of similar CSS in JS solutions.
In the case of SPA application situation is clearer, there are 3 competitors: React, Angular 2+ and a newbie, Vue; a detailed description and comparison of each is a topic for another blog post. Most developers choose one of them. Some people will agree with the image below:

The number of mobile application frameworks for javascripts from which we can choose is also huge. There are: React Native, Ionic, NativeScript, Cordova. React Native is gaining popularity and wider support. We need to choose the mobile app technology for our project carefully, maybe for the simpler ones PWA would be enough?

The first Backbone JS applications were huge and each month the application was getting bigger and bigger. When the bug appeared it was hard to tell in what  state application actually is; if it had been Redux or MobX debugging would  not have been so painful and time consuming.

The world of js is a huge one, and I only mentioned a few core libraries. If we dig into a specific area like SVG graphics, WebGl, Web Components, Server side rendering more and more good, well-known libraries will appear and each one deserves their own article. Server side rendering is a “hot” concept nowadays as we have entered the SPA era, where all content is rendered client side. Still, there are crawlers and social tools which do not understand javascript.
For the all non believers: JQuery is not dead! jQuery is still one of the most popular library on the internet. If you think jQuery is always ‘evil’ please read this.

State of Javascript in 2018

So, what is the current state of javascript? It’s widespread use says it all.

You can check out what the current trends are. Take heed and read the conclusions in each section while choosing the technology for a new project. The newest “hot” technology may not always be the best choice because of the lack of documentation and solved issues. Frameworks which are mature have a lot of contributors. Most errors have already been solved and if a new ones appear, there is always a person who will quickly address our problem and solve it.

How to be a good front-end developer in 2018? Naturally, we all need to follow the news frequently. So much is happening everyday, and it is always helpful to go to news aggregators such as: DailyJs. You will not became a technology specialist simply by reading articles, it just gives you an idea of what to learn next 🙂

A few years ago front-end developers were able to do everything their employer needed. It was not too difficult to be knowledgeable about css/sass, vanilla js, existing SPA frameworks.
Nowadays it’s a bit more complicated, it’s hard to be a specialist in everything and it’s not possible to reach master level in everything related to front-end. We need to specialize and choose our path to become: React, Angular 2+, Ember, Ionic, NativeScript, Styling etc. specialist.

Espeo Choices

Espeo choices

In Espeo we follow current trends. For our projects we use fresh well-known and well documented technologies.

In the case of creating new SPA projects React with Redux and Angular 2+ are a way to go, with increasing React dominance. Developers use React Native or native applications for mobile solutions. However, React Native can’t solve all mobile problems. As far as styling Sass or styled Components are concerned we use them extensively in our projects. Webpack and npm scripts have become a standard for all the projects we develop.

We are open to new technologies and still explore new possibilities such as: Vue, TypeScript, Reason…and many more. Possibly, we’ll only consider a few of them for our future projects.
 
[contact-form-7 id=”13387″ title=”Contact download_8_reasons”]