Skip to content

Commit fd773eb

Browse files
author
Olivier Bonnaure
committed
feat: improve live_reload
1 parent a48c29a commit fd773eb

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

public/live_reload.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* when using the beans CLI development server.
55
*/
66

7-
(function() {
7+
(function () {
88
'use strict';
99

1010
// Configuration
@@ -29,7 +29,9 @@
2929
z-index: 9999;
3030
transition: background-color 0.3s ease;
3131
`;
32-
document.body.appendChild(indicator);
32+
if (document.body !== null) {
33+
document.body.appendChild(indicator);
34+
}
3335
return indicator;
3436
}
3537

@@ -44,8 +46,13 @@
4446
// Reload stylesheets to force CSS refresh
4547
function reloadStylesheets() {
4648
document.querySelectorAll('link[rel="stylesheet"]').forEach(link => {
47-
const newHref = link.href.replace(/\?.*$/, '') + '?v=' + Date.now();
48-
link.href = newHref;
49+
try {
50+
const url = new URL(link.href);
51+
url.searchParams.set('v', Date.now());
52+
link.href = url.toString();
53+
} catch (e) {
54+
console.error('Failed to reload stylesheet:', link.href, e);
55+
}
4956
});
5057
}
5158

@@ -62,11 +69,11 @@
6269
Window.lob_ws = new WebSocket(WS_URL);
6370
}
6471

65-
Window.lob_ws.onopen = function() {
72+
Window.lob_ws.onopen = function () {
6673
updateStatus(true);
6774
};
6875

69-
Window.lob_ws.onmessage = async function(event) {
76+
Window.lob_ws.onmessage = async function (event) {
7077
try {
7178
const data = JSON.parse(event.data);
7279
if (data.type === 'file_changed') {
@@ -103,20 +110,22 @@
103110
document.body.appendChild(newScript);
104111
});
105112
}
106-
}, 100);
113+
// Run any script here
114+
try { hljs.highlightAll(); } catch (e) { console.log(e); }
115+
}, 1);
107116
}
108117
}
109118
} catch (e) {
110119
console.log('Received:', event.data);
111120
}
112121
};
113122

114-
Window.lob_ws.onclose = function() {
123+
Window.lob_ws.onclose = function () {
115124
updateStatus(false);
116125
scheduleReconnect();
117126
};
118127

119-
Window.lob_ws.onerror = function(error) {
128+
Window.lob_ws.onerror = function (error) {
120129
updateStatus(false);
121130
scheduleReconnect();
122131
};

0 commit comments

Comments
 (0)