没有不值得去解决的问题,也没有不值得去学习的技术!

SEO for SPAs: Why We Ultimately Prerendered HTML for 103 A Tour of Go Course Pages

Figure 1: In Google Search Console, multiple different A Tour of Go course URLs are identified as "Duplicate without user-selected canonical".

作者:

在

In the previous articles, I have been adjusting around the Google AdSense display ad issues for A Tour of Go.

The initial problem was:

The course pages clearly had large visual blank spaces, but Auto Ads never inserted standard display ads.

After further analysis, I made several decisions in succession:

  • Not abandoning A Tour of Go’s original SPA architecture for AdSense;
  • Not relying on Google to automatically select ad placements for the courses, but adding explicit manual course ad slots;
  • The first version, Plan A, used MutationObserver to track SPA page changes;
  • Later, I switched to Plan B, letting the ads directly follow the mount() / $destroy / unmount() lifecycle of the Angular course view.

However, beyond the ad issues, there had actually always been another long-known problem:

SPAs are not friendly to search engines.

This problem was not discovered only after doing the AdSense optimization this time.

It was just that in the past, there was never a strong enough reason for me to decide to do a more complete Prerender for the entire course page system.

This time, the needs from both SEO and advertising directions happened to converge.

103 URLs, clearly 103 different pages in the eyes of users

A Tour of Go’s course pages have many independent URLs.

For example:

Plaintext
/tour/welcome/1
/tour/basics/1
/tour/basics/2
/tour/moretypes/26
/tour/methods/24
/tour/concurrency/7

From a user’s perspective, these are of course different pages.

Each URL:

  • Has a different course title;
  • Has different body text;
  • Has different Go example code;
  • Has different previous/next page relationships;
  • Explains completely different Go concepts.

The entire official course consists of:

Plaintext
103

pages.

So from a content model perspective, it should be:

Plaintext
103 URLs
=
103 independent course pages

The problem is:

The browser ultimately seeing 103 different pages does not mean that when search engines first access these URLs, they can also immediately see 103 distinctly different HTML documents.

This is exactly where traditional client-side SPAs are troublesome.

Search Console once classified multiple course URLs as duplicate pages

Google Search Console had already shown fairly obvious signals previously.

Multiple different A Tour of Go course URLs were grouped into:

“Duplicate without user-selected canonical”

this status category.

Figure 1: In Google Search Console, multiple different A Tour of Go course URLs are identified as "Duplicate without user-selected canonical".
Figure 1: In Google Search Console, multiple different A Tour of Go course URLs are identified as “Duplicate without user-selected canonical”.

The examples in the screenshot are not different parameters of the same page.

They are actually different courses, for example:

Plaintext
/tour/moretypes/2
/tour/moretypes/21
/tour/moretypes/26
/tour/moretypes/8
/tour/moretypes/15
/tour/basics/12

To a learner, the differences between these pages are obviously huge.

But search engines had not fully established this distinction in the past.

This is actually a very typical type of problem in SPA SEO.

A changed URL does not mean the initially returned HTML has changed

Traditional server-rendered pages usually work like this:

Plaintext
GET /tour/basics/1
        ↓
Server generates basics/1 HTML
        ↓
Browser receives complete course content

Requesting again:

Plaintext
GET /tour/basics/2
        ↓
Server generates basics/2 HTML
        ↓
Browser receives another complete course content

That is, different URLs are distinctly different starting from the very first HTTP response.

While a traditional SPA is more like:

Plaintext
GET /tour/basics/1
        ↓
Receives SPA shell
        ↓
Loads JavaScript
        ↓
Angular starts up
        ↓
Renders basics/1 based on route

Another URL:

Plaintext
GET /tour/basics/2
        ↓
Receives a highly similar SPA shell
        ↓
Loads JavaScript
        ↓
Angular starts up
        ↓
Renders basics/2 based on route

For modern browsers, this is not a problem.

After JavaScript executes, the user can ultimately see the correct course page.

Google also has the ability to execute JavaScript.

But there is always a problem here:

Why should search engines first fetch a highly similar shell, and then rely on a second stage of JavaScript execution to understand what is actually different about these URLs?

If the server already knows which course section the current URL corresponds to, it can completely provide this information directly in the very first response.

SEO is an old problem, but this time there was an additional advertising-related reason

Purely from an SEO perspective, there was actually long a reason to do Prerender.

But at that time, I always needed to weigh:

  • Whether it would significantly increase implementation complexity;
  • Whether it would break the original SPA;
  • Whether it would increase the cost of syncing with upstream;
  • Whether it was worth adding an extra generation process for 103 course pages.

While continuing to troubleshoot AdSense this time, another consideration emerged.

As previously discovered, Google Auto Ads’ understanding of the main body of A Tour of Go’s courses was not ideal.

The page clearly had large visual blank spaces, but Google never inserted standard display ads there.

Later, even after adding manual ad slots, what ads Google ultimately returned still required understanding the current page’s content.

For example:

Plaintext
/tour/methods/24

actually talks about Go’s image.Image interface.

And:

Plaintext
/tour/concurrency/7

talks about equivalent binary trees and Go concurrency.

In terms of content topics, the two are completely different.

If Google could directly get the following when requesting these two URLs for the first time:

  • This page’s title;
  • This page’s body text;
  • This page’s example code;
  • This page’s description;

it would obviously be clearer in terms of page semantics than first receiving a highly similar SPA shell.

A special note here is needed:

I do not have data to prove that Prerender will definitely increase AdSense fill rate, nor do I have evidence to prove it will definitely make ads more relevant.

This is not a directly conclusive causal relationship.

A more accurate consideration is:

If search engines and other Google systems can directly understand the actual content of the current page when they first fetch the URL, then at least they can be provided with a more complete and explicit page context.

From an engineering perspective, this is obviously more reasonable than having 103 URLs first return highly similar client-side shells.

Thus:

Long-standing SEO issues + page context potentially helping ad systems

ultimately drove this Prerender implementation together.

The goal is not to abandon the SPA

It is easy to form a misunderstanding here.

Adding Prerender does not mean overturning the SPA that the previous article had just decided to keep.

What I truly want to achieve is:

Plaintext
Search engine / first HTTP request
        ↓
Directly receives complete course HTML

Normal browser
        ↓
First sees complete course HTML
        ↓
Angular starts up
        ↓
Takes over the page
        ↓
Continues SPA navigation afterwards

That is:

The first visit acts like a complete independent page, while subsequent interactions remain SPA.

These two goals do not conflict.

It could even be said that this was the ideal compromise at the time:

Plaintext
Server-side
Provides complete page identity

Client-side
Continues to maintain the upstream SPA experience

Not just prerendering a few representative pages

Having decided to solve this problem, I did not want to only give special treatment to a few courses with high search traffic.

The official course itself is 103 pages.

So the final approach was full coverage:

Plaintext
103 official course URLs
        ↓
103 independent Prerender HTML files

In the official production bundle, you can directly see:

Plaintext
========== PRERENDER COUNT ==========
103

And the directory actually contains:

Plaintext
prerender/basics/10.html
prerender/basics/11.html
prerender/basics/12.html
...

prerender/generics/3.html

prerender/methods/10.html
prerender/methods/11.html
...

prerender/moretypes/6.html
prerender/moretypes/7.html
...

prerender/welcome/1.html
prerender/welcome/2.html
...
Figure 2: A total of 103 Prerender HTML files are generated in the official production bundle, covering all official course pages including welcome, basics, generics, methods, moretypes, etc.
Figure 2: A total of 103 Prerender HTML files are generated in the official production bundle, covering all official course pages including welcome, basics, generics, methods, moretypes, etc.

This was very important to me.

Because if it were only:

Plaintext
Homepage prerender
+
A few popular pages prerender

then it would actually still not solve:

Course URLs themselves should have independent page identities

this core problem.

Since 103 course pages officially exist in the Catalog, the server should ultimately be able to independently describe these 103 pages.

The Prerender HTML is not just an empty shell

The next step was to confirm:

whether these HTML files actually had real content.

I did not want to rely on browser screenshots here.

Because what the browser ultimately displays might have already been processed by Angular and JavaScript.

The most direct method is:

Bash
curl https://go-dev.shuijingwanwq.com/tour/basics/1

Then directly check the raw HTML returned by the server.

The result shows:

