How to update the favicon and title dynamically with the App Router #82902
-
|
We are migrating from One thing we couldn't make work is to update the favicon and title dynamically depending on the user having new messages, similar to how Slack does. It's currently working using the How to do that using the I see that you can define metadata with How to achieve this? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I was able to make it work in an imperative way using const useCustomHead = ({ title }: { title: string }) => {
// selectors that retrieve the amount of unread messages and notifications...
React.useEffect(() => {
document.title = fullTitle;
// Update favicon
const link: HTMLLinkElement =
document.querySelector("link[rel~='icon']") ||
document.createElement('link');
link.rel = 'icon';
link.type =
hasTalkUnviewed || hasNotificationUnviewed
? 'image/svg+xml'
: 'image/x-icon';
link.href = hasTalkUnviewed
? '/static/main-icons/message.svg'
: hasNotificationUnviewed
? '/static/main-icons/notification.svg'
: '/favicon.ico';
document.head.appendChild(link);
}, [fullTitle, hasTalkUnviewed, hasNotificationUnviewed]);
}; |
Beta Was this translation helpful? Give feedback.
I was able to make it work in an imperative way using
useEffectin ourPageWrappercomponent that wraps the content of every page: