1
- import { join } from "path"
2
-
3
1
import Head from "next/head"
4
2
import { useRouter } from "next/router"
5
3
import { useTranslation } from "next-i18next"
6
4
7
- import { DEFAULT_LOCALE , SITE_URL } from "@/lib/constants"
5
+ import { getFullUrl } from "@/lib/utils/url"
6
+
7
+ import { SITE_URL } from "@/lib/constants"
8
8
9
9
type NameMeta = {
10
10
name : string
@@ -33,7 +33,7 @@ const PageMetadata = ({
33
33
canonicalUrl,
34
34
author,
35
35
} : PageMetadataProps ) => {
36
- const { locale, asPath } = useRouter ( )
36
+ const { locale, locales , asPath } = useRouter ( )
37
37
const { t } = useTranslation ( )
38
38
39
39
const desc = description || t ( "site-description" )
@@ -51,10 +51,7 @@ const PageMetadata = ({
51
51
* @example ethereum.org/about/ -> ethereum.org/about
52
52
* @example ethereum.org/pt-br/web3/ -> ethereum.org/pt-br/web3
53
53
*/
54
- const url = new URL (
55
- join ( locale === DEFAULT_LOCALE ? "" : locale ! , path ) ,
56
- SITE_URL
57
- ) . href . replace ( / \/ $ / , "" )
54
+ const url = getFullUrl ( locale , path )
58
55
const canonical = canonicalUrl || url
59
56
60
57
/* Set fallback ogImage based on path */
@@ -105,6 +102,16 @@ const PageMetadata = ({
105
102
/>
106
103
) ) }
107
104
< link rel = "canonical" key = { canonical } href = { canonical } />
105
+ { locales
106
+ ?. filter ( ( loc ) => loc !== locale )
107
+ . map ( ( loc ) => (
108
+ < link
109
+ key = { loc }
110
+ rel = "alternate"
111
+ hrefLang = { loc }
112
+ href = { getFullUrl ( loc , path ) }
113
+ />
114
+ ) ) }
108
115
</ Head >
109
116
)
110
117
}
0 commit comments