HTML
<title>包 – 包、变量和函数 – Go 语言之旅</title>

The page already has:

HTML
<h2>包</h2>

The body text also exists directly:

Plaintext
Every Go program is made up of packages.
The program starts running in package main.

Not only that, even the example source code has directly entered the initial HTML:

HTML
<textarea ...>
package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println("My favorite number is", rand.Intn(10))
}
</textarea>
Figure 3: When directly fetching /tour/basics/1 via HTTP, the returned HTML already includes this page's title, Chinese course body text, and real Go example source code.
Figure 3: When directly fetching /tour/basics/1 via HTTP, the returned HTML already includes this page’s title, Chinese course body text, and real Go example source code.

This point is very critical.

The process is now no longer:

Plaintext
Google
↓
Gets the shell
↓
Executes Angular
↓
Waits for course content to appear

But rather:

Plaintext
Google
↓
GET /tour/basics/1
↓
First HTTP response
↓
Already knows:
"This page is about packages"

Whether JavaScript executes subsequently no longer determines whether the search engine can obtain the most basic course semantics.

Why should the example source code also be included in the Prerender

Initially, if only considering SEO, one might think:

Having the title and body text is enough; the code can wait for Angular to load.

But A Tour of Go is a bit different from ordinary articles.

The code is part of the course content.

For example, one page talks about:

Plaintext
package

Another page talks about:

Plaintext
for

And another page talks about:

Plaintext
interface

The example code itself is an important semantic part of the page’s topic.

If the Prerender only outputs:

Plaintext
Course title
Course description

but leaves the right-side example code empty, then the page is still not in a complete initial course state.

So ultimately, I wanted the initial HTML to be as close as possible to what the user should actually see.

This also foreshadowed another problem later on:

Since the source code is already in the HTML, why might the code area still briefly appear empty when the browser first opens the page?

This problem later became a very important bug during the integration of Prerender and Angular hydration.

But that is content for the next article.

Body text alone is not enough; each URL also needs a clear SEO identity

Solving the duplicate page problem cannot rely solely on:

The body text of these two pages is different.

Each URL also needs to explicitly state for itself:

Who I am.

Therefore, the official pages also added independent:

  • <title>
  • meta description
  • canonical

For example, /tour/basics/1 now directly returns:

HTML
<title>包 – 包、变量和函数 – Go 语言之旅</title>

canonical:

HTML
<link rel="canonical"
      href="https://go-dev.shuijingwanwq.com/tour/basics/1"/>

description:

HTML
<meta name="description"
      content="讲解 Go 程序由包组成、程序从 main 包开始运行,以及导入路径最后一项通常与包名相同的惯例。"/>
Figure 4: The first HTTP HTML for /tour/basics/1 already directly includes an independent title, canonical, and description.
Figure 4: The first HTTP HTML for /tour/basics/1 already directly includes an independent title, canonical, and description.

I feel this picture is particularly meaningful when placed next to the first Search Console screenshot.

Previously, Google was facing:

Plaintext
Many different course URLs
        ↓
Page identity not explicit enough
        ↓
Some classified as duplicate pages

Now it has become:

Plaintext
/tour/basics/1
        ↓
Independent title
Independent description
Independent canonical
Independent body text
Independent example source code

Every URL has started to truly become an independently understandable page.

Course SEO metadata also cannot simply rely on template concatenation

There is another problem:

The descriptions for 103 pages cannot all be written as:

Plaintext
Learn content related to the Go language.

Although that formally provides:

HTML
<meta name="description">

in reality, there is still not much page differentiation.

If you want Google to truly understand the page’s topic, the description should correspond to the current course content.

For example, the “package” page describes:

Plaintext
Explains that Go programs are made up of packages, that programs start running from the main package,
and the convention that the last item of the import path is usually the same as the package name.

Another page about slices should describe slices.

A page about goroutines should describe goroutines.

So later on, the course SEO metadata also became an independent set of locale-level content assets.

There is a project boundary here that is also very important:

Course SEO metadata does not belong to TranslationUnit.

It does not enter:

Plaintext
TranslationUnit
→ Quality Check
→ Final Review
→ promotion

that formal translation gate.

The reason is that it was originally locale-level metadata generated additionally for page SEO, not a formal translation unit of the upstream present.Section.

