Version switcher fails to stay on the same page when `canonical_version` is set in mike
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
-
Setting
canonical_version -
Github Pages Version
0.5(withcanonical_version). You can switch FROM this versions to others just fine -
Github Pages Version
0.4. Switch TO0.5and get redirected to the homepage -
0.5sitemap -
0.4sitemap
Reproduction
9.5.25-version-switcher-canonical.zip
Steps to reproduce
- unzip
9.5.25-version-switcher-canonical.zipand navigate to the extracted folder - (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)
- Install the dependencies using
pip install --upgrade --force-reinstall mike==2.1.1 mkdocs-material==9.5.25 mkdocs==1.6.0 - In
mkdocs.ymlremove the- infofrom thepluginssection and save the changes - Deploy a first version using
mike deploy canonical - In
mkdocs.ymlremove thecanonical_version: latestfrommikein thepluginssection and save the changes - Deploy a second version using
mike deploy no-canonical latest - Serve the documentation locally using
mike serve
Working as expected:
- Open http://localhost:8000/canonical/subpage/
- Switch to the no-canonical version
- The version shows the correct page
- (Optional: check http://localhost:8000/no-canonical/sitemap.xml of the target version)
Not working as expected:
- Open http://localhost:8000/no-canonical/subpage/
- Switch to the canonical version
- Redirected to the homepage
- (Optional: check http://localhost:8000/canonical/sitemap.xml of the target version)
Browser
Chrome
Before submitting
- [X] I have read and followed the bug reporting guidelines.
- [X] I have attached links to the documentation, and possibly related issues and discussions.
- [X] I assure that I have removed all customizations before submitting this bug report.
- [X] I have attached a .zip file with a minimal reproduction using the built-in info plugin.
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 ☺️
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
Thanks for the PR! Please give me some time to check it out and give it a spin.
I'm trying an alternative approach to fixing this in #7350. That approach should also make the version switcher work correctly with mike serve or if the website is published somewhere other than at site_url again.
I also set up some unit tests to stay sane while changing this behavior, hence the 6 commits.
Reopening until released.
Released as part of 9.5.39.