commit deca07de1c8456a7d84102db279ec4c07ba4945b
parent f4bedce994cdcaf00e89b6cd7bfb68b50c3412ba
Author: Silas Brack <silasbrack@gmail.com>
Date: Tue, 9 Jun 2026 20:07:33 +0200
feat: replace full page reload with targeted comment refresh on SSE
Fetch just the comments HTML fragment instead of reloading the entire
page when a new comment arrives via SSE. Preserves scroll position.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/templates.rs b/src/templates.rs
@@ -31,6 +31,13 @@ pub struct StoryTemplate {
}
#[derive(Template)]
+#[template(path = "comments.html")]
+pub struct CommentsTemplate {
+ pub comments: Vec<CommentWithMeta>,
+ pub is_logged_in: bool,
+}
+
+#[derive(Template)]
#[template(path = "submit.html")]
pub struct SubmitTemplate {
pub error: Option<String>,
diff --git a/templates/story.html b/templates/story.html
@@ -88,7 +88,11 @@
(function() {
var evtSource = new EventSource('/api/records/v1/comment/subscribe/*?filter[story_id]={{ story.id }}');
evtSource.onmessage = function(event) {
- window.location.reload();
+ fetch('/story/{{ story.id }}/comments')
+ .then(function(r) { return r.text(); })
+ .then(function(html) {
+ document.getElementById('comments-section').innerHTML = html;
+ });
};
})();
</script>