Skip to content

Version switcher fails to stay on the same page when canonical_version is set in mike Β #7226

@kevinh-hcl

Description

@kevinh-hcl

Context

We are using mkdocs-material together with mike for our documentation.

To improve SEO we set the canonical_version in mike. This sets the rel="canonical" link on every page and also leads to the sitemap.xml in each version using the canonical links (with latest). This seems to be the expected way of handling canonical URLs according to Google Search Central and others.

Bug description

When switching from a sub page of any version to a version that was built with the canonical_version you get redirected to the homepage of the version. The expected behaviour would be to stay on the same sub page, but in a different version.

From what I can see this is probably due to mkdocs-material using the sitemap of the version to determine if a sub page is available in that version. Since the sitemap always uses the canonical_version instead of the actual version, the page is not found and it falls back to the homepage.

Related links

Related documentation

Pointer from a previous discussion

Related mike documentation and code

Reproduction in a fork from mkdocs-material-example-versioning

Reproduction

9.5.25-version-switcher-canonical.zip

Steps to reproduce

  1. unzip 9.5.25-version-switcher-canonical.zip and navigate to the extracted folder
  2. (Optional: I had to remove the venv due to the size limitation in the .zip upload. Please follow https://squidfunk.github.io/mkdocs-material/guides/creating-a-reproduction/#environment to create a venv and activate it)
  3. Install the dependencies using pip install --upgrade --force-reinstall mike==2.1.1 mkdocs-material==9.5.25 mkdocs==1.6.0
  4. In mkdocs.yml remove the - info from the plugins section and save the changes
  5. Deploy a first version using mike deploy canonical
  6. In mkdocs.yml remove the canonical_version: latest from mike in the plugins section and save the changes
  7. Deploy a second version using mike deploy no-canonical latest
  8. Serve the documentation locally using mike serve

Working as expected:

  1. Open http://localhost:8000/canonical/subpage/
  2. Switch to the no-canonical version
  3. The version shows the correct page
  4. (Optional: check http://localhost:8000/no-canonical/sitemap.xml of the target version)

Not working as expected:

  1. Open http://localhost:8000/no-canonical/subpage/
  2. Switch to the canonical version
  3. Redirected to the homepage
  4. (Optional: check http://localhost:8000/canonical/sitemap.xml of the target version)

Browser

Chrome

Before submitting

Metadata

Metadata

Assignees

No one assigned

    Labels

    change requestIssue requests a new feature or improvementresolvedIssue is resolved, yet unreleased if open

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions