Skip to content

Commit 718d52a

Browse files
committed
chore: Move liveReload code into script.
1 parent fe1a062 commit 718d52a

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ stats/
4646

4747
demos/dev/**
4848
!/demos/dev/example.html
49+
!/demos/dev/reload.js

demos/dev/example.html

+1-27
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,6 @@
3333
el.innerHTML = svg;
3434
</script>
3535

36-
<script>
37-
// Set to false to disable live reload
38-
const liveReload = true;
39-
// Connect to the server and reload the page if the server sends a reload message
40-
const connectToEvents = () => {
41-
const events = new EventSource('/events');
42-
const loadTime = Date.now();
43-
events.onmessage = (event) => {
44-
const time = JSON.parse(event.data);
45-
if (time && time > loadTime) {
46-
location.reload();
47-
}
48-
};
49-
events.onerror = () => {
50-
// No need to log this error, as it's not useful.
51-
events.close();
52-
// Try to reconnect after 1 second in case of errors
53-
setTimeout(connectToEvents, 1000);
54-
};
55-
events.onopen = () => {
56-
console.log('Connected to live reload server');
57-
};
58-
};
59-
if (liveReload) {
60-
setTimeout(connectToEvents, 1000);
61-
}
62-
</script>
36+
<script src="/dev/reload.js"></script>
6337
</body>
6438
</html>

demos/dev/reload.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Connect to the server and reload the page if the server sends a reload message
2+
const connectToEvents = () => {
3+
const events = new EventSource('/events');
4+
const loadTime = Date.now();
5+
events.onmessage = (event) => {
6+
const time = JSON.parse(event.data);
7+
if (time && time > loadTime) {
8+
location.reload();
9+
}
10+
};
11+
events.onerror = (error) => {
12+
console.error(error);
13+
events.close();
14+
// Try to reconnect after 1 second in case of errors
15+
setTimeout(connectToEvents, 1000);
16+
};
17+
events.onopen = () => {
18+
console.log('Connected to live reload server');
19+
};
20+
};
21+
22+
setTimeout(connectToEvents, 500);

tsconfig.eslint.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,14 @@
55
// ensure that nobody can accidentally use this config for a build
66
"noEmit": true
77
},
8-
"include": ["packages", "tests", "scripts", "cypress", "__mocks__", "./.eslintrc.cjs", "./*"]
8+
"include": [
9+
"packages",
10+
"tests",
11+
"scripts",
12+
"cypress",
13+
"__mocks__",
14+
"./.eslintrc.cjs",
15+
"./*",
16+
"demos/dev"
17+
]
918
}

0 commit comments

Comments
 (0)