Fix network check for sync

The network check was incorrectly reporting a lack of connection when sync was enabled. This commit addresses the issue.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-22 13:41:13 +00:00
parent 06cef28b9b
commit 6af72b9150
2 changed files with 148 additions and 93 deletions

View File

@@ -9,15 +9,27 @@ import { setNetworkStatus } from './status';
*/
export const checkNetworkStatus = async (): Promise<boolean> => {
try {
// 먼저 navigator.onLine으로 기본 확인
if (navigator.onLine) {
console.log('[네트워크] 기본 온라인 상태 확인: 온라인');
setNetworkStatus('online');
return true;
}
console.log('[네트워크] 기본 온라인 상태 확인: 오프라인, 추가 확인 시도...');
// 더 빠른 타임아웃 설정 (2초)
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 2000);
// 실패 방지를 위해 작은 이미지 요청으로 변경
const timestamp = new Date().getTime();
const url = `https://www.google.com/favicon.ico?t=${timestamp}`;
// 신뢰성 있는 확인을 위해 로컬 리소스 요청
// CORS 이슈와 실패 방지를 위해 자체 페이지 내 리소스 확인
try {
// 타임스탬프로 캐시 방지
const timestamp = new Date().getTime();
// 자체 파비콘이나 작은 리소스 확인
const url = `/favicon.ico?t=${timestamp}`;
const response = await fetch(url, {
method: 'HEAD',
signal: controller.signal,
@@ -27,22 +39,31 @@ export const checkNetworkStatus = async (): Promise<boolean> => {
clearTimeout(timeoutId);
if (response.ok) {
console.log('[네트워크] 로컬 리소스 확인 성공');
setNetworkStatus('online');
return true;
} else {
console.log(`[네트워크] 상태 확인 실패: ${response.status}`);
setNetworkStatus('offline');
return false;
console.log(`[네트워크] 로컬 리소스 확인 실패: ${response.status}`);
// 여전히 온라인일 수 있으므로 navigator.onLine 신뢰
const isOnline = navigator.onLine;
setNetworkStatus(isOnline ? 'online' : 'offline');
return isOnline;
}
} catch (error) {
clearTimeout(timeoutId);
console.error('[네트워크] 네트워크 연결 확인 실패:', error);
setNetworkStatus('offline');
return false;
console.warn('[네트워크] 로컬 리소스 확인 중 오류:', error);
// 여전히 온라인일 수 있으므로 navigator.onLine 신뢰
const isOnline = navigator.onLine;
console.log(`[네트워크] navigator.onLine 결과 사용: ${isOnline ? '온라인' : '오프라인'}`);
setNetworkStatus(isOnline ? 'online' : 'offline');
return isOnline;
}
} catch (error) {
console.error('[네트워크] 연결 확인 중 예상치 못한 오류:', error);
setNetworkStatus('offline');
return false;
// 최후의 수단으로 navigator.onLine 사용
const isOnline = navigator.onLine;
setNetworkStatus(isOnline ? 'online' : 'offline');
return isOnline;
}
};