Skip to content

Commit 6cbe3a1

Browse files
committed
refactor: optimize version retrieval logic
1 parent b0bc963 commit 6cbe3a1

File tree

2 files changed

+19
-49
lines changed

2 files changed

+19
-49
lines changed

frontend/src/pages/DownloadPage.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ type VersionItem = {
4646
timestamp?: number;
4747
};
4848

49-
const GITHUB_DB_URL =
50-
"https://raw.githubusercontent.com/LiteLDev/minecraft-windows-gdk-version-db/refs/heads/main/historical_versions.json";
51-
const GITCODE_DB_URL =
52-
"https://github.bibk.top/LiteLDev/minecraft-windows-gdk-version-db/raw/refs/heads/main/historical_versions.json";
53-
5449
export const DownloadPage: React.FC = () => {
5550
const { t, i18n } = useTranslation();
5651
const navigate = useNavigate();
@@ -182,27 +177,7 @@ export const DownloadPage: React.FC = () => {
182177
}
183178
}, [i18n?.language]);
184179

185-
const pickDbUrl = useMemo(
186-
() => (isChinaUser ? GITCODE_DB_URL : GITHUB_DB_URL),
187-
[isChinaUser]
188-
);
189180

190-
const fetchDbJson = async (): Promise<any> => {
191-
const first = pickDbUrl;
192-
const second = first === GITCODE_DB_URL ? GITHUB_DB_URL : GITCODE_DB_URL;
193-
const tried: string[] = [];
194-
for (const url of [first, second]) {
195-
if (!url) continue;
196-
tried.push(url);
197-
try {
198-
const res = await fetch(url, { cache: "no-store" });
199-
if (res.ok) {
200-
return await res.json();
201-
}
202-
} catch {}
203-
}
204-
throw new Error(`All mirrors failed: ${tried.join(", ")}`);
205-
};
206181

207182
const startMirrorTests = async (urls: string[]) => {
208183
if (!urls || urls.length === 0) return;
@@ -424,7 +399,8 @@ export const DownloadPage: React.FC = () => {
424399
) {
425400
data = await minecraft.FetchHistoricalVersions(Boolean(isChinaUser));
426401
} else {
427-
data = await fetchDbJson();
402+
// data = await fetchDbJson();
403+
data = { previewVersions: [], releaseVersions: [] };
428404
}
429405
const preview: VersionItem[] = (data.previewVersions || []).map(
430406
(v: any) => ({
@@ -452,7 +428,6 @@ export const DownloadPage: React.FC = () => {
452428
} catch {}
453429
} catch (e) {
454430
console.error("Failed to fetch versions", e);
455-
setItems([]);
456431
}
457432
};
458433
try {
@@ -474,7 +449,7 @@ export const DownloadPage: React.FC = () => {
474449
) {
475450
data = await minecraft.FetchHistoricalVersions(Boolean(isChinaUser));
476451
} else {
477-
data = await fetchDbJson();
452+
data = { previewVersions: [], releaseVersions: [] };
478453
}
479454
const preview: VersionItem[] = (data.previewVersions || []).map(
480455
(v: any) => ({

internal/mcservice/system.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ type KnownFolder struct {
2020

2121
func FetchHistoricalVersions(preferCN bool) map[string]interface{} {
2222
const githubURL = "https://raw.githubusercontent.com/LiteLDev/minecraft-windows-gdk-version-db/refs/heads/main/historical_versions.json"
23-
const gitcodeURL = "https://github.bibk.top/LiteLDev/minecraft-windows-gdk-version-db/raw/refs/heads/main/historical_versions.json"
24-
urls := []string{githubURL, gitcodeURL}
23+
const proxyURL = "https://github.bibk.top/LiteLDev/minecraft-windows-gdk-version-db/raw/refs/heads/main/historical_versions.json"
24+
const gitcodeURL = "https://raw.gitcode.com/dreamguxiang/minecraft-windows-gdk-version-db/raw/main/historical_versions.json"
25+
26+
urls := []string{githubURL, proxyURL, gitcodeURL}
2527
if preferCN {
26-
urls = []string{gitcodeURL, githubURL}
28+
urls = []string{gitcodeURL, proxyURL, githubURL}
2729
}
28-
client := &http.Client{Timeout: 8 * time.Second}
30+
31+
client := &http.Client{Timeout: 5 * time.Second}
2932
var lastErr error
3033
for _, u := range urls {
3134
req, err := http.NewRequest(http.MethodGet, u, nil)
@@ -35,38 +38,30 @@ func FetchHistoricalVersions(preferCN bool) map[string]interface{} {
3538
}
3639
req.Header.Set("Accept", "application/json")
3740
req.Header.Set("Cache-Control", "no-cache")
41+
req.Header.Set("User-Agent", uarand.GetRandom())
42+
3843
resp, err := client.Do(req)
3944
if err != nil {
4045
lastErr = err
4146
continue
4247
}
48+
49+
var obj map[string]interface{}
4350
func() {
4451
defer resp.Body.Close()
4552
if resp.StatusCode != http.StatusOK {
4653
lastErr = fmt.Errorf("status %d", resp.StatusCode)
4754
return
4855
}
49-
dec := json.NewDecoder(resp.Body)
50-
var obj map[string]interface{}
51-
if derr := dec.Decode(&obj); derr != nil {
56+
if derr := json.NewDecoder(resp.Body).Decode(&obj); derr != nil {
5257
lastErr = derr
5358
return
5459
}
5560
}()
56-
if lastErr == nil {
57-
resp2, err2 := client.Get(u)
58-
if err2 != nil {
59-
lastErr = err2
60-
continue
61-
}
62-
defer resp2.Body.Close()
63-
var obj2 map[string]interface{}
64-
if derr2 := json.NewDecoder(resp2.Body).Decode(&obj2); derr2 != nil {
65-
lastErr = derr2
66-
continue
67-
}
68-
obj2["_source"] = u
69-
return obj2
61+
62+
if lastErr == nil && obj != nil {
63+
obj["_source"] = u
64+
return obj
7065
}
7166
}
7267
if lastErr != nil {

0 commit comments

Comments
 (0)