duplicate canonical URLs for paths with and without trailing slashes #9128
Labels
bug
An error in the Docusaurus core causing instability or issues with its execution
status: needs triage
This issue has not been triaged by maintainers
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clear
oryarn clear
command.rm -rf node_modules yarn.lock package-lock.json
and re-installing packages.Description
When the same page can be accessed from URLs with and without a trailing slash crawlers can index the page twice without understanding this is the same page.
The way to improve SEO and mark these pages as a single page is using a
<link rel="canonical" .../ >
element in the header.Docusaurus is adding this element but the
href
value it receives is being set dynamically based on the current location pathname.This makes the canonical URL different between the pages that end with a trailing slash and those that don't.
I tried to see if the
trailingSlash
option indocusaurus.config.js
has any effect on the behavior but it doesn't seem to do anything with how the canonical link is generated.Looking at the code I can see the canonical URL is just using the location pathname as it is and calling
useBaseURL
.docusaurus/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx
Lines 59 to 65 in 26ae416
A small experiment of calling
useBaseURL
with 2 paths, one with a trailing slash and one without shows that it truly doesn't do anything with it to make it canonical:https://stackblitz.com/edit/github-gwvqkk?file=docs%2Findex.mdx,src%2Fcomponents%2FTestUseBaseURLBehavior%2Findex.tsx
Reproducible demo
https://stackblitz.com/edit/github-gwvqkk?file=docs%2Findex.mdx,src%2Fcomponents%2FTestUseBaseURLBehavior%2Findex.tsx
Steps to reproduce
<link rel="canonical" ...>
element and see it has the current URL without a trailing slash<link rel="canonical" ...>
again and see it now has a URL with a trailing slash.Expected behavior
The
<link rel="canonical" ...>
element should produce the same URL for the same page no matter if accessed with or without a trailing slash.Preferably this can also respect
trailingSlash
option fromdocusaurus.config.js
.Actual behavior
The
<link rel="canonical" ...>
element produces different URLs.Your environment
Self-service
The text was updated successfully, but these errors were encountered: