Skip to content

Commit b022ce6

Browse files
authored
Merge pull request #16050 from ethereum/remove-fixed-locale
No locale prefix for default locale
2 parents 4a26a35 + dfe2637 commit b022ce6

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

next-sitemap.config.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
const i18nConfig = require("./i18n.config.json")
22
const locales = i18nConfig.map(({ code }) => code)
33

4+
const defaultLocale = "en"
5+
46
/** @type {import('next-sitemap').IConfig} */
57
module.exports = {
68
siteUrl: process.env.SITE_URL || "https://ethereum.org",
79
generateRobotsTxt: true,
810
transform: async (_, path) => {
911
const rootPath = path.split("/")[1]
1012
if (path.endsWith("/404")) return null
11-
const isDefaultLocale = !locales.includes(rootPath) || rootPath === "en"
13+
const isDefaultLocale =
14+
!locales.includes(rootPath) || rootPath === defaultLocale
15+
16+
// Strip default-locale (en) prefix from paths; drop the `/en` root entry
17+
let loc = path
18+
if (rootPath === defaultLocale) {
19+
// Drop the `/en` root entry to avoid duplicating `/`
20+
if (path === `/${defaultLocale}` || path === `/${defaultLocale}/`)
21+
return null
22+
const defaultLocalePrefix = new RegExp(`^/${defaultLocale}(/|$)`)
23+
loc = path.replace(defaultLocalePrefix, "/")
24+
}
25+
1226
return {
13-
loc: path,
27+
loc,
1428
changefreq: isDefaultLocale ? "weekly" : "monthly",
1529
priority: isDefaultLocale ? 0.7 : 0.5,
1630
}

src/components/Matomo.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { usePathname } from "next/navigation"
55
import { init, push } from "@socialgouv/matomo-next"
66

77
import { IS_PREVIEW_DEPLOY } from "@/lib/utils/env"
8+
import { normalizePathForMatomo } from "@/lib/utils/matomo"
89

910
export default function Matomo() {
1011
const pathname = usePathname()
@@ -40,8 +41,11 @@ export default function Matomo() {
4041
return setPreviousPath(pathname)
4142
}
4243

43-
push(["setReferrerUrl", `${previousPath}`])
44-
push(["setCustomUrl", pathname])
44+
const normalizedPreviousPath = normalizePathForMatomo(previousPath)
45+
const normalizedPathname = normalizePathForMatomo(pathname)
46+
47+
push(["setReferrerUrl", normalizedPreviousPath])
48+
push(["setCustomUrl", normalizedPathname])
4549
push(["deleteCustomVariables", "page"])
4650
setPreviousPath(pathname)
4751
// In order to ensure that the page title had been updated,

src/i18n/routing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const routing = defineRouting({
77
locales: LOCALES_CODES,
88
defaultLocale: DEFAULT_LOCALE,
99
localeCookie: false,
10+
localePrefix: "as-needed",
1011
})
1112

1213
// Lightweight wrappers around Next.js' navigation APIs

src/lib/utils/matomo.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
import { push } from "@socialgouv/matomo-next"
22

3+
import { DEFAULT_LOCALE, LOCALES_CODES } from "@/lib/constants"
4+
35
import { IS_PROD } from "./env"
46

57
export const MATOMO_LS_KEY = "ethereum-org.matomo-opt-out"
68

9+
/**
10+
* Normalizes paths to ensure consistent Matomo tracking.
11+
* With localePrefix: "as-needed", English paths don't have /en prefix,
12+
* but we want to track them as /en paths for analytics consistency.
13+
*/
14+
export const normalizePathForMatomo = (pathname: string): string => {
15+
const hasLocalePrefix = LOCALES_CODES.some((locale) =>
16+
pathname.startsWith(`/${locale}/`)
17+
)
18+
19+
if (hasLocalePrefix) {
20+
return pathname
21+
}
22+
23+
// For paths without locale prefix (English content), add /en prefix
24+
return `/${DEFAULT_LOCALE}${pathname}`
25+
}
26+
727
export interface MatomoEventOptions {
828
eventCategory: string
929
eventAction: string
@@ -27,6 +47,10 @@ export const trackCustomEvent = ({
2747
if (isOptedOut) return
2848

2949
// Set custom URL removing any query params or hash fragments
30-
window && push([`setCustomUrl`, window.location.href.replace(/[?#].*$/, "")])
50+
if (window) {
51+
const normalizedPathname = normalizePathForMatomo(window.location.pathname)
52+
const normalizedUrl = window.location.origin + normalizedPathname
53+
push([`setCustomUrl`, normalizedUrl])
54+
}
3155
push([`trackEvent`, eventCategory, eventAction, eventName, eventValue])
3256
}

src/lib/utils/url.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export const addSlashes = (href: string): string => {
5050
}
5151

5252
export const getFullUrl = (locale: string | undefined, path: string) =>
53-
addSlashes(new URL(join(locale || DEFAULT_LOCALE, path), SITE_URL).href)
53+
DEFAULT_LOCALE === locale || !locale
54+
? addSlashes(new URL(path, SITE_URL).href)
55+
: addSlashes(new URL(join(locale, path), SITE_URL).href)
5456

5557
// Remove trailing slash from slug and add leading slash
5658
export const normalizeSlug = (slug: string) => {

0 commit comments

Comments
 (0)