mirror of
https://github.com/afoim/fuwari.git
synced 2026-01-31 00:53:19 +08:00
refactor(umami): 重构 Umami 数据获取逻辑并添加缓存机制
将重复的 Umami 数据获取逻辑提取为全局工具函数 添加本地缓存和并发请求处理 增加 401 错误时的自动重试机制
This commit is contained in:
47
public/js/umami-share.js
Normal file
47
public/js/umami-share.js
Normal file
@@ -0,0 +1,47 @@
|
||||
(function (global) {
|
||||
const cacheKey = 'umami-share-cache';
|
||||
const cacheTTL = 3600_000; // 1h
|
||||
|
||||
async function fetchShareData(baseUrl, shareId) {
|
||||
const cached = localStorage.getItem(cacheKey);
|
||||
if (cached) {
|
||||
try {
|
||||
const parsed = JSON.parse(cached);
|
||||
if (Date.now() - parsed.timestamp < cacheTTL) {
|
||||
return parsed.value;
|
||||
}
|
||||
} catch {
|
||||
localStorage.removeItem(cacheKey);
|
||||
}
|
||||
}
|
||||
const res = await fetch(`${baseUrl}/api/share/${shareId}`);
|
||||
if (!res.ok) {
|
||||
throw new Error('获取 Umami 分享信息失败');
|
||||
}
|
||||
const data = await res.json();
|
||||
localStorage.setItem(cacheKey, JSON.stringify({ timestamp: Date.now(), value: data }));
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Umami 分享数据(websiteId、token)。
|
||||
* 在缓存 TTL 内复用;并用全局 Promise 避免并发请求。
|
||||
* @param {string} baseUrl
|
||||
* @param {string} shareId
|
||||
* @returns {Promise<{websiteId: string, token: string}>}
|
||||
*/
|
||||
global.getUmamiShareData = function (baseUrl, shareId) {
|
||||
if (!global.__umamiSharePromise) {
|
||||
global.__umamiSharePromise = fetchShareData(baseUrl, shareId).catch((err) => {
|
||||
delete global.__umamiSharePromise;
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
return global.__umamiSharePromise;
|
||||
};
|
||||
|
||||
global.clearUmamiShareCache = function () {
|
||||
localStorage.removeItem(cacheKey);
|
||||
delete global.__umamiSharePromise;
|
||||
};
|
||||
})(window);
|
||||
@@ -107,13 +107,8 @@ const { remarkPluginFrontmatter } = await entry.render();
|
||||
}
|
||||
|
||||
try {
|
||||
// 第一步:获取网站ID和token
|
||||
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
||||
if (!shareResponse.ok) {
|
||||
throw new Error('获取分享信息失败');
|
||||
}
|
||||
const shareData = await shareResponse.json();
|
||||
const { websiteId, token } = shareData;
|
||||
// 调用全局工具获取 Umami 分享数据
|
||||
const { websiteId, token } = await getUmamiShareData(umamiConfig.baseUrl, umamiConfig.shareId);
|
||||
|
||||
// 第二步:获取统计数据
|
||||
const currentTimestamp = Date.now();
|
||||
@@ -125,6 +120,11 @@ const { remarkPluginFrontmatter } = await entry.render();
|
||||
}
|
||||
});
|
||||
|
||||
if (statsResponse.status === 401) {
|
||||
clearUmamiShareCache();
|
||||
return await fetchPostCardViews(slug);
|
||||
}
|
||||
|
||||
if (!statsResponse.ok) {
|
||||
throw new Error('获取统计数据失败');
|
||||
}
|
||||
|
||||
@@ -97,19 +97,14 @@ const className = Astro.props.class;
|
||||
{slug && (
|
||||
<script define:vars={{ slug, umamiConfig }}>
|
||||
// 获取访问量统计
|
||||
async function fetchPageViews() {
|
||||
async function fetchPageViews(isRetry = false) {
|
||||
if (!umamiConfig.enable) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 第一步:获取网站ID和token
|
||||
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
||||
if (!shareResponse.ok) {
|
||||
throw new Error('获取分享信息失败');
|
||||
}
|
||||
const shareData = await shareResponse.json();
|
||||
const { websiteId, token } = shareData;
|
||||
// 调用全局工具获取 Umami 分享数据
|
||||
const { websiteId, token } = await getUmamiShareData(umamiConfig.baseUrl, umamiConfig.shareId);
|
||||
|
||||
// 第二步:获取统计数据
|
||||
const currentTimestamp = Date.now();
|
||||
@@ -122,6 +117,10 @@ const className = Astro.props.class;
|
||||
});
|
||||
|
||||
if (!statsResponse.ok) {
|
||||
if (statsResponse.status === 401 && !isRetry) {
|
||||
clearUmamiShareCache();
|
||||
return fetchPageViews(true);
|
||||
}
|
||||
throw new Error('获取统计数据失败');
|
||||
}
|
||||
|
||||
|
||||
@@ -53,13 +53,8 @@ const config = profileConfig;
|
||||
}
|
||||
|
||||
try {
|
||||
// 第一步:获取网站ID和token
|
||||
const shareResponse = await fetch(`${umamiConfig.baseUrl}/api/share/${umamiConfig.shareId}`);
|
||||
if (!shareResponse.ok) {
|
||||
throw new Error('获取分享信息失败');
|
||||
}
|
||||
const shareData = await shareResponse.json();
|
||||
const { websiteId, token } = shareData;
|
||||
// 调用全局工具获取 Umami 分享数据
|
||||
const { websiteId, token } = await getUmamiShareData(umamiConfig.baseUrl, umamiConfig.shareId);
|
||||
|
||||
// 第二步:获取全站统计数据(不指定url参数获取全站数据)
|
||||
const currentTimestamp = Date.now();
|
||||
@@ -71,6 +66,12 @@ const config = profileConfig;
|
||||
}
|
||||
});
|
||||
|
||||
if (statsResponse.status === 401) {
|
||||
// token 失效,清理缓存后重新获取一次
|
||||
clearUmamiShareCache();
|
||||
return await loadSiteStats();
|
||||
}
|
||||
|
||||
if (!statsResponse.ok) {
|
||||
throw new Error('获取统计数据失败');
|
||||
}
|
||||
|
||||
@@ -113,6 +113,7 @@ const bannerOffset =
|
||||
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<meta name="generator" content={Astro.generator} />
|
||||
<script src="/js/umami-share.js" defer></script>
|
||||
|
||||
{/* <!-- Content Security Policy -->
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' https://giscus.app https://hpic.072103.xyz https://umami.2x.nz https://hm.baidu.com https://www.googletagmanager.com https://www.google-analytics.com https://pagead2.googlesyndication.com https://googleads.g.doubleclick.net https://static.cloudflareinsights.com https://*.adtrafficquality.google; style-src 'self' 'unsafe-inline' https://giscus.app https://fonts.googleapis.com https://api.iconify.design; font-src 'self' https://fonts.gstatic.com https://api.iconify.design; img-src 'self' data: https: http:; connect-src 'self' https://umami.2x.nz https://hm.baidu.com https://www.google-analytics.com https://analytics.google.com https://api.iconify.design https://static.cloudflareinsights.com https://pic.2x.nz https://q2.qlogo.cn https://ep1.adtrafficquality.google https://googleads.g.doubleclick.net; frame-src 'self' https://giscus.app *.bilibili.com https://www.google.com https://googleads.g.doubleclick.net https://support.nodeget.com https://*.adtrafficquality.google; object-src 'none'; base-uri 'self'; form-action 'self';"> */}
|
||||
|
||||
Reference in New Issue
Block a user