刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const EMPTY_FAVICON = 'data:,';
const INVALID_BRAND_PATHS = new Set(['null', 'undefined']);
const ABSOLUTE_URL_RE = /^(?:[a-z][a-z\d+.-]*:)?\/\//i;
 
let faviconRequestId = 0;
 
function normalizeBrandPath(value?: null | string) {
  const path = value?.trim();
  return path && !INVALID_BRAND_PATHS.has(path.toLowerCase()) ? path : '';
}
 
function resolveBrandAssetUrl(value?: null | string, apiUrl = '') {
  const path = normalizeBrandPath(value);
  if (!path || ABSOLUTE_URL_RE.test(path) || path.startsWith('blob:') || path.startsWith('data:')) return path;
 
  return `${apiUrl.replace(/\/$/, '')}/${path.replace(/^\//, '')}`;
}
 
function getFaviconLink() {
  let link = document.querySelector<HTMLLinkElement>('link[rel="icon"]');
  if (link) return link;
 
  link = document.createElement('link');
  link.rel = 'icon';
  document.head.append(link);
  return link;
}
 
function preloadImage(url: string) {
  return new Promise<void>((resolve, reject) => {
    const image = new Image();
    image.addEventListener('load', () => resolve(), { once: true });
    image.addEventListener('error', () => reject(new Error(`Failed to load brand image: ${url}`)), { once: true });
    image.src = url;
  });
}
 
async function applyFavicon(configuredUrl?: null | string) {
  const requestId = ++faviconRequestId;
  const link = getFaviconLink();
  const url = normalizeBrandPath(configuredUrl);
 
  if (!url) {
    link.href = EMPTY_FAVICON;
    return;
  }
 
  try {
    await preloadImage(url);
    if (requestId === faviconRequestId) link.href = url;
  } catch {
    if (requestId === faviconRequestId) link.href = EMPTY_FAVICON;
  }
}
 
export { applyFavicon, EMPTY_FAVICON, normalizeBrandPath, resolveBrandAssetUrl };