Skip to content

Commit

Permalink
fix: make favicon setting global
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The setting `favicon` now only accepts one global
value, see https://vike.dev/favicon#global
  • Loading branch information
brillout committed Aug 2, 2024
1 parent 48a0d1f commit dfab891
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 18 deletions.
3 changes: 2 additions & 1 deletion vike-solid/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default {
env: { server: true },
},
favicon: {
env: { server: true, client: true },
env: { server: true },
global: true,
},
lang: {
env: { server: true, client: true },
Expand Down
17 changes: 0 additions & 17 deletions vike-solid/renderer/onRenderClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const onRenderClient: OnRenderClientAsync = async (pageContext): ReturnType<OnRe

const title = getHeadSetting("title", pageContext) || "";
const lang = getHeadSetting("lang", pageContext) || "en";
const favicon = getHeadSetting("favicon", pageContext);

// We skip if the value is undefined because we shouldn't remove values set in HTML (by the Head setting).
// - This also means that previous values will leak: upon client-side navigation, the title set by the previous
Expand All @@ -44,21 +43,5 @@ const onRenderClient: OnRenderClientAsync = async (pageContext): ReturnType<OnRe
// can set the value to `null` to ensure that previous values are overridden.
if (title !== undefined) document.title = title;
if (lang !== undefined) document.documentElement.lang = lang;
if (favicon !== undefined) setFavicon(favicon);
}
};

// https://stackoverflow.com/questions/260857/changing-website-favicon-dynamically/260876#260876
function setFavicon(faviconUrl: string | null) {
let link: HTMLLinkElement | null = document.querySelector("link[rel~='icon']");
if (!faviconUrl) {
if (link) document.head.removeChild(link);
return;
}
if (!link) {
link = document.createElement("link");
link.rel = "icon";
document.head.appendChild(link);
}
link.href = faviconUrl;
}

0 comments on commit dfab891

Please sign in to comment.