Update templates/index.html
Browse files- templates/index.html +61 -3
templates/index.html
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="fa" dir="rtl">
|
| 3 |
<head>
|
|
@@ -653,6 +671,39 @@
|
|
| 653 |
|
| 654 |
btnRestart.addEventListener('click', initializeForm);
|
| 655 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
|
| 657 |
const proceedWithVideoGeneration = async () => {
|
| 658 |
const persianPrompt = promptInput.value.trim();
|
|
@@ -696,7 +747,8 @@
|
|
| 696 |
const uploadFile = async (file, name) => {
|
| 697 |
const formData = new FormData();
|
| 698 |
formData.append('files', file);
|
| 699 |
-
|
|
|
|
| 700 |
if (!response.ok) throw new Error(`خطا در آپلود ${name}: ${response.statusText}`);
|
| 701 |
return (await response.json())[0];
|
| 702 |
};
|
|
@@ -721,7 +773,8 @@
|
|
| 721 |
],
|
| 722 |
"fn_index": VIDEO_FN_INDEX, "session_hash": sessionHash
|
| 723 |
};
|
| 724 |
-
|
|
|
|
| 725 |
method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(joinPayload)
|
| 726 |
});
|
| 727 |
if (!joinResponse.ok) throw new Error(`خطا در اتصال به صف: ${joinResponse.statusText}`);
|
|
@@ -788,7 +841,12 @@
|
|
| 788 |
};
|
| 789 |
|
| 790 |
} catch (errorCaught) {
|
| 791 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 792 |
generateButton.disabled = false;
|
| 793 |
}
|
| 794 |
};
|
|
|
|
| 1 |
+
با توجه به توضیحات شما، مشکل "Failed to fetch" به احتمال زیاد به دلیل یک مشکل ارتباطی موقتی با سرور پس از بروز خطای قبلی یا ناپایداری لحظهای در اتصال اینترنت شما رخ میدهد. برای مقاومسازی کد و ارائه بازخورد بهتر به کاربر، تغییرات زیر را در کد شما اعمال کردهام:
|
| 2 |
+
|
| 3 |
+
۱. پیادهسازی مکانیزم تلاش مجدد خودکار (Retry Mechanism): یک تابع جدید به نام fetchWithRetry اضافه شده است. این تابع در صورت بروز خطای "Failed to fetch"، به طور خودکار تا ۳ بار دیگر با فاصله زمانی فزاینده برای برقراری ارتباط تلاش میکند. این کار مقاومت کد را در برابر مشکلات لحظهای شبکه به شدت افزایش میدهد.
|
| 4 |
+
|
| 5 |
+
۲. بهبود پیام خطا: بلوک catch اصلی در کد اصلاح شده است تا به طور مشخص خطای "Failed to fetch" را شناسایی کند. در صورت بروز این خطا، به جای نمایش پیام فنی، یک پیام واضح و کاربردی به زبان فارسی نمایش داده میشود که از کاربر میخواهد اتصال اینترنت خود را بررسی کرده و مجدداً تلاش کند.
|
| 6 |
+
|
| 7 |
+
۳. بهکارگیری مکانیزم تلاش مجدد: تمام درخواستهای کلیدی شبکه در فرآیند ساخت ویدیو (آپلود فایلها و ارسال درخواست به صف پردازش) اکنون از تابع fetchWithRetry استفاده میکنند تا از قابلیت تلاش مجدد خودکار بهرهمند شوند.
|
| 8 |
+
|
| 9 |
+
این تغییرات باعث میشود که برنامه شما تجربه کاربری بهتری داشته باشد، کمتر با خطاهای ارتباطی مواجه شود و در صورت بروز مشکل، راهنمایی واضحتری به کاربر ارائه دهد.
|
| 10 |
+
|
| 11 |
+
در ادامه کد اصلاحشده را مشاهده میکنید:
|
| 12 |
+
|
| 13 |
+
code
|
| 14 |
+
Html
|
| 15 |
+
download
|
| 16 |
+
content_copy
|
| 17 |
+
expand_less
|
| 18 |
+
|
| 19 |
<!DOCTYPE html>
|
| 20 |
<html lang="fa" dir="rtl">
|
| 21 |
<head>
|
|
|
|
| 671 |
|
| 672 |
btnRestart.addEventListener('click', initializeForm);
|
| 673 |
|
| 674 |
+
// --- START: NEW AND IMPROVED CODE ---
|
| 675 |
+
/**
|
| 676 |
+
* Attempts to fetch a resource with automatic retries on network errors.
|
| 677 |
+
* @param {string} url - The URL to fetch.
|
| 678 |
+
* @param {object} options - Fetch options.
|
| 679 |
+
* @param {number} retries - The number of times to retry.
|
| 680 |
+
* @param {number} delay - The initial delay between retries in ms.
|
| 681 |
+
* @param {number} backoff - The multiplier for the delay.
|
| 682 |
+
* @returns {Promise<Response>}
|
| 683 |
+
*/
|
| 684 |
+
async function fetchWithRetry(url, options, retries = 3, delay = 1000, backoff = 2) {
|
| 685 |
+
let lastError;
|
| 686 |
+
for (let i = 0; i < retries; i++) {
|
| 687 |
+
try {
|
| 688 |
+
return await fetch(url, options);
|
| 689 |
+
} catch (error) {
|
| 690 |
+
lastError = error;
|
| 691 |
+
// Only retry on specific, recoverable network errors
|
| 692 |
+
if (error instanceof TypeError && error.message.includes('Failed to fetch')) {
|
| 693 |
+
if (i < retries - 1) { // Don't wait on the last attempt
|
| 694 |
+
await new Promise(resolve => setTimeout(resolve, delay));
|
| 695 |
+
delay *= backoff; // Exponential backoff
|
| 696 |
+
}
|
| 697 |
+
} else {
|
| 698 |
+
// Don't retry for other errors (e.g., HTTP 4xx/5xx). Throw immediately.
|
| 699 |
+
throw error;
|
| 700 |
+
}
|
| 701 |
+
}
|
| 702 |
+
}
|
| 703 |
+
throw lastError; // If all retries fail, throw the last captured error.
|
| 704 |
+
}
|
| 705 |
+
// --- END: NEW AND IMPROVED CODE ---
|
| 706 |
+
|
| 707 |
|
| 708 |
const proceedWithVideoGeneration = async () => {
|
| 709 |
const persianPrompt = promptInput.value.trim();
|
|
|
|
| 747 |
const uploadFile = async (file, name) => {
|
| 748 |
const formData = new FormData();
|
| 749 |
formData.append('files', file);
|
| 750 |
+
// --- MODIFIED: Use fetchWithRetry instead of fetch ---
|
| 751 |
+
const response = await fetchWithRetry(`${VIDEO_SPACE_URL}gradio_api/upload`, { method: 'POST', body: formData });
|
| 752 |
if (!response.ok) throw new Error(`خطا در آپلود ${name}: ${response.statusText}`);
|
| 753 |
return (await response.json())[0];
|
| 754 |
};
|
|
|
|
| 773 |
],
|
| 774 |
"fn_index": VIDEO_FN_INDEX, "session_hash": sessionHash
|
| 775 |
};
|
| 776 |
+
// --- MODIFIED: Use fetchWithRetry instead of fetch ---
|
| 777 |
+
const joinResponse = await fetchWithRetry(`${VIDEO_SPACE_URL}gradio_api/queue/join`, {
|
| 778 |
method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(joinPayload)
|
| 779 |
});
|
| 780 |
if (!joinResponse.ok) throw new Error(`خطا در اتصال به صف: ${joinResponse.statusText}`);
|
|
|
|
| 841 |
};
|
| 842 |
|
| 843 |
} catch (errorCaught) {
|
| 844 |
+
// --- MODIFIED: Improved error handling for fetch failures ---
|
| 845 |
+
if (errorCaught instanceof TypeError && errorCaught.message.includes('Failed to fetch')) {
|
| 846 |
+
showCriticalError("خطا در ارتباط با سرور رخ داد. لطفاً اتصال اینترنت خود را بررسی کرده و مجدداً تلاش کنید. این مشکل معمولاً موقتی است.");
|
| 847 |
+
} else {
|
| 848 |
+
showCriticalError(`خطای اولیه: ${errorCaught.message}`);
|
| 849 |
+
}
|
| 850 |
generateButton.disabled = false;
|
| 851 |
}
|
| 852 |
};
|