Skip to content

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

Open
@TomPeirs

Description

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

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

  • Public source code: Unfortunately not possible
  • Public site URL: https://docs.boc-group.com/adonis/en/
  • Docusaurus version used: beta-14
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): latest
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): Windows 10

Reproducible demo

Thank you

Self-service

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

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