Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -28,7 +49,7 @@ on('/instant/events', function(ev) {

// reload the page
if (url.href == location.href) {
location.reload()
reloadPage()
return
}

Expand All @@ -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()
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down