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
5
+ import { filterRealLocales } from "@/lib/utils/translations"
6
+ import { getFullUrl } from "@/lib/utils/url"
7
7
import { getOgImage } from "@/lib/utils/metadata"
8
-
9
- import { DEFAULT_LOCALE , SITE_URL } from "@/lib/constants"
8
+ import { SITE_URL } from "@/lib/constants"
10
9
11
10
type NameMeta = {
12
11
name : string
@@ -35,9 +34,11 @@ const PageMetadata = ({
35
34
canonicalUrl,
36
35
author,
37
36
} : PageMetadataProps ) => {
38
- const { locale, asPath } = useRouter ( )
37
+ const { locale, locales : rawLocales , asPath } = useRouter ( )
39
38
const { t } = useTranslation ( )
40
39
40
+ const locales = filterRealLocales ( rawLocales )
41
+
41
42
const desc = description || t ( "site-description" )
42
43
const siteTitle = t ( "site-title" )
43
44
const fullTitle = `${ title } | ${ siteTitle } `
@@ -47,7 +48,7 @@ const PageMetadata = ({
47
48
const slug = path . split ( "/" )
48
49
49
50
// Set canonical URL w/ language path to avoid duplicate content
50
- const url = new URL ( join ( locale ! , path ) , SITE_URL ) . href
51
+ const url = getFullUrl ( locale , path )
51
52
const canonical = canonicalUrl || url
52
53
53
54
/* Set fallback ogImage based on path */
@@ -83,6 +84,14 @@ const PageMetadata = ({
83
84
/>
84
85
) ) }
85
86
< link rel = "canonical" key = { canonical } href = { canonical } />
87
+ { locales . map ( ( loc ) => (
88
+ < link
89
+ key = { loc }
90
+ rel = "alternate"
91
+ hrefLang = { loc }
92
+ href = { getFullUrl ( loc , path ) }
93
+ />
94
+ ) ) }
86
95
</ Head >
87
96
)
88
97
}
0 commit comments