Sub topic 9
Rendering JavaScript-dependent pages
This chapter assumes you know how a crawler fetches a page over HTTP and extracts links from HTML, as covered in the earlier chapters. It also assumes you can read short Python and HTML snippets. Here you will see why some pages look empty to a crawler, how search engines render JavaScript, and how to check what a crawler actually sees.
Why raw HTML is not always enough
A crawler's first fetch returns the HTML that the server sent. On a server-rendered page, that HTML already holds the text and the links. On a client-rendered page, the server sends a nearly empty shell and a script. The browser runs the script, which fetches data and builds the content.
Here is a shell for a client-rendered page:
<!DOCTYPE html>
<html>
<head><title>Loading...</title></head>
<body>
<div id="app"></div>
<script src="/bundle.js"></script>
</body>
</html>A parser that reads only this HTML finds no text and no links. The crawler must run bundle.js to see what a visitor sees. Running scripts is called rendering.
The rendering step in the pipeline
Rendering is expensive. It needs a full browser engine, memory, and time for scripts and network calls to finish. Fetching raw HTML is cheap by comparison. Large search engines therefore treat rendering as a separate stage that runs after the first fetch.
The flow works like this:
- The crawler fetches the raw HTML and records the status code.
- It extracts any links already present in that HTML and adds them to the frontier.
- The page goes into a render queue. Some engines render every page. Others render only when the raw HTML looks incomplete.
- A headless browser loads the page, runs scripts, and waits for the page to settle. A headless browser is a real browser engine that runs without a visible window.
- The engine takes a snapshot of the final DOM. The DOM (Document Object Model) is the live tree of elements in the page after scripts have run.
- The engine extracts text and links from that snapshot and sends them on to tokenization and indexing.
Because step 3 is a queue, rendered content can reach the index later than raw HTML content. The delay varies. It can be seconds or much longer, depending on the engine and the site.
What the renderer can and cannot do
A rendering crawler behaves like a browser with some restrictions. Treat these as typical behaviors and check your target engine's documentation for exact limits.
| Behavior | What usually happens |
|---|---|
| Runs JavaScript | Yes, with a recent browser engine |
| Waits for network calls | Only for a limited time, then it snapshots the page |
| Clicks, scrolls, types | No. It does not interact like a user |
| Stores cookies and local storage | Not kept between page loads |
| Honors robots.txt for scripts and API calls | Yes. Blocked resources are not fetched |
| Shows a permission prompt (location, notifications) | Declined or ignored |
Each row leads to a common failure.
- Content that needs a click. If text appears only after a user presses a button, the renderer never sees it. Put the content in the DOM on load, even if it is visually collapsed.
- Content that needs scrolling. Infinite scroll loads more items when the user scrolls. The renderer does not scroll, so later items may never load. Give each batch its own URL, such as
/products?page=2, and link to it. - Slow data. If the data call takes longer than the renderer waits, the snapshot is taken with a loading spinner in it.
- Blocked resources. If robots.txt disallows
/static/js/or/api/, the renderer cannot load the script or the data. The page then renders empty, even though the HTML itself is allowed.
Links in rendered pages
Link extraction depends on real anchor elements. A crawler follows <a href="...">. It does not reliably follow other patterns.
<!-- Crawlable: a real anchor with a resolvable href -->
<a href="/guide/crawling">Crawling guide</a>
<!-- Not reliably crawlable: no href, behavior lives in a script -->
<span onclick="goTo('/guide/crawling')">Crawling guide</span>
<!-- Not crawlable: the fragment is dropped, so all routes look like one URL -->
<a href="/#/guide/crawling">Crawling guide</a>The third case matters for older single-page applications. A fragment is the part of a URL after #. Crawlers drop fragments when they normalize URLs, so /#/a and /#/b both collapse to /. Use the History API so that each view has a real path, such as /guide/crawling, and make sure the server returns the right content for that path.
Soft 404s in single-page apps
A single-page application often serves the same shell for every path. The script then decides what to show. For an unknown path, the script may show "Page not found" but the server still sends status 200. A crawler sees a successful response and may index a page of error text. This is called a soft 404.
Fix it in one of two ways:
- Have the server return a real
404status for unknown paths. - If the server cannot know, have the script redirect to a URL that returns
404, or add<meta name="robots" content="noindex">to the error view.
Choosing a rendering strategy
You can control how much work the crawler has to do by choosing where the HTML is built.
| Approach | Where HTML is built | Crawler effect | Trade-off |
|---|---|---|---|
| Client-side rendering | In the browser | Needs the render queue. Indexing may be delayed. | Simple to host, most fragile for search |
| Server-side rendering (SSR) | On the server, per request | Content is in the first response | Needs a server that runs the app |
| Static generation | At build time | Content is in the first response and cheap to serve | Content is only as fresh as the last build |
| Hydration | Server sends HTML, then the browser attaches scripts | Content is in the first response | More complex code to maintain |
Hydration means the browser takes HTML that already exists and makes it interactive by attaching event handlers. For search, SSR, static generation, and hydration all put the important text and links in the raw HTML. That makes indexing less dependent on the render queue.
Dynamic rendering is a workaround where a server sends pre-rendered HTML to crawlers and the normal app to users. Search engines now describe it as a temporary workaround, not a long-term solution. It adds a second code path that can drift out of sync with what users see.
Checking what a crawler sees
The quickest test is to compare raw HTML with the rendered result. The script below fetches the raw HTML and checks whether a phrase from the visible page is in it. If the phrase is missing, the content is added by JavaScript.
from urllib.request import Request, urlopen
def raw_contains(url, phrase):
req = Request(url, headers={"User-Agent": "RawHtmlCheck/1.0"})
with urlopen(req, timeout=10) as resp:
html = resp.read().decode("utf-8", errors="replace")
return phrase.lower() in html.lower()
url = "https://example.com/guide"
phrase = "How crawlers work"
if raw_contains(url, phrase):
print("Phrase is in the raw HTML.")
else:
print("Phrase is missing from the raw HTML. It may depend on JavaScript.")To see the rendered side, use a headless browser. This example uses Playwright, a third-party library installed with pip install playwright followed by playwright install chromium.
from playwright.sync_api import sync_playwright
def rendered_html(url):
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(url, wait_until="networkidle", timeout=30000)
html = page.content()
browser.close()
return html
html = rendered_html("https://example.com/guide")
print(len(html))
print("How crawlers work" in html)page.content() returns the DOM after scripts have run. If the phrase appears here but not in the raw HTML, the page depends on rendering. Your own browser's "view source" shows the raw HTML, and the Elements panel in developer tools shows the rendered DOM. Search engines also offer URL inspection tools that show the rendered HTML they stored.
Be aware of one limit. Your Playwright run uses a fast machine, a warm connection, and no robots.txt rules. The search engine's renderer may wait less, block some resources, and run later. A page that passes your test can still fail in the engine's renderer, so also check the engine's own tool.
Common errors
- Blocking script or API paths in robots.txt. The page HTML is allowed but the data is not, so the rendered page is empty.
- Putting
noindexin raw HTML and removing it with JavaScript. Engines may stop at the rawnoindexand never render the page. Do not rely on scripts to cancel anoindex. - Setting the canonical URL only in JavaScript. Raw HTML and rendered HTML can then disagree about the canonical. Put it in the raw HTML.
- Using fragments for routing. All routes collapse into one URL.
- Returning
200for missing pages. This creates soft 404s. - Loading key text after a user event. The renderer does not click or scroll.
Exercise
A product page's raw HTML contains only an empty <div id="app">. A visitor sees the product description. Your robots.txt contains Disallow: /api/, and the description is loaded from /api/products/42. Explain why the page may be indexed with no description, and give two fixes.
Show answer
The renderer honors robots.txt, so it cannot fetch /api/products/42. The script gets no data and the snapshot contains an empty app, even though the HTML URL itself is allowed. Fix 1: remove or narrow the Disallow: /api/ rule so the product endpoint can be fetched. Fix 2: use server-side rendering or static generation so the description is already in the raw HTML and does not depend on the API call during rendering.
Exercise
A site uses links like <a href="/#/pricing"> and <a href="/#/about">. Why does a crawler treat these as one page, and how do you fix it?
Show answer
Crawlers drop the fragment (everything after #) when normalizing URLs, so both links become /. Fix it by using the History API so each view has a real path such as /pricing and /about, and make the server return the correct content for each path.