Adding Canonical URLs in Next.js
📖 Scenario: You are building a blog website using Next.js. To help search engines understand the main URL for each blog post and avoid duplicate content issues, you want to add canonical URLs to your pages.
🎯 Goal: Create a Next.js page component that includes a canonical URL link tag in the HTML head section. This canonical URL should point to the main URL of the current page.
📋 What You'll Learn
Create a Next.js page component named
BlogPost.Use the
Head component from next/head to add elements to the HTML head.Create a variable
canonicalUrl that holds the full canonical URL string.Add a
<link> tag with rel="canonical" and href set to canonicalUrl inside <Head>.Render a simple heading with the blog post title.
💡 Why This Matters
🌍 Real World
Adding canonical URLs is important for SEO to avoid duplicate content penalties and to tell search engines which URL is the main one for a page.
💼 Career
Web developers often need to optimize websites for search engines by adding canonical URLs and meta tags to improve site ranking and user experience.
Progress0 / 4 steps