Search Sreeni's Insights

Filter by content type
technical April 8, 2020 9 min read

Lessons Learned: A Faster SPA with Worse SEO and Ads - Part 3

Lessons from building an AngularJS SPA that improved performance but introduced SEO and advertising problems, including the fixes and trade-offs.

Share:

Lessons Learned: A Faster SPA with Worse SEO and Ads - Part 3

In the previous article, we discussed the people side of revamping our Web Forms marketing sites, the developer mindset, the move to Scrum and what happened when the decision maker became the bottleneck. In this article, we are going to see the challenges of building our first Single Page Application (SPA) with AngularJS, how the performance improved but the SEO and the ads got worse, and the mistakes we made along the way.

TL;DR Summary

  • Building a SPA is a completely different way of developing for the web, with a different tech stack, a different testing approach and an application that lives in the browser, disconnected from the server.
  • The performance of searching and rendering the fund data improved.
  • But a SPA is not SEO and ad friendly out of the box. The search engine impressions dropped, and the ad revenue dropped with them.
  • We had to use additional services like Prerender to make the pages crawlable, and rework the ads and analytics for a single page that never reloads.
  • Soon after, AngularJS was replaced by Angular 2, a complete rewrite, and our new front-end was already on a framework heading to its sunset.
  • Our mistakes, choosing the SPA mainly for performance, choosing WCF without looking at the market, and introducing too many changes at the same time.

The SPA Challenges

Nobody had built a SPA before

None of the team members had worked on a SPA or AngularJS before. In Web Forms, the server owns everything, the page, the state and the flow. In a SPA, the application runs in the browser. It loads once, and then it talks to the server only through the APIs. Routing, state, rendering, validation and error handling all move to the client. For the team, it felt like the application was disconnected from the server, and in a way it was.

Simple things in Web Forms became new problems,

  • Where do we keep the state when there is no view state and no session?
  • How do we handle the authentication when there is no postback?
  • What happens when the API call fails in the middle of a user action?
  • How do we deal with the browser back button in an application with one page?

A different set of tech stacks

The front-end brought a completely new set of tools. JavaScript instead of C#, npm and Bower for the packages, Gulp and Grunt for the build, minification and bundling, and AngularJS with its own concepts, modules, controllers, directives, services, scopes and the digest cycle. Each of them was a learning curve on its own, and debugging a problem across the browser, the build tools and the API was a new skill for the team.

Writing front-end unit tests

We wanted the same discipline on the front-end as on the back-end, so we added front-end unit tests with Jasmine and Karma. For a team that was just learning to write unit tests in C#, writing them in JavaScript, mocking the AngularJS services and the HTTP calls, and running them in a browser runner was a big step. AngularJS was designed with testability in mind, with its dependency injection, but it only helps when the code is written to use it.

Performance Improved

The good news first. The performance goal was achieved. The fund search, the factsheets, the prices and the performance pages became much faster. Filtering a list or changing a period on a performance chart no longer needed a full page postback, the APIs returned only the data, and the browser rendered it.

But SEO and Ads Got Worse

This is where the SPA bit us. Since these were marketing sites, the SEO and the ads were among the primary things, and the old system was strong in both. After the move, the SEO impressions dropped, and since the ads depend on the traffic and the page views, the ad revenue dropped as well. Let us take a closer look at why.

Implementing SEO for a SPA

