mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Use /favicon.ico with DDG fallback
This commit is contained in:
parent
860e6390f1
commit
a355ecfdb0
1 changed files with 24 additions and 4 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
import { useConfig } from '@/components/hooks';
|
import { useConfig } from '@/components/hooks';
|
||||||
import { FAVICON_URL, GROUPED_DOMAINS } from '@/lib/constants';
|
import { FAVICON_URL, GROUPED_DOMAINS } from '@/lib/constants';
|
||||||
|
|
||||||
|
|
@ -13,10 +14,29 @@ export function Favicon({ domain, ...props }) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = config?.faviconUrl || FAVICON_URL;
|
|
||||||
const hostName = domain ? getHostName(domain) : null;
|
const hostName = domain ? getHostName(domain) : null;
|
||||||
const domainName = GROUPED_DOMAINS[hostName]?.domain || hostName;
|
const domainName = hostName ? GROUPED_DOMAINS[hostName]?.domain || hostName : null;
|
||||||
const src = hostName ? url.replace(/\{\{\s*domain\s*}}/, domainName) : null;
|
const primaryUrl = hostName
|
||||||
|
? config?.faviconUrl
|
||||||
|
? config.faviconUrl.replace(/\{\{\s*domain\s*}}/, domainName)
|
||||||
|
: `https://${domainName}/favicon.ico`
|
||||||
|
: null;
|
||||||
|
const fallbackUrl = hostName
|
||||||
|
? FAVICON_URL.replace(/\{\{\s*domain\s*}}/, domainName)
|
||||||
|
: null;
|
||||||
|
const [src, setSrc] = useState(primaryUrl);
|
||||||
|
|
||||||
return hostName ? <img src={src} width={16} height={16} alt="" {...props} /> : null;
|
useEffect(() => {
|
||||||
|
setSrc(primaryUrl);
|
||||||
|
}, [primaryUrl]);
|
||||||
|
|
||||||
|
function handleError() {
|
||||||
|
if (src && fallbackUrl && src !== fallbackUrl) {
|
||||||
|
setSrc(fallbackUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostName && src ? (
|
||||||
|
<img src={src} width={16} height={16} alt="" onError={handleError} {...props} />
|
||||||
|
) : null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue