Show progress for uploads and updates, and reload when they are done
An uploaded bundle gets a progress bar over the page; a running update an overlay that follows its steps. A page that finished working, or that was sent to the login page by a restart, is loaded whole. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// A form marked data-upload is sent with a progress bar over the page: a
|
||||
// bundle is hundreds of megabytes. The server answers with a redirect once it
|
||||
// has taken the file; the browser follows it and we go to where it ended up.
|
||||
(function () {
|
||||
function size(n) {
|
||||
return n >= 1 << 30 ? (n / (1 << 30)).toFixed(2) + " GiB" : (n / (1 << 20)).toFixed(1) + " MiB";
|
||||
}
|
||||
|
||||
function overlay() {
|
||||
var box = document.createElement("div");
|
||||
box.className = "upload-overlay";
|
||||
box.setAttribute("role", "dialog");
|
||||
box.setAttribute("aria-modal", "true");
|
||||
var card = document.createElement("div");
|
||||
card.className = "upload-card";
|
||||
var title = document.createElement("h2");
|
||||
var bar = document.createElement("progress");
|
||||
bar.max = 100;
|
||||
var text = document.createElement("p");
|
||||
text.className = "muted";
|
||||
card.append(title, bar, text);
|
||||
box.append(card);
|
||||
document.body.append(box);
|
||||
return { box: box, title: title, bar: bar, text: text };
|
||||
}
|
||||
|
||||
document.addEventListener("submit", function (e) {
|
||||
var form = e.target;
|
||||
if (!form.hasAttribute("data-upload") || e.defaultPrevented || !window.XMLHttpRequest || !window.FormData) return;
|
||||
e.preventDefault();
|
||||
var ui = overlay();
|
||||
var started = Date.now();
|
||||
ui.title.textContent = "Uploading…";
|
||||
ui.bar.value = 0;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", form.action);
|
||||
xhr.upload.onprogress = function (ev) {
|
||||
if (!ev.lengthComputable) {
|
||||
ui.bar.removeAttribute("value");
|
||||
ui.text.textContent = size(ev.loaded) + " sent";
|
||||
return;
|
||||
}
|
||||
var secs = Math.max((Date.now() - started) / 1000, 0.5);
|
||||
var rate = ev.loaded / secs;
|
||||
ui.bar.value = (ev.loaded / ev.total) * 100;
|
||||
ui.text.textContent = size(ev.loaded) + " of " + size(ev.total) + " (" + Math.floor((ev.loaded / ev.total) * 100) + " %), " +
|
||||
size(rate) + "/s";
|
||||
};
|
||||
xhr.upload.onload = function () {
|
||||
// All sent; the router checks and unpacks it before it answers.
|
||||
ui.title.textContent = "Verifying…";
|
||||
ui.bar.removeAttribute("value");
|
||||
ui.text.textContent = "The router is checking the signature and unpacking the bundle.";
|
||||
};
|
||||
function failed(msg) {
|
||||
ui.box.remove();
|
||||
alert(msg);
|
||||
}
|
||||
xhr.onerror = function () { failed("The upload failed: the router could not be reached."); };
|
||||
xhr.onabort = function () { failed("The upload was cancelled."); };
|
||||
xhr.onload = function () {
|
||||
// The answer is the page the server redirected to (with its message).
|
||||
if (xhr.responseURL && xhr.responseURL !== location.href) location.href = xhr.responseURL;
|
||||
else location.reload();
|
||||
};
|
||||
xhr.send(new FormData(form));
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user