Skip to content

Commit

Permalink
use existing applyTrailingSlash function
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jul 21, 2023
1 parent 29f68c2 commit 786d843
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
keyboardFocusedClassName,
} from '@docusaurus/theme-common/internal';
import {useLocation} from '@docusaurus/router';
import {applyTrailingSlash} from '@docusaurus/utils-common';
import SearchMetadata from '@theme/SearchMetadata';

// TODO move to SiteMetadataDefaults or theme-common ?
Expand Down Expand Up @@ -58,12 +59,19 @@ function AlternateLangHeaders(): JSX.Element {
// Default canonical url inferred from current page location pathname
function useDefaultCanonicalUrl() {
const {
siteConfig: {url: siteUrl, trailingSlash = false},
siteConfig: {url: siteUrl, baseUrl, trailingSlash},
} = useDocusaurusContext();

// TODO using useLocation().pathname is not reliable to create a canonical url
// Ex: use CDN features so that 2 paths serve the same html file (/a and /b)
// Then we would obtain 2 distinct canonical urls after React hydration: bad
const {pathname} = useLocation();
return (
siteUrl + useBaseUrl(pathname).replace(/\/+$/, trailingSlash ? '/' : '')
);

const canonicalPathname = applyTrailingSlash(useBaseUrl(pathname), {
trailingSlash,
baseUrl,
});
return siteUrl + canonicalPathname;
}

// TODO move to SiteMetadataDefaults or theme-common ?
Expand Down

0 comments on commit 786d843

Please sign in to comment.