Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEO: add hreflangs #12259

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/components/PageMetadata.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { join } from "path"

import Head from "next/head"
import { useRouter } from "next/router"
import { useTranslation } from "next-i18next"

import { DEFAULT_LOCALE, SITE_URL } from "@/lib/constants"
import { filterRealLocales } from "@/lib/utils/translations"
import { getFullUrl } from "@/lib/utils/url"

import { SITE_URL } from "@/lib/constants"

type NameMeta = {
name: string
Expand Down Expand Up @@ -33,9 +34,11 @@ const PageMetadata = ({
canonicalUrl,
author,
}: PageMetadataProps) => {
const { locale, asPath } = useRouter()
const { locale, locales: rawLocales, asPath } = useRouter()
const { t } = useTranslation()

const locales = filterRealLocales(rawLocales)

const desc = description || t("site-description")
const siteTitle = t("site-title")
const fullTitle = `${title} | ${siteTitle}`
Expand All @@ -45,7 +48,7 @@ const PageMetadata = ({
const slug = path.split("/")

// Set canonical URL w/ language path to avoid duplicate content
const url = new URL(join(locale!, path), SITE_URL).href
const url = getFullUrl(locale, path)
const canonical = canonicalUrl || url

/* Set fallback ogImage based on path */
Expand Down Expand Up @@ -96,6 +99,14 @@ const PageMetadata = ({
/>
))}
<link rel="canonical" key={canonical} href={canonical} />
{locales.map((loc) => (
<link
key={loc}
rel="alternate"
hrefLang={loc}
href={getFullUrl(loc, path)}
/>
))}
</Head>
)
}
Expand Down
14 changes: 11 additions & 3 deletions src/lib/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import path from "path"
import { join } from "path"

import { DISCORD_PATH, MAIN_CONTENT_ID } from "@/lib/constants"
import {
DEFAULT_LOCALE,
DISCORD_PATH,
MAIN_CONTENT_ID,
SITE_URL,
} from "@/lib/constants"

export const isDiscordInvite = (href: string): boolean =>
href.includes(DISCORD_PATH) && !href.includes("http")
Expand Down Expand Up @@ -31,7 +36,10 @@ export const isHrefActive = (

export const isHash = (href: string): boolean => href.startsWith("#")

export const getFullUrl = (locale: string | undefined, path: string) =>
addSlashes(new URL(join(locale || DEFAULT_LOCALE, path), SITE_URL).href)

export const addSlashes = (href: string): string => {
if (isExternal(href)) return href
return path.join("/", href, "/")
return join("/", href, "/")
}
Loading