Astro - dynamically adjust favicon

So, I'm just pretty new regarding Astro. To change my favicon I assume I would need to do:
export interface Props {
favicon?: image;
}

const { favicon = 'path' } = Astro.props as Props;
export interface Props {
favicon?: image;
}

const { favicon = 'path' } = Astro.props as Props;
<link rel="icon" type="image/svg+xml" href={favicon} />
<link rel="icon" type="image/svg+xml" href={favicon} />
That is correct, right? Now, is there a way to dynamically adjust this favicon? Let's say, if user has light theme, use black favicon, and if user has dark theme, use white favicon.
8 Replies
Myndi
MyndiOP•3y ago
Actually, I should pass a string, right? Not sure what I can pass on. Besides string, integer? and boolean. Okay, the string question is solved, the other one remains! worrySmile yes, url has to be a string.
Kevin Powell
Kevin Powell•3y ago
In this case, there is a much simpler solution 😄
<link href="favicon-light.png" rel="icon"
media="(prefers-color-scheme: light)"
/>
<link href="dark.png rel="icon"
media="(prefers-color-scheme: dark)"/>
<link href="favicon-light.png" rel="icon"
media="(prefers-color-scheme: light)"
/>
<link href="dark.png rel="icon"
media="(prefers-color-scheme: dark)"/>
Props wouldn't really help us in this situation anyway, since I don't think we could change them with JS, since the alternative is to use JS to check the user's preference and have it switch out the icon.
Myndi
MyndiOP•3y ago
Props did help!
Myndi
MyndiOP•3y ago
Brave (dark theme).
Myndi
MyndiOP•3y ago
Edge (light theme).
Myndi
MyndiOP•3y ago
It's dynamically passing the correct favicon :D
<link rel="icon" type="image/svg+xml" href={favicon} media="(prefers-color-scheme: dark)" />
<link rel="icon" type="image/svg+xml" href={faviconLight} media="(prefers-color-scheme: light)" />
<link rel="icon" type="image/svg+xml" href={favicon} media="(prefers-color-scheme: dark)" />
<link rel="icon" type="image/svg+xml" href={faviconLight} media="(prefers-color-scheme: light)" />
Thank you for the help Hearts
Kevin Powell
Kevin Powell•3y ago
Ah, combined with props, very nice 🙂
Want results from more Discord servers?
Add your server