Skip to content

Instantly share code, notes, and snippets.

@Blazing-Mike
Created February 26, 2024 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blazing-Mike/f3fe272a796fce9b9d6661e2e5832788 to your computer and use it in GitHub Desktop.
Save Blazing-Mike/f3fe272a796fce9b9d6661e2e5832788 to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react';
async function performNetworkCheck() {
const endpoint = 'https://www.google.com';
try {
const response = await fetch(endpoint, {
method: 'HEAD',
cache: 'no-cache',
mode: 'no-cors',
});
return response.ok;
} catch (error) {
throw error; // Rethrow for retries
}
}
function useOfflineCheck(options : any = {}) {
const {
// Optional configuration properties:
threshold = 1000, // Minimum interval between offline checks (ms)
maxRetries = 3, // Maximum number of retries before assuming offline
retryDelay = 1500, // Delay between retries (ms)
} = options;
const [isOffline, setIsOffline] = useState(false);
const [retries, setRetries] = useState(0);
useEffect(() => {
const handleOnline = () => {
setIsOffline(false);
setRetries(0); // Reset retries on reconnection
};
const handleOffline = () => {
if (retries < maxRetries) {
setTimeout(() => {
performNetworkCheck()
.then(() => handleOnline()) // online
.catch(() => {
setRetries(retries + 1); // Retry if error occurs
});
}, retryDelay);
} else {
setIsOffline(true);
}
};
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
// Initial network check:
performNetworkCheck()
.then(() => {
setIsOffline(false);
setRetries(0);
})
.catch(() => {
setIsOffline(true);
});
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, [threshold, maxRetries, retryDelay, retries]);
return isOffline;
}
export default useOfflineCheck;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment