Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tips for favicon caching behavior #122

Open
JusticeMatthew opened this issue May 24, 2024 · 1 comment
Open

Tips for favicon caching behavior #122

JusticeMatthew opened this issue May 24, 2024 · 1 comment

Comments

@JusticeMatthew
Copy link

favicons love to be cached which can cause them to not show up correctly a lot of time in the dev environment, it comes up fairly consistently in the discord

This can be worked around by appending the favicon with some info specifically related to the caching

- <link rel="icon" href="/favicon.ico"

+ <link rel="icon" href="/favicon.ico?"

or

+ <link rel="icon" href="/favicon.ico?v1"
@BryceRussell
Copy link
Member

BryceRussell commented May 24, 2024

One downside to this is that users have to manually update the path every time the favicon is updated.

Another way of doing this is using an imported image, which includes cache busting hashes automatically:

---
import favicon from '../favicon.svg';
---

<link rel="icon" type="image/svg+xml" href={favicon.svg} />

Although this does not work while in dev mode, the hash is only applied when building. To get this working in dev, you can use a hack like this:

---
import favicon from '../favicon.svg'

const faviconSrc = import.meta.env.DEV
	// Prevent caching in dev with a Unix time stamp
	? `${favicon.src}?${(new Date()).valueOf()}`
	// Prevent caching in production with the default cache busting hashes
	: favicon.src
---

<link rel="icon" type="image/svg+xml" href={faviconSrc} />

One issue with this hack is that the favicon will never be cached in dev mode, even if it hasn't changed, because the Unix time stamp will always return a unique path. But, this shouldn't be a problem because it only happens in dev mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants