History API and Indexing
You'll learn how using pushState to change URLs in single-page apps affects search engine crawling and why each route needs to be independently accessible.
The Core Problem
In single-page applications, history.pushState() lets you update the browser's URL bar without reloading the page—great for user experience! But here's the catch: when you navigate from /products to /products/shoes using pushState, search engine crawlers don't execute that JavaScript journey. They request /products/shoes directly.
If that URL only works after JavaScript runs and changes state, the crawler hits a dead end. It won't execute your entire app logic to reach that route—it expects the URL to work as a standalone entry point.
Think of It Like Building Doors
Imagine a hotel where every room is accessible only by walking through the lobby and following signs. That's how many SPAs work with pushState. But search engines are like guests who teleport directly to room numbers. If Room 302 doesn't have its own door to the outside, they can't get in.
Each route must be indexable as a unique URL means every URL your app pushes to the history stack should:
- Return valid HTML when requested directly (via SSR or static pre-rendering)
- Include proper metadata for that specific page
- Work without requiring JavaScript execution to display core content
The Solution Pattern
When you use pushState('/about'), ensure /about is also:
- A valid server route that responds with appropriate content
- Pre-rendered as a static file, or
- Handled by server-side rendering that generates the page on-demand
Crawlers follow links and index URLs—they don't replay user interactions.
Key Takeaway: Every URL created with pushState must function as an independent entry point with its own content and metadata, not just as a JavaScript-triggered state change within your app.