From 55cf5f71c950430f1b2bcfa023b0011793bf1ea8 Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Wed, 18 Nov 2026 10:03:53 +0100 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F --- pkgs/router-webui/static/live.js | 7 ++- pkgs/router-webui/static/style.css | 5 ++ pkgs/router-webui/static/upload.js | 69 +++++++++++++++++++++++++ pkgs/router-webui/templates/layout.html | 1 + pkgs/router-webui/templates/update.html | 13 +++-- pkgs/router-webui/update.go | 30 ++++++++++- pkgs/router-webui/update_test.go | 19 +++++++ 7 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 pkgs/router-webui/static/upload.js diff --git a/pkgs/router-webui/static/live.js b/pkgs/router-webui/static/live.js index 2e6cfb5..b1030a4 100644 --- a/pkgs/router-webui/static/live.js +++ b/pkgs/router-webui/static/live.js @@ -14,12 +14,17 @@ setTimeout(function () { fetch(location.pathname + location.search,{ credentials: "same-origin", headers: { "X-Live": "1" } }) .then(function (r) { - if (r.redirected || !r.ok) throw new Error("not the page"); + // Sent elsewhere: the web UI restarted and forgot the session. Show where it leads. + if (r.redirected) { location.reload(); return null; } + if (!r.ok) throw new Error("not the page"); return r.text(); }) .then(function (html) { + if (html === null) return; var next = new DOMParser().parseFromString(html, "text/html").getElementById("live"); if (!next) throw new Error("no content"); + // Done: load the page whole, as it is when nothing is going on. + if (!next.getAttribute("data-live")) { location.reload(); return; } var old = live.querySelector("pre.log"); var atEnd = !old || old.scrollTop + old.clientHeight >= old.scrollHeight - 8; var kept = old ? old.scrollTop : 0; diff --git a/pkgs/router-webui/static/style.css b/pkgs/router-webui/static/style.css index f9e825e..15d9088 100644 --- a/pkgs/router-webui/static/style.css +++ b/pkgs/router-webui/static/style.css @@ -58,6 +58,11 @@ h1 { font-size: 1.45rem; font-weight: 600; letter-spacing: -.01em; margin: .25re h2 { font-size: .95rem; font-weight: 600; margin: 0 0 .6rem; } section { margin: 0 0 1.5rem; } section.busy, section.pending { padding: .75rem 1rem; border-radius: var(--radius); background: var(--warn); } +.upload-overlay { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, .55); padding: 1rem; } +.upload-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 32px rgba(0, 0, 0, .35); padding: 1.25rem 1.5rem; width: min(30rem, 100%); } +.upload-card h2 { margin: 0 0 .75rem; } +.upload-card progress { width: 100%; height: 1rem; accent-color: var(--accent); } +.upload-card p { margin: .6rem 0 0; font-size: .9rem; } .flash, .error { padding: .6rem .9rem; border-radius: var(--radius); margin: 0 0 1rem; } .flash { background: var(--ok); } .error { background: color-mix(in srgb, var(--bad) 12%, transparent); } diff --git a/pkgs/router-webui/static/upload.js b/pkgs/router-webui/static/upload.js new file mode 100644 index 0000000..25a3a23 --- /dev/null +++ b/pkgs/router-webui/static/upload.js @@ -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)); + }); +})(); diff --git a/pkgs/router-webui/templates/layout.html b/pkgs/router-webui/templates/layout.html index 676cf6e..c492002 100644 --- a/pkgs/router-webui/templates/layout.html +++ b/pkgs/router-webui/templates/layout.html @@ -44,5 +44,6 @@ + diff --git a/pkgs/router-webui/templates/update.html b/pkgs/router-webui/templates/update.html index a41011d..57e254b 100644 --- a/pkgs/router-webui/templates/update.html +++ b/pkgs/router-webui/templates/update.html @@ -10,8 +10,15 @@ {{if $d.Status.Running}} -

Updating…

Verifying, importing and building the new version. This page refreshes on its own; - the web UI restarts when the new version is switched on.

+ {{else if $d.Pending}}

Waiting for confirmation

A new configuration is active and not confirmed yet. Confirm it on the Overview, or the router @@ -23,7 +30,7 @@

A .bundle file made with nix run .#release. It is installed only if it is signed with this router's update key. The settings are kept (and migrated if the new version needs it); the new version must be confirmed afterwards, or the router goes back.

-
+

diff --git a/pkgs/router-webui/update.go b/pkgs/router-webui/update.go index d8a01b2..7593a96 100644 --- a/pkgs/router-webui/update.go +++ b/pkgs/router-webui/update.go @@ -242,8 +242,34 @@ func unitRun(unit string) UpdateStatus { func (s System) UpdateStatus() UpdateStatus { return unitRun("router-update.service") } +// updateProgress is how far router-update has come, from the last step its log +// names: a share of the whole (the build at the end takes the longest, so the +// bar is only a guide) and what it is doing. +func updateProgress(log string) (int, string) { + steps := []struct { + marker string + pct int + what string + }{ + {"router-update: verifying", 10, "Verifying the signature"}, + {"router-update: importing", 30, "Importing the release"}, + {"router-update: installed", 50, "Checking the version"}, + {"router-update: building and activating", 70, "Building and activating the new version"}, + } + pct, what := 5, "Starting" + best := -1 + for _, s := range steps { + if i := strings.LastIndex(log, s.marker); i > best { + best, pct, what = i, s.pct, s.what + } + } + return pct, what +} + type updateData struct { Installed Release + Progress int // percent, while updating + Step string // what it does now Status UpdateStatus URL string Offer *Available @@ -253,12 +279,14 @@ type updateData struct { func (a *app) updateData() updateData { st := a.sys.Status() - return updateData{ + d := updateData{ Installed: a.sys.installedRelease(), Status: a.sys.UpdateStatus(), URL: a.sys.updateURL(), Pending: st.RollbackIn > 0, } + d.Progress, d.Step = updateProgress(d.Status.Log) + return d } func (a *app) updatePage(w http.ResponseWriter, r *http.Request, sess session) { diff --git a/pkgs/router-webui/update_test.go b/pkgs/router-webui/update_test.go index d14f26b..71a9e40 100644 --- a/pkgs/router-webui/update_test.go +++ b/pkgs/router-webui/update_test.go @@ -244,3 +244,22 @@ func TestUpdateUploadAndInstall(t *testing.T) { t.Fatalf("the page lacks the saved server:\n%s", page) } } + +func TestUpdateProgress(t *testing.T) { + log := "" + last := 0 + for _, step := range []struct{ line, what string }{ + {"", "Starting"}, + {"router-update: verifying /nix/store/x-release\n", "Verifying the signature"}, + {"router-update: importing it\n", "Importing the release"}, + {"router-update: installed 0.1.0, update 0.2.0\n", "Checking the version"}, + {"router-update: building and activating 0.2.0\nbuilding the system\n", "Building and activating the new version"}, + } { + log += step.line + pct, what := updateProgress(log) + if what != step.what || pct <= last { + t.Fatalf("after %q: %d%% %q (before: %d%%)", step.line, pct, what, last) + } + last = pct + } +}