Vite server has a middleware named viteHMRPingMiddleware
in this source code
So we have a simple way to check if Vite HMR server is running by calling a ping http request to the url endpoint (with timeout 1s)
function checkViteHMR() {
fetch('http://localhost:5173/_ping', {
method: 'HEAD',
headers: {
'Accept': 'text/x-vite-ping'
}
})
.then(response => {
if (response.status === 204) {
// Do something here
console.log('Vite HMR is running');
} else {
console.log('Received response with status:', response.status);
}
})
.catch(error => {
console.error('Error making request:', error);
});
}