Skip to content

JAVASCRIPT SEO

Canonical tag changes after hydration: diagnosis and fixes

Your server declares one canonical URL. Your JavaScript declares another. Which signal does Google receive?

1. Problem / Symptoms

Server HTML declares one URL while the rendered DOM declares another or contains two canonical elements. The conflict may appear only after client-side navigation. Search Console may report a different Google-selected canonical; that is a separate observation, not proof that hydration caused the selection.

Record the requested URL, final URL, declared canonical and Google-selected canonical. Compare a direct page load with navigation to the same route from inside the application.

2. Quick Answer

Use one canonical rule that stays consistent between server HTML and the rendered page. Remove React effects or apps that overwrite the server’s declared URL. In Next.js App Router, define metadata in a Server Component and inspect the actual output. Retest direct loads and client-side navigation. A canonical is a preference signal; validating it does not guarantee that Google will select it or index the page.

3. Root Causes

Investigate competing head managers, an effect based on window.location, an inherited staging domain, or incorrect parameter and language rules. These are hypotheses; identify the responsible file before changing anything.

Next.js metadata streaming is a separate consideration. Check the installed version and behaviour for the tested user-agent. A canonical missing from the first response chunk does not establish a hydration change. Inspect the full response and final DOM.

4. Reproduce the Issue

Download the reproducible examples. Extract the archive and run node serve.mjs, then use the local commands below. Requires Node.js 20.9 or later. Next.js and Liquid instructions are included.

The local canonical fixture deliberately changes a canonical with JavaScript. It is not an incident observed on this website. Its initial HTML contains:

<link rel="canonical" href="https://example.test/services/">

Compare the result before and after scripts run. Execute this read-only expression in the browser console:

[...document.querySelectorAll('link[rel~="canonical"]')].map(el => ({
  raw: el.getAttribute('href'),
  resolved: el.href,
  inHead: document.head.contains(el)
}));

In a React application, the following effect illustrates code that introduces the conflict and should be removed:

// Anti-pattern: do not keep this effect in the fixed application.
useEffect(() => {
  document.querySelector('link[rel="canonical"]')
    ?.setAttribute('href', 'https://example.test/wrong/');
}, []);

5. Diagnostic Workflow

  1. Save the complete GET response, headers and redirect chain. Check for HTTP Link headers that also declare a canonical.
  2. Count canonical elements and inspect their location. Compare the raw attribute with the resolved absolute URL.
  3. Repeat direct loads, internal navigation and language switching. Include dynamic routes and parameterised URLs.
  4. Search the source for canonical, alternates, Helmet and effects that modify the head. Assign metadata to a single owner.
  5. In URL Inspection, compare the declaration and Google-selected canonical in indexed data. The live test helps inspect the current page; it does not establish a new canonical selection in the index.

6. Fix and Validation

This Next.js App Router example uses static metadata and metadataBase. Do not export metadata from a use client component. For dynamic routes, derive the URL from route data and editorial rules rather than interface state.

// app/layout.tsx — Next.js App Router
import type { Metadata } from 'next';
export const metadata: Metadata = {
  metadataBase: new URL('https://example.test'),
};
export default function Layout({ children }: { children: React.ReactNode }) {
  return <html lang="en"><body>{children}</body></html>;
}

// app/services/page.tsx — Server Component
import type { Metadata } from 'next';
export const metadata: Metadata = {
  alternates: { canonical: '/services/' },
};
export default function Services() { return <h1>Services</h1>; }

Give each translation its own canonical and express the language relationship through hreflang. Align sitemaps, redirects and internal links with the preferred URLs.

Accept the change when the intended route is declared once, remains stable after navigation and returns the expected page. The Next.js example is checked with a local build and HTTP response; this does not certify every Next.js version or configuration.

7. Common Mistakes

  1. Pointing every route’s canonical at the homepage.
  2. Automatically stripping every parameter, including one that identifies a distinct page.
  3. Pointing an English canonical at Romanian instead of defining a translation relationship.
  4. Adding another canonical to “correct” the first one.
  5. Treating noindex or robots.txt as automatic substitutes for canonicalisation.

8. Official References & Related Guides

Google’s documentation explains canonicalisation signals; Next.js documents its API and metadata streaming. The React example illustrates the conflict mechanism. No Search Console data or client outcomes are claimed.

Continue your investigation