Weak signal counts as no signal

With a signal so weak requests hang, the phone still says it's online, so
nothing fell back: Log Flight waited minutes with no feedback, and pages
never reached the offline page.

- Log Flight and Report Squawk say "Sending…" and, after 10 seconds
  without an answer (60 with photos), keep the entry on the device as
  with no signal. When the top bar is already red they keep it right
  away. If it got through after all, the copy sent later is dropped
  (same client_key).
- What's kept on the device isn't sent while the top bar is red, gives
  up after 15 seconds, and goes again as soon as the server answers.
- Every page of the app falls back to its kept copy or the offline page:
  after 10 seconds, then 3 while requests keep failing, and at once right
  after a failure. Downloads made while you wait (.sam.zip, records
  package, PDFs) have no time limit.

Checked on an Android 14 emulator with a connection that accepts
requests and never answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-15 13:34:13 -05:00
1 parent d9de1be562
commit e4bc3a5b7b
8 files changed
+134 -24

No files matched your search

+3 -3
View File
@@ -2,7 +2,7 @@
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<%= form_with model: [@aircraft, @flight_log], class: "card card-body space-y-5 max-w-3xl",
data: { controller: "offline-form", offline_form_kind_value: "flight", offline_form_tail_value: @aircraft.tail_number,
action: "submit->offline-form#submit turbo:submit-start->offline-form#remember turbo:fetch-request-error->offline-form#networkFailed turbo:before-fetch-response->offline-form#gatewayFailed" } do |f| %>
action: "submit->offline-form#submit turbo:before-fetch-request->offline-form#limit turbo:submit-start->offline-form#remember turbo:fetch-request-error->offline-form#networkFailed turbo:before-fetch-response->offline-form#gatewayFailed" } do |f| %>
<%= f.hidden_field :client_key, data: { offline_form_target: "key" } %>
<div class="hidden rounded-md bg-sky-50 px-3 py-2 text-sm text-sky-900 ring-1 ring-sky-200" data-offline-form-target="notice" role="status"></div>
<%= f.errors_summary %>
@@ -40,8 +40,8 @@
</div>
<p class="hint"><%= t(".saving_adds_the_tach_time") %></p>
<div class="flex flex-wrap gap-2">
<%= f.submit t(".log_flight") %>
<%= f.submit t(".log_and_next_leg"), name: "next_leg", class: "btn-secondary", title: t(".log_and_next_leg_title") %>
<%= f.submit t(".log_flight"), data: { turbo_submits_with: t(".sending") } %>
<%= f.submit t(".log_and_next_leg"), name: "next_leg", class: "btn-secondary", title: t(".log_and_next_leg_title"), data: { turbo_submits_with: t(".sending") } %>
<%= link_to t(".cancel"), aircraft_flight_logs_path(@aircraft), class: "btn-secondary" %>
</div>
<% end %>
+53 -13
View File
@@ -9,6 +9,8 @@
// - each aircraft's photo (put in sam-images ahead of time), shown when offline.
// The forms and pages are saved ahead of time for all your aircraft
// (offline_prep_controller.js), not only the ones you've opened.
// Any other page, when the server can't be reached (no signal, or one so
// weak nothing comes back within NETWORK_TIMEOUT), gets the offline page.
// Nothing else is cached; signing out clears the forms and pages.
// The version changes with every build, so a release refreshes the cached shell.
const VERSION = "<%= j "#{SystemInfo.new.revision}-#{I18n.locale}" %>"
@@ -24,6 +26,19 @@ const SHORTCUTS = { "/go/log-flight": "flight_logs", "/go/report-squawk": "squaw
// A proxy in front of the app (Traefik, Authentik) answers these when the app
// itself can't be reached: treat them like no connection.
const UNREACHABLE = [ 502, 503, 504 ]
// A weak signal can leave a request hanging for minutes: give up after this.
const NETWORK_TIMEOUT = 10 * 1000
// Downloads made while you wait (the .sam.zip, the records package, PDFs)
// can take longer than that to start, so they get no time limit.
const DOWNLOADS = /\.\w+$|^\/aircraft\/\d+\/(export|records)$/
// After a request fails, pages go straight to the kept copy or the offline
// page for a moment instead of trying again (Turbo follows a failed visit
// with a full page load, which would otherwise wait all over again), then
// for a while longer wait only briefly, so moving around offline is quick.
const RECENTLY = 5 * 1000
const STILL_OFFLINE = 60 * 1000
const OFFLINE_TIMEOUT = 3 * 1000
let failedAt = 0
const SHELL = [ "/offline", "/offline.js", "/icon.svg", "/icon-192.png", "/favicon.svg" ]
self.addEventListener("install", (event) => {
@@ -53,11 +68,9 @@ self.addEventListener("fetch", (event) => {
event.respondWith(keptPage(request, PAGE_CACHE))
} else if (request.mode === "navigate" && SHORTCUTS[url.pathname]) {
event.respondWith(shortcut(request, SHORTCUTS[url.pathname]))
} else if (request.mode === "navigate") {
// Always go to the network for pages; show the offline page if it's unreachable.
event.respondWith(fetch(request)
.then((response) => UNREACHABLE.includes(response.status) ? offlinePage() : response)
.catch(() => offlinePage()))
} else if (wantsHtml) {
// Any other page comes from the network; the offline page when it's unreachable.
event.respondWith(page(request))
} else if (SHELL.includes(url.pathname)) {
// The offline page's own script and icon: fresh when online, kept copy when not.
event.respondWith(fetch(request).then((r) => r.ok ? r : caches.match(url.pathname).then((hit) => hit || r))
@@ -74,6 +87,39 @@ self.addEventListener("fetch", (event) => {
})
const offlinePage = () => caches.match("/offline")
// The network, within NETWORK_TIMEOUT. A proxy's "can't reach the app" counts
// as no connection. Rejects when there isn't one (and remembers that).
async function network(request) {
const since = Date.now() - failedAt
if (since < RECENTLY) throw new Error("offline")
const wait = since < STILL_OFFLINE ? OFFLINE_TIMEOUT : NETWORK_TIMEOUT
let timer
try {
const limit = DOWNLOADS.test(new URL(request.url).pathname) ? [] :
[ new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("timed out")), wait) }) ]
const response = await Promise.race([ fetch(request), ...limit ])
if (UNREACHABLE.includes(response.status)) throw new Error("unreachable")
failedAt = 0
return response
} catch (error) {
failedAt = Date.now()
throw error
} finally {
clearTimeout(timer)
}
}
// A page with no kept copy. A Turbo fetch gets the error instead, so Turbo
// falls back to a full page load, which then gets the offline page.
async function page(request) {
try {
return await network(request)
} catch (error) {
if (request.mode !== "navigate") throw error
return offlinePage()
}
}
const savedImage = async (request, response) => {
const key = imageKey(request.url)
return (key && await caches.open(IMAGE_CACHE).then((c) => c.match(key))) || response || Response.error()
@@ -114,12 +160,7 @@ async function keptPage(request, cacheName) {
const cache = await caches.open(cacheName)
const key = request.url.split("?")[0]
try {
const response = await fetch(request)
if (UNREACHABLE.includes(response.status)) {
const kept = await cache.match(key)
if (kept) return kept
return request.mode === "navigate" ? offlinePage() : response
}
const response = await network(request)
const html = (response.headers.get("Content-Type") || "").includes("text/html")
if (response.ok && !response.redirected && html) await cache.put(key, response.clone())
return response
@@ -136,8 +177,7 @@ async function keptPage(request, cacheName) {
// of that kind (one aircraft), otherwise the offline page, which lists them.
async function shortcut(request, kind) {
try {
const response = await fetch(request)
if (!UNREACHABLE.includes(response.status)) return response
return await network(request)
} catch (_) { /* offline */ }
const cache = await caches.open(FORM_CACHE)
const forms = (await cache.keys()).filter((r) => new URL(r.url).pathname.includes(`/${kind}/new`))
+2 -2
View File
@@ -1,6 +1,6 @@
<%= form_with model: [aircraft, squawk], multipart: true, class: "card card-body space-y-4 max-w-2xl",
data: (squawk.new_record? ? { controller: "offline-form show-when", offline_form_kind_value: "squawk", offline_form_tail_value: aircraft.tail_number,
action: "submit->offline-form#submit turbo:submit-start->offline-form#remember turbo:fetch-request-error->offline-form#networkFailed turbo:before-fetch-response->offline-form#gatewayFailed" } : { controller: "show-when" }) do |f| %>
action: "submit->offline-form#submit turbo:before-fetch-request->offline-form#limit turbo:submit-start->offline-form#remember turbo:fetch-request-error->offline-form#networkFailed turbo:before-fetch-response->offline-form#gatewayFailed" } : { controller: "show-when" }) do |f| %>
<% if squawk.new_record? %>
<%= f.hidden_field :client_key, data: { offline_form_target: "key" } %>
<div class="hidden rounded-md bg-sky-50 px-3 py-2 text-sm text-sky-900 ring-1 ring-sky-200" data-offline-form-target="notice" role="status"></div>
@@ -25,7 +25,7 @@
<% end %>
<% if squawk.persisted? %><%= f.field :notes, as: :textarea, rows: 3 %><% end %>
<div class="flex gap-2">
<%= f.submit squawk.new_record? ? t(".report_squawk") : t(".save") %>
<%= f.submit squawk.new_record? ? t(".report_squawk") : t(".save"), data: { turbo_submits_with: t(".sending") } %>
<%= link_to t(".cancel"), aircraft_squawks_path(aircraft), class: "btn-secondary" %>
</div>
<% end %>