Skip to content

SEO: hreflang alternate link wrongful destination if baseURL contains defaultlocale. #6462

@TomPeirs

Description

@TomPeirs

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

The hreflang attribute in the head of a document is pointing to the wrong i18n paths.
https://docusaurus.io/docs/seo#global-metadata

Take the following URL:
TestURL: https://docs.boc-group.com/adoit/en/docs/14.0/user_manual/

If you inspect the DOM you will find the following:
<link data-react-helmet="true" rel="alternate" href="https://docs.boc-group.com/adoit/en/de/docs/14.0/user_manual/" hreflang="de">
AND
<link data-react-helmet="true" rel="alternate" href="https://docs.boc-group.com/adoit/docs/14.0/user_manual/" hreflang="de">
In the first alternate URL the i18n locale is added behind the baseURL.
In the second alternate URL the default locale is removed from the baseURL
Meaning that both links will not work

The alternate URL is pointing to /i18n-veriable/path.. and therefore this URL points to /en/de/ instead of /de/ because my baseurl already has the defaultlocale /en/ part of the baseurl. (I have 2 deployments, one for each locale.)
-> This leads to algolia trying to crawl a ton of wrongfull records.

Steps to reproduce

  1. Configure Base URL to include locale.
  2. In package.json build the application in a non traditional way by building to a dedicated output.
    Meaning that in the output directory on the root level there is no output, only in subfolder /en and /de (for each locale):
    "build_i18n": "cross-env LOCALE='en' BASE_URL='/adoit/en/' docusaurus build --locale en --out-dir build/en && cross-env LOCALE='de' BASE_URL='/adoit/de/' docusaurus build --locale de --out-dir build/de",
  3. Example docusaurus.config.js: baseUrl: process.env.BASE_URL,

With this setup you basically build your project for each language to a dedicated output, and via a deployment instance of your choice you can deploy to your liking.

However , with the traditional i18n config like below docusaurus will automtically add the hreflang attribute by appending the i18n variable to the baseURL. But docusaurus does not consider your deployment.

    i18n: {
        defaultLocale: 'en',
        locales: ['en', 'de'],
    },

** !! Docusaurus makes the assumption by default that your default locale is NOT part of your URL.**

A very similar issue arrises with the LocaleDropdownNavbarItem, if you have a setup as described above, then docusaurus by default makes the assumption that your defaultlocale is not part of the URL, and hence the logic is there if you switch the locale via the dropdown then they just add + on switching.
(Here I overcame this by swizzling the component and adding my own checks.)

Expected behavior

I would expect that docusaurus gives the configuration possibility for deploying with defaultlocale as part of the URL or not.

This issue relates to: #4723
(@slorber)
Is this related to #6075 as well?

Actual behavior

wrote it as part of description sorry.

Your environment

Reproducible demo

Thank you

Self-service

  • I'd be willing to fix this bug myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn error in the Docusaurus core causing instability or issues with its executiondomain: i18nRelated to the i18n system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions