2

I'm building an e-commerce site with Next.js 14 that supports multiple regions and languages, and I'm struggling with canonical URL implementation. Here's my situation:

We have URLs like /en/us/products/123 and /en/eu/products/123 for different regions

Some products appear in multiple categories (/electronics/phone-123 vs /apple/phone-123)

We need to handle URL parameters properly (color=red should be in canonical, utm_source should not)

Should I create a centralized canonical service or handle it per-page?

We're using both SSG and SSR across different pages

What's the best way to filter URL parameters for canonical tags?

How to handle hreflang properly for language + region combinations (en-US, en-GB )?

1 Answer 1

2

You can solve this by creating one single source of truth for your
canonical url logic. In a multi region and multi language Next project,
you want a rule based system rather than many manual solutions in each
page.

The idea is simple:

  1. Decide what the primary location of a product or page is

  2. Remove all tracking params from the final canonical url

  3. Map every region or language to the primary location for that product

The most stable way is to create a tiny helper function that runs both
on server and on client. This function receives the full url, strips
query params that you do not want in SEO, and then rebuilds the correct
canonical url based on your region and language config.

Example of the logic in short form:

export function makeCanonicalUrl(rawUrl, region, language) {
  const noQuery = new URL(rawUrl).origin +
    new URL(rawUrl).pathname
  const base = "https://yourdomain.com"
  const part = "/" + region + "/" + language
  return base + part + new URL(rawUrl).pathname
}

All lines are under the limit you want and written in the format you
prefer.

For products that appear in more than one category, pick one of the
categories as the main one and keep that as the canonical version. The
other versions can use rel alternate to point to the correct variations
for region and language.

For hreflang you should output a list of all region and language pairs
that match the product. Next has support for this inside metadata or
your own layout. You can render a full set of alternate links with
region and language codes that match each url.

Answer to your questions in short form:

  • Do not build a central service in another place. Keep the logic in a
    small helper that you call inside metadata of each page.

  • Filter query params inside that helper. Use only the clean path.

  • For hreflang just output all region and language pairs.

  • SSG and SSR do not change the solution because both can call the same
    helper logic.

This gives you predictable and stable canonical output across all
regions and languages.

(This is what I think, I don't guarantee that this is the solution)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.