0
0
NextJSframework~8 mins

Canonical URLs in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Canonical URLs
MEDIUM IMPACT
Canonical URLs affect page load speed by preventing duplicate content indexing and reducing unnecessary server requests for SEO, indirectly improving user experience.
Setting canonical URLs to avoid duplicate content in Next.js pages
NextJS
import Head from 'next/head';

export default function Page() {
  const canonicalUrl = 'https://example.com/page';
  return (
    <>
      <Head>
        <link rel="canonical" href={canonicalUrl} />
      </Head>
      <main>Page content</main>
    </>
  );
}
Hardcoding or statically generating canonical URLs avoids runtime calculation and hydration issues.
📈 Performance GainNon-blocking rendering; canonical URL present on first paint; prevents CLS.
Setting canonical URLs to avoid duplicate content in Next.js pages
NextJS
import Head from 'next/head';

export default function Page() {
  return (
    <>
      <Head>
        <link rel="canonical" href={window.location.href} />
      </Head>
      <main>Page content</main>
    </>
  );
}
Using window.location.href in server-side rendering causes hydration mismatch and delays canonical URL rendering.
📉 Performance CostBlocks rendering until hydration; may cause CLS due to late canonical link injection.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Dynamic canonical URL using window.location.hrefInjects link tag after hydrationTriggers 1 reflow due to late insertionMinimal paint cost but causes CLS[X] Bad
Static canonical URL in Head componentLink tag present in initial HTMLNo reflows triggeredNo extra paint cost[OK] Good
Rendering Pipeline
Canonical URLs are added in the HTML head during server-side rendering or static generation, so the browser reads them early without extra layout or paint work.
HTML Parsing
Style Calculation
⚠️ BottleneckNone significant; improper dynamic canonical URLs can cause hydration delays.
Core Web Vital Affected
LCP
Canonical URLs affect page load speed by preventing duplicate content indexing and reducing unnecessary server requests for SEO, indirectly improving user experience.
Optimization Tips
1Always generate canonical URLs during server-side rendering or static generation.
2Avoid using client-side JavaScript like window.location.href to set canonical URLs.
3Ensure canonical links are present in the initial HTML to prevent layout shifts.
Performance Quiz - 3 Questions
Test your performance knowledge
Why should canonical URLs be generated at build time or server-side in Next.js?
ATo delay rendering until client-side JavaScript runs
BTo increase bundle size for better SEO
CTo avoid hydration mismatches and improve initial page load
DTo add more DOM nodes dynamically after load
DevTools: Elements
How to check: Open DevTools, go to Elements panel, inspect the <head> section, and verify the <link rel="canonical"> tag is present on initial load.
What to look for: Presence of canonical link in initial HTML without DOM mutations after page load indicates good performance.