From 579f6abb29dbaf97bb2cb29aef88522c40081ed2 Mon Sep 17 00:00:00 2001 From: Jasper Mayone Date: Sun, 8 Nov 2026 23:25:39 -0500 Subject: [PATCH 1/3] feat(archives): the archive page updates itself while captures run --- app/javascript/controllers/index.js | 3 ++ app/javascript/controllers/poll_controller.js | 33 +++++++++++++++++++ app/views/archives/show.html.erb | 10 ++++-- docs/design.md | 2 ++ test/controllers/archives_controller_test.rb | 16 +++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/javascript/controllers/poll_controller.js diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index 54f62cf..b58f961 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -10,4 +10,7 @@ application.register("drawer", DrawerController) import FlashController from "./flash_controller" application.register("flash", FlashController) +import PollController from "./poll_controller" +application.register("poll", PollController) + export { application } diff --git a/app/javascript/controllers/poll_controller.js b/app/javascript/controllers/poll_controller.js new file mode 100644 index 0000000..f6f0234 --- /dev/null +++ b/app/javascript/controllers/poll_controller.js @@ -0,0 +1,33 @@ +import { Controller } from "@hotwired/stimulus" +import { Turbo } from "@hotwired/turbo-rails" + +// Reloads the page every `interval` ms while the element is on it. The archive page renders it +// only while captures run, so the first page without it ends the polling: Turbo morphs the new +// page into the old one (the turbo-refresh-method meta), scroll and focus stay where they were, +// and when the element is gone this controller disconnects. The page stays quiet in a hidden +// tab and catches up when the tab is shown again. +export default class extends Controller { + static values = { interval: { type: Number, default: 5000 } } + + connect() { + this.onVisibility = () => this.schedule() + document.addEventListener("visibilitychange", this.onVisibility) + this.schedule() + } + + disconnect() { + document.removeEventListener("visibilitychange", this.onVisibility) + clearTimeout(this.timer) + } + + schedule() { + clearTimeout(this.timer) + if (document.hidden) return + this.timer = setTimeout(() => this.refresh(), this.intervalValue) + } + + refresh() { + if (document.hidden) return this.schedule() + Turbo.visit(window.location.href, { action: "replace" }) + } +} diff --git a/app/views/archives/show.html.erb b/app/views/archives/show.html.erb index cc26aa0..24d3bf3 100644 --- a/app/views/archives/show.html.erb +++ b/app/views/archives/show.html.erb @@ -69,12 +69,18 @@ <% end %> <% unless @archive.finished? %> -
+ <%# The poll controller reloads the page every few seconds; Turbo morphs the result in place. %> + <% content_for :head do %> + + + <% end %> + +
<%= icon(:loader_circle, size: 18, class: "spinner") %>

Captures are running

-

Reload this page in a moment.

+

This page updates itself as they finish.

