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!

set titles dynamically on client-side

Blue Mousefdc126d37d1d
diff --git a/app/App.tsx b/app/App.tsx
index 76fe3f594..cac063400 100644
--- a/app/App.tsx
+++ b/app/App.tsx
@@ -3,6 +3,7 @@ import React, {
} from 'react'
import { useLocation, Outlet } from '@remix-run/react'
import { useSelector } from 'react-redux'
+import { useTranslation } from 'react-i18next'
// @ts-ignore
import { useAlert } from '@blaumaus/react-alert'
import cx from 'clsx'
@@ -29,6 +30,7 @@ import { alertsActions } from 'redux/reducers/alerts'
import { StateType, useAppDispatch } from 'redux/store'
import UIActions from 'redux/reducers/ui'
import routesPath from 'routesPath'
+import { getPageMeta } from 'utils/server'
import { getRefreshToken } from 'utils/refreshToken'
import { authMe } from './api'
@@ -71,9 +73,14 @@ interface IApp {
ssrTheme: 'dark' | 'light'
}
+const TITLE_BLACKLIST = [
+ '/projects/', '/captchas/',
+]
+
const App: React.FC<IApp> = ({ ssrTheme }) => {
const dispatch = useAppDispatch()
const { pathname } = useLocation()
+ const { t } = useTranslation('common')
const alert = useAlert()
const {
loading, authenticated,
@@ -164,6 +171,15 @@ const App: React.FC<IApp> = ({ ssrTheme }) => {
}
}, [message, type]) // eslint-disable-line
+ useEffect(() => {
+ if (_some(TITLE_BLACKLIST, (page) => _includes(pathname, page))) {
+ return
+ }
+
+ const { title } = getPageMeta(t, undefined, pathname)
+ document.title = title
+ }, [t, pathname])
+
const isMinimalFooter = _some(minimalFooterPages, (page) => _includes(pathname, page))
if (!accessToken || !loading) {
diff --git a/app/pages/Captcha/Settings/CaptchaSettings.tsx b/app/pages/Captcha/Settings/CaptchaSettings.tsx
index a61488dc0..3fd43dfaf 100644
--- a/app/pages/Captcha/Settings/CaptchaSettings.tsx
+++ b/app/pages/Captcha/Settings/CaptchaSettings.tsx
@@ -20,7 +20,7 @@ import PropTypes from 'prop-types'
import { ExclamationTriangleIcon, TrashIcon, ClipboardDocumentIcon } from '@heroicons/react/24/outline'
import { withAuthentication, auth } from 'hoc/protected'
-import { isSelfhosted } from 'redux/constants'
+import { isSelfhosted, TITLE_SUFFIX } from 'redux/constants'
import {
createProject, updateProject, deleteCaptchaProject, resetCaptchaProject, reGenerateCaptchaSecretKey, getProjectsNames, createCaptchaInherited,
} from 'api'
@@ -83,6 +83,7 @@ const CaptchaSettings = ({
}) => string,
} = useTranslation('common')
const { pathname } = useLocation()
+ // @ts-ignore
const { id }: {
id: string,
} = useParams()
@@ -291,6 +292,13 @@ const CaptchaSettings = ({
const title = isSettings ? `${t('project.settings.settings')} ${form.name}` : t('project.settings.create')
+ useEffect(() => {
+ let pageTitle = isSettings ? `${t('project.settings.settings')} ${form.name}` : t('project.settings.create')
+ pageTitle += ` ${TITLE_SUFFIX}`
+
+ document.title = pageTitle
+ }, [form, t])
+
const onRegenerateSecretKey = async () => {
try {
const res = await reGenerateCaptchaSecretKey(id)
diff --git a/app/pages/Captcha/View/ViewCaptcha.tsx b/app/pages/Captcha/View/ViewCaptcha.tsx
index d10f3075e..f2326effc 100644
--- a/app/pages/Captcha/View/ViewCaptcha.tsx
+++ b/app/pages/Captcha/View/ViewCaptcha.tsx
@@ -32,7 +32,7 @@ import { getItem, setItem } from 'utils/localstorage'
import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
tbPeriodPairs, getProjectCaptchaCacheKey, timeBucketToDays, getProjectCacheCustomKey, roleViewer,
- MAX_MONTHS_IN_PAST, TimeFormat, chartTypes,
+ MAX_MONTHS_IN_PAST, TimeFormat, chartTypes, TITLE_SUFFIX,
} from 'redux/constants'
import { ICaptchaProject, IProject, ILiveStats } from 'redux/models/IProject'
import { IUser } from 'redux/models/IUser'
@@ -85,6 +85,7 @@ const ViewProject = ({
} = useTranslation('common')
const [periodPairs, setPeriodPairs] = useState(tbPeriodPairs(t))
const dashboardRef = useRef(null)
+ // @ts-ignore
const { id }: {
id: string,
} = useParams()
@@ -132,6 +133,10 @@ const ViewProject = ({
const { name } = project as IProject
+ useEffect(() => {
+ document.title = `${name} ${TITLE_SUFFIX}`
+ }, [project])
+
// @ts-ignore
const sharedRoles = useMemo(() => _find(user.sharedProjects, p => p.project.id === id)?.role || {}, [user, id])
diff --git a/app/pages/Project/Settings/ProjectSettings.tsx b/app/pages/Project/Settings/ProjectSettings.tsx
index 62a3a901e..deb683240 100644
--- a/app/pages/Project/Settings/ProjectSettings.tsx
+++ b/app/pages/Project/Settings/ProjectSettings.tsx
@@ -19,7 +19,7 @@ import PropTypes from 'prop-types'
import { ExclamationTriangleIcon, TrashIcon, RocketLaunchIcon } from '@heroicons/react/24/outline'
import { withAuthentication, auth } from 'hoc/protected'
-import { isSelfhosted } from 'redux/constants'
+import { isSelfhosted, TITLE_SUFFIX } from 'redux/constants'
import { IProject } from 'redux/models/IProject'
import { IUser } from 'redux/models/IUser'
import { IProjectForShared, ISharedProject } from 'redux/models/ISharedProject'
@@ -145,6 +145,7 @@ const ProjectSettings = ({
}) => string,
} = useTranslation('common')
const { pathname } = useLocation()
+ // @ts-ignore
const { id }: {
id: string,
} = useParams()
@@ -378,6 +379,13 @@ const ProjectSettings = ({
const title = isSettings ? `${t('project.settings.settings')} ${form.name}` : t('project.settings.create')
+ useEffect(() => {
+ let pageTitle = isSettings ? `${t('project.settings.settings')} ${form.name}` : t('project.settings.create')
+ pageTitle += ` ${TITLE_SUFFIX}`
+
+ document.title = pageTitle
+ }, [form, t])
+
return (
<div
className={cx('min-h-min-footer bg-gray-50 dark:bg-slate-900 flex flex-col py-6 px-4 sm:px-6 lg:px-8', {
diff --git a/app/pages/Project/View/ViewProject.tsx b/app/pages/Project/View/ViewProject.tsx
index 9f672b583..2bc70523a 100644
--- a/app/pages/Project/View/ViewProject.tsx
+++ b/app/pages/Project/View/ViewProject.tsx
@@ -46,7 +46,7 @@ import {
tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,
timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, PROJECT_TABS,
TimeFormat, getProjectForcastCacheKey, chartTypes, roleAdmin, TRAFFIC_PANELS_ORDER, PERFORMANCE_PANELS_ORDER, isSelfhosted, tbPeriodPairsCompare,
- PERIOD_PAIRS_COMPARE, filtersPeriodPairs, IS_ACTIVE_COMPARE, PROJECTS_PROTECTED, getProjectCacheCustomKeyPerf, isBrowser,
+ PERIOD_PAIRS_COMPARE, filtersPeriodPairs, IS_ACTIVE_COMPARE, PROJECTS_PROTECTED, getProjectCacheCustomKeyPerf, isBrowser, TITLE_SUFFIX,
} from 'redux/constants'
import { IUser } from 'redux/models/IUser'
import { IProject, ILiveStats } from 'redux/models/IProject'
@@ -390,8 +390,12 @@ const ViewProject = ({
// { name } is a project name from project
const { name } = project
- // TODO: DISPLAY PAGE TITLE IN TABS LATER
- // const pageTitle = user?.showLiveVisitorsInTitle ? `πŸ‘€ ${liveStats[id]} - ${name}` : name
+ useEffect(() => {
+ let pageTitle = user?.showLiveVisitorsInTitle ? `πŸ‘€ ${liveStats[id]} - ${name}` : name
+ pageTitle += ` ${TITLE_SUFFIX}`
+
+ document.title = pageTitle
+ }, [project, user])
// sharedRoles is a role for shared project
const sharedRoles = useMemo(() => _find(user.sharedProjects, p => p.project.id === id)?.role || {}, [user, id])
diff --git a/app/utils/server.ts b/app/utils/server.ts
index 58141700e..000c9abe6 100644
--- a/app/utils/server.ts
+++ b/app/utils/server.ts
@@ -26,14 +26,21 @@ interface IPageMeta {
export const getPageMeta = (
t: (key: string) => string,
- url: string,
+ url?: string,
+ _pathname?: string,
): IPageMeta => {
- const { pathname } = new URL(url)
- // TODO: Use t()
- let result = {
+ const DEFAULT_RESULT = {
title: t('titles.main'),
}
+ if (!url && !_pathname) {
+ return DEFAULT_RESULT
+ }
+
+ const pathname = _pathname || new URL(url as string).pathname
+
+ let result = DEFAULT_RESULT
+
switch (pathname) {
case routes.signin:
result = {