What I wish I had known about single page applications

[Ed. note: While we take some time to rest up over the holidays and prepare for next year, we are re-publishing our top ten posts for the year. Please enjoy our favorite work this year and we’ll see you in 2022.]

As a Java developer, I’ve spent most of my skilled life engaged on the components of software program techniques that most individuals don’t see. The so-called back-end of the software program stack. However recently I’ve discovered myself eager to department out, and dabbling extra in HTML and UI growth.

A few years in the past, this pure curiosity led me to start out a brand new facet undertaking. My undertaking was meant to be a hobby application that might solely be utilized by me and some associates, so I didn’t spend an excessive amount of time fascinated with a long run roadmap or necessities. The primary aim was to get it working quick.

I settled on JHipster, a growth platform for constructing net purposes utilizing trendy know-how: Angular, React or Vue for the consumer facet, and Spring plus Gradle or Maven for the server facet. It’s been round for years, may be very properly documented, and has nice group assist.

Inside a number of weeks I had a functioning utility that met all my wants. However a humorous factor occurred quickly after I launched. Different individuals began utilizing the appliance. Understanding I had created one thing helpful for a big viewers was actually satisfying. And so I did what every other developer who’s already stretched skinny and attempting to stability a full time job and a household and interest tasks would do: I spent my nights, weekends, and each free second I had engaged on it.

Nevertheless, the extra I attempted to enhance it, the tougher issues obtained. I spent a whole lot of time trying up methods to do new issues that weren’t a part of the boilerplate setup. I used to be studying a number of the limitations that now felt like main roadblocks. And after a number of months, it grew to become clear to me that my selection of applied sciences was turning into a hindrance to creating the appliance higher. Finally, I made a decision to re-write most of it utilizing frameworks that have been extra acquainted to me.

I need to pause right here and make clear: JHipster and Angular aren’t dangerous platforms. Removed from it. I’d suggest them in heartbeat for the appropriate undertaking.

After I say they have been turning into a hindrance, what I actually imply is that my lack of information of the applied sciences had come again to chew me. For all the explanations I had chosen them, there have been loads of different causes which may have made me suppose in a different way, had I identified about them.

It was a traditional developer lament: I didn’t know what I didn’t know.

And what I got here to appreciate is that my utility was struggling in a number of key areas that have been a direct results of the platforms I had chosen. Issues like website positioning, social sharing, and caching. Options that didn’t matter for a interest utility, however have been important to the long run success of a rising product.

Although I finally ended up re-writing the appliance and it has continued to thrive, I feel it’s necessary to mirror again on the early days of the undertaking. There are such a lot of classes to study from each success and failures. I needed to dive into a number of the issues I discovered the arduous manner once I wrote my first single-page app, and hopefully assist others who discover themselves in the identical boat.

Anatomy of a single-page utility

Earlier than diving into a number of the points I bumped into, it’s price breaking down the fundamental rules of a single-page app. That is meant to be a excessive degree overview and never a technical deep dive on any particular platform.

Net browsers work utilizing a standard client-server mannequin. An internet browser (the consumer) sends a request for some web page to an internet host (the server). The server sends again some HTML to the online browser. That HTML contains structured textual content together with hyperlinks to different recordsdata akin to photographs, CSS, JavaScript, and so forth. The mixture of the HTML, together with these different recordsdata, permits the online browser to render a significant net web page for the consumer.

And thus the world turns. You click on a hyperlink, your net browser sends the request to the server, and the server sends again some HTML. Each response again from the server is the complete HTML doc required to render an internet web page.

A single-page app breaks this paradigm. The net browser sends the preliminary request and nonetheless will get again some HTML. However the response from the server is only a naked bones HTML doc with no actual content material. By itself, this HTML is generic and doesn’t symbolize something particular concerning the website.

As a substitute, it accommodates a handful of placeholder parts, together with some hyperlinks to JavaScript recordsdata. These JavaScript recordsdata are the center and soul of the single-page app. As soon as loaded, they ship again requests to the server to fetch content material, after which dynamically replace the HTML within the net browser to create a significant net web page.

By all outward appearances, the appliance behaves like a standard website. The consumer sees HTML with photographs and buttons and interacts the very same manner. However below the hood issues are very a lot completely different.

Search engine marketing

Search engine marketing (website positioning) is the method of formatting net web page content material so that’s straightforward for net crawlers to know it.

Identical to an internet browser, Google and different net crawlers request the HTML contents for pages on a website. However as an alternative of rendering it like an internet browser would, it analyzes the HTML to semantically perceive what the online web page is about.

