Skip to content

Commit

Permalink
Fallback to favicon.png when favicon.dark.png not found
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentDepth committed Mar 27, 2023
1 parent 8f80c5c commit 8a98219
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions components/Header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import Link from 'next/link'
import Image from 'next/image'
import BLOG from '@/blog.config'
Expand Down Expand Up @@ -35,6 +35,18 @@ const NavBar = () => {
const Header = ({ navBarTitle, fullWidth }) => {
const { dark } = useTheme()

// Favicon

const resolveFavicon = fallback => !fallback && dark ? '/favicon.dark.png' : '/favicon.png'
const [favicon, _setFavicon] = useState(resolveFavicon())
const setFavicon = fallback => _setFavicon(resolveFavicon(fallback))

useEffect(
() => setFavicon(),
// eslint-disable-next-line react-hooks/exhaustive-deps
[dark]
)

const useSticky = !BLOG.autoCollapsedNavBar
const navRef = useRef(/** @type {HTMLDivElement} */ undefined)
const sentinelRef = useRef(/** @type {HTMLDivElement} */ undefined)
Expand Down Expand Up @@ -87,10 +99,11 @@ const Header = ({ navBarTitle, fullWidth }) => {
<div className="flex items-center">
<Link href="/" aria-label={BLOG.title}>
<Image
src={dark ? '/favicon.dark.png' : '/favicon.png'}
src={favicon}
width={24}
height={24}
alt={BLOG.title}
onError={() => setFavicon(true)}
/>
</Link>
{navBarTitle ? (
Expand Down

0 comments on commit 8a98219

Please sign in to comment.