Meta Tags in SPAs
You'll learn how to dynamically update page metadata as users navigate through a single-page application without full page reloads.
The SPA Metadata Problem
In traditional multi-page applications, each route loads a new HTML document with its own <title>, <meta> tags, and Open Graph properties. Single-page apps break this pattern—the initial HTML loads once, then JavaScript handles navigation. Without intervention, every route would share the same metadata from that first HTML shell, making social shares and search listings show identical titles and descriptions regardless of which page users visit.
Dynamic Meta Tag Management
Since SPAs control the page through JavaScript, you must programmatically update the document's metadata whenever the route changes. This means:
- Setting
document.titledirectly - Selecting and modifying
<meta>tags in the<head> - Creating or updating Open Graph and Twitter Card tags
- Ensuring canonical URLs reflect the current route
Most modern frameworks provide utilities or lifecycle hooks that trigger on route changes, making this the perfect moment to update metadata based on the new page's data.
The Timing Challenge
The catch: bots and social crawlers may not wait for your JavaScript to execute. This is why techniques like server-side rendering or static pre-rendering (covered in earlier lessons) remain critical—they ensure metadata exists in the initial HTML response. For client-side navigation after the app loads, dynamic updates improve the user experience (browser tabs show correct titles) and help with subsequent bot visits.
Key Takeaway:
In SPAs, metadata doesn't automatically update with route changes—you must use JavaScript to dynamically modify document.title and <meta> tags on navigation, while relying on SSR or pre-rendering to ensure bots see correct metadata on initial page loads.