And that’s the place I bumped into my first downside. HTML is structured and simple for computer systems and bots to know, however they don’t essentially perceive JavaScript. Whereas there are many articles that debate whether or not or not Google’s crawler executes JavaScript when crawling a web site, my expertise was that it didn’t.

The rationale I knew it is because Google was telling me that it didn’t perceive my content material. After I seemed on the search analytics for my website, it was rating for precisely one key phrase. And that key phrase had nothing to do with my web site.

Because the graphic reveals, Google thought my web site was associated to Maven proxy configuration. For sure, my interest undertaking was not in any manner associated to the open supply Apache undertaking.

So what was occurring? My web site had tons of, if not hundreds, of distinctive net pages with various content material. But, Google solely confirmed it in search outcomes to individuals trying to find the phrases “mvnw proxy”. Even when website positioning wasn’t a principal focus of mine, after a number of months Google ought to have been capable of confirm that my website wasn’t about Maven proxy configuration.

The issue was that Google’s crawler was not executing the required JavaScript recordsdata that made my single-page utility perform. It was loading the template HTML file and scanning its construction for hints about what my website was truly about.

And like most single-page apps, the default HTML included numerous useful developer data that’s supposed for use for trouble-shooting, however by no means truly displayed in an internet browser when issues are working correctly. Even worse, the template is identical for all URLs on the location, so Google obtained the identical (incorrect) interpretation for each web page it crawled.

Above is a snippet from the HTML template. You possibly can see the phrases mvnw and proxy present up in varied locations, and Google was utilizing these alerts to interpret my web site. Had Google’s crawler accurately utilized the related JavaScript, it could have seen that each URL on the location had much more distinctive and significant content material.

Social Sharing

One other space I bumped into issues with was social sharing. My web site allowed customers to create dynamic content material, and in addition included numerous static content material that others may discover helpful. And within the early days of launching, I certainly noticed that individuals have been sharing hyperlinks to my web sites throughout varied social media platforms:

Social networks, very similar to serps, depend on the content material in net pages to know what the online web page is about. In contrast to serps although, they rely much less on the seen content material of the online web page (the textual content and pictures people see) and extra on the metadata (the stuff contained in the HTML that us people don’t care a lot about).

For instance, if you share a hyperlink to a web site on Fb, the very first thing that occurs is Fb reads the webpage and generates a pleasant preview of that article. The preview has a title, a line or two of descriptive textual content, and a picture. 

However these previews aren’t generated magically or utilizing some refined AI algorithm. Fb is counting on metadata contained in the HTML header space to create previews. It’s solely as much as web site house owners to offer the knowledge Fb must construct a significant preview of each web page on their web site. Many CMS techniques akin to WordPress make this very easy with plugins. However I used to be writing a model new utility from scratch and must create this metadata by myself.

In my case, Fb was falling sufferer to the identical downside Google was. It was studying the template HTML file as-is, and never making use of the JavaScript that might assist fill within the metadata and create significant previews.

What this meant for my customers is that each hyperlink from my web site that was shared to Fb and different social networks was producing the very same preview. Whether or not it was customers sharing their customized content material, one of many static pages that I auto generated, and even the house web page, each hyperlink shared on social media had the identical preview.

Functionally talking, nothing was incorrect. A consumer might nonetheless click on on the preview and be taken to the right URL on the location. However I didn’t like the concept that the preview wasn’t useful. In any case, I used to be attempting to develop my consumer base. If somebody shared my content material to their family and friends, I needed to have the very best likelihood of individuals clicking these hyperlinks so they may uncover the location.


One other space I shortly grew to become involved with was caching. With a small consumer base initially, I by no means apprehensive a lot about costly database queries or web page load occasions.

However as new customers began to make use of the web site, I began to get apprehensive about this stuff. Have been my queries as performant as attainable? Was I taxing MongoDB with too many requests? Would new customers quit if a web page took too lengthy to load?

I’m by no means a fan of untimely optimizations, however this was an space that I felt would grow to be an issue shortly if issues saved trending the way in which they have been. So I began fascinated with methods to enhance a number of the MongoDB queries and web page load bottlenecks. And one of many first issues that got here to thoughts is caching.

I’ve labored with a wide range of enterprise caching applied sciences akin to Coherence, Ignite, Redis, and others. For what I used to be trying to do, these all felt like overkill. Plus, they’d add to the compute and reminiscence prices of a undertaking that was nonetheless technically solely a interest.

As a substitute, I made a decision this was an ideal use case for CloudFlare. I used to be already utilizing CloudFlare as my DNS supplier as a result of they supply a ton of nice options totally free. And a kind of nice options is web page caching. It’s free and requires no extra coding on my half.

