Skip to content

Instantly share code, notes, and snippets.

@fanfare
Last active August 27, 2022 14:06
Show Gist options
  • Save fanfare/edca5a90fa0d5b31eb0980293d9664af to your computer and use it in GitHub Desktop.
Save fanfare/edca5a90fa0d5b31eb0980293d9664af to your computer and use it in GitHub Desktop.
live-server bugfix: refresh CSS and maintain scroll position
<!-- Code injected by live-server -->
<script type="text/javascript">
// <![CDATA[ <-- For SVG support
if ('WebSocket' in window) {
(function() {
function refreshCSS() {
var sheets = [].slice.call(document.getElementsByTagName("link"));
var head = document.getElementsByTagName("head")[0];
let target = sheets.length
for (var i = 0; i < target; ++i) {
var elem = (sheets[i]).cloneNode(true);
var rel = elem.rel;
if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
}
head.insertAdjacentHTML("beforeend", elem.outerHTML);
((i)=>{
setTimeout(function(){
head.removeChild(sheets[i]);
}, 100)
})(i)
}
}
var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
var address = protocol + window.location.host + window.location.pathname + '/ws';
var socket = new WebSocket(address);
socket.onmessage = function(msg) {
if (msg.data == 'reload') window.location.reload();
else if (msg.data == 'refreshcss') refreshCSS();
};
})();
}
// ]]>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment