Skip to main content
When using query strings to manage client-side state, it’s important to consider the SEO implications. Search engines may index multiple variations of the same page with different query parameters, leading to duplicate content issues.

Canonical URLs for local state

If your page uses query strings for local-only state (like UI preferences, filters, or pagination), you should add a canonical URL to your page. This tells SEO crawlers to ignore the query string and index the page without it.

Next.js App Router

In the Next.js app router, this is done via the metadata object:
app/products/page.tsx

Next.js Pages Router

For the pages router, use the Head component:
pages/products.tsx

Canonical URLs with meaningful query strings

If however the query string is defining what content the page is displaying (like YouTube’s watch URLs: https://www.youtube.com/watch?v=dQw4w9WgXcQ), your canonical URL should contain the relevant query strings. You can use your parsers to read the search params and serialize the canonical URL:
app/watch/page.tsx

When to use canonical URLs

Use canonical URLs when:
  • UI state: Filters, sorting, view modes, expanded sections
  • Pagination: Page numbers that don’t change core content
  • Client preferences: Theme, language (if not affecting content), layout options
  • Temporary state: Modal open/close, selected tab
Don’t use canonical URLs (or include query params in canonical) when:
  • Content identification: Unique resource IDs (like video IDs, article slugs)
  • Search queries: User search terms that change results
  • Deep linking: State that defines what the user should see
  • Shareability: URLs meant to be bookmarked or shared

Multiple query parameters

When you have multiple query parameters, decide which ones are meaningful for SEO:
app/products/page.tsx

robots.txt considerations

For query parameters that are purely for UI state, you can also use robots.txt to tell search engines to ignore specific parameters:
public/robots.txt
Be careful with robots.txt rules. They affect all pages on your site and can accidentally block important content if not configured correctly.

Open Graph and Twitter Cards

When sharing links on social media, you may want to remove query parameters from the Open Graph URL:
app/products/page.tsx
Or include them if they’re meaningful:

Google Search Console

Monitor how Google indexes your pages with different query parameters:
  1. Check the URL Parameters section in Google Search Console
  2. Configure how Googlebot should treat specific parameters
  3. Use the URL Inspection tool to verify canonical tags are working

Best practices summary

  1. Always set a canonical URL when using query strings for UI state
  2. Include meaningful query params in canonical URLs for content identification
  3. Use loaders and serializers to keep canonical URL logic DRY
  4. Test your canonical tags using browser DevTools or SEO analysis tools
  5. Monitor in Search Console to ensure proper indexing
  6. Be consistent - use the same canonical URL strategy across your site