Right here’s the way it works. CloudFlare acts as a reverse proxy to my web site. This implies all requests are actually going by way of their infrastructure earlier than being forwarded to my web site. Amongst different issues, this enables them to cache my server responses throughout their huge community of worldwide information facilities. All I’ve to do is configure which set of URLs I need them to cache and the way lengthy they need to be cached, and CloudFlare handles all of the heavy lifting. All with out me writing a single line of code.

Periodically, the cached content material will expire and CloudFlare might want to go the request on to my server. However on mixture, this method can drastically cut back load in your server as a result of far fewer requests find yourself getting by way of. As a bonus, the consumer expertise is improved as a result of returning the cached content material is way quicker than having your net server generate it from scratch.

This all sounded nice in principle. However in apply, it wasn’t fairly working as I anticipated. After making the change and enabling web page caching, there was no considerable distinction in my MongoDB cluster sources. I used to be anticipating to see an considerable lower in useful resource utilization as far fewer queries can be made. However as an alternative I noticed issues largely staying the identical.

So what was happening? All of it goes again to the template HTML file that single-page apps use. CloudFlare doesn’t execute any JavaScript previous to caching the response. It merely takes the uncooked HTML response from the origin server and caches it. In my case, that response was the template HTML. And the online result’s that each web page I needed to cache, was actually simply caching the identical template HTML.

So when a consumer requested a kind of cached pages, CloudFlare merely returned the naked bones template HTML to them. And in flip, the consumer’s net browser would load this template and obtain the required JavaScript recordsdata. These JavaScript recordsdata would ship a bunch of requests to my server to get the true content material for the web page, which means no matter costly queries I hoped to keep away from have been all the time going to be executed.

Expertise envy

One other normal space I want I had been extra cognizant of is know-how envy. As a developer wanting to enhance my ability set and worth to potential employers, I’m continuously looking out for brand spanking new know-how. However generally it’s straightforward to get envious of what others do, with out realizing the abilities you have already got are invaluable too.

This occurs quite a bit on social media. I’m continuously in awe, and albeit a bit jealous, of what a few of different individuals create and construct. I see buzzwords and know-how and suppose to myself, “I actually must know methods to use this stuff.” And whereas there may be worth in studying new issues, it’s extra necessary to be adaptable and know the appropriate instruments for the job. Because the saying goes, a jack of all trades however a grasp of none.

And that’s precisely what occurred when constructing my first single-page app. I used to be so caught up within the quick time period pleasure of studying a brand new know-how, I by no means stopped to think about how that call would affect the way forward for the app.

And ultimately this got here again to hang-out me. As new characteristic requests got here in or I had new concepts, I discovered the selection of framework ever extra irritating. Every time I needed so as to add a brand new web page, for instance, there have been a number of areas of Angular code to the touch: routers, controllers, companies, templates, and so forth. I used to be continuously trying up methods to do issues, and went down my justifiable share of rabbit holes attempting to determine “issues” that ultimately proved to be my very own doing.

In a way, that is how we study. We will take all of the programming boot camps and programs below the solar, however actual studying comes from frustration. Actual information and understanding come from hours spent stepping by way of code in a debugger. Sadly for me, this undertaking had taken on new which means and it was not the appropriate context for me to discover and study.


This isn’t meant to be a critique of single-page apps on the entire. It’s a cautionary story that I feel many builders can relate to. The joy of studying one thing new, making a functioning utility from the bottom up, after which reflecting on the teachings discovered alongside the way in which. And although I ultimately re-wrote my utility utilizing know-how I used to be extra conversant in, the expertise was invaluable.

Errors are good. They assist us study. They assist us make higher choices down the highway. I’ve already obtained new concepts for a way I can use single-page apps for different tasks I’m engaged on. And this time I’ll believe understanding they’re the appropriate device for the job. I also can now confidently communicate to shoppers and inform them once I suppose a single-page app is smart for his or her use case. And I can talk about my expertise with colleagues, serving to them to keep away from a number of the pitfalls I needed to cope with. Ultimately, this was nonetheless only a interest undertaking, and the expertise I gained will go a great distance.

In an ideal world, builders would have all the knowledge they want up entrance. Each requirement, each change in scope can be identified forward of time. However this isn’t how our trade works. We create roadmaps as finest we are able to, and we select applied sciences that make sense given what we all know. As a rule, our greatest laid plans don’t pan out the way in which we envision. And that’s okay.

Tags: single page application

More Posts