mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-09 14:22:27 +02:00
fix(theme): canonical url should be not change after hydration if url accessed with/without trailing slash (#9130)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com> Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
cd61c7bddc
commit
4ea0a70f93
2 changed files with 23 additions and 4 deletions
|
@ -16,6 +16,7 @@ import {
|
||||||
keyboardFocusedClassName,
|
keyboardFocusedClassName,
|
||||||
} from '@docusaurus/theme-common/internal';
|
} from '@docusaurus/theme-common/internal';
|
||||||
import {useLocation} from '@docusaurus/router';
|
import {useLocation} from '@docusaurus/router';
|
||||||
|
import {applyTrailingSlash} from '@docusaurus/utils-common';
|
||||||
import SearchMetadata from '@theme/SearchMetadata';
|
import SearchMetadata from '@theme/SearchMetadata';
|
||||||
|
|
||||||
// TODO move to SiteMetadataDefaults or theme-common ?
|
// TODO move to SiteMetadataDefaults or theme-common ?
|
||||||
|
@ -58,10 +59,19 @@ function AlternateLangHeaders(): JSX.Element {
|
||||||
// Default canonical url inferred from current page location pathname
|
// Default canonical url inferred from current page location pathname
|
||||||
function useDefaultCanonicalUrl() {
|
function useDefaultCanonicalUrl() {
|
||||||
const {
|
const {
|
||||||
siteConfig: {url: siteUrl},
|
siteConfig: {url: siteUrl, baseUrl, trailingSlash},
|
||||||
} = useDocusaurusContext();
|
} = useDocusaurusContext();
|
||||||
|
|
||||||
|
// TODO using useLocation().pathname is not a super idea
|
||||||
|
// See https://github.com/facebook/docusaurus/issues/9170
|
||||||
const {pathname} = useLocation();
|
const {pathname} = useLocation();
|
||||||
return siteUrl + useBaseUrl(pathname);
|
|
||||||
|
const canonicalPathname = applyTrailingSlash(useBaseUrl(pathname), {
|
||||||
|
trailingSlash,
|
||||||
|
baseUrl,
|
||||||
|
});
|
||||||
|
|
||||||
|
return siteUrl + canonicalPathname;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO move to SiteMetadataDefaults or theme-common ?
|
// TODO move to SiteMetadataDefaults or theme-common ?
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||||
import {useLocation} from '@docusaurus/router';
|
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
|
* Permits to obtain the url of the current page in another locale, useful to
|
||||||
|
@ -35,17 +36,25 @@ export function useAlternatePageUtils(): {
|
||||||
}) => string;
|
}) => string;
|
||||||
} {
|
} {
|
||||||
const {
|
const {
|
||||||
siteConfig: {baseUrl, url},
|
siteConfig: {baseUrl, url, trailingSlash},
|
||||||
i18n: {defaultLocale, currentLocale},
|
i18n: {defaultLocale, currentLocale},
|
||||||
} = useDocusaurusContext();
|
} = useDocusaurusContext();
|
||||||
|
|
||||||
|
// TODO using useLocation().pathname is not a super idea
|
||||||
|
// See https://github.com/facebook/docusaurus/issues/9170
|
||||||
const {pathname} = useLocation();
|
const {pathname} = useLocation();
|
||||||
|
|
||||||
|
const canonicalPathname = applyTrailingSlash(pathname, {
|
||||||
|
trailingSlash,
|
||||||
|
baseUrl,
|
||||||
|
});
|
||||||
|
|
||||||
const baseUrlUnlocalized =
|
const baseUrlUnlocalized =
|
||||||
currentLocale === defaultLocale
|
currentLocale === defaultLocale
|
||||||
? baseUrl
|
? baseUrl
|
||||||
: baseUrl.replace(`/${currentLocale}/`, '/');
|
: baseUrl.replace(`/${currentLocale}/`, '/');
|
||||||
|
|
||||||
const pathnameSuffix = pathname.replace(baseUrl, '');
|
const pathnameSuffix = canonicalPathname.replace(baseUrl, '');
|
||||||
|
|
||||||
function getLocalizedBaseUrl(locale: string) {
|
function getLocalizedBaseUrl(locale: string) {
|
||||||
return locale === defaultLocale
|
return locale === defaultLocale
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue