What is a Single Page Application?
In the post- How to master Front-End Development? , I mentioned Angular is useful in building complex single-page applications. In fact, with the growing popularity of Angular, this term has become quite a buzzword (maybe the other way round).
The term Single Page Application (SPA) can be a little bit confusing in the first impression because hardly any website that we come across seems to have only a single page. In fact, a real-world complex website may have hundreds of pages.
Some of the biggest websites that we know- Facebook, YouTube, Gmail, Twitter, etc. all are SPAs. So, why are we calling such complex websites Single Page Applications? And, if they are Single Page, then what is Multi-Page?
To understand this let's first go through some ways in which a website can be built.
Static Websites
In the HTML section of How to master Front-End Development? , I illustrated the snapshot of a simple HTML page I designed long back. It was a replica of my school exam question paper.
Let's say I decide to create a website that will just contain these question papers for anyone to refer to. The most natural way would be to create a server that serves this static HTML page whenever someone visits a certain URL (maybe, www.schoolexamsarchives.com).
I know that the content of my website isn't going to change that much as it's just kind of archives. And once or twice when I need to change it I can simply edit that HTML being served and I am good to go.
Also, the website has two more sections- About and Contact Me. When a user opens one of these sections, a different HTML page is sent back from the server to be rendered in the browser.
So, that is a Static website basically with 3 HTML pages.
Dynamic Websites
Let's say I want to build a simple web application that generates invoices. People provide their names, the products purchased and their prices, etc. The server receives this information and based on a template it prepares an invoice based on the information it received and returns the invoice back to the browser.
You can think of similar use cases of a dynamic webpage on your own.
This forms a dynamic website.
Multi-Page Applications
Till now, the examples that we saw had 1 thing in common though- Each time we navigated to a different section on the website, the browser sent an HTTP request to the server. The server either returned back the static HTML pages or some HTML pages with dynamic content.
And this is how traditional websites used to work. And still, most of the websites on the Internet work like this only. These are called Multi-Page Applications (MPA) because there are several separate HTML pages which the server is sending based on the request.
Single Page Applications
An HTTP request is a costly operation (the expense being time). In the process of sending a request, the server processing that, and then the server returning a response, we experience a delay. The delay can be more or less depending on the internet speed, load on the server, etc. Now, usually on a website every time you will be clicking some link, navigating somewhere, etc. And at every click, the process of HTTP request and response is going on which slows down things and the user has to wait till the page loads.
Out of this requirement of a better UX, the idea of Single Page Applications originated. In a SPA, the server sends a single HTML page only once. But that HTML remains attached to a lot of JavaScript code. Now, whenever we click a link or navigate to a certain URL, the Javascript running in the background changes the content of the HTML. So, there is a single page of HTML but effectively the content inside of that page is being changed.
This way, the request for a different HTML page was never sent to the server and everything happened inside the browser itself. This reduces the delay significantly and gives a feeling of almost instant loading of everything thus enhancing the User Experience.
Furthermore, sometimes the difference between the look of the webpages of two different URLs on a website is very little. In a SPA, the JavaScript code just changes the part which was different (the rest of the page being exactly the same). However, in MPA even for this little change an HTTP request and response is necessary.
Great! However, with server side rendering coming into picture, SPAs are not lagging in SEO as well. There are various factors, but in my opinion, unless the content is really really static, one might prefer SPAs over MPAs anyday.
ReplyDeleteVery true indeed!
Delete