<% end %> diff --git a/docs/design.md b/docs/design.md index e0023a0..2e7fc11 100644 --- a/docs/design.md +++ b/docs/design.md @@ -161,6 +161,8 @@ Stash uses `patchwork-central` (not the restricted database), schema `stash`. Ar ## Jobs +While an archive runs, its page reloads itself every five seconds (the `poll` Stimulus controller, rendered only on an unfinished archive) and Turbo morphs the new page into the old one, so the captures fill in without a flash and scroll stays put. The first finished page carries no controller, and the polling ends with it. Polling pauses in a hidden tab. + `ArchiveJob` makes the capture rows from the router and enqueues one `CaptureJob` per capture. `CaptureJob` runs one capturer, stores its artifacts, and updates the archive's status. The jobs queue is `captures`, with a small concurrency (`GOOD_JOB_MAX_THREADS`, default 2), because Chromium and yt-dlp are heavy. ## Environments diff --git a/test/controllers/archives_controller_test.rb b/test/controllers/archives_controller_test.rb index 9ee3c17..43b3f2d 100644 --- a/test/controllers/archives_controller_test.rb +++ b/test/controllers/archives_controller_test.rb @@ -27,6 +27,22 @@ class ArchivesControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_includes response.body, archive.public_id assert_not_includes response.body, "Delete this archive" + # Finished: nothing polls, and the page does not ask Turbo to morph. + assert_select "[data-controller=poll]", count: 0 + assert_select "meta[name=turbo-refresh-method]", count: 0 + end + + test "an archive that is still running polls for its result and morphs the page" do + archive = create_archive_with_captures({ "page" => "running", "git" => "queued" }) + sign_in_as(:viewer) + + get archive_path(archive) + + assert_response :success + assert_select "[data-controller=poll][data-poll-interval-value='5000']", count: 1 + assert_select "meta[name=turbo-refresh-method][content=morph]", count: 1 + assert_select "meta[name=turbo-refresh-scroll][content=preserve]", count: 1 + assert_includes response.body, "This page updates itself as they finish" end test "the list searches by URL, host and note" do From fb65e3f5137f0120b7935f46be011ad3324766cb Mon Sep 17 00:00:00 2001 From: Jasper Mayone Date: Sun, 8 Nov 2026 23:40:04 -0500 Subject: [PATCH 2/3] fix(poll): keep polling after a morph that keeps the element --- app/javascript/controllers/poll_controller.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/javascript/controllers/poll_controller.js b/app/javascript/controllers/poll_controller.js index f6f0234..a97a9ff 100644 --- a/app/javascript/controllers/poll_controller.js +++ b/app/javascript/controllers/poll_controller.js @@ -29,5 +29,9 @@ export default class extends Controller { refresh() { if (document.hidden) return this.schedule() Turbo.visit(window.location.href, { action: "replace" }) + // The morph keeps this element, and this controller with it, while captures still run; the + // next tick has to be set here, because connect() does not run again. When the new page has + // no poll element, the morph removes it and disconnect() clears the timer. + this.schedule() } } From 10bb3091dc6eafcdc3c905e072f7826786257783 Mon Sep 17 00:00:00 2001 From: Jasper Mayone Date: Sun, 8 Nov 2026 23:47:38 -0500 Subject: [PATCH 3/3] fix(poll): refresh through turbo's page refresh, keep typed values and open details, skip error pages --- .../controllers/drawer_controller.js | 3 +- app/javascript/controllers/poll_controller.js | 56 +++++++++++++++---- app/views/archives/show.html.erb | 16 ++++-- docs/design.md | 2 +- test/controllers/archives_controller_test.rb | 4 +- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/app/javascript/controllers/drawer_controller.js b/app/javascript/controllers/drawer_controller.js index db10b79..2ffb00f 100644 --- a/app/javascript/controllers/drawer_controller.js +++ b/app/javascript/controllers/drawer_controller.js @@ -13,7 +13,8 @@ export default class extends Controller { static targets = ["panel", "scrim", "button", "content"] connect() { - this.closeOnVisit = () => this.close() + // A refresh of the current page (the poll controller) is a visit too; it must not close the menu. + this.closeOnVisit = (event) => { if (event.detail?.url !== window.location.href) this.close() } document.addEventListener("turbo:before-visit", this.closeOnVisit) // Past 64rem the CSS shows the panel in place, so an open drawer must // let go of the page (inert, scrim) when the viewport grows past it. diff --git a/app/javascript/controllers/poll_controller.js b/app/javascript/controllers/poll_controller.js index a97a9ff..7ee669c 100644 --- a/app/javascript/controllers/poll_controller.js +++ b/app/javascript/controllers/poll_controller.js @@ -1,22 +1,32 @@ import { Controller } from "@hotwired/stimulus" import { Turbo } from "@hotwired/turbo-rails" -// Reloads the page every `interval` ms while the element is on it. The archive page renders it -// only while captures run, so the first page without it ends the polling: Turbo morphs the new -// page into the old one (the turbo-refresh-method meta), scroll and focus stay where they were, -// and when the element is gone this controller disconnects. The page stays quiet in a hidden -// tab and catches up when the tab is shown again. +// Refreshes the page every `interval` ms while the element is on it, through Turbo's page +// refresh with morphing: the new page is merged into the old one, scroll and focus stay. The +// archive page renders the element only while captures run, so the first page without it ends +// the polling: the morph removes the element and this controller disconnects. +// +// What the refresh must not do: cancel a visit or a form the person started (Turbo.session.refresh +// skips while a visit is current; forms are checked here), wipe what they typed or close a +//
they opened (the morph attribute hook below), replace the page with an error page +// when the server hiccups (the fetch response hook), or run in a hidden tab. export default class extends Controller { static values = { interval: { type: Number, default: 5000 } } connect() { - this.onVisibility = () => this.schedule() + this.onVisibility = () => { if (!document.hidden) this.refresh() } + this.onMorphAttribute = (event) => this.keepLocalState(event) + this.onFetchResponse = (event) => this.skipErrorResponse(event) document.addEventListener("visibilitychange", this.onVisibility) + document.addEventListener("turbo:before-morph-attribute", this.onMorphAttribute) + document.addEventListener("turbo:before-fetch-response", this.onFetchResponse) this.schedule() } disconnect() { document.removeEventListener("visibilitychange", this.onVisibility) + document.removeEventListener("turbo:before-morph-attribute", this.onMorphAttribute) + document.removeEventListener("turbo:before-fetch-response", this.onFetchResponse) clearTimeout(this.timer) } @@ -27,11 +37,35 @@ export default class extends Controller { } refresh() { - if (document.hidden) return this.schedule() - Turbo.visit(window.location.href, { action: "replace" }) - // The morph keeps this element, and this controller with it, while captures still run; the - // next tick has to be set here, because connect() does not run again. When the new page has - // no poll element, the morph removes it and disconnect() clears the timer. + const busy = document.hidden || Turbo.navigator.currentVisit || Turbo.navigator.formSubmission + if (!busy) { + this.refreshing = true + Turbo.session.refresh(document.baseURI, { method: "morph", scroll: "preserve" }) + } + // The morph keeps this element, and this controller with it, while captures still run, so + // the next tick is set here; connect() does not run again. When the new page has no poll + // element, the morph removes it and disconnect() clears the timer. this.schedule() } + + // A value the person typed, and a
they opened, are theirs, not the server's. + keepLocalState(event) { + const { attributeName } = event.detail + const target = event.target + const typed = attributeName === "value" && (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) + const opened = attributeName === "open" && target instanceof HTMLDetailsElement + if (typed || opened) event.preventDefault() + } + + // A refresh that gets a 502 from a proxy or a 500 from the app must not replace the page with + // the error page: drop that visit, keep what is shown, and try again at the next tick. The + // visit has to be stopped as well, or it stays current and blocks every later refresh. + skipErrorResponse(event) { + const response = event.detail.fetchResponse + if (this.refreshing && response && !response.succeeded) { + event.preventDefault() + Turbo.navigator.stop() + } + this.refreshing = false + } } diff --git a/app/views/archives/show.html.erb b/app/views/archives/show.html.erb index 24d3bf3..22fd50a 100644 --- a/app/views/archives/show.html.erb +++ b/app/views/archives/show.html.erb @@ -68,13 +68,17 @@
<% end %> - <% unless @archive.finished? %> - <%# The poll controller reloads the page every few seconds; Turbo morphs the result in place. %> - <% content_for :head do %> - - - <% end %> + <%# + A page refresh (the poll controller, while captures run) morphs this page and keeps the scroll. + Both metas stay on the finished page too: Turbo reads them from the page it refreshes. + %> + + <% content_for :head do %> + + + <% end %> + <% unless @archive.finished? %>
<%= icon(:loader_circle, size: 18, class: "spinner") %> diff --git a/docs/design.md b/docs/design.md index 2e7fc11..66e579b 100644 --- a/docs/design.md +++ b/docs/design.md @@ -161,7 +161,7 @@ Stash uses `patchwork-central` (not the restricted database), schema `stash`. Ar ## Jobs -While an archive runs, its page reloads itself every five seconds (the `poll` Stimulus controller, rendered only on an unfinished archive) and Turbo morphs the new page into the old one, so the captures fill in without a flash and scroll stays put. The first finished page carries no controller, and the polling ends with it. Polling pauses in a hidden tab. +While an archive runs, its page refreshes itself every five seconds (the `poll` Stimulus controller, rendered only on an unfinished archive) through Turbo's page refresh with morphing, so the captures fill in without a flash and scroll stays put. The first finished page carries no controller, and the polling ends with it. The refresh never cancels a visit or a form the person started, keeps what they typed and the `
` they opened, leaves the page alone when the server answers an error, and pauses in a hidden tab. `ArchiveJob` makes the capture rows from the router and enqueues one `CaptureJob` per capture. `CaptureJob` runs one capturer, stores its artifacts, and updates the archive's status. The jobs queue is `captures`, with a small concurrency (`GOOD_JOB_MAX_THREADS`, default 2), because Chromium and yt-dlp are heavy. diff --git a/test/controllers/archives_controller_test.rb b/test/controllers/archives_controller_test.rb index 43b3f2d..c209377 100644 --- a/test/controllers/archives_controller_test.rb +++ b/test/controllers/archives_controller_test.rb @@ -27,9 +27,9 @@ class ArchivesControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_includes response.body, archive.public_id assert_not_includes response.body, "Delete this archive" - # Finished: nothing polls, and the page does not ask Turbo to morph. + # Finished: nothing polls. The morph metas stay, so the last refresh of a running page morphs too. assert_select "[data-controller=poll]", count: 0 - assert_select "meta[name=turbo-refresh-method]", count: 0 + assert_select "meta[name=turbo-refresh-method][content=morph]", count: 1 end test "an archive that is still running polls for its result and morphs the page" do