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

Should You Give Up on SPAs for AdSense? A Trade-off in A Tour of Go’s Page Architecture

Figure 2: The current _content/tour/ in golang/website. Lesson content, static assets, and templates are all continuously maintained in this upstream directory.

作者:

在

In the previous article, I documented a seemingly counterintuitive problem:

The A Tour of Go lesson pages clearly had large visual blank spaces, Google AdSense Auto Ads were enabled, and adsbygoogle.js was loading normally, but standard page display ads never appeared.

After further inspecting the page structure, I gradually narrowed the problem down to the rather unique DOM and layout approach of A Tour of Go itself.

At that point, a very natural thought occurred to me:

Since the existing SPA page structure is not conducive to displaying ads, should I simply abandon the SPA and convert each lesson into a traditional standalone page?

Purely from an AdSense perspective, this approach is actually quite attractive.

But once I actually started evaluating it, I found the problem was far from as simple as “whether the page refreshes or not.”

I ultimately decided to keep the SPA, and the biggest reason was not even page switching speed, but rather:

My multilingual version still needs to track and sync with the official A Tour of Go upstream over the long term. If I change the page architecture to a different implementation for the sake of ads, the short-term gain is convenience, but the long-term cost could be a continuously accumulating maintenance burden.

A Tour of Go’s “next page” was never a full page refresh to begin with

First, I needed to confirm one fundamental fact:

The official A Tour of Go itself uses SPA-style lesson navigation.

To verify this, I opened:

Plaintext
https://go.dev/tour/welcome/1

Then, in the browser developer tools’ Network tab, I filtered to keep only HTML requests, and clicked “next page”.

The page quickly navigated to:

Plaintext
https://go.dev/tour/welcome/2

The lesson content had also completely switched.

But no new Document / HTML request appeared in the Network tab.

Figure 1: After the official A Tour of Go switches from welcome/1 to welcome/2, the address and lesson content have changed, but there is no new HTML document request in the Network tab.
Figure 1: After the official A Tour of Go switches from welcome/1 to welcome/2, the address and lesson content have changed, but there is no new HTML document request in the Network tab.

This means that “next page” is not:

Plaintext
Browser requests /tour/welcome/2
        ↓
Server returns full HTML
        ↓
Browser reloads the page

But rather closer to:

Plaintext
Existing page
    ↓
Angular handles navigation
    ↓
Switch lesson data and view
    ↓
URL updates
    ↓
Continue using the current Document

From a user experience perspective, this approach is very natural.

Lesson switching is fast and doesn’t require reloading the entire site every time.

The code editor, lesson table of contents, and other frontend states all remain within the same application lifecycle.

But this is exactly what causes trouble for AdSense.

The ad lifecycle that is natural for ordinary websites does not exist in an SPA

For traditional multi-page websites, the logic for ads is actually very simple.

The user opens the first article:

Plaintext
GET /article/1
↓
Full HTML
↓
AdSense initializes
↓
Ad request

Clicking for the next one:

Plaintext
GET /article/2
↓
New full HTML
↓
AdSense re-initializes
↓
New ad request

Every page load inherently forms a natural lifecycle boundary.

But an SPA does not work this way.

In A Tour of Go:

Plaintext
/tour/basics/1
↓
Click next page
↓
/tour/basics/2

Although the user thinks they have entered another page, the Document in the browser is not recreated.

This means:

You cannot simply expect AdSense to naturally restart a full page ad flow after every “next page” click, as it would on a traditional website.

So, at that point, there were roughly two paths forward.

Path 1: Abandon the SPA

The first path is the most direct.

Change the current lesson navigation into actual page jumps:

Plaintext
/tour/basics/1
↓
Full page load
↓
/tour/basics/2
↓
Full page load
↓
/tour/basics/3

After doing this, at least a few problems would become significantly simpler.

AdSense becomes simpler

Every lesson switch is a new page visit.

Ad scripts, page content, and ad requests can all reload with the page.

No longer need to consider:

After an SPA route switch, when should old ads be destroyed, and when should new ads be created?

Analytics become simpler

Analytics systems like Google Analytics and Baidu Tongji are also more natural with traditional pages.

Every new Document is a new page visit.

Under an SPA, virtual pageviews need to be handled manually.

SEO is theoretically more intuitive

Every URL returns its own complete HTML from the server:

Plaintext
/tour/basics/1
/tour/basics/2
/tour/methods/1

Search engines don’t need to wait for client-side JavaScript to execute before they can see the actual lesson content.

From these perspectives, abandoning the SPA is indeed tempting.

If this project were just a website I built entirely from scratch, I might seriously consider doing so.

But the A Tour of Go multilingual project has another very important constraint.

This project is not a completely standalone Tour

My goal has never been to reimplement an “A Tour of Go-like website”.

Rather, it is to maintain a multilingual version based on the official A Tour of Go.

The official current lesson content is primarily located at:

Plaintext
golang/website/_content/tour/

It includes:

Plaintext
basics/
concurrency/
flowcontrol/
generics/
methods/
moretypes/
welcome/

static/
template/

basics.article
concurrency.article
flowcontrol.article
...
Figure 2: The current _content/tour/ in golang/website. Lesson content, static assets, and templates are all continuously maintained in this upstream directory.
Figure 2: The current _content/tour/ in golang/website. Lesson content, static assets, and templates are all continuously maintained in this upstream directory.

There is another detail worth noting in this screenshot:

Some upstream lesson files have just been modified within the past few days.

For example, you can see:

Plaintext
yesterday
5 days ago
last week

This means A Tour of Go is not a frozen codebase that will never change.

The upstream will continue to modify:

  • Lesson text;
  • Example code;
  • Templates;
  • Static assets;
  • Frontend behavior;
  • Go version-related content.

At this point, “changing the SPA to a traditional multi-page architecture” is no longer just a one-time development effort.

It becomes:

From now on, every upstream sync must consider whether my forked architecture can remain compatible.

My project even officially records the upstream revision

This upstream relationship is not just a verbal “occasional reference”.

go-tour-i18n itself officially records the upstream baseline.

The project status saves something like:

Plaintext
golang/website master@645042eb...

When the upstream changes, this baseline is updated through my own sync process.

The production bundle also writes into release.json:

Plaintext
upstream_commit=645042eb...
Figure 3: go-tour-i18n officially records the golang/website upstream baseline and writes the upstream commit into the production release info.
Figure 3: go-tour-i18n officially records the golang/website upstream baseline and writes the upstream commit into the production release info.

This means that for this project:

Plaintext
upstream revision
        ↓
source sync
        ↓
TranslationUnit status
        ↓
re-translate / review (if needed)
        ↓
build
        ↓
publish
        ↓
production

It is inherently part of a long-term engineering process.

So when I finally evaluated the SPA problem, my main concern was no longer:

Is it hard to change to ordinary pages?

But rather:

Years from now, will I still be willing to maintain this version that differs from upstream?

Comparing the two paths side by side again

My final judgment at the time can roughly be summarized in the table below.

DimensionKeep SPAAbandon SPA
Closeness to upstream architectureHighSignificantly reduced
Subsequent upstream syncChanges are relatively centralizedRequires continuous adaptation to my own page architecture
“Next page” experienceNative SPA fast switchingFull page refresh per page
Editor and lesson interactionCloser to upstreamRequires reconfirming state and initialization behavior
AdSenseRequires active management of SPA ad lifecycleNaturally re-executes on every page load
Page analyticsRequires handling SPA pageviewsOrdinary pageviews are more natural
SEORequires extra effort to solve client-side rendering issuesServer-side standalone pages are more direct
Initial developmentAd and SEO handling is more complexSeems more direct
Long-term maintenanceSmaller difference from upstreamCustom differences continuously accumulate

If I only look at a single row:

Plaintext
AdSense

Then I would likely choose to abandon the SPA.

But if I include the last row:

Plaintext
Long-term maintenance

The conclusion starts to change.

One-time complexity and long-term complexity are not the same thing

This actually involves an engineering issue I increasingly value.

Some approaches seem “simple” only because they postpone complexity until later.

For example, abandoning the SPA.

When first implementing it, maybe I only need to:

Plaintext
Modify the next page link
↓
Server directly returns the corresponding page
↓
Let the browser reload

It looks very simple.

But later, once the upstream modifies the Tour’s:

  • Routing;
  • Angular controller;
  • editor lifecycle;
  • lesson data structure;
  • template;
  • navigation;
  • Playground behavior;

My non-SPA implementation would need to evaluate item by item:

Can this change still be synced directly?

Which part is already unusable?

Do I need to reimplement it again for my own architecture?

Over time, the gap between the two codebases will only grow larger.

This is the so-called maintenance fork.

While the other path is initially more troublesome:

Keep the SPA, and then separately solve the SPA ad lifecycle.

But its complexity is mainly concentrated in the new ad layer I add.

The ideal state could be:

Plaintext
Upstream Tour
    ↓
Keep the original structure as much as possible
    +
My own locale layer
    +
My own ad lifecycle layer

Rather than:

Plaintext
Upstream Tour
    ↓
Continuous conversion
    ↓
My own alternative page framework

The difference between these two architectures will be huge after maintaining the project for a few years.

Ads should adapt to the project, not the project adapt to ads

This is also the core reason I ultimately decided to keep the SPA.

Google AdSense is important for this project.

It affects whether the site can cover some of the server, domain, and maintenance costs going forward.

But ads are ultimately an add-on capability.

The most core things for the A Tour of Go project remain:

  • Official Go lesson content;
  • Keeping in sync with upstream;
  • Multilingual translation;
  • Playground;
  • Lesson interaction;
  • Long-term maintainability.

If I significantly modify these foundational architectures for the convenience of ads, the priorities are actually reversed.

So the final principle became:

Do not abandon the SPA for AdSense.

Furthermore:

Try to have the ad implementation adapt to A Tour of Go, rather than redesigning A Tour of Go for ads.

But keeping the SPA does not automatically solve the ad problem

Of course, making this decision does not mean the problem is solved.

Quite the opposite.

Choosing to keep the SPA means actively accepting the problem that must be solved next:

Plaintext
User enters lesson page
↓
Display an ad
↓
Click "next page"
↓
Document does not refresh
↓
What happens to the old ad?
↓
Does the new lesson need a new ad?
↓
When to request again?
↓
How to prevent ad nodes from continuously accumulating?

And there is an even more fundamental problem:

Where exactly should the ad be inserted?

The original Auto Ads didn’t even use the large visual blank space on the page.

If the application explicitly provides its own ad slots, where should they be placed:

  • Below the left text column?
  • Below the right editor?
  • Outside the two columns?
  • Above the footer?
  • Below the footer?

The page preview in the Google AdSense backend later happened to give a very interesting answer.

It did find a place “where an ad can be placed”.

But that position made me realize:

The position Google thinks is suitable for an ad is not necessarily the position where I want users to see ads.

So the problem entered the next stage:

Since I decided to keep the SPA, where exactly should the lesson ads be placed, and who should manage their lifecycle?

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

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

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

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

关于我  ·  GitHub  ·  邮件联系