forked from sveltejs/sapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsapper-dev-client.js
47 lines (36 loc) · 1.04 KB
/
sapper-dev-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
let source;
function check() {
if (typeof module === 'undefined') return;
if (module.hot.status() === 'idle') {
module.hot.check(true).then(modules => {
console.log(`[SAPPER] applied HMR update`);
});
}
}
export function connect(port) {
if (source || !window.EventSource) return;
source = new EventSource(`http://${window.location.hostname}:${port}/__sapper__`);
window.source = source;
source.onopen = function(event) {
console.log(`[SAPPER] dev client connected`);
};
source.onerror = function(error) {
console.error(error);
};
source.onmessage = function(event) {
const data = JSON.parse(event.data);
if (!data) return; // just a heartbeat
if (data.action === 'reload') {
window.location.reload();
}
if (data.status === 'completed') {
check();
}
};
// Close the event source before the window is unloaded to prevent an error
// ("The connection was interrupted while the page was loading.") in Firefox
// when the page is reloaded.
window.addEventListener('beforeunload', function() {
source.close();
});
}