Let’s begin with – what exactly are Single Page Applications? Basically, these are applications that require only one page to be loaded to the browser. They are built around the concept of dynamically rewriting the content of the single page. This is completely different from loading pre-rendered pages from the server. This approach essentially helps single page applications to avoid interruption caused by rendering the pages on the server and offers a seamless experience to users.
So, how do they work?
When the browser makes the first request to the server, the server sends back the index.html file. And that’s it – that’s the only time an HTML file is served. This HTML file has a script tag for the .js file which takes control of the HTML file. All the subsequent calls just return the data, usually in the JSON format. So, essentially, the application uses the JSON data to update the page dynamically. The client handles the job of transferring data to HTML, unlike the traditional applications where every time an application calls the server, the server will render the entire HTML page and the client triggers a fresh page.
What are the advantages of SPAs?
What about the disadvantages?
Where there are pros, there are bound to be cons too. Here are a few disadvantages we must know about:
There used to be concerns that, since there is no markup except the initial index.html file, search engines cannot index them – however, that is not true anymore, as Google can now crawl AJAX calls too.
So, while single page applications seem quite simple, they could be very useful while building a good user experience as they are fast, and use up less network bandwidth and offer users all the information they need at a glance!