akhaliq HF Staff commited on
Commit
ba1db2b
·
1 Parent(s): 4b79bb6

fix loading issue

Browse files
Files changed (1) hide show
  1. frontend/src/lib/api.ts +84 -3
frontend/src/lib/api.ts CHANGED
@@ -98,10 +98,62 @@ class ApiClient {
98
  return this.token;
99
  }
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  async getModels(): Promise<Model[]> {
 
 
 
 
 
 
 
102
  try {
 
103
  const response = await this.client.get<Model[]>('/api/models');
104
- return response.data;
 
 
 
 
 
 
 
 
105
  } catch (error: any) {
106
  // Handle connection errors gracefully
107
  const isConnectionError =
@@ -115,7 +167,13 @@ class ApiClient {
115
  error.response?.status === 502;
116
 
117
  if (isConnectionError) {
118
- // Backend is not available - return empty array instead of throwing
 
 
 
 
 
 
119
  console.warn('Backend not available, cannot load models');
120
  return [];
121
  }
@@ -125,8 +183,24 @@ class ApiClient {
125
  }
126
 
127
  async getLanguages(): Promise<{ languages: Language[] }> {
 
 
 
 
 
 
 
128
  try {
 
129
  const response = await this.client.get<{ languages: Language[] }>('/api/languages');
 
 
 
 
 
 
 
 
130
  return response.data;
131
  } catch (error: any) {
132
  // Handle connection errors gracefully
@@ -141,7 +215,14 @@ class ApiClient {
141
  error.response?.status === 502;
142
 
143
  if (isConnectionError) {
144
- // Backend is not available - return default languages instead of throwing
 
 
 
 
 
 
 
145
  console.warn('Backend not available, using default languages');
146
  return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] };
147
  }
 
98
  return this.token;
99
  }
100
 
101
+ // Cache helpers
102
+ private getCachedData<T>(key: string, maxAgeMs: number): T | null {
103
+ if (typeof window === 'undefined') return null;
104
+
105
+ try {
106
+ const cached = localStorage.getItem(key);
107
+ if (!cached) return null;
108
+
109
+ const { data, timestamp } = JSON.parse(cached);
110
+ const age = Date.now() - timestamp;
111
+
112
+ if (age > maxAgeMs) {
113
+ localStorage.removeItem(key);
114
+ return null;
115
+ }
116
+
117
+ return data;
118
+ } catch (error) {
119
+ console.error(`Failed to get cached data for ${key}:`, error);
120
+ return null;
121
+ }
122
+ }
123
+
124
+ private setCachedData<T>(key: string, data: T): void {
125
+ if (typeof window === 'undefined') return;
126
+
127
+ try {
128
+ localStorage.setItem(key, JSON.stringify({
129
+ data,
130
+ timestamp: Date.now()
131
+ }));
132
+ } catch (error) {
133
+ console.error(`Failed to cache data for ${key}:`, error);
134
+ }
135
+ }
136
+
137
  async getModels(): Promise<Model[]> {
138
+ // Check cache first (24 hour TTL)
139
+ const cached = this.getCachedData<Model[]>('anycoder_models', 24 * 60 * 60 * 1000);
140
+ if (cached) {
141
+ console.log('Using cached models:', cached.length, 'models');
142
+ return cached;
143
+ }
144
+
145
  try {
146
+ console.log('Fetching models from API...');
147
  const response = await this.client.get<Model[]>('/api/models');
148
+ const models = response.data;
149
+
150
+ // Cache the successful response
151
+ if (models && models.length > 0) {
152
+ this.setCachedData('anycoder_models', models);
153
+ console.log('Cached', models.length, 'models');
154
+ }
155
+
156
+ return models;
157
  } catch (error: any) {
158
  // Handle connection errors gracefully
159
  const isConnectionError =
 
167
  error.response?.status === 502;
168
 
169
  if (isConnectionError) {
170
+ // Try to return stale cache if available
171
+ const staleCache = this.getCachedData<Model[]>('anycoder_models', Infinity);
172
+ if (staleCache && staleCache.length > 0) {
173
+ console.warn('Backend not available, using stale cached models');
174
+ return staleCache;
175
+ }
176
+
177
  console.warn('Backend not available, cannot load models');
178
  return [];
179
  }
 
183
  }
184
 
185
  async getLanguages(): Promise<{ languages: Language[] }> {
186
+ // Check cache first (24 hour TTL)
187
+ const cached = this.getCachedData<Language[]>('anycoder_languages', 24 * 60 * 60 * 1000);
188
+ if (cached) {
189
+ console.log('Using cached languages:', cached.length, 'languages');
190
+ return { languages: cached };
191
+ }
192
+
193
  try {
194
+ console.log('Fetching languages from API...');
195
  const response = await this.client.get<{ languages: Language[] }>('/api/languages');
196
+ const languages = response.data.languages;
197
+
198
+ // Cache the successful response
199
+ if (languages && languages.length > 0) {
200
+ this.setCachedData('anycoder_languages', languages);
201
+ console.log('Cached', languages.length, 'languages');
202
+ }
203
+
204
  return response.data;
205
  } catch (error: any) {
206
  // Handle connection errors gracefully
 
215
  error.response?.status === 502;
216
 
217
  if (isConnectionError) {
218
+ // Try to return stale cache if available
219
+ const staleCache = this.getCachedData<Language[]>('anycoder_languages', Infinity);
220
+ if (staleCache && staleCache.length > 0) {
221
+ console.warn('Backend not available, using stale cached languages');
222
+ return { languages: staleCache };
223
+ }
224
+
225
+ // Fall back to default languages
226
  console.warn('Backend not available, using default languages');
227
  return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] };
228
  }