Update pages in place instead of reloading them

A page that is working fetches itself again and swaps only its content;
the log on the Packages page follows new text.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F
This commit is contained in:
2026-11-18 00:18:18 +01:00
co-authored by Claude Sonnet 5.5
parent 4ee28b812b
commit c2115aba03
6 changed files with 46 additions and 8 deletions
+3 -3
View File
@@ -368,7 +368,7 @@ func securityHeaders(next http.Handler) http.Handler {
type page struct {
Title string
CSRF string
Refresh int // seconds; 0 = none
Live int // seconds between updates of the content without a reload (live.js); 0 = none
Flash string
Error string
LoggedIn bool
@@ -495,9 +495,9 @@ func (a *app) overview(w http.ResponseWriter, r *http.Request, sess session) {
Data: overviewData{Status: st, Dashboard: a.dashboardData(r.URL.Query().Get("range"))}}
switch {
case st.Applying || st.ConfirmRunning:
p.Refresh = 2
p.Live = 2
case st.RollbackIn > 0:
p.Refresh = 10
p.Live = 10
}
a.render(w, "overview", p)
}
+38
View File
@@ -0,0 +1,38 @@
// A page whose #live element has data-live="N" is fetched again every N seconds
// while it says so, and only the content is replaced (no reload, so the scroll
// position and the log's place stay). A log that was at its end follows new text.
(function () {
function follow(root) {
root.querySelectorAll("pre.log").forEach(function (pre) { pre.scrollTop = pre.scrollHeight; });
}
var live = document.getElementById("live");
if (!live) return;
follow(live);
function poll() {
var secs = parseInt(live.getAttribute("data-live"), 10);
if (!secs) return;
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");
return r.text();
})
.then(function (html) {
var next = new DOMParser().parseFromString(html, "text/html").getElementById("live");
if (!next) throw new Error("no content");
var old = live.querySelector("pre.log");
var atEnd = !old || old.scrollTop + old.clientHeight >= old.scrollHeight - 8;
var kept = old ? old.scrollTop : 0;
live.innerHTML = next.innerHTML;
if (next.getAttribute("data-live")) live.setAttribute("data-live", next.getAttribute("data-live"));
else live.removeAttribute("data-live");
var pre = live.querySelector("pre.log");
if (pre) pre.scrollTop = atEnd ? pre.scrollHeight : kept;
})
// The web UI restarts when an upgrade is switched on: keep trying.
.catch(function () {})
.then(poll);
}, secs * 1000);
}
poll();
})();
+2 -2
View File
@@ -3,7 +3,6 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{if .Refresh}}<meta http-equiv="refresh" content="{{.Refresh}}">{{end}}
<title>{{.Title}} · router</title>
<link rel="stylesheet" href="/static/style.css">
</head>
@@ -41,8 +40,9 @@
<h1>{{.Title}}</h1>
{{if .Flash}}<p class="flash">{{.Flash}}</p>{{end}}
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
{{template "content" .}}
<div id="live"{{if .Live}} data-live="{{.Live}}"{{end}}>{{template "content" .}}</div>
</main>
<script src="/static/menu.js"></script>
<script src="/static/live.js"></script>
</body>
</html>
+1 -1
View File
@@ -26,7 +26,7 @@
{{if $d.Updating}}
<section class="busy"><h2>Updating…</h2><p>The router is installing a new version of nixos-router.</p></section>
{{else if $d.Run.Running}}
<section class="busy"><h2>Working…</h2><p>This page refreshes on its own. The web UI restarts when an upgrade is switched on.</p></section>
<section class="busy"><h2>Working…</h2><p>This page updates on its own. The web UI restarts when an upgrade is switched on.</p></section>
{{else if $d.Pending}}
<section class="pending"><h2>Waiting for confirmation</h2>
<p>A new system is active and not confirmed yet. Confirm it on the <a href="/">Overview</a>, or the router goes back to the
+1 -1
View File
@@ -265,7 +265,7 @@ func (a *app) updatePage(w http.ResponseWriter, r *http.Request, sess session) {
d := a.updateData()
p := page{Title: "Update", CSRF: sess.csrf, LoggedIn: true, Data: d, Flash: r.URL.Query().Get("msg")}
if d.Status.Running {
p.Refresh = 3
p.Live = 3
}
a.render(w, "update", p)
}
+1 -1
View File
@@ -194,7 +194,7 @@ func (a *app) upgradePage(w http.ResponseWriter, r *http.Request, sess session)
d := a.upgradeData()
p := page{Title: "Packages", CSRF: sess.csrf, LoggedIn: true, Data: d, Flash: r.URL.Query().Get("msg")}
if d.Run.Running {
p.Refresh = 3
p.Live = 3
}
a.render(w, "upgrade", p)
}