But this does not mean it can go without review.

It still belongs to the final locale surface, and needs to ensure:

  • Content accuracy;
  • No exaggeration;
  • Consistency with the actual course content;
  • Consistent glossary terminology;
  • Sufficient differentiation between pages.

This is also why the entire multilingual engineering effort cannot focus solely on TranslationUnit.

A locale that actually goes live also includes a lot of visible content outside of TranslationUnit.

Prerender must not break the original Angular SPA

The most dangerous part of doing Prerender is actually not:

Whether an HTML file can be generated.

But rather:

After generation, can Angular still take over normally?

The ideal state needs to simultaneously satisfy:

Plaintext
Initial HTML
Complete
        +
Angular startup
Normal
        +
SPA next page
Normal

Failure in any of these is unacceptable.

If Angular is broken for the sake of SEO:

Not acceptable.

If, for the sake of the SPA, the initial HTML degenerates back into an empty shell:

Equally unacceptable.

So what really needed to be solved this time was the handoff between two rendering models:

Plaintext
Server Prerender
        ↓
Browser initial DOM
        ↓
Angular bootstrap
        ↓
Hydration / route takeover
        ↓
CodeMirror initialization
        ↓
SPA normal operation

And reality soon proved:

This handoff chain was not as simple as imagined.

Once the direction for solving the search engine problem was clear, browser problems emerged instead

From an SEO perspective, the direction for Prerender was already very clear.

The server’s first response can provide:

  • The current course title;
  • The current course body text;
  • The Go example source code;
  • canonical;
  • description;
  • The page’s true URL identity.

But after opening these pages in a normal browser, a series of new problems were exposed.

The most obvious of these included:

  • Even though the example code was already in the HTML, the editor might briefly be blank when first opened;
  • After Angular took over, the page underwent multiple visual changes;
  • The footer moved while the route DOM was being replaced;
  • In a few cases, “next page” temporarily did not respond normally;
  • The takeover relationship between CodeMirror and the Prerender textarea still needed to be handled.

More troublingly, after these problems were gradually resolved, the real AdSense environment produced another problem that was hard to reproduce locally:

Google’s ad script directly modified the inline style of an ancestor node of the course layout, compressing the height of the entire course page.

As a result, the footer was pulled into the first screen again.

This also demonstrated a very practical reality:

An architectural solution being correct at the SEO level and passing in automated tests does not mean it will definitely be stable in a real production environment where Angular, CodeMirror, and AdSense coexist.

Ultimately, another round of production-level integration and finishing touches was still needed.

Why it was ultimately worth doing Prerender

After experiencing these subsequent problems, if asked again:

Is it worth doing Prerender for 103 course pages?

My answer is still:

Worth it.

Because without doing it, the page identity of 103 URLs would always rely more on client-side JavaScript.

And now, at least, it is possible to achieve:

Plaintext
One official course URL
=
One independent HTTP page identity
=
One independent Prerender HTML
=
Independent title
=
Independent description
=
Independent canonical
=
Independent course body text
=
Independent example source code

At the same time, users still retain the following after the page finishes loading:

Plaintext
Angular SPA
+
Fast next page
+
CodeMirror
+
Playground

This is the truly desired end result:

Not abandoning the SPA for SEO, but making the SPA also become an independent page that search engines can directly understand at the initial HTTP level.

And this AdSense optimization became another important factor that pushed this years-old SPA SEO problem truly into the formal resolution phase.

Next step: the real difficulty is how Prerender and SPA can stably coexist

At this point, the search engine side solution has basically taken shape.

But for normal users, there is one last round of trickier problems.

After Prerender, Angular, CodeMirror, and manual AdSense ads simultaneously entered the real page, the following appeared in succession:

Plaintext
Initial source code blank
↓
Multiple visual changes
↓
Occasional next page anomalies
↓
Footer layout changes
↓
Real AdSense inline style pollution

These problems cannot be solved by simply choosing another architecture.

Because the previous core decisions had already been confirmed:

  • The SPA must be kept;
  • Prerender must be kept;
  • Manual course ads must be kept;
  • Differences with upstream must still be minimized as much as possible.

What remains to be done is:

To make these already determined components truly able to work together stably.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系