Skip to content

Commit 275d57a

Browse files
committed
Fix CORS problem hopefully (408 steam error code)
1 parent b8c5f81 commit 275d57a

3 files changed

Lines changed: 86 additions & 22 deletions

File tree

src/components/SteamWidget.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,18 @@ body.dark-mode .steam-game-fallback {
124124
}
125125

126126
.steam-loading,
127-
.steam-no-games {
127+
.steam-no-games,
128+
.steam-error {
128129
text-align: center;
129130
padding: 10px;
130131
color: #8B8B8B;
131132
font-style: italic;
133+
}
134+
135+
.steam-error {
136+
color: #E74C3C;
137+
}
138+
139+
body.dark-mode .steam-error {
140+
color: #F08080;
132141
}

src/components/SteamWidget.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import './SteamWidget.css';
55
export const SteamWidget: React.FC = () => {
66
const [games, setGames] = useState<SteamGame[]>([]);
77
const [loading, setLoading] = useState(true);
8+
const [error, setError] = useState<string | null>(null);
89

910
useEffect(() => {
1011
const loadGames = async () => {
1112
try {
1213
setLoading(true);
14+
setError(null);
1315
const recentGames = await fetchRecentGames();
1416
setGames(recentGames);
1517
} catch (error) {
1618
console.error('Error loading Steam games:', error);
19+
setError('Failed to load Steam games');
1720
} finally {
1821
setLoading(false);
1922
}
@@ -30,6 +33,11 @@ export const SteamWidget: React.FC = () => {
3033
<div className="steam-games-list">
3134
{loading ? (
3235
<div className="steam-loading">Loading games... ✨</div>
36+
) : error ? (
37+
<div className="steam-error">
38+
<div>🎮 Unable to load games right now</div>
39+
<div style={{ fontSize: '0.8em', opacity: 0.7 }}>Steam API temporarily unavailable</div>
40+
</div>
3341
) : games.length > 0 ? (
3442
games.map((game) => (
3543
<a

src/utils/steam.ts

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,69 @@ export interface SteamRecentGamesResponse {
1515
};
1616
}
1717

18+
// more cors proxy cuz reliability issues
19+
const CORS_PROXIES = [
20+
'https://api.codetabs.com/v1/proxy?quest=',
21+
'https://api.allorigins.win/get?url=',
22+
'https://corsproxy.io/?',
23+
'https://cors-anywhere.herokuapp.com/',
24+
];
25+
26+
const fetchWithProxy = async (apiUrl: string, proxyIndex = 0): Promise<any> => {
27+
if (proxyIndex >= CORS_PROXIES.length) {
28+
throw new Error('All proxy services failed');
29+
}
30+
31+
const proxy = CORS_PROXIES[proxyIndex];
32+
let proxyUrl: string;
33+
34+
// Different proxies have different URL formats
35+
if (proxy.includes('allorigins')) {
36+
proxyUrl = `${proxy}${encodeURIComponent(apiUrl)}`;
37+
} else if (proxy.includes('codetabs')) {
38+
proxyUrl = `${proxy}${encodeURIComponent(apiUrl)}`;
39+
} else {
40+
proxyUrl = `${proxy}${apiUrl}`;
41+
}
42+
43+
try {
44+
const controller = new AbortController();
45+
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
46+
47+
const response = await fetch(proxyUrl, {
48+
method: 'GET',
49+
headers: {
50+
'Accept': 'application/json',
51+
},
52+
signal: controller.signal
53+
});
54+
55+
clearTimeout(timeoutId);
56+
57+
if (!response.ok) {
58+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
59+
}
60+
61+
const data = await response.json();
62+
63+
// Handle different proxy response formats
64+
if (proxy.includes('allorigins') && data.contents) {
65+
return JSON.parse(data.contents);
66+
} else if (proxy.includes('codetabs') && data) {
67+
return data;
68+
} else if (data) {
69+
return data;
70+
} else {
71+
throw new Error('No valid data in response');
72+
}
73+
74+
} catch (error) {
75+
console.warn(`Proxy ${proxy} failed:`, error);
76+
// Try next proxy
77+
return fetchWithProxy(apiUrl, proxyIndex + 1);
78+
}
79+
};
80+
1881
/**
1982
* Fetch recently played Steam games
2083
*/
@@ -28,31 +91,15 @@ export const fetchRecentGames = async (): Promise<SteamGame[]> => {
2891
return [];
2992
}
3093

31-
// Use CORS proxy directly to avoid CORS errors
94+
// Build Steam API URL
3295
const apiUrl = `https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v1/?key=${STEAM_API_KEY}&steamid=${STEAM_ID}&format=json`;
33-
const proxyUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(apiUrl)}`;
3496

35-
const response = await fetch(proxyUrl, {
36-
method: 'GET',
37-
headers: {
38-
'Accept': 'application/json',
39-
}
40-
});
41-
42-
if (!response.ok) {
43-
throw new Error(`Proxy request failed: ${response.status}`);
44-
}
45-
46-
const proxyData = await response.json();
47-
48-
if (!proxyData.contents) {
49-
throw new Error('No data received from proxy');
50-
}
51-
52-
const steamData: SteamRecentGamesResponse = JSON.parse(proxyData.contents);
97+
// Try fetching with different proxy services
98+
const steamData: SteamRecentGamesResponse = await fetchWithProxy(apiUrl);
5399

54100
if (!steamData.response || !steamData.response.games) {
55-
throw new Error('Invalid Steam API response');
101+
console.warn('No recent games found in Steam API response');
102+
return [];
56103
}
57104

58105
return steamData.response.games.slice(0, 4); // Show top 4 games

0 commit comments

Comments
 (0)