-
Notifications
You must be signed in to change notification settings - Fork 3
/
_client.js
23 lines (23 loc) · 1.14 KB
/
_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
var port = window.__HOT_RLD_PORT__ || 9909
var es = new window.EventSource('//localhost:' port)
es.addEventListener('.js', function (event) {
console.log('[hot-rld] ' event.data ' updated')
var script = document.createElement('script')
script.type = 'text/javascript'
script.src = event.data '?cachebust=' Date.now()
var prevNode = document.querySelector('script[src^="' event.data '"]')
if (!prevNode) return console.log('[hot-rld] cannot find <script> tag to replace')
prevNode.parentNode.replaceChild(script, prevNode)
})
es.addEventListener('.css', function (event) {
console.log('[hot-rld] ' event.data ' updated')
var link = document.createElement('link')
link.rel = 'stylesheet'
link.href = event.data '?cachebust=' Date.now()
var prevNode = document.querySelector('link[href^="' event.data '"]')
if (!prevNode) return console.log('[hot-rld] cannot find <link> tag to replace')
prevNode.parentNode.insertBefore(link, prevNode.nextSibling)
function removePrevNode () { if (prevNode.parentNode) prevNode.parentNode.removeChild(prevNode) }
link.onload = removePrevNode
setTimeout(removePrevNode, 500)
})