|
1 | 1 | // Fetch and display project metadata |
2 | 2 | (async function () { |
| 3 | + // Helper to safely get element and set text |
| 4 | + function safeSetText(elementId, text) { |
| 5 | + try { |
| 6 | + const el = document.getElementById(elementId); |
| 7 | + if (el) { |
| 8 | + el.textContent = text; |
| 9 | + } |
| 10 | + } catch (e) { |
| 11 | + console.warn("Failed to update element:", elementId, e); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + // Helper to set default values |
| 16 | + function setDefaults() { |
| 17 | + safeSetText("crates-downloads", "-"); |
| 18 | + safeSetText("github-stars", "-"); |
| 19 | + safeSetText("github-downloads", "-"); |
| 20 | + safeSetText("github-releases", "-"); |
| 21 | + } |
| 22 | + |
3 | 23 | try { |
4 | 24 | // Fetch metadata from our API endpoint (generated during build) |
5 | 25 | const basePath = |
6 | 26 | window.location.pathname.split("/").slice(0, -1).join("/") || ""; |
7 | | - const response = await fetch(`${basePath}/metadata.json`); |
| 27 | + const metadataUrl = `${basePath}/metadata.json`; |
| 28 | + |
| 29 | + // Use fetch with timeout |
| 30 | + const controller = new AbortController(); |
| 31 | + const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout |
| 32 | + |
| 33 | + const response = await fetch(metadataUrl, { |
| 34 | + signal: controller.signal, |
| 35 | + cache: 'no-cache' |
| 36 | + }); |
| 37 | + |
| 38 | + clearTimeout(timeoutId); |
| 39 | + |
8 | 40 | if (!response.ok) { |
9 | | - throw new Error("Failed to fetch metadata"); |
| 41 | + throw new Error(`Failed to fetch metadata: ${response.status} ${response.statusText}`); |
10 | 42 | } |
11 | 43 |
|
12 | 44 | const metadata = await response.json(); |
13 | 45 |
|
14 | 46 | // Update crates.io downloads |
15 | | - const cratesEl = document.getElementById("crates-downloads"); |
16 | | - if (cratesEl && metadata.crates_downloads) { |
17 | | - cratesEl.textContent = formatNumber(metadata.crates_downloads); |
| 47 | + if (metadata.crates_downloads !== undefined) { |
| 48 | + safeSetText("crates-downloads", formatNumber(metadata.crates_downloads)); |
18 | 49 | } |
19 | 50 |
|
20 | 51 | // Update GitHub stars |
21 | | - const starsEl = document.getElementById("github-stars"); |
22 | | - if (starsEl && metadata.github_stars) { |
23 | | - starsEl.textContent = formatNumber(metadata.github_stars); |
| 52 | + if (metadata.github_stars !== undefined) { |
| 53 | + safeSetText("github-stars", formatNumber(metadata.github_stars)); |
24 | 54 | } |
25 | 55 |
|
26 | 56 | // Update GitHub binary downloads |
27 | | - const downloadsEl = document.getElementById("github-downloads"); |
28 | | - if (downloadsEl && metadata.github_binary_downloads) { |
29 | | - downloadsEl.textContent = formatNumber(metadata.github_binary_downloads); |
| 57 | + if (metadata.github_binary_downloads !== undefined) { |
| 58 | + safeSetText("github-downloads", formatNumber(metadata.github_binary_downloads)); |
30 | 59 | } |
31 | 60 |
|
32 | 61 | // Update GitHub releases |
33 | | - const releasesEl = document.getElementById("github-releases"); |
34 | | - if (releasesEl && metadata.github_releases) { |
35 | | - releasesEl.textContent = formatNumber(metadata.github_releases); |
| 62 | + if (metadata.github_releases !== undefined) { |
| 63 | + safeSetText("github-releases", formatNumber(metadata.github_releases)); |
36 | 64 | } |
37 | 65 | } catch (error) { |
38 | | - console.error("Error loading metadata:", error); |
39 | | - // Set default values on error |
40 | | - document.getElementById("crates-downloads").textContent = "-"; |
41 | | - document.getElementById("github-stars").textContent = "-"; |
42 | | - document.getElementById("github-downloads").textContent = "-"; |
43 | | - document.getElementById("github-releases").textContent = "-"; |
| 66 | + // Log error but don't break the page |
| 67 | + if (error.name !== 'AbortError') { |
| 68 | + console.warn("Error loading metadata (this is non-critical):", error.message); |
| 69 | + } |
| 70 | + // Set default values on error - these are already "-" by default in HTML |
| 71 | + setDefaults(); |
44 | 72 | } |
45 | 73 | })(); |
46 | 74 |
|
|
0 commit comments