Skip to content

feat(useFavicon): add temporary option to restore favicon on unmount#367

Open
udohjeremiah wants to merge 1 commit intouidotdev:mainfrom
udohjeremiah:main
Open

feat(useFavicon): add temporary option to restore favicon on unmount#367
udohjeremiah wants to merge 1 commit intouidotdev:mainfrom
udohjeremiah:main

Conversation

@udohjeremiah
Copy link
Copy Markdown

Feature Request: Support temporary favicon with cleanup on unmount

Current behavior

useFavicon sets the favicon but never restores the original when the component unmounts, making it impossible to use it temporarily on a specific page or component.

Proposed API

Add an optional options parameter with a temporary flag:

useFavicon(url, { temporary: true });

Notes on implementation

  • options is stored in a ref (following the same pattern as useScript) so changes to the options object don't cause the effect to re-run unnecessarily.
  • If no link[rel~="icon"] exists in the document (e.g. frameworks that only use a preload hint instead of an icon link), the hook creates one and removes it on cleanup rather than restoring an empty href. This handles environments like TanStack Start correctly.
  • If the link already existed, the original href is restored as expected.

Use case

Setting a page-specific favicon that should revert to the app's default favicon when navigating away.

// Reverts to original favicon when component unmounts
useFavicon("https://example.com/favicon.ico", { temporary: true });

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant