feat(about): 增强 GitHub 数据获取与降级机制
- 为用户数据请求添加本地 JSON 降级方案 - 为仓库数据请求添加本地 JSON 降级方案 - 拆分用户与仓库数据获取逻辑,提高可维护性 - 增加对 AbortError 等异常情况的处理 - 移除全局异常处理中的默认配置兜底逻辑 - 优化超时设置,区分不同资源的加载时间限制
This commit is contained in:
42
js/about.js
42
js/about.js
@@ -284,8 +284,26 @@ class DataManager {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Parallel Fetch with timeout
|
// Parallel Fetch with timeout
|
||||||
|
let userData, repoData;
|
||||||
|
|
||||||
|
try {
|
||||||
const uRes = await this.fetchWithTimeout(`https://api.github.com/users/${user}`, { timeout: 1000 });
|
const uRes = await this.fetchWithTimeout(`https://api.github.com/users/${user}`, { timeout: 1000 });
|
||||||
const userData = uRes.ok ? await uRes.json() : (window.SiteConfig?.defaults?.user);
|
if (uRes.ok) {
|
||||||
|
userData = await uRes.json();
|
||||||
|
} else {
|
||||||
|
const fallbackUser = await this.fetchWithTimeout("./data/github_user.json", { timeout: 200 });
|
||||||
|
userData = await fallbackUser.json();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// Handle abort errors and other fetch errors
|
||||||
|
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 allRepos = [];
|
||||||
let page = 1;
|
let page = 1;
|
||||||
const perPage = 100;
|
const perPage = 100;
|
||||||
@@ -298,7 +316,21 @@ class DataManager {
|
|||||||
if (repos.length < perPage || allRepos.length >= 500) break; // 足量
|
if (repos.length < perPage || allRepos.length >= 500) break; // 足量
|
||||||
page++;
|
page++;
|
||||||
}
|
}
|
||||||
let repoData = allRepos.length ? allRepos : (window.SiteConfig?.defaults?.repos);
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
// 过滤掉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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user