feat: Add live-reload

This commit is contained in:
Sidharth Vinod
2023-08-13 18:51:02 +05:30
parent 350aaf9b51
commit 5e210753b7
4 changed files with 105 additions and 18 deletions

View File

@@ -30,5 +30,33 @@ graph TB
console.log(svg);
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 = (error) => {
console.error(error);
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) {
connectToEvents();
}
</script>
</body>
</html>