Sub topic 9
Making Links Crawlable
This chapter assumes you know basic HTML, including the a element and its href attribute. It also assumes you know how a browser requests a page over HTTP, and that you know what a crawler is. By the end you will be able to tell which links a search engine can follow, fix the ones it can't, and check the result.
What a crawler needs from a link
A crawler discovers new pages by extracting URLs from pages it already knows about. The most reliable source of URLs is an a element with a real href attribute. Google's documentation states that it can only follow links that are a elements with an href attribute. Other search engines work on the same principle.
A crawler does not behave like a person. It doesn't click, hover, scroll, type, or wait for an animation. It reads the HTML, and for many pages it also runs JavaScript to build the final DOM (the page structure after scripts have run). Then it collects every href it finds. A navigation method that exists only as behavior after a user action is invisible to that process.
A link is crawlable when all three of these hold:
- It is an
aelement. - Its
hrefresolves to a real URL that returns a page. - It is present in the HTML or in the rendered DOM without any user interaction.
Markup that works and markup that doesn't
The simplest way to check your understanding is to compare patterns side by side.
<!-- Crawlable: a element with a real href -->
<a href="/guides/url-slugs/">guide to writing URL slugs</a>
<!-- Not crawlable: no a element, no href -->
<span onclick="location.href='/guides/url-slugs/'">guide to writing URL slugs</span>The first line gives a crawler a URL and a description of the target. The second gives it neither. Browsers navigate fine with the second form, so the bug is easy to miss during manual testing.
| Pattern | Crawlable? | Why |
|---|---|---|
| `<a href="/guides/">Guides</a>` | Yes | Standard link with a resolvable URL. |
| `<a href="https://example.com/guides/">Guides</a>` | Yes | Absolute URLs work the same as relative ones. |
| `<a href="/guides/" onclick="track()">Guides</a>` | Yes | The `href` is present. The extra handler is ignored. |
| `<a onclick="go('/guides/')">Guides</a>` | No | No `href`, so there is no URL to extract. |
| `<a href="javascript:void(0)">Guides</a>` | No | The `href` is a script, not a page URL. |
| `<a href="#" onclick="go('/guides/')">Guides</a>` | No | `#` points to the current page. The real target is hidden in the handler. |
| `<a href="/#/guides/">Guides</a>` | Poor | Fragments (everything after `#`) are not treated as separate pages. |
| `<button onclick="location.href='/guides/'">Guides</button>` | No | A button is not a link element. |
| `<div role="link" tabindex="0">Guides</div>` | No | ARIA roles help assistive technology but give a crawler no URL. |
Buttons and links have different jobs
Use an a element when the action moves the user to another URL. Use a button when the action changes something on the current page, such as opening a menu or submitting a form. This split also helps accessibility, because screen readers announce links and buttons differently and users expect different keyboard behavior from each.
JavaScript navigation without losing the href
Single-page applications often intercept clicks to avoid a full page reload. That is fine as long as the underlying markup is still a normal link. The handler enhances the link. It does not replace it.
<nav>
<a href="/guides/url-slugs/" data-spa-link>URL slug guide</a>
<a href="/guides/internal-links/" data-spa-link>Internal linking guide</a>
</nav>
<script>
document.addEventListener("click", (event) => {
const link = event.target.closest("a[data-spa-link]");
if (!link) return;
// Let the browser handle modified clicks (new tab, download, etc.)
if (event.metaKey || event.ctrlKey || event.shiftKey || event.button !== 0) return;
event.preventDefault();
history.pushState({}, "", link.href);
// Render the new view here, for example loadView(link.pathname);
});
</script>Each link still has a real href. A crawler reads that and requests the URL. A user's click is intercepted and handled without a reload. Each URL must also work when requested directly, so the server (or a pre-render step) has to return content for /guides/url-slugs/ and not just the app shell.
Most routing libraries provide a link component that renders an a with an href. Verify the rendered output instead of trusting the component name.
Hash routing
Older single-page setups use URLs such as /#/guides/url-slugs. The server only ever sees /. Search engines treat everything after # as a position inside one page, so all of those "pages" collapse into one URL. Use the History API (pushState) with real paths instead.
Content that appears only after interaction
Links must be in the HTML or rendered DOM without anyone doing anything. These situations commonly hide them:
- "Load more" buttons. If the next batch of items is fetched only on click, the links in that batch never appear for a crawler. Provide paginated URLs such as
/blog/page/2/as real links, and optionally enhance them with JavaScript. - Infinite scroll. The crawler does not scroll. Offer the same paginated URLs as a fallback.
- Tabs and accordions. Content that is in the DOM but visually hidden is generally still read. Content that is fetched only when a tab is clicked is not.
- Search forms and dropdown filters. A crawler does not type queries or choose options. Pages reachable only through a form submission have no link pointing to them.
- Links inside iframes. These belong to the framed document, not to your page.
Relative URLs, base, and image links
A relative href such as ../slugs/ is resolved against the current page URL, or against a base element if one exists. Mistakes here create links that work in your browser but point at wrong URLs in the crawler's view. Root-relative paths (/guides/url-slugs/) are the least error-prone, and absolute URLs are equally safe. If you use a base element, test every relative link on the page.
Image links are crawlable, since the a wraps the image. The alt text of the image then acts as the anchor text:
<a href="/guides/url-slugs/">
<img src="/img/slug-diagram.png" alt="Diagram of the parts of a URL slug" width="640" height="360">
</a>An image with an empty alt inside a link gives the crawler a URL with no description at all.
Detecting uncrawlable patterns with a script
You can find many of these problems automatically. This script scans HTML and reports anchors with a missing or script-based href, and click handlers on elements that are not links.
from html.parser import HTMLParser
class UncrawlableFinder(HTMLParser):
def __init__(self):
super().__init__()
self.problems = []
def handle_starttag(self, tag, attrs):
attributes = dict(attrs)
if tag == "a":
href = (attributes.get("href") or "").strip()
if not href:
self.problems.append(("a without usable href", "(missing or empty)"))
elif href == "#" or href.lower().startswith("javascript:"):
self.problems.append(("not a page URL", href))
elif "onclick" in attributes:
self.problems.append((f"click handler on {tag}", attributes["onclick"]))
sample = """
<a href="/guides/url-slugs/">guide to URL slugs</a>
<a href="javascript:void(0)">Open</a>
<a>Bare anchor</a>
<span onclick="location.href='/more/'">More</span>
<button onclick="go()">Go</button>
"""
finder = UncrawlableFinder()
finder.feed(sample)
for kind, detail in finder.problems:
print(f"{kind}: {detail}")Running it prints:
not a page URL: javascript:void(0)
a without usable href: (missing or empty)
click handler on span: location.href='/more/'
click handler on button: go()The script reads raw HTML only. It can't see links that scripts add later, and a click handler on a button is sometimes legitimate. Treat its output as a list of items to review, not as a verdict.
Source HTML versus rendered DOM
When a page builds its links with JavaScript, there are two versions to compare:
- Source HTML: what the server sends. You can see it with "View page source" or
curl. - Rendered DOM: what exists after scripts run. You can see it in the browser's Elements panel.
Google renders JavaScript, but rendering happens in a separate step that can lag behind the first fetch. Other crawlers may not render at all. Links that exist in the source HTML are discovered fastest and by the widest set of crawlers. For important navigation such as the main menu, category hubs, and pagination, put the links in the server-delivered HTML.
To check a page:
- Fetch the raw HTML with
curl -s https://example.com/page/and search for thehrefyou expect. - If it is missing, open the page in a browser and inspect the DOM. If the link appears only there, it depends on rendering.
- Use the URL Inspection tool in Google Search Console to see the rendered HTML that Google got, and confirm the link is present.
Other things that block discovery
A well-formed link can still fail to be followed or counted:
- robots.txt. If the target path is disallowed, the crawler will not fetch it. It may still know the URL exists from the link.
rel="nofollow"and similar attributes. These change how a link is treated. The next chapters cover them in detail.- Login walls. Crawlers have no credentials, so a link to a page that requires sign-in leads nowhere useful.
- Broken targets. A link whose URL returns 404 or a long redirect chain wastes crawl effort.
Common mistakes
- Using
<a href="#">with a click handler for real navigation. - Building a menu out of
divorspanelements withonclick. - Relying on hash routes (
/#/page) for content you want indexed. - Hiding product or article lists behind "Load more" with no paginated URLs.
- Wrapping an image in a link and leaving the
alttext empty. - Assuming a routing component outputs real
hrefattributes without inspecting the HTML it produces.
Exercise
A site's category menu uses <li onclick="window.location='/shoes/'">Shoes</li>. A teammate says it works fine because clicking it loads the page. Explain why search engines may never discover /shoes/ through this menu, and write the corrected markup.
Show answer
A crawler extracts URLs from a elements with an href. An li with an onclick handler has no href, and crawlers do not click, so the menu gives them no URL. The fix is a normal link inside the list item: <li><a href="/shoes/">Shoes</a></li>.
Exercise
An article archive loads older posts only when the visitor presses a "Load more" button, which fetches JSON and inserts new items. Old posts are not linked from anywhere else. What is the likely SEO outcome, and how would you fix it without removing the button?
Show answer
The older posts are likely to be orphan pages for crawlers. The crawler never presses the button, so it never sees links to them. Add paginated archive URLs such as /blog/page/2/, linked with real a href elements in the HTML. Then enhance those links with JavaScript so the button loads the next page's content for users who have scripts.
Exercise
You run curl on a page and the main navigation links are absent, but they appear in the browser's Elements panel. What does this tell you, and why does it matter for important navigation?
Show answer
The links are added by JavaScript after the page loads, so they exist only in the rendered DOM. Google can usually render and find them, but later than links in the source HTML, and other crawlers may not render at all. Important navigation should be delivered in the server-sent HTML.