diff options
-rw-r--r-- | assets/js/index.js | 10 | ||||
-rw-r--r-- | static/js/update.ts | 17 | ||||
-rw-r--r-- | themes/default/layouts/partials/navigator-middle.html | 1 |
3 files changed, 20 insertions, 8 deletions
diff --git a/assets/js/index.js b/assets/js/index.js index 83d3943..ef35f63 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -93,7 +93,7 @@ }); })(); (function() { - function check(url, method, callback) { + function fetch(url, method, callback) { const http = new XMLHttpRequest(); http.onreadystatechange = function() { if (http.readyState === 4 && http.status === 200) { @@ -108,14 +108,16 @@ } function update() { const url = self.location.href.split("#")[0].split("?")[0]; - check(url, "HEAD", function(request) { + fetch(url, "HEAD", function(request) { const local = document.querySelector('meta[name="last-modified"]').content; const remote = request.getResponseHeader("last-modified") || ''; const modified = Date.parse(document.lastModified) !== Date.parse(remote); const drift = Date.parse(remote || local) - Date.parse(local); + let indicator = document.querySelector("a[data-update]"); + indicator.cloneNode(); + if (drift === 0) return; if (remote && modified) { - const indicator = document.querySelector("a[data-update]"); - check(url, "GET"); + fetch(url, "GET"); indicator.href = url; indicator.removeAttribute("id"); indicator.dataset.update = "refresh"; diff --git a/static/js/update.ts b/static/js/update.ts index cee1c92..94a509a 100644 --- a/static/js/update.ts +++ b/static/js/update.ts @@ -1,5 +1,5 @@ (function () { - function check(url, method, callback) { + function fetch(url, method, callback) { const http = new XMLHttpRequest(); http.onreadystatechange = function () { if (http.readyState === 4 && http.status === 200) { @@ -15,15 +15,24 @@ function update() { const url = self.location.href.split("#")[0].split("?")[0]; - check(url, "HEAD", function (request) { + fetch(url, "HEAD", function (request) { const local = document.querySelector('meta[name="last-modified"]').content; const remote = request.getResponseHeader("last-modified") || ''; const modified = Date.parse(document.lastModified) !== Date.parse(remote); const drift = Date.parse(remote || local) - Date.parse(local); + let indicator = document.querySelector("a[data-update]"); + const anchor = indicator.cloneNode(); + + function reset() { + indicator.href = anchor.href; + indicator.setAttribute("id", anchor.id); + indicator.dataset.update = anchor.dataset.update; + } + + if (drift === 0) return; if (remote && modified) { - const indicator = document.querySelector("a[data-update]"); - check(url, "GET"); + fetch(url, "GET"); indicator.href = url; indicator.removeAttribute("id"); indicator.dataset.update = "refresh"; diff --git a/themes/default/layouts/partials/navigator-middle.html b/themes/default/layouts/partials/navigator-middle.html index f616a2b..efe5153 100644 --- a/themes/default/layouts/partials/navigator-middle.html +++ b/themes/default/layouts/partials/navigator-middle.html @@ -18,6 +18,7 @@ data-update="" id="{{ $id }}" href="{{ $href }}" + target="_self" > {{ safeHTML $icon }} {{ safeHTML $refresh }} |