Canonical and noindex with JavaScript: what Google does
Google can read canonical and robots tags that JavaScript adds, but its docs warn against relying on it. See where raw and rendered HTML conflict, and how to fix it.
By Crawlito · 9 min read
In short
- Google says JavaScript can add or change a robots meta tag and can inject a canonical link, and that the best way to set a canonical is still plain HTML.
- If the raw HTML contains noindex, Google says it may skip rendering and JavaScript execution, so a script that tries to remove the tag may not work as expected.
- Google warns that a canonical injected by JavaScript must be the only canonical on the page, and should not point somewhere other than the canonical in the original HTML.
- A crawler that does not run JavaScript only sees the raw HTML, and in Vercel and MERJ's December 2024 study the OpenAI, Anthropic, Meta, ByteDance and Perplexity crawlers did not execute JavaScript.
- The safest setup is the final canonical, robots meta tag and title in the HTML your server sends, so every crawler reads the same answer.
On this page
- Does Google read canonical and robots tags that JavaScript adds?
- Why does noindex in the raw HTML stop JavaScript from changing it?
- Canonical tags changed by JavaScript: when they work and when they conflict
- Titles and meta descriptions set after load
- What crawlers that do not render JavaScript see
- How to compare raw and rendered meta tags
- How to fix it: put the final tags in the server HTML
- Frequently asked questions
- Check your own pages
Google can read a canonical tag or a robots meta tag that JavaScript adds, but it does not recommend relying on it, and it documents one case where it does not work: a noindex in the raw HTML that a script tries to remove. Crawlers that do not run JavaScript never see the change at all. If you set these tags with React Helmet, Google Tag Manager or a client-side router, the safe rule is to put the final tags in the HTML your server sends.
This post covers what Google's documentation says about each tag, what breaks when the raw HTML and the rendered page disagree, what other crawlers see, and how to compare the two versions yourself. It stays with what Google documents. Where something is not documented, it says so.
Does Google read canonical and robots tags that JavaScript adds?
Yes, with caveats. Google's JavaScript SEO basics describes the process: Googlebot crawls a page, queues it for rendering if it returns a 200 status code, and later a headless Chromium renders the page and runs its JavaScript. The rendered result is what Google uses for indexing. The same guide says a page may wait in the render queue for a few seconds, and sometimes longer.
For each tag, Google's guidance differs:
| Tag | Can JavaScript set it? | What Google says |
|---|---|---|
<link rel="canonical"> | Yes | Google does not recommend it. HTML is the best way. If you do use JavaScript, inject one tag only. |
<meta name="robots"> | Yes | Use it carefully. Removing or changing a noindex with JavaScript may not work. |
<title> | Yes | You can set or change it with JavaScript. |
<meta name="description"> | Yes | You can set or change it with JavaScript. |
Separately, Google's robots meta tag documentation says Google typically renders pages in order to index them, but that rendering is not guaranteed. It does not say anything specific about robots tags injected by scripts, so "typically" is the honest summary.
Why does noindex in the raw HTML stop JavaScript from changing it?
Because of how Google schedules rendering. The JavaScript SEO basics guide says pages enter the render queue unless a robots meta tag or header says not to index them. It also warns that when Google encounters noindex, it may skip rendering and JavaScript execution, so removing or changing the tag with JavaScript may not work as expected. The guide's advice is direct: if you want a page indexed, do not put a noindex tag in the original page code.
This is one way a staging leftover can turn into a live problem. A page ships with <meta name="robots" content="noindex"> in the server HTML, and a script on the client removes it once the app loads. In a browser, the rendered page looks fine, with no noindex in the Elements panel. Google may never run the script that removes it. The cause is covered from the staging side in site noindex by mistake.
The reverse direction is documented as a working pattern. Google's JavaScript troubleshooting guide shows a script that adds a noindex robots meta tag to an error view in a single-page app, as one way to avoid soft 404s. So adding noindex with JavaScript is something Google describes. Removing it is the case it warns about.
Two other details from the robots meta documentation matter here:
- Google says that when robots rules conflict, the more restrictive one applies. If the raw HTML and a script leave two robots tags on the page, the stricter directive wins.
- Rules are only read if Google can fetch the page. If robots.txt disallows the URL, Google never sees the tag at all. robots.txt vs meta robots explains that trap in more detail.
Canonical tags changed by JavaScript: when they work and when they conflict
Google's guidance for canonicals is more specific than for any other tag. In the "Set the canonical URL" section of the JavaScript SEO basics guide, Google says the best way to set the canonical is HTML. It says JavaScript can set it, and that Google picks up an injected canonical when it renders, but it recommends against using JavaScript for this. It adds two conditions if you do:
- Do not use JavaScript to point the canonical at a different URL than the one in the original HTML. If you cannot set the canonical in HTML, leave it out of the original HTML and set it only with JavaScript.
- The injected tag must be the only
rel="canonical"link on the page. Google warns that conflicting or multiple canonical link tags may lead to unexpected results.
Google's page on consolidating duplicate URLs adds that a canonical link element is accepted only inside the HTML head, and that you should not specify different canonical URLs for the same page using different techniques, for example an HTML link and an HTTP header. It calls a rel="canonical" link annotation a strong signal that the specified URL should become canonical.
That leaves three situations worth separating:
| Raw HTML | After JavaScript runs | What the documentation covers |
|---|---|---|
| No canonical | One canonical injected | Allowed. Google picks it up when it renders. |
| Canonical A | Canonical A (unchanged) | Fine, and the recommended setup. |
| Canonical A | Canonical B, or A and B both present | Google advises against it and warns of unexpected results. Which one Google would pick is not documented. |
The third row is the risky one, and it is easy to create by accident. Say a framework ships a default canonical in the server template, and a client-side head library then adds a second tag or rewrites the first. In the browser it looks consistent. In the raw HTML, the first canonical is already there. Each crawler may then act on whichever version it reads.
Titles and meta descriptions set after load
Google says you can use JavaScript to set or change both the <title> element and the meta description, so this works for Google. What it does not do is protect you from stale or generic raw HTML. If the server sends <title>App</title> and a script changes it to the real page title, Google can see the final title after rendering, but any crawler that skips rendering sees App on every URL.
Titles also affect how a page is described to people and tools, which is why the raw value matters. Page titles and meta descriptions for AI search covers how to write them. The point here is only where they should live: in the server HTML.
What crawlers that do not render JavaScript see
A crawler that does not run JavaScript reads the raw HTML and nothing else. Whatever a script changes later is invisible to it, including a corrected canonical or a removed noindex.
Google documents that its own crawler renders. For AI crawlers, the best public measurement is Vercel and MERJ's December 2024 analysis, which reported that the crawlers from OpenAI (OAI-SearchBot, ChatGPT-User, GPTBot), Anthropic (ClaudeBot), Meta, ByteDance and Perplexity did not execute JavaScript. The same article says Google's Gemini uses Googlebot's infrastructure and Applebot renders JavaScript, so the finding is not true of every AI-related crawler. Treat it as a snapshot from December 2024, not a permanent fact. Do AI crawlers run JavaScript? covers what is and is not documented.
There is a second gap. Whether a given AI crawler or AI product reads canonical tags or robots meta tags at all is not documented in the same way Google documents its own behavior. noindex, nosnippet and X-Robots-Tag for AI search lists what each operator says about robots rules. So even a perfect raw-HTML canonical is a signal for Google and similar search systems, not a promise that every assistant acts on it.
How to compare raw and rendered meta tags
Compare the two versions of the head. Start with the raw HTML, which is what a non-rendering crawler reads:
curl -sL --compressed https://example.com/page \
| grep -iE '<title|rel="canonical"|name="(robots|googlebot|description)"'Look at response headers too, since an X-Robots-Tag header can add noindex outside the HTML:
curl -sIL https://example.com/page | grep -iE '^(HTTP|x-robots-tag|link)'To see both versions in one place, paste this into the DevTools console on the live page. It fetches the raw HTML again, parses it without running scripts, and prints the same fields from both documents:
const pick = (doc) => ({
title: doc.title,
canonical: [...doc.querySelectorAll('link[rel="canonical" i]')].map((l) => l.getAttribute("href")),
robots: [...doc.querySelectorAll('meta[name="robots" i], meta[name="googlebot" i]')].map((m) => m.content),
description: [...doc.querySelectorAll('meta[name="description" i]')].map((m) => m.content),
});
const raw = await (await fetch(location.href, { cache: "no-store" })).text();
console.log("raw", pick(new DOMParser().parseFromString(raw, "text/html")));
console.log("rendered", pick(document));Any difference between the two lines is a place where crawlers can disagree. Two canonicals, a different href, or a robots value that exists in only one version are all problems to fix. The snippet runs with your browser's cookies and user agent, so it is a check of your own rendering, not of what a firewall serves to bots. How to see your page as an AI crawler, with curl covers the user-agent side.
For Google specifically, Search Console's URL Inspection tool shows two canonical fields on an indexed URL: the user-declared canonical and the Google-selected canonical. Google's URL Inspection help says there is no guarantee Google chooses your preferred one, and that canonical selection happens at indexing time. So the live test cannot predict it; only the indexed data shows Google's choice. The same help page says the "Indexing allowed?" field reports whether a page explicitly disallows indexing, and that live and indexed states can differ when a noindex has been removed but not yet reindexed.
How to fix it: put the final tags in the server HTML
The fix is the same for all four tags. Make the server response contain the value you want crawlers to use.
- Render the
headon the server. With a framework that supports server rendering or static generation, use its head or metadata API so the canonical, robots tag and title are in the first response. Check your framework's documentation for how its head helper behaves during server rendering, and confirm the result with the curl command below rather than assuming it. - Set the canonical once. One
rel="canonical"link in thehead, with an absolute URL, produced by one piece of code. If a template and a client library both emit one, remove one. - Do not use
noindexas a placeholder. If a page should be indexed, its raw HTML should not carrynoindex, even temporarily. Leave the tag out until you decide the page must be hidden. - Use JavaScript only for additions Google documents. Adding
noindexto an error view in a single-page app is a documented use. Replacing a raw tag with a different one is the pattern to avoid. - Retest both versions. Run the curl command and the console snippet again. The two outputs should match.
If you cannot change the server yet, Google's fallback for canonicals is to leave the tag out of the original HTML and set one with JavaScript. That is the documented second-best option, with the limits above: Google can use it, and non-rendering crawlers will not.
Frequently asked questions
Does Google respect a canonical tag added with JavaScript?
Google says it picks up an injected canonical when it renders the page, but that HTML is the best place for it. Canonical is a strong signal, and Google says there is no guarantee it chooses your preferred URL. The injected tag must be the only canonical on the page.
Can I remove noindex with JavaScript?
Google says not to rely on it. When Google sees noindex in the original HTML it may skip rendering and JavaScript execution, so a script that removes the tag may not run. Remove it from the server HTML.
Should I set the page title with JavaScript?
Google says you can set or change the title and meta description with JavaScript. A crawler that does not render sees only the title in the raw HTML, so put the real title there.
Do AI crawlers read my canonical or robots meta tag?
It depends on the operator, and many do not document it. A crawler that does not run JavaScript can only read tags that are in the raw HTML, which is another reason to put them there.
Is a JavaScript-set canonical enough for duplicate URLs?
It can work for Google if it is the only canonical and renders correctly. A server-rendered canonical removes the dependence on rendering, so it is the more dependable option.
Check your own pages
Compare one page's raw and rendered head with the commands above, then run the free Crawlito check on the same URL. It takes a single page and needs no account. If you have a larger site, a full-site audit covers up to 500 pages.
Filed under Rendering and content. Topics: javascript, rendering, canonical, meta-robots, google, troubleshooting