A server-rendered page sends the full HTML to the search engine. A SPA sends an almost empty HTML page with a script, and the content appears only after the JavaScript runs and the API calls return. At that time,

  • Most crawlers didn’t execute JavaScript, so they saw an empty page. Google could render JavaScript, but not reliably and not for every page, and other search engines and the social media link previews (the cards shown when a link is shared) couldn’t.
  • The page title, the meta description and the other tags were the same for every route unless we updated them from the code on every route change.
  • The URLs needed care. The default AngularJS URLs with a hash (#/funds/123) are not treated as separate pages by the crawlers, so we had to move to the HTML5 mode URLs and configure the server to return the application for every route.
  • The sitemap and the canonical URLs had to be generated from the data, because the pages no longer existed on the server.

Ads implementation

The ads were also built for the old world, one page load, one ad request, one impression. In a SPA, the page loads only once. Moving from one fund to another doesn’t reload the page, so the ad slots don’t refresh and the analytics doesn’t record a new page view unless we tell it. We had to rework the ads to refresh the slots on every route change, and track the virtual page views manually in the analytics. Until then, we were losing impressions on every navigation.

Additional services to fix the crawling

To fix the page crawling issue, we had to use additional services like Prerender. Prerender runs the SPA in a headless browser, captures the fully rendered HTML and serves it to the crawlers, while the users still get the SPA. It solved the crawling problem, but it was one more service to pay for, configure and monitor, one more place where the content could be stale, and one more moving part in a system that was supposed to become simpler.

In simple terms, we had made the site faster for the users, but harder to see for the search engines and less effective for the ads, which were exactly the things a marketing site lives on.

AngularJS Went to Its Sunset

Just as we were getting comfortable with AngularJS, Angular 2 was born. It was not an upgrade, it was a complete rewrite, with TypeScript, components instead of controllers and scopes, a new module system and a new way of building. There was no simple migration path from AngularJS. Angular 2 continued to mature quickly, and the community and the tooling moved with it.

So, the front-end we had just built, with a team that had just learned AngularJS, was already on a framework heading to its sunset. It was the second time in the same project that we had chosen a technology just before the market moved away from it, first WCF and then AngularJS.

Mistakes We Made

Let me be honest about the mistakes we made. These are the lessons I carry into every project today.

Performance was the top most consideration

Performance was the main reason for choosing the SPA, and the SPA delivered it. But for a marketing site, performance is not the only thing that matters. SEO, discoverability, sharing and the ad revenue matter as much, and in some cases more. We optimized one dimension and hurt the others. The performance increased, but the SEO impressions reduced and the ad revenue reduced with them.

If I had to do it again, I would choose the architecture based on the nature of the site. A marketing and content site needs server-side rendering for the SEO and the ads, with client-side interactivity only where it adds value, like the fund search and the charts. A full SPA is a better fit for an application behind a login, where the SEO doesn’t matter.

Choosing WCF

We chose WCF based on its technical capabilities, the team’s experience and its extensibility, without analyzing what was new in the market and where it was going. Web API was already the better fit for HTTP and JSON services, and the industry was moving to it. The switch in the middle of the project cost us time, rework and the team’s confidence.

The lesson is to look at the direction of the technology, not only at its features and our comfort with it. Ask where the vendor and the community are investing, what the new projects are using and what the roadmap says.

Introducing a lot of changes at the same time

We changed the architecture, the back-end technology, the front-end technology, the testing practices and the development process, all at the same time, with a team that was new to all of them. Every change on its own was reasonable. Together, they were too much. When something went wrong, it was hard to tell whether the cause was the technology, the process or the people, and the resistance against one change spread to all of them.

The lesson is to sequence the changes. Introduce Scrum with the familiar technology, or the new technology with the familiar process, but not everything at once.

Self-Reflection

Looking back, the revamp solved the problems we started with. The performance improved, the front-end and the back-end were separated, the code was testable and the sites were no longer tied to each other. But it created new problems that we didn’t foresee, in the area where the old system was strongest.

The biggest lesson for me as an architect is that every architecture decision is a trade-off, and the trade-offs must be evaluated against all the business goals, not just the technical problem in front of us. We asked “how do we make it faster?”, when we should have asked “how do we make it faster without losing what the business depends on?”.

Wrap up

In this article, we have looked at the challenges of building our first SPA with AngularJS, how the performance improved but the SEO and the ads got worse, the additional services we had to use to fix the crawling, AngularJS heading to its sunset with the birth of Angular 2, and the mistakes we made. This brings us to the end of this series. I really appreciate that you took the time to read this series and I hope our experience helps you to avoid some of the mistakes we made.

Related Posts