vue-hooks-plus
Version:
Vue hooks library
26 lines (25 loc) • 770 B
JavaScript
import { computed, unref, watchEffect } from "vue";
const UseFaviconImgTypeMap = {
SVG: "image/svg+xml",
ICO: "image/x-icon",
GIF: "image/gif",
PNG: "image/png"
};
function useFavicon(href) {
const _href = computed(() => unref(href));
watchEffect(() => {
var _a;
if (!_href.value)
return;
const cutUrl = (_a = _href.value) == null ? void 0 : _a.split(".");
const imgSuffix = cutUrl[cutUrl.length - 1].toLocaleUpperCase();
const link = document.querySelector("link[rel*='icon']") || document.createElement("link");
link.type = UseFaviconImgTypeMap[imgSuffix];
link.href = _href.value;
link.rel = "shortcut icon";
document.getElementsByTagName("head")[0].appendChild(link);
});
}
export {
useFavicon as default
};