feat(about): 增强 GitHub 数据获取与降级机制

- 为用户数据请求添加本地 JSON 降级方案
- 为仓库数据请求添加本地 JSON 降级方案
- 拆分用户与仓库数据获取逻辑,提高可维护性
- 增加对 AbortError 等异常情况的处理
- 移除全局异常处理中的默认配置兜底逻辑
- 优化超时设置,区分不同资源的加载时间限制
This commit is contained in:
hehh
2025-11-29 21:02:32 +08:00
parent 1b94b8bf93
commit ccda6074be

View File

@@ -284,21 +284,53 @@ class DataManager {
try { try {
// Parallel Fetch with timeout // Parallel Fetch with timeout
const uRes = await this.fetchWithTimeout(`https://api.github.com/users/${user}`, { timeout: 1000 }); let userData, repoData;
const userData = uRes.ok ? await uRes.json() : (window.SiteConfig?.defaults?.user);
let allRepos = []; try {
let page = 1; const uRes = await this.fetchWithTimeout(`https://api.github.com/users/${user}`, { timeout: 1000 });
const perPage = 100; if (uRes.ok) {
while (page <= 50) { // 最多抓取500条直到满足条件或为空 userData = await uRes.json();
const rRes = await this.fetchWithTimeout(`https://api.github.com/users/${user}/repos?sort=pushed&direction=desc&per_page=${perPage}&page=${page}`, { timeout: 3000 }); } else {
if (!rRes.ok) break; const fallbackUser = await this.fetchWithTimeout("./data/github_user.json", { timeout: 200 });
const repos = await rRes.json(); userData = await fallbackUser.json();
if (!Array.isArray(repos) || repos.length === 0) break; }
allRepos = allRepos.concat(repos); } catch (err) {
if (repos.length < perPage || allRepos.length >= 500) break; // 足量 // Handle abort errors and other fetch errors
page++; if (err.name === 'AbortError') {
console.warn("GitHub user fetch aborted, using fallback data");
}
const fallbackUser = await this.fetchWithTimeout("./data/github_user.json", { timeout: 200 });
userData = await fallbackUser.json();
}
try {
let allRepos = [];
let page = 1;
const perPage = 100;
while (page <= 50) { // 最多抓取500条直到满足条件或为空
const rRes = await this.fetchWithTimeout(`https://api.github.com/users/${user}/repos?sort=pushed&direction=desc&per_page=${perPage}&page=${page}`, { timeout: 3000 });
if (!rRes.ok) break;
const repos = await rRes.json();
if (!Array.isArray(repos) || repos.length === 0) break;
allRepos = allRepos.concat(repos);
if (repos.length < perPage || allRepos.length >= 500) break; // 足量
page++;
}
if (allRepos.length) {
repoData = allRepos;
} else {
const fallbackRepos = await this.fetchWithTimeout("./data/github_repos.json", { timeout: 300 });
repoData = await fallbackRepos.json();
}
} catch (err) {
// Handle abort errors and other fetch errors
if (err.name === 'AbortError') {
console.warn("GitHub repos fetch aborted, using fallback data");
}
const fallbackRepos = await this.fetchWithTimeout("./data/github_repos.json", { timeout: 300 });
repoData = await fallbackRepos.json();
} }
let repoData = allRepos.length ? allRepos : (window.SiteConfig?.defaults?.repos);
// 过滤掉fork项目并按星数排序 // 过滤掉fork项目并按星数排序
if (Array.isArray(repoData)) { if (Array.isArray(repoData)) {
@@ -329,10 +361,8 @@ class DataManager {
} catch (e) { } catch (e) {
console.warn("GH API Fail", e); console.warn("GH API Fail", e);
const githubUser = await this.fetchWithTimeout("./data/github_user.json", { timeout: 1000 }); this.renderUser(window.SiteConfig?.defaults?.user);
const githubRepos = await this.fetchWithTimeout("./data/github_repos.json", { timeout: 1000 }); this.renderRepos(window.SiteConfig?.defaults?.repos);
this.renderUser(githubUser ? githubUser : window.SiteConfig?.defaults?.user);
this.renderRepos(githubRepos ? githubRepos : window.SiteConfig?.defaults?.repos);
} }
} }