I’m working on a website where different pages need unique meta descriptions based on their content.
I’m trying to understand how to generate these descriptions dynamically with JavaScript and ensure search engines can access the correct metadata for each page.
Is it better to update meta tags on the client side, or should they be generated on the server? What are the main technical considerations for SEO when choosing between these approaches?
I’d appreciate any examples or suggestions based on practical web development experience.
To update or create a meta description dynamically in the browser, you can target the existing <meta name="description"> tag or create one if it does not exist:
function setMetaDescription(description) {
let metaTag = document.querySelector('meta[name="description"]');
if (!metaTag) {
metaTag = document.createElement('meta');
metaTag.setAttribute('name', 'description');
document.head.appendChild(metaTag);
}
metaTag.setAttribute('content', description);
}
When updating meta tags purely in the client DOM, the initial HTML delivered from the server lacks the specific meta description. Although Google’s crawler processes JavaScript, rendering can be queued and delayed. More importantly, most social media and link preview crawlers (such as Open Graph scrapers for Slack, Twitter/X, LinkedIn, Discord) do not execute JavaScript at all and will only inspect the raw initial HTML.
If SEO and link previews are important, generating meta tags on the server is much more reliable. With SSR or static site generation, the server injects the unique meta tags into the initial response so crawlers immediately parse them without needing to execute client scripts. If search engine visibility and link sharing are priorities for these pages, generating the tags on the server is generally the safer approach.