Make the banner and sidebar counts live on every page

One 5-minute poll (/new-since/) now drives the new-articles banner, the
tab dot and the sidebar counts, across all subscriptions rather than the
selected feed/tag. The banner lives in the base layout with a
server-rendered baseline id and reloads the current view on click; new
articles are never inserted automatically.

Sidebar counts are patched in place by the poll, and immediately after
reading or starring (HX-Trigger: countsChanged). Ingestion, filter rules
and subscription changes now drop the cached counts instead of waiting
out the TTL.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5.5 committed 2026-11-03 07:39:36 -07:00
1 parent fed5bcc49e
commit 2a3e9f632e
18 files changed
+399 -196

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
// Keeps every authenticated page current without ever moving the article
// list under the reader. One poll (/new-since/) drives three things:
// - the "N new articles" banner, wherever in the sidebar tree you are;
// - a red dot on the browser tab's favicon, visible from a background tab;
// - the sidebar's unread/starred counts, patched in place.
// New articles are never inserted automatically: the banner click reloads
// the current view. A full page load resets the baseline (data-since,
// rendered into the page) — exactly when the user has seen the new articles.
// The poll deliberately keeps running in a hidden tab (that's what the dot is
// for); it also fires when the tab becomes visible again, and as soon as a
// read/star action tells us the counts changed (the countsChanged event the
// server sends via HX-Trigger).
(function () {
var POLL_MS = 300000;
var MIN_GAP_MS = 15000; // coalesce bursts (rapid reading, tab switches)
var script = document.currentScript;
var url = script && script.dataset.url;
var icon = document.querySelector('link[rel="icon"]');
var banner = document.querySelector(".new-articles-banner");
if (!url) return;
var originalHref = icon ? icon.href : null;
var baseline = parseInt(script.dataset.since, 10);
if (isNaN(baseline)) baseline = null;
var timer = null;
var inFlight = false;
var rerun = false; // a read happened while a poll was in flight: its answer may predate it
var lastPoll = 0;
var badged = null; // the dotted icon's data: URL, drawn once on first need
var showing = false;
function drawDot() {
return new Promise(function (resolve) {
var image = new Image();
image.onload = function () {
var size = 64;
var canvas = document.createElement("canvas");
canvas.width = canvas.height = size;
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0, size, size);
ctx.beginPath();
ctx.arc(size * 0.74, size * 0.26, size * 0.23, 0, 2 * Math.PI);
ctx.fillStyle = "#e53935";
ctx.fill();
ctx.lineWidth = size * 0.07;
ctx.strokeStyle = "#ffffff";
ctx.stroke();
resolve(canvas.toDataURL("image/png"));
};
image.onerror = function () {
resolve(null);
};
image.src = originalHref;
});
}
// Replace the <link> rather than editing href: some browsers ignore an
// in-place change to an existing favicon element.
function setIcon(href) {
var fresh = icon.cloneNode();
fresh.href = href;
icon.parentNode.replaceChild(fresh, icon);
icon = fresh;
}
function showDot(on) {
if (!icon || on === showing) return Promise.resolve();
showing = on;
if (!on) {
setIcon(originalHref);
return Promise.resolve();
}
var ready = badged ? Promise.resolve(badged) : drawDot();
return ready.then(function (href) {
badged = href;
if (href && showing) setIcon(href);
});
}
function setCount(el, value) {
var text = el.hasAttribute("data-starred-count") ? "(" + value + ")" : String(value);
el.textContent = value ? text : "";
el.hidden = !value;
}
function updateCounts(counts) {
if (!counts) return;
document.querySelectorAll(".unread-count[data-feed-id]").forEach(function (el) {
setCount(el, counts.feeds[el.dataset.feedId] || 0);
});
document.querySelectorAll(".unread-count[data-tag-id]").forEach(function (el) {
setCount(el, counts.tags[el.dataset.tagId] || 0);
});
document.querySelectorAll("[data-starred-count]").forEach(function (el) {
setCount(el, counts.starred || 0);
});
}
function updateBanner(count) {
if (!banner) return;
banner.hidden = !count;
if (count) {
banner.textContent =
count + " new article" + (count === 1 ? "" : "s") + " \u2014 click to refresh";
}
}
function schedule() {
clearTimeout(timer);
timer = setTimeout(poll, POLL_MS);
}
function poll() {
if (inFlight) {
rerun = true;
return;
}
inFlight = true;
lastPoll = Date.now();
var query = baseline === null ? "" : "?since=" + baseline;
fetch(url + query, { credentials: "same-origin", headers: { Accept: "application/json" } })
.then(function (response) {
// A redirect means the session ended (we'd be reading the login
// page) — stop polling instead of hammering it.
if (!response.ok || response.redirected) throw new Error("stop");
return response.json();
})
.then(function (data) {
inFlight = false;
if (baseline === null) baseline = data.latest_id;
updateCounts(data.counts);
updateBanner(data.new_count);
showDot(data.new_count > 0);
if (rerun) {
rerun = false;
poll();
} else {
schedule();
}
})
.catch(function (error) {
inFlight = false;
rerun = false;
if (error.message !== "stop") schedule();
});
}
function pollSoon() {
var wait = Math.max(0, MIN_GAP_MS - (Date.now() - lastPoll));
clearTimeout(timer);
timer = setTimeout(poll, wait);
}
document.addEventListener("visibilitychange", function () {
if (!document.hidden) pollSoon();
});
document.body.addEventListener("countsChanged", function () {
// Reading is interactive: refresh the sidebar now, not after the gap.
clearTimeout(timer);
timer = setTimeout(poll, 150);
});
poll();
})();
+9 -2
View File
@@ -41,7 +41,14 @@ document.addEventListener("click", function (event) {
toggleArticle(link);
return;
}
if (event.target.closest(".new-articles-banner")) {
location.reload();
var banner = event.target.closest(".new-articles-banner");
if (banner) {
// Reloading keeps the current filter/tag/feed in the URL; from any
// other page (settings, search) go to the stream instead.
if (location.pathname === banner.dataset.streamUrl) {
location.reload();
} else {
location.href = banner.dataset.streamUrl;
}
}
});
-85
View File
@@ -1,85 +0,0 @@
// Marks the browser tab with a red dot on its favicon when new unread
// articles have arrived since this page loaded — visible from a background
// tab, unlike the in-page "new articles" banner. Runs on every
// authenticated page; a full page load resets the baseline (and the icon),
// which is exactly when the user has seen the new articles.
(function () {
var POLL_MS = 30000;
var url = document.currentScript && document.currentScript.dataset.url;
var icon = document.querySelector('link[rel="icon"]');
if (!url || !icon) return;
var originalHref = icon.href;
var baseline = null;
var badged = null; // the dotted icon's data: URL, drawn once on first need
var showing = false;
function drawDot() {
return new Promise(function (resolve) {
var image = new Image();
image.onload = function () {
var size = 64;
var canvas = document.createElement("canvas");
canvas.width = canvas.height = size;
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0, size, size);
ctx.beginPath();
ctx.arc(size * 0.74, size * 0.26, size * 0.23, 0, 2 * Math.PI);
ctx.fillStyle = "#e53935";
ctx.fill();
ctx.lineWidth = size * 0.07;
ctx.strokeStyle = "#ffffff";
ctx.stroke();
resolve(canvas.toDataURL("image/png"));
};
image.onerror = function () {
resolve(null);
};
image.src = originalHref;
});
}
// Replace the <link> rather than editing href: some browsers ignore an
// in-place change to an existing favicon element.
function setIcon(href) {
var fresh = icon.cloneNode();
fresh.href = href;
icon.parentNode.replaceChild(fresh, icon);
icon = fresh;
}
function showDot(on) {
if (on === showing) return Promise.resolve();
showing = on;
if (!on) {
setIcon(originalHref);
return Promise.resolve();
}
var ready = badged ? Promise.resolve(badged) : drawDot();
return ready.then(function (href) {
badged = href;
if (href && showing) setIcon(href);
});
}
function poll() {
var query = baseline === null ? "" : "?since=" + baseline;
fetch(url + query, { credentials: "same-origin", headers: { Accept: "application/json" } })
.then(function (response) {
// A redirect means the session ended (we'd be reading the login
// page) — stop polling instead of hammering it.
if (!response.ok || response.redirected) throw new Error("stop");
return response.json();
})
.then(function (data) {
if (baseline === null) baseline = data.latest_id;
else showDot(data.new_count > 0);
setTimeout(poll, POLL_MS);
})
.catch(function (error) {
if (error.message !== "stop") setTimeout(poll, POLL_MS);
});
}
poll();
})();