The symptom
Google Search Console showed the HTML that Google had stored for public pages. The body said “Redirecting to” and little else. Nothing was failing: every request returned 200, the site worked, and shared links had the right picture and title.
The cause
Planit is a React app, and link-preview bots don’t run JavaScript. To give them something to read, Django middleware recognizes their user agents and returns a small page of preview tags. This was its list:
BOT_USER_AGENTS = (
'whatsapp',
'facebookexternalhit',
'twitterbot',
...
'pinterest',
'googlebot',
'bingbot',
'applebot',
'ia_archiver',
)
A preview bot reads the tags and leaves. A search engine keeps the page it was given. The template made it worse, and its own comment holds the wrong assumption:
<!-- Redirect real browsers to the React SPA
(bots ignore this) -->
<script>
window.location.replace("{{ url }}");
</script>
...
<p>Redirecting to
<a href="{{ url }}">{{ title }}</a></p>
Google does run JavaScript. It was being handed an empty page that told it to go somewhere else.
Constraints
- Previews on WhatsApp, Facebook and others had to keep working.
- The same list existed in both Django and Nginx, and the two had to change together.
- Private and unpublished trips must never appear in a preview.
The fix
The fix was mostly a removal. Search engines came out of the list, and the redirect script came out of the template:
BOT_USER_AGENTS = (
'whatsapp',
'facebookexternalhit',
...
- 'pinterest',
- 'googlebot',
- 'bingbot',
- 'applebot',
- 'ia_archiver',
+ 'pinterestbot',
+ 'pinterest/0.',
)
A comment above the list now says why search engines must stay off it, so the next person doesn’t add them back.
The Pinterest entry changed too. Matching the word “pinterest” also matched people reading inside the Pinterest app, who were being sent the preview page as well.
That let search engines reach the React app. In the following weeks I moved the public pages to server-rendered HTML, so an article’s text is there before any script runs.
How it was tested
The tests send each kind of visitor to each kind of public page. Search engines must reach the real one:
for user_agent in SEARCH_ENGINE_UAS:
for path in self.paths:
self.assertIs(
self.run_middleware(
path, user_agent),
self.passthrough)
| Suite | Covers | Result |
|---|---|---|
| Preview routing, backend | Search engines, browsers, preview bots, private and unpublished content | 16 of 16 passed |
| Sitemap, backend | Public pages only, canonical addresses | 10 of 10 passed |
| Article pages, backend | Server-rendered text, metadata and links | 24 of 24 passed |
The live site was also asked for one article three times, changing only the user agent:
| Sent as | Received | Size |
|---|---|---|
| Chrome | The article | 116,091 bytes |
| Googlebot | The same article | 116,091 bytes |
| The preview page | 2,467 bytes |
Outcome and limits
Search engines and readers now receive the same page. I don’t claim a ranking or traffic gain. The indexing reports I have can’t separate this change from the work that followed it.