squidfunk / mkdocs-material

Documentation that simply works
https://squidfunk.github.io/mkdocs-material/
MIT License
19.09k stars 3.39k forks source link

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

Open kevinh-hcl opened 1 month ago

kevinh-hcl commented 1 month ago

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

squidfunk commented 1 month ago

Thanks for reporting and the very detailed explanations. Yes, it sounds like a currently unhandled combination of configuration settings when using mike. I'm not sure it's a bug, since we don't advertise the canonical_version setting anywhere in our documentation, so I'll tag this as an chance request. Since you have already invested some time into tracking down the cause, would you like to work on this in a PR? Happy to collaborate. Shouldn't be too hard to support ☺️

kevinh-hcl commented 1 month ago

Thank you. I drafted a first idea of how this could be handled in https://github.com/squidfunk/mkdocs-material/pull/7227. Any feedback is appreciated

squidfunk commented 1 month ago

Thanks for the PR! Please give me some time to check it out and give it a spin.