Sub topic 9
Rendering JavaScript-dependent pages
This chapter assumes you know how a crawler fetches HTML and extracts links from <a href> tags. It covers what changes when a page's content only appears after JavaScript runs.
Why raw HTML is not always enough
A basic crawler reads the HTML the server returns. Many sites send a nearly empty page and let JavaScript build the content in the browser. This is called client-side rendering. The crawler sees the shell, not the text.
<!doctype html>
<html>
<head><title>Guide</title></head>
<body>
<div id="app"></div>
<script src="/app.js"></script>
</body>
</html>Here app.js fills in #app. Without running it, there is nothing to tokenize and no links to follow.
How the engine renders
Rendering means loading the page in a headless browser. This is a real browser engine with no visible window. It runs the scripts, fetches the resources they request, and produces the final DOM (the document tree after scripts have changed it). The indexer then uses that DOM for text and links.
Rendering costs far more CPU and time than a plain fetch. Because of this, engines usually split the work into two phases. They fetch and parse the raw HTML first. Then they queue the page for rendering, which may happen later.
The delay between the two phases means JavaScript-only content can be indexed later than content in the raw HTML.
Checking what the crawler sees
A quick test is to look for a visible phrase in the raw HTML. If it is missing, the content depends on rendering.
from urllib.request import urlopen
def in_raw_html(url, phrase):
with urlopen(url, timeout=10) as resp:
html = resp.read().decode("utf-8", errors="replace")
return phrase.lower() in html.lower()
print(in_raw_html("https://example.com/", "Example Domain"))If this prints False for text you can see in a browser, a crawler that does not render would miss it.
Common errors
- Links without
href. A<span onclick="go()">is not a link to a crawler. Use<a href="/page">. - Content behind interaction. Text that loads only after a click, scroll, or login usually never appears in the rendered DOM.
- Blocked resources. If robots.txt disallows
/app.jsor the API it calls, the renderer cannot load them. The page then renders empty. - Fragment-only routing. URLs like
/#/productsare treated as one URL, because crawlers drop the fragment. Use real paths. - Slow or failing scripts. Renderers stop waiting after a limit. Errors or long API calls can leave the DOM incomplete.
- Changing directives with JavaScript. A
noindexin the raw HTML may be seen before rendering. Do not depend on scripts to add or remove it.
Summary of trade-offs
| Approach | What the crawler gets | Risk |
|---|---|---|
| Server-side rendering | Full content in raw HTML | More server work |
| Static generation | Prebuilt HTML files | Rebuild needed for updates |
| Client-side rendering | Empty shell until rendered | Delayed or missed indexing |
Exercise
A page shows its product descriptions in a browser, but the raw HTML contains only an empty div and a script tag. The script loads data from /api/products, and robots.txt disallows /api/. What is the likely indexing result, and what is one fix?
Show answer
The renderer cannot fetch /api/products, so the rendered DOM stays empty and the descriptions are not indexed. Fixes include allowing /api/ in robots.txt, or rendering the descriptions on the server so they are in the raw HTML.