ai-chatbot/components/favicon-updater.tsx
Your Name 83f7f3d6d1
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
fix login crash from DOM cleanup race
2026-02-11 23:19:35 +08:00

57 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useEffect } from "react";
export function FaviconUpdater() {
useEffect(() => {
let isDisposed = false;
// 从 API 获取最新的 favicon 设置
fetch("/api/site-settings")
.then((res) => res.json())
.then((settings) => {
if (!settings.favicon || isDisposed) {
return;
}
const faviconUrl = settings.favicon;
const timestamp = Date.now();
const urlWithCache = `${faviconUrl}${faviconUrl.includes("?") ? "&" : "?"}v=${timestamp}`;
// 仅处理本组件创建的 link,避免删除 Next/React 管理的 head 节点
const oldLinks = document.querySelectorAll(
'link[data-managed-favicon="true"]'
);
oldLinks.forEach((link) => {
if (link.parentNode) {
link.parentNode.removeChild(link);
}
});
const createManagedLink = (rel: string, href: string, type?: string) => {
const link = document.createElement("link");
link.rel = rel;
link.href = href;
link.setAttribute("data-managed-favicon", "true");
if (type) {
link.type = type;
}
document.head.appendChild(link);
};
createManagedLink("icon", urlWithCache, "image/x-icon");
createManagedLink("apple-touch-icon", urlWithCache);
createManagedLink("shortcut icon", urlWithCache);
})
.catch((error) => {
console.error("[Favicon] Error updating favicon:", error);
});
return () => {
isDisposed = true;
};
}, []);
return null;
}