Discover how a simple link can boost your website's search ranking and avoid SEO headaches!
Why Canonical URLs in NextJS? - Purpose & Use Cases
Imagine you have a website where the same page can be reached by multiple web addresses, like example.com/page and example.com/page?ref=123. You try to tell search engines which one is the main page by adding notes manually in every HTML file.
Manually adding these notes is slow and easy to forget. Search engines might get confused and rank the wrong page, hurting your site's visibility. It's like telling different friends different addresses for the same party, causing confusion.
Canonical URLs let you mark the main version of a page clearly in your code. Next.js helps you add these links automatically or easily, so search engines always know which page to show in search results.
<link rel="canonical" href="https://example.com/page"> <!-- added manually in every page -->
import Head from 'next/head'; export default function Page() { return ( <> <Head> <link rel="canonical" href="https://example.com/page" /> </Head> {/* Page content here */} </> ); }
This makes your website SEO-friendly by preventing duplicate content issues and improving search engine rankings effortlessly.
A blog with many tags and filters can have many URLs for the same article. Using canonical URLs tells Google which article URL is the main one, so readers find the right page easily.
Manually managing page versions is confusing and error-prone.
Canonical URLs clearly mark the main page for search engines.
Next.js makes adding canonical URLs simple and reliable.
Practice
<Head> component?Solution
Step 1: Understand the role of canonical URLs
Canonical URLs help search engines know the main page to avoid duplicate content issues.Step 2: Recognize Next.js usage
Using<Head>in Next.js allows adding a<link rel="canonical">tag for SEO purposes.Final Answer:
To tell search engines which URL is the main version of a page -> Option BQuick Check:
Canonical URL = main page URL [OK]
- Thinking canonical URLs are for user navigation
- Confusing canonical URLs with CSS or scripts
- Using canonical URLs to load external files
Solution
Step 1: Identify correct Next.js Head usage
The<Head>component is used to add tags inside the HTML head.Step 2: Check canonical link tag syntax
The canonical URL uses<link rel="canonical" href="URL" />syntax.Final Answer:
<Head><link rel="canonical" href="https://example.com/page" /></Head> -> Option AQuick Check:
Canonical link tag = rel="canonical" [OK]
- Using lowercase <head> instead of Next.js <Head>
- Using <meta> instead of <link> for canonical
- Confusing stylesheet link with canonical link
<head> tag?
import Head from 'next/head';
export default function Page() {
return (
<>
My Page
Content here
</>
);
}Solution
Step 1: Review the Head component content
The component adds a<title>and a<link rel="canonical">with full URL.Step 2: Understand rendered HTML in head
Next.js renders exactly the tags inside<Head>as HTML head children.Final Answer:
<title>My Page</title><link rel="canonical" href="https://example.com/mypage" /> -> Option AQuick Check:
Head content = title + canonical link [OK]
- Confusing canonical link with stylesheet link
- Using meta tag instead of link for canonical
- Using relative URL instead of full URL in canonical
import Head from 'next/head';
export default function Page() {
return (
);
}Solution
Step 1: Check the href attribute value
The href is "example.com/page" without "https://", which is invalid for canonical URLs.Step 2: Verify Next.js Head usage
The<Head>can contain<link>tags, and rel="canonical" is correct.Final Answer:
The href URL is missing the protocol (https://) -> Option DQuick Check:
Canonical href needs full URL [OK]
- Omitting protocol in href URL
- Thinking <Head> can't have <link> tags
- Using wrong rel attribute value
/blog/post1 and /blog/post1?ref=twitter. To avoid duplicate content issues, how should you add canonical URLs in both pages?Solution
Step 1: Understand duplicate content from query parameters
Both URLs show the same content but differ by query string, which can cause SEO duplication.Step 2: Use canonical URL to point to main page
Both pages should have the canonical URL set to the main clean URL without query parameters.Final Answer:
Add <link rel="canonical" href="https://example.com/blog/post1" /> in both pages' <Head> -> Option CQuick Check:
Canonical URL = main page URL without query [OK]
- Using full URL with query as canonical
- Skipping canonical tags on query pages
- Setting different canonicals for same content
Ready to prove it?
Pass the test and this pattern moves unknown
