@@ -34,31 +34,52 @@ export default function Space({ theme = 'light' }) {
3434
3535 const isDark = theme === 'dark' ;
3636
37- // Fetch both ISS position + crew
38- async function fetchData ( ) {
39- try {
40- setLoading ( true ) ;
41- setError ( null ) ;
42- const [ issRes , crewRes ] = await Promise . all ( [
43- fetch ( 'http://api.open-notify.org/iss-now.json' ) ,
44- fetch ( 'http://api.open-notify.org/astros.json' )
45- ] ) ;
46- if ( ! issRes . ok || ! crewRes . ok ) throw new Error ( 'Failed to fetch' ) ;
47- const issJson = await issRes . json ( ) ;
48- const crewJson = await crewRes . json ( ) ;
49- setIss ( issJson ) ;
50- setCrew ( crewJson . people || [ ] ) ;
51- setLastUpdated ( new Date ( ) ) ;
52- } catch ( e ) {
53- setError ( e ) ;
54- } finally {
55- setLoading ( false ) ;
56- }
37+ // Fetch both ISS position + crew
38+ async function fetchData ( ) {
39+ try {
40+ setLoading ( true ) ;
41+ setError ( null ) ;
42+
43+ // Add a small delay between requests to avoid rate limiting
44+ const issRes = await fetch ( '/api/iss-now.json' ) ;
45+ if ( ! issRes . ok ) throw new Error ( 'Failed to fetch ISS position' ) ;
46+ const issJson = await issRes . json ( ) ;
47+
48+ // Wait 500ms before the second request
49+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
50+
51+ const crewRes = await fetch ( '/api/astros.json' ) ;
52+ if ( ! crewRes . ok ) throw new Error ( 'Failed to fetch crew data' ) ;
53+ const crewJson = await crewRes . json ( ) ;
54+
55+ setIss ( issJson ) ;
56+ setCrew ( crewJson . people || [ ] ) ;
57+ setLastUpdated ( new Date ( ) ) ;
58+ } catch ( e ) {
59+ setError ( e ) ;
60+ } finally {
61+ setLoading ( false ) ;
5762 }
63+ }
64+
65+ useEffect ( ( ) => {
66+ let isMounted = true ;
67+ const controller = new AbortController ( ) ;
68+
69+ const loadData = async ( ) => {
70+ if ( isMounted ) {
71+ await fetchData ( ) ;
72+ }
73+ } ;
74+
75+ loadData ( ) ;
76+
77+ return ( ) => {
78+ isMounted = false ;
79+ controller . abort ( ) ;
80+ } ;
81+ } , [ ] ) ;
5882
59- useEffect ( ( ) => {
60- fetchData ( ) ;
61- } , [ ] ) ;
6283
6384 // Proper dark/light theme colors
6485 const bgColor = isDark ? '#0f172a' : '#f8fafc' ;
0 commit comments