Fixing favicon changes on other browsers (#1017)

This commit is contained in:
Jesús Espino 2021-08-17 22:50:49 +02:00 committed by GitHub
parent 7bc168ad84
commit 83a45ae706
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View file

@ -49,13 +49,19 @@ const MainApp = () => {
}, [])
useEffect(() => {
const link = (document.querySelector("link[rel*='icon']") || document.createElement('link')) as HTMLLinkElement
const oldLink = document.querySelector("link[rel*='icon']") as HTMLLinkElement
if (!oldLink) {
return () => null
}
const restoreData = {
type: link.type,
rel: link.rel,
href: link.href,
type: oldLink.type,
rel: oldLink.rel,
href: oldLink.href,
}
return () => {
document.querySelectorAll("link[rel*='icon']").forEach((n) => n.remove())
const link = document.createElement('link') as HTMLLinkElement
link.type = restoreData.type
link.rel = restoreData.rel
link.href = restoreData.href

View file

@ -241,11 +241,15 @@ class Utils {
// favicon
static setFavicon(icon?: string): void {
const href = icon ? `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">${icon}</text></svg>` : ''
const link = (document.querySelector("link[rel*='icon']") || document.createElement('link')) as HTMLLinkElement
if (!icon) {
document.querySelector("link[rel*='icon']")?.remove()
return
}
const link = document.createElement('link') as HTMLLinkElement
link.type = 'image/x-icon'
link.rel = 'shortcut icon'
link.href = href
link.href = `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">${icon}</text></svg>`
document.querySelectorAll("link[rel*='icon']").forEach((n) => n.remove())
document.getElementsByTagName('head')[0].appendChild(link)
}