📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
(improvement) SEO (#384)
Andrii Romasiun926da5cc593d
diff --git a/app/components/LocaleLinks.tsx b/app/components/LocaleLinks.tsxnew file mode 100644index 000000000..f21d0cd8b--- /dev/null+++ b/app/components/LocaleLinks.tsx@@ -0,0 +1,57 @@+import { useMemo } from 'react'+import { useLocation, Location } from '@remix-run/react'+import { whitelist, whitelistWithCC, MAIN_URL } from 'redux/constants'+import _map from 'lodash/map'++const getUrlFromLocation = (location: Location) => {+ const { pathname, hash, search } = location+ const urlObject = new URL(`${MAIN_URL}${pathname}${search}${hash}`)++ return urlObject+}++const getAlternateLinks = (location: Location) => {+ const urlObject = getUrlFromLocation(location)++ const lnglessUrl = urlObject.toString()++ const alternateLinks = _map(whitelist, (lc) => {+ urlObject.searchParams.set('lng', lc)++ return {+ rel: 'alternate',+ hrefLang: lc,+ href: urlObject.toString(),+ }+ })++ const alternateLinksWithCountryCodes = _map(whitelistWithCC, (value, key) => {+ urlObject.searchParams.set('lng', key)++ return {+ rel: 'alternate',+ hrefLang: value,+ href: urlObject.toString(),+ }+ })++ return {+ altLinks: [...alternateLinks, ...alternateLinksWithCountryCodes],+ lnglessUrl,+ }+}++export const LocaleLinks = () => {+ const location = useLocation()++ const altLinks = useMemo(() => getAlternateLinks(location), [location])++ return (+ <>+ {_map(altLinks.altLinks, (link) => (+ <link key={link.hrefLang} {...link} />+ ))}+ <link rel='alternate' href={altLinks.lnglessUrl} hrefLang='x-default' />+ </>+ )+}diff --git a/app/components/SEO.tsx b/app/components/SEO.tsxnew file mode 100644index 000000000..ef94cc19c--- /dev/null+++ b/app/components/SEO.tsx@@ -0,0 +1,51 @@+import { useTranslation } from 'react-i18next'+import { useLocation } from '@remix-run/react'+import _startsWith from 'lodash/startsWith'+import _toUpper from 'lodash/toUpper'+// import { MAIN_URL } from 'redux/constants'+import { getPageMeta } from 'utils/server'+import { getOgImageUrl } from 'redux/constants'++export const SEO = () => {+ const { t, i18n: { language } } = useTranslation('common')+ const { pathname } = useLocation()+ const { title, prefixLessTitle } = getPageMeta(t, undefined, pathname)++ const isBlogPage = _startsWith(pathname, '/blog')+ const isMainPage = pathname === '/'+ const ogImageUrl = getOgImageUrl(prefixLessTitle)++ return (+ <>+ {!isBlogPage && (+ <>+ <title>{title}</title>+ <meta property='og:title' content='Swetrix' />+ <meta name='twitter:title' content='Swetrix | Ultimate open-source web analytics to satisfy all your needs' />+ <meta name='description' content='Swetrix is a cookieless, privacy-first and GDPR-compliant Google Analytics alternative' />+ <meta name='twitter:description' content='Swetrix is a cookieless, privacy-first and GDPR-compliant Google Analytics alternative' />+ <meta property='og:description' content='Swetrix is a cookieless, privacy-first and GDPR-compliant Google Analytics alternative' />+ {isMainPage ? (+ <>+ <meta name='twitter:image' content='https://swetrix.com/assets/og_image.png' />+ <meta property='og:image' content='https://swetrix.com/assets/og_image.png' />+ </>+ ) : (+ <>+ <meta name='twitter:image' content={ogImageUrl} />+ <meta property='og:image' content={ogImageUrl} />+ </>+ )}+ </>+ )}+ <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:type' content='website' />+ <meta name='language' content={_toUpper(language)} />+ <meta http-equiv='content-language' content={_toUpper(language)} />+ </>+ )+}diff --git a/app/redux/constants/index.ts b/app/redux/constants/index.tsindex a1941b8fe..0d096bbb9 100644--- a/app/redux/constants/index.ts+++ b/app/redux/constants/index.ts@@ -1,3 +1,5 @@+import _endsWith from 'lodash/endsWith'+const displayDateOptions: Intl.DateTimeFormatOptions = {year: 'numeric',month: 'short',@@ -343,6 +345,12 @@ export const isSelfhosted = Boolean(isBrowser ? window.REMIX_ENV?.SELFHOSTED : process.env.SELFHOSTED,)+export const getOgImageUrl = (title: string) => {+ const apiUrl = _endsWith(API_URL, '/') ? API_URL.slice(0, -1) : API_URL++ return `${apiUrl}/v1/og-image?title=${title}`+}+// Functionsexport const getProjectCacheKey = (period: string, timeBucket: string, mode: 'periodical' | 'cumulative', filters?: any): string => `${period}${timeBucket}${mode}${filters ? JSON.stringify(filters) : ''}}`export const getProjectCaptchaCacheKey = (period: string, timeBucket: string, filters?: any): string => `${period}${timeBucket}captcha${filters ? JSON.stringify(filters) : ''}}`diff --git a/app/root.tsx b/app/root.tsxindex 8bb692a39..a8cf1a0da 100644--- a/app/root.tsx+++ b/app/root.tsx@@ -15,7 +15,7 @@ import {} from '@remix-run/react'import { store } from 'redux/store'import {- whitelist, isBrowser, CONTACT_EMAIL, LS_THEME_SETTING, isSelfhosted, whitelistWithCC,+ isBrowser, CONTACT_EMAIL, LS_THEME_SETTING, isSelfhosted,} from 'redux/constants'import {getCookie, generateCookieString,@@ -23,9 +23,7 @@ import {import { ExclamationTriangleIcon, ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'import { Provider } from 'react-redux'import clsx from 'clsx'-import _map from 'lodash/map'import _replace from 'lodash/replace'-import _startsWith from 'lodash/startsWith'// @ts-ignoreimport { transitions, positions, Provider as AlertProvider } from '@blaumaus/react-alert'import BillboardCss from 'billboard.js/dist/billboard.min.css'@@ -37,8 +35,10 @@ import { useTranslation } from 'react-i18next'import AppWrapper from 'App'import { detectLanguage } from 'i18n'import {- detectTheme, getPageMeta, isAuthenticated, isWWW,+ detectTheme, isAuthenticated, isWWW,} from 'utils/server'+import { LocaleLinks } from 'components/LocaleLinks'+import { SEO } from 'components/SEO'import mainCss from 'styles/index.css'import tailwindCss from 'styles/tailwind.css'@@ -226,61 +226,20 @@ export const handle = {export default function App() {const {- locale, url, theme, REMIX_ENV, isAuthed, pathname,+ locale, url, theme, REMIX_ENV, isAuthed,} = useLoaderData<typeof loader>()- const { i18n, t } = useTranslation('common')- const { title } = getPageMeta(t, url)+ const { i18n } = useTranslation('common')const urlObject = new URL(url)urlObject.searchParams.delete('lng')- const lnglessUrl = urlObject.toString()-- const alternateLinks = _map(whitelist, (lc) => {- urlObject.searchParams.set('lng', lc)-- return {- rel: 'alternate',- hrefLang: lc,- href: urlObject.toString(),- }- })- const alternateLinksWithCountryCodes = _map(whitelistWithCC, (value, key) => {- urlObject.searchParams.set('lng', key)-- return {- rel: 'alternate',- hrefLang: value,- href: urlObject.toString(),- }- })-useChangeLanguage(locale)- const isBlogPage = _startsWith(pathname, '/blog')-return (<html className={theme} lang={locale} dir={i18n.dir()}><head><meta charSet='utf-8' />- {!isBlogPage && (- <>- <title>{title}</title>- <meta property='og:title' content='Swetrix' />- <meta name='description' content='Swetrix is a cookieless, privacy-first and GDPR-compliant Google Analytics alternative' />- <meta name='twitter:description' content='Swetrix is a cookieless, privacy-first and GDPR-compliant Google Analytics alternative' />- <meta property='og:description' content='Swetrix is a cookieless, privacy-first and GDPR-compliant Google Analytics alternative' />- </>- )}- <meta name='theme-color' content='#818cf8' />- <meta name='twitter:title' content='Swetrix | Ultimate open-source web analytics to satisfy all your needs' />- <meta name='twitter:site' content='@swetrix' />- <meta name='twitter:card' content='summary_large_image' />- <meta name='twitter:image' content='https://swetrix.com/assets/og_image.png' />- <meta property='og:image' content='https://swetrix.com/assets/og_image.png' />- <meta property='og:site_name' content='Swetrix' />- <meta property='og:url' content='https://swetrix.com' />- <meta property='og:type' content='website' />+ <SEO /><meta name='google' content='notranslate' /><link rel='icon' type='image/x-icon' href='/favicon.ico' />{/* <meta name='apple-mobile-web-app-title' content='Swetrix' /> */}@@ -290,10 +249,7 @@ export default function App() {<Links />{theme === 'dark' && <link rel='stylesheet' href={FlatpickrDarkCss} />}{theme === 'light' && <link rel='stylesheet' href={FlatpickrLightCss} />}- {_map([...alternateLinks, ...alternateLinksWithCountryCodes], (link) => (- <link key={link.hrefLang} {...link} />- ))}- <link rel='alternate' href={lnglessUrl} hrefLang='x-default' />+ <LocaleLinks /><link rel='preload' href={`/locales/${locale}.json`} as='fetch' type='application/json' crossOrigin='anonymous' /><script// eslint-disable-next-line react/no-dangerdiff --git a/app/routes/blog.$category.$slug.tsx b/app/routes/blog.$category.$slug.tsxindex b005f4be6..d86b8bdac 100644--- a/app/routes/blog.$category.$slug.tsx+++ b/app/routes/blog.$category.$slug.tsx@@ -2,7 +2,7 @@ import type { LoaderFunction, LinksFunction, V2_MetaFunction } from '@remix-run/import { redirect, json } from '@remix-run/node'import singlePostCss from 'css/mdfile.css'import { getPost } from 'utils/getPosts'-import { isSelfhosted, TITLE_SUFFIX } from 'redux/constants'+import { isSelfhosted, TITLE_SUFFIX, getOgImageUrl } from 'redux/constants'import Post from 'pages/Blog/Post'export const links: LinksFunction = () => {@@ -10,6 +10,8 @@ export const links: LinksFunction = () => {}export const meta: V2_MetaFunction = (loaderData: any) => {+ const ogImageUrl = getOgImageUrl(loaderData?.data?.title)+return [{title: `${loaderData?.data?.title || 'Blog'} ${TITLE_SUFFIX}`,@@ -18,6 +20,10 @@ export const meta: V2_MetaFunction = (loaderData: any) => {property: "og:title",content: `${loaderData?.data?.title || 'Blog'} ${TITLE_SUFFIX}`,},+ {+ property: "twitter:title",+ content: `${loaderData?.data?.title || 'Blog'} ${TITLE_SUFFIX}`,+ },{property: "og:description",content: loaderData?.data?.intro || '',@@ -30,6 +36,14 @@ export const meta: V2_MetaFunction = (loaderData: any) => {property: "description",content: loaderData?.data?.intro || '',},+ {+ property: "og:image",+ content: ogImageUrl,+ },+ {+ property: "twitter:image",+ content: ogImageUrl,+ },]}diff --git a/app/routes/blog.$slug.tsx b/app/routes/blog.$slug.tsxindex afbef58c4..3556fab9b 100644--- a/app/routes/blog.$slug.tsx+++ b/app/routes/blog.$slug.tsx@@ -10,7 +10,7 @@ import {getPost, getSlugFromFilename, getDateFromFilename,} from 'utils/getPosts'import { getSitemap } from 'api'-import { isSelfhosted, TITLE_SUFFIX} from 'redux/constants'+import { isSelfhosted, TITLE_SUFFIX, getOgImageUrl } from 'redux/constants'import Post from 'pages/Blog/Post'export const links: LinksFunction = () => {@@ -18,6 +18,8 @@ export const links: LinksFunction = () => {}export const meta: V2_MetaFunction = (loaderData: any) => {+ const ogImageUrl = getOgImageUrl(loaderData?.data?.title)+return [{title: `${loaderData?.data?.title || 'Blog'} ${TITLE_SUFFIX}`,@@ -26,6 +28,10 @@ export const meta: V2_MetaFunction = (loaderData: any) => {property: "og:title",content: `${loaderData?.data?.title || 'Blog'} ${TITLE_SUFFIX}`,},+ {+ property: "twitter:title",+ content: `${loaderData?.data?.title || 'Blog'} ${TITLE_SUFFIX}`,+ },{property: "og:description",content: loaderData?.data?.intro || '',@@ -38,6 +44,14 @@ export const meta: V2_MetaFunction = (loaderData: any) => {property: "description",content: loaderData?.data?.intro || '',},+ {+ property: "og:image",+ content: ogImageUrl,+ },+ {+ property: "twitter:image",+ content: ogImageUrl,+ },]}diff --git a/app/routes/blog.tsx b/app/routes/blog.tsxindex f45dab392..711a907ef 100644--- a/app/routes/blog.tsx+++ b/app/routes/blog.tsx@@ -16,6 +16,10 @@ export const meta: V2_MetaFunction = () => {property: "og:title",content: `Blog ${TITLE_SUFFIX}`,},+ {+ property: "twitter:title",+ content: `Blog ${TITLE_SUFFIX}`,+ },]}diff --git a/app/utils/server.ts b/app/utils/server.tsindex 903eb5703..a03d9aa28 100644--- a/app/utils/server.ts+++ b/app/utils/server.ts@@ -99,6 +99,7 @@ export function isWWW(url: URL): boolean {interface IPageMeta {title: string+ prefixLessTitle: string}export const getPageMeta = (@@ -108,10 +109,13 @@ export const getPageMeta = (): IPageMeta => {const DEFAULT_RESULT = {title: t('titles.main'),- }+ } as Partial<IPageMeta>if (!url && !_pathname) {- return DEFAULT_RESULT+ return {+ title: DEFAULT_RESULT.title as string,+ prefixLessTitle: DEFAULT_RESULT.title as string,+ }}const pathname = _pathname || new URL(url as string).pathname@@ -241,7 +245,8 @@ export const getPageMeta = (// todo: create_alert, alert_settings, project_protected_password,+ result.prefixLessTitle = result.titleresult.title += ` ${TITLE_SUFFIX}`- return result+ return result as IPageMeta}