Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

i18n fixes

Blue Mousecc13473ea772
diff --git a/web/app/components/LocaleLinks.tsx b/web/app/components/LocaleLinks.tsx
index 4ebfe8ce5..769b2e68c 100644
--- a/web/app/components/LocaleLinks.tsx
+++ b/web/app/components/LocaleLinks.tsx
@@ -3,7 +3,7 @@ import _some from 'lodash/some'
import { useMemo } from 'react'
import { useLocation, Location } from 'react-router'
-import { whitelist, whitelistWithCC, MAIN_URL } from '~/lib/constants'
+import { whitelist, whitelistWithCC, MAIN_URL, defaultLanguage } from '~/lib/constants'
const getUrlFromLocation = (location: Location) => {
const { pathname, hash, search } = location
@@ -15,25 +15,42 @@ const getUrlFromLocation = (location: Location) => {
const getAlternateLinks = (location: Location) => {
const urlObject = getUrlFromLocation(location)
- const lnglessUrl = urlObject.toString()
+ // Ensure x-default always points to URL without the ?lng parameter
+ const urlWithoutLng = new URL(urlObject.toString())
+ urlWithoutLng.searchParams.delete('lng')
+ const lnglessUrl = urlWithoutLng.toString()
const alternateLinks = _map(whitelist, (lc) => {
- urlObject.searchParams.set('lng', lc)
+ const href =
+ lc === defaultLanguage
+ ? lnglessUrl
+ : (() => {
+ const tmp = new URL(lnglessUrl)
+ tmp.searchParams.set('lng', lc)
+ return tmp.toString()
+ })()
return {
rel: 'alternate',
hrefLang: lc,
- href: urlObject.toString(),
+ href,
}
})
const alternateLinksWithCountryCodes = _map(whitelistWithCC, (value, key) => {
- urlObject.searchParams.set('lng', key)
+ const href =
+ key === defaultLanguage
+ ? lnglessUrl
+ : (() => {
+ const tmp = new URL(lnglessUrl)
+ tmp.searchParams.set('lng', key)
+ return tmp.toString()
+ })()
return {
rel: 'alternate',
hrefLang: value,
- href: urlObject.toString(),
+ href,
}
})
diff --git a/web/app/components/SEO.tsx b/web/app/components/SEO.tsx
index f6e6c2cba..20f0fab53 100644
--- a/web/app/components/SEO.tsx
+++ b/web/app/components/SEO.tsx
@@ -3,7 +3,7 @@ import _toUpper from 'lodash/toUpper'
import { useTranslation } from 'react-i18next'
import { useLocation } from 'react-router'
-import { getOgImageUrl } from '~/lib/constants'
+import { getOgImageUrl, MAIN_URL, defaultLanguage } from '~/lib/constants'
import routes from '~/utils/routes'
import { getPageMeta } from '~/utils/server'
@@ -12,7 +12,7 @@ export const SEO = () => {
t,
i18n: { language },
} = useTranslation('common')
- const { pathname } = useLocation()
+ const { pathname, search, hash } = useLocation()
const { title, prefixLessTitle } = getPageMeta(t, undefined, pathname)
const isBlogPage = _startsWith(pathname, '/blog')
@@ -21,6 +21,17 @@ export const SEO = () => {
const isProjectViewPage = _startsWith(pathname, '/projects/')
const ogImageUrl = getOgImageUrl(prefixLessTitle)
+ const canonicalUrl = (() => {
+ const url = new URL(`${MAIN_URL}${pathname}${search}${hash}`)
+ if (language === defaultLanguage) {
+ url.searchParams.delete('lng')
+ } else {
+ // Ensure the canonical of non-default locales includes the language param
+ url.searchParams.set('lng', language)
+ }
+ return url.toString()
+ })()
+
return (
<>
{!isBlogPage ? (
@@ -60,11 +71,12 @@ export const SEO = () => {
) : null}
</>
) : null}
+ <link rel='canonical' href={canonicalUrl} />
<meta name='theme-color' content='#818cf8' />
<meta name='twitter:site' content='@swetrix' />
<meta name='twitter:card' content='summary_large_image' />
<meta property='og:site_name' content='Swetrix' />
- <meta property='og:url' content='https://swetrix.com' />
+ <meta property='og:url' content={canonicalUrl} />
<meta property='og:type' content='website' />
<meta name='language' content={_toUpper(language)} />
<meta httpEquiv='content-language' content={_toUpper(language)} />