From 31c02b1064e08648923be541f245473e1b065a2d Mon Sep 17 00:00:00 2001 From: Ori Shalom Date: Fri, 7 Jul 2023 18:01:53 +0300 Subject: [PATCH 1/6] fix canonical url --- .../docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index 9a30c775fff2..f3aaf7eb4bc2 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -58,10 +58,10 @@ function AlternateLangHeaders(): JSX.Element { // Default canonical url inferred from current page location pathname function useDefaultCanonicalUrl() { const { - siteConfig: {url: siteUrl}, + siteConfig: {url: siteUrl, trailingSlash = false}, } = useDocusaurusContext(); const {pathname} = useLocation(); - return siteUrl + useBaseUrl(pathname); + return siteUrl + useBaseUrl(pathname).replace(/\/+$/, '') + (trailingSlash ? '/' : ''); } // TODO move to SiteMetadataDefaults or theme-common ? From c8e6d64cd41abc1657863c258404cb7a62266b82 Mon Sep 17 00:00:00 2001 From: Ori Shalom Date: Fri, 7 Jul 2023 21:43:11 +0000 Subject: [PATCH 2/6] run yarn format --- .../src/theme/SiteMetadata/index.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index f3aaf7eb4bc2..2d4f13667d1a 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -61,7 +61,11 @@ function useDefaultCanonicalUrl() { siteConfig: {url: siteUrl, trailingSlash = false}, } = useDocusaurusContext(); const {pathname} = useLocation(); - return siteUrl + useBaseUrl(pathname).replace(/\/+$/, '') + (trailingSlash ? '/' : ''); + return ( + siteUrl + + useBaseUrl(pathname).replace(/\/+$/, '') + + (trailingSlash ? '/' : '') + ); } // TODO move to SiteMetadataDefaults or theme-common ? From d0857870ec12840b377ceeeb1fd9fa118ecdc821 Mon Sep 17 00:00:00 2001 From: Ori Shalom Date: Mon, 17 Jul 2023 13:38:23 +0300 Subject: [PATCH 3/6] Update packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx Co-authored-by: Joshua Chen --- .../docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index 2d4f13667d1a..46c78fd4743a 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -63,8 +63,7 @@ function useDefaultCanonicalUrl() { const {pathname} = useLocation(); return ( siteUrl + - useBaseUrl(pathname).replace(/\/+$/, '') + - (trailingSlash ? '/' : '') + useBaseUrl(pathname).replace(/\/+$/, trailingSlash ? '/' : '') ); } From 29f68c2b371b59d298a543087fdc56606ac96e07 Mon Sep 17 00:00:00 2001 From: Ori Shalom Date: Mon, 17 Jul 2023 21:49:04 +0000 Subject: [PATCH 4/6] run yarn format --- .../docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index 46c78fd4743a..052c0f40f45a 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -62,8 +62,7 @@ function useDefaultCanonicalUrl() { } = useDocusaurusContext(); const {pathname} = useLocation(); return ( - siteUrl + - useBaseUrl(pathname).replace(/\/+$/, trailingSlash ? '/' : '') + siteUrl + useBaseUrl(pathname).replace(/\/+$/, trailingSlash ? '/' : '') ); } From 786d843dc8b6d20888431576cbe131d09000f740 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 21 Jul 2023 16:16:31 +0200 Subject: [PATCH 5/6] use existing applyTrailingSlash function --- .../src/theme/SiteMetadata/index.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index 052c0f40f45a..a4a3dd975eac 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -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 ? @@ -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 ? From 6474b3ad8af6a43f7580c93355c30d445ea45d47 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 21 Jul 2023 16:37:59 +0200 Subject: [PATCH 6/6] applyTrailingSlash to hreflang as well + add comment to useLocation metadata problem --- .../src/theme/SiteMetadata/index.tsx | 6 +++--- .../src/utils/useAlternatePageUtils.ts | 13 +++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index a4a3dd975eac..4a275e819c17 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -62,15 +62,15 @@ function useDefaultCanonicalUrl() { 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 + // TODO using useLocation().pathname is not a super idea + // See https://github.com/facebook/docusaurus/issues/9170 const {pathname} = useLocation(); const canonicalPathname = applyTrailingSlash(useBaseUrl(pathname), { trailingSlash, baseUrl, }); + return siteUrl + canonicalPathname; } diff --git a/packages/docusaurus-theme-common/src/utils/useAlternatePageUtils.ts b/packages/docusaurus-theme-common/src/utils/useAlternatePageUtils.ts index ae0e55dcddff..073aa8956f3b 100644 --- a/packages/docusaurus-theme-common/src/utils/useAlternatePageUtils.ts +++ b/packages/docusaurus-theme-common/src/utils/useAlternatePageUtils.ts @@ -7,6 +7,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import {useLocation} from '@docusaurus/router'; +import {applyTrailingSlash} from '@docusaurus/utils-common'; /** * Permits to obtain the url of the current page in another locale, useful to @@ -35,17 +36,25 @@ export function useAlternatePageUtils(): { }) => string; } { const { - siteConfig: {baseUrl, url}, + siteConfig: {baseUrl, url, trailingSlash}, i18n: {defaultLocale, currentLocale}, } = useDocusaurusContext(); + + // TODO using useLocation().pathname is not a super idea + // See https://github.com/facebook/docusaurus/issues/9170 const {pathname} = useLocation(); + const canonicalPathname = applyTrailingSlash(pathname, { + trailingSlash, + baseUrl, + }); + const baseUrlUnlocalized = currentLocale === defaultLocale ? baseUrl : baseUrl.replace(`/${currentLocale}/`, '/'); - const pathnameSuffix = pathname.replace(baseUrl, ''); + const pathnameSuffix = canonicalPathname.replace(baseUrl, ''); function getLocalizedBaseUrl(locale: string) { return locale === defaultLocale