diff --git a/client/index.js b/client/index.js index e071a3a..6d065d0 100644 --- a/client/index.js +++ b/client/index.js @@ -5,21 +5,42 @@ var on = require('sendevent') var token +function reloadPage() { + // Firefox has a quirk where it times out and disconnects an EventSource + // connection when the host (not hostname) is “localhost” and it does + // a regular reload from memory cache. Instead, we must do a forced reload from + // the server and also flag it so that sendevent doesn’t also do + // a forced reload just to be sure (and thereby adding an unnecessary reload). + // Also, when we do a forced reload, Firefox loses the current scroll position + // so we must save that so checking for the existence of the object that contains + // the location to restore is how we know that we have asked for a force reload. + if (navigator.userAgent.includes('Firefox') && document.location.host === 'localhost') { + window.locationToRestoreAfterForcedReload = { + x: window.scrollX, + y: window.scrollY + } + location.reload(true) + } else { + // In every other case, we can just do a regular reload. + location.reload() + } +} + on('/instant/events', function(ev) { if (ev.token) { if (!token) token = ev.token - if (token != ev.token) return location.reload() + if (token != ev.token) return reloadPage() } // reload page if it contains an element with the given class name if (ev.className) { - if (find.byClass(ev.className)) location.reload() + if (find.byClass(ev.className)) reloadPage() return } // reload page if it contains an element that matches the given selector if (ev.selector) { - if (find.bySelector(ev.selector)) location.reload() + if (find.bySelector(ev.selector)) reloadPage() return } @@ -28,7 +49,7 @@ on('/instant/events', function(ev) { // reload the page if (url.href == location.href) { - location.reload() + reloadPage() return } @@ -38,5 +59,5 @@ on('/instant/events', function(ev) { // look for a script el = find.byURL('script', 'src', url) - if (el) location.reload() + if (el) reloadPage() }) diff --git a/package.json b/package.json index 560be0c..3e1ab84 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "filewatcher": "^3.0.1", "node-graceful": "^2.0.1", "send": "^0.17.1", - "sendevent": "^1.0.4", + "sendevent": "^1.0.6", "stacked": "^1.1.1", "tamper": "^0.1.0" },