chore: Move liveReload code into script.

This commit is contained in:
Sidharth Vinod
2023-08-17 14:30:47 +05:30
parent fe1a06271a
commit 718d52a72c
4 changed files with 34 additions and 28 deletions

View File

@@ -33,32 +33,6 @@ graph TB
el.innerHTML = svg;
</script>
<script>
// Set to false to disable live reload
const liveReload = true;
// Connect to the server and reload the page if the server sends a reload message
const connectToEvents = () => {
const events = new EventSource('/events');
const loadTime = Date.now();
events.onmessage = (event) => {
const time = JSON.parse(event.data);
if (time && time > loadTime) {
location.reload();
}
};
events.onerror = () => {
// No need to log this error, as it's not useful.
events.close();
// Try to reconnect after 1 second in case of errors
setTimeout(connectToEvents, 1000);
};
events.onopen = () => {
console.log('Connected to live reload server');
};
};
if (liveReload) {
setTimeout(connectToEvents, 1000);
}
</script>
<script src="/dev/reload.js"></script>
</body>
</html>