Skip to content

Commit 7bc4df9

Browse files
authored
Merge pull request #214 from ethereum/pagemetadata-images
Fix PageMetadata/OG images
2 parents 4fe387f + 855a667 commit 7bc4df9

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/components/PageMetadata.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import Head from "next/head"
44
import { useRouter } from "next/router"
55
import { useTranslation } from "next-i18next"
66

7-
import { DEFAULT_LOCALE, SITE_URL } from "@/lib/constants"
8-
9-
import ogImageDapps from "@/public/doge-computer.png"
10-
import ogImageDevelopers from "@/public/enterprise-eth.png"
11-
import ogImageDefault from "@/public/home/hero.png"
12-
import ogImageUpgrades from "@/public/upgrades/upgrade_doge.png"
7+
import { SITE_URL } from "@/lib/constants"
138

149
type NameMeta = {
1510
name: string
@@ -44,33 +39,38 @@ const PageMetadata: React.FC<IProps> = ({
4439
const desc = description || t("site-description")
4540
const siteTitle = t("site-title")
4641
const fullTitle = `${title} | ${siteTitle}`
42+
const origin = process.env.NEXT_PUBLIC_SITE_URL || SITE_URL // TODO: Remove .env var usage after launch
43+
44+
// Remove any query params (?) or hash links (#)
45+
const path = asPath.replace(/[\?\#].*/, "")
46+
const slug = path.split("/")
4747

4848
/* Set canonical URL w/ language path to avoid duplicate content */
4949
/* e.g. set ethereum.org/about/ to ethereum.org/en/about/ */
50-
const canonical = canonicalUrl || join(SITE_URL, DEFAULT_LOCALE, asPath)
51-
const url = locale ? join(SITE_URL, locale, asPath) : canonical
50+
const url = new URL(join(locale!, path), origin).href
51+
const canonical = canonicalUrl || url
5252

5353
/* Set fallback ogImage based on path */
54-
let ogImage = ogImageDefault.src
54+
let ogImage = "/home/hero.png"
5555

56-
if (asPath.includes("/developers/")) {
57-
ogImage = ogImageDevelopers.src
56+
if (slug.includes("developers")) {
57+
ogImage = "/enterprise-eth.png"
5858
}
5959

60-
if (asPath.includes("/dapps/")) {
61-
ogImage = ogImageDapps.src
60+
if (slug.includes("dapps")) {
61+
ogImage = "/doge-computer.png"
6262
}
6363

64-
if (asPath.includes("/roadmap/")) {
65-
ogImage = ogImageUpgrades.src
64+
if (slug.includes("roadmap")) {
65+
ogImage = "/upgrades/upgrade_doge.png"
6666
}
6767

6868
if (image) {
6969
ogImage = image
7070
}
7171

72-
const ogImageUrl = join(SITE_URL, ogImage)
73-
const metadata: Array<Meta> = [
72+
const ogImageUrl = new URL(ogImage, origin).href
73+
const metadata: Meta[] = [
7474
{ name: `description`, content: desc },
7575
{ name: `image`, content: ogImageUrl },
7676
{ property: `og:title`, content: fullTitle },

0 commit comments

Comments
 (0)