-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathlivereload-hugo-plugin.js
48 lines (38 loc) · 1.22 KB
/
livereload-hugo-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal
navigation to another content page.
*/
function HugoReload() {}
HugoReload.identifier = 'hugoReloader';
HugoReload.version = '0.9';
HugoReload.prototype.reload = function (path, options) {
var prefix = '__hugo_navigate';
if (path.lastIndexOf(prefix, 0) !== 0) {
return false;
}
path = path.substring(prefix.length);
var portChanged = options.overrideURL && options.overrideURL != window.location.port;
if (!portChanged && window.location.pathname === path) {
window.location.reload();
} else {
if (portChanged) {
window.location = location.protocol + '//' + location.hostname + ':' + options.overrideURL + path;
} else {
window.location.pathname = path;
}
}
return true;
};
(function () {
LiveReload.addPlugin(HugoReload);
// Close the WebSocket connection when the user leaves.
window.addEventListener('pagehide', () => {
console.log('Disconnecting LiveReload');
LiveReload.connector.disconnect();
});
// Open the connection when the page is loaded or restored from bfcache.
window.addEventListener('pageshow', () => {
console.log('Connecting LiveReload');
LiveReload.connector.connect();
});
})();