Demystifying Single Page Applications

Dhrubajyoti Basu Roy
Dhrubajyoti Basu Roy
5 min read
Posted on August 12, 2022
Demystifying Single Page Applications

Overview

Over the past several decades, the web has gone through rapid transformation, not only in terms of the enhanced user experience but also how the underlying technologies have evolved.  Lately, there has been a lot of buzzwords concerning Single Page Applications (SPAs). Let us dive in to understand what SPAs really are and should you consider using them for your next project.
 

Spoiler Alerts!

Quite contrary to what the name suggests, typically an SPA no way intends to serve your entire application on a single platter (a web page to be precise). Rather, it’s all about how many times your web page is rendered from the server. While traditional web applications render a fresh HTML page from the server every time the client makes a request, in case of SPAs a web page is rendered only once, just during the initial load.
 

How Things Work?

When the client makes an initial request, the server responds by sending an HTML document containing the required CSS and JavaScript. Till this point SPAs behave no differently. The client makes subsequent requests through AJAX query, and in return the server sends JSON data. Without any additional rendering or page reloads, the JSON data simply updates the existing web page using JavaScript APIs.

SPA Architecture

SPAs rely on both client-side and server-side rendering technologies. In case of client-side rendering, the server sends a simple HTML file with linked CSS and JavaScript. The SPA collects the data, create views, and inserts it into a Document Object Model (DOM). In case of server-side rendering, the server itself performs majority of the tasks, leaving the SPA framework to create a virtual DOM and execute subsequent actions.
 

Why Use SPAs?

Although initial load time could be high, navigation could be really smooth with faster response creating a seamless and desirable user experience.

SPAs generally incorporate excellent caching mechanism. They stock data from the server and can work effectively in low bandwidth environments and even offline.

SPAs are built on frameworks that are easy to debug using Google Chrome’s developer tools.

The backend and UI relies on separate code base and hence can be modified independently. The SPAs are cross-platform apps, which means you can use the backend code to create mobile and desktop applications as well. So, if you plan to create mobile apps to support your web apps, building an SPA could be a smart choice.

SPAs should also be your first choice if you intend to create social networking applications or build Software-as-a-Service (SaaS) based solutions.

Some of the leading applications based on SPAs are Gmail, Google Maps, Facebook, Netflix, Airbnb, and PayPal.
 

Trade-offs and Things to Consider

SPAs cannot store browser history. Hence, you cannot navigate back and forth inside an SPA based web application. If you hit the browser’s back button, you will be redirected to the previous web page. There is no way you can get to the previous state of the application.

SPAs are vulnerable to cyber-attacks. Hackers can use cross-site scripting (XSS) to inject malicious code into your web applications. Hence, if your applications contain sensitive data that could be compromised, it is not advisable to use SPAs.

Search engine optimization (SEO) is quite challenging for SPA based web apps. The JavaScript files should be indexed so that the search engines can identify them using web crawlers.
 

The SPAs rely on some key JavaScript frameworks for carrying out complex client-side rendering tasks. The notable ones are Angular, React, and Vue. Angular is one of the oldest frameworks having a community of sizable contributors. Google uses Angular to build many of its web apps. React is the most popular choice with the largest community support. It can be integrated with a lot of different frameworks and technologies. Vue is a lightweight framework that offers simplicity and flexibility. It is the latest in the list of popular frameworks that we discussed.
 

Conclusion

SPAs are great choices to build fast and reliable web apps that provide exceptional user experiences even in conditions where internet speeds are unreliable. Whether you wish to build your next mobile app, a new social media app, a content streaming platform, or a SaaS application, you could not go wrong with SPAs.

Well, by now you already know that many of the apps you have been regularly using are SPAs. Have you built an SPA or contemplating one in the near future?