Skip to the indexed answers
Digital Marketing Index

usdigitalmarketingindex.com SEO

How can conflicting canonical tags in initial HTML and JavaScript-rendered HTML be diagnosed?

Read the canonical tag in the HTML the server sends, read it again in the HTML after scripts have run, and compare the two. If they name different URLs, or if rendering leaves two canonical tags in the document, the page is giving conflicting instructions. Google’s documentation addresses this case directly: “you shouldn’t use JavaScript to change the canonical URL to something else than the URL you specified as the canonical URL in the original HTML”, and “The best way to set the canonical URL is to use HTML” (Google’s JavaScript SEO basics). The fix is to have one source decide the canonical URL, and to make the served and rendered values identical.

Reading initial and rendered tags

Collect both values for the same URL.

Served HTML. Fetch the page without running scripts and extract the tag:

$ curl -s https://www.example.com/shoes/trail-runner | grep -i 'rel="canonical"'

Viewing the page source in a browser shows the same thing.

Rendered HTML. In Search Console, inspect the URL, run a live test, and open the rendered HTML. Search it for canonical. In a browser, the equivalent is to load the page, wait, and run this in the console:

Array.from(document.querySelectorAll('link[rel="canonical"]'))
  .map(function (link) { return link.href; });

The command returns every canonical tag in the rendered document. More than one entry is itself a finding.

A diff of the two versions for a conflicting page, with example URLs:

<head>
    <title>Trail Runner shoe</title>
-   <link rel="canonical" href="https://www.example.com/shoes/trail-runner">
+   <link rel="canonical" href="https://www.example.com/shoes/">
  </head>

The line marked with a minus is what the server sent. The line marked with a plus is what the document contained after scripts ran. The server says the product page is canonical. The rendered page says the category is.

URL Inspection also reports the user-declared canonical it read and the canonical Google selected. If the declared value shown there differs from the tag in your served HTML, rendering changed it.

Typical framework causes

Conflicts come from two parts of a system each believing it owns the head of the document.

Cause What happens
A layout sets a default canonical and the page component fails to override it Every page declares the layout's default, such as the home page or the parent section
The client router does not update the head on navigation The first page's canonical persists across views
A head-management library appends a tag without removing the server's Two canonical tags with different values
The server builds the canonical from the configured site URL and the client builds it from the browser's address Differences in host, protocol, trailing slash, or parameters
A tag manager or third-party script injects a canonical A value nobody on the development team set
Data for the page loads after render, and the canonical is computed before it arrives A placeholder or undefined value in the tag

To find which applies, load the page with scripts disabled and enabled, and step through the head as scripts execute, using a mutation breakpoint on the head element in browser developer tools.

Single-source-of-truth fix

Choose one place where the canonical URL is determined, and make everything else read from it.

  1. Compute the canonical URL on the server, from the route and a configured canonical host, not from the incoming request’s address.
  2. Output it in the served HTML.
  3. If the client framework manages the head, give it the same value from the same function, so that hydration leaves the tag unchanged.
  4. On client-side navigation, replace the tag with the new route’s value. Replace, never append.
  5. Remove any canonical injection from tag managers and third-party scripts.

Google’s documentation allows one alternative: if the canonical cannot be set in the HTML, JavaScript can set it, provided it is left out of the original HTML. That gives a single value as well. What must not exist is a served value and a different rendered value.

A correct page, served and rendered:

<link rel="canonical" href="https://www.example.com/shoes/trail-runner">

Post-fix comparison

Repeat the collection and compare.

Check Pass condition
Canonical in served HTML One tag, the intended URL
Canonical in rendered HTML One tag, identical to the served value
Count of canonical tags after render Exactly one
After client-side navigation to another route The tag shows the new route's URL
Direct request to that second route Served tag matches what navigation produced
With a tracking parameter added Tag shows the clean URL in both versions
URL Inspection, live test User-declared canonical equals the intended URL
A sample from every template All pass

Automate the first three rows in the test suite: for a list of routes, fetch the served HTML, render the page in a headless browser, and assert that the two canonical values are equal and that only one tag exists. A conflict of this kind is introduced by a code change and is invisible on screen, so a test is the dependable way to catch its return.

After deployment, request reindexing for a few affected URLs and re-inspect them once recrawled.

Remember what a correct tag does and does not settle. A consistent canonical tells Google one preference clearly. Google may still select another URL if other signals point elsewhere. Working through that broader case is the subject of the guide to what to investigate when Google chooses a different canonical from the one declared.

The rule: one canonical value, decided in one place, identical before and after scripts run.

Link trail

Reader comments on this answer

Add your comment to this answer

Your email address will not be published. Required fields are marked *