ai-chatbot/components/favicon-updater.tsx
Your Name ec8959af19
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
修复favicon配置不生效并改为动态拉取
2026-02-14 15:33:48 +08:00

50 lines
1.2 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;
const timestamp = Date.now();
const urlWithCache = `/api/favicon?v=${timestamp}`;
Promise.resolve().then(() => {
if (isDisposed) {
return;
}
// 仅处理本组件创建的 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);
});
return () => {
isDisposed = true;
};
}, []);
return null;
}