mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Log flights offline: the form is kept by the service worker, flights queue on the device (IndexedDB) and send when back online; one-time keys prevent duplicates
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
08ba017c7e
commit
191eb030e8
14 files changed
+314
-14
No files matched your search
@@ -1,6 +1,10 @@
|
||||
<% content_for :title, "Log Flight" %>
|
||||
<%= render "shared/aircraft_header", aircraft: @aircraft %>
|
||||
<%= form_with model: [@aircraft, @flight_log], class: "card card-body space-y-5 max-w-3xl" do |f| %>
|
||||
<%= form_with model: [@aircraft, @flight_log], class: "card card-body space-y-5 max-w-3xl",
|
||||
data: { controller: "offline-flight", offline_flight_tail_value: @aircraft.tail_number,
|
||||
action: "submit->offline-flight#submit turbo:submit-start->offline-flight#remember turbo:fetch-request-error->offline-flight#networkFailed" } do |f| %>
|
||||
<%= f.hidden_field :client_key, data: { offline_flight_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-flight-target="notice" role="status"></div>
|
||||
<%= f.errors_summary %>
|
||||
<div class="grid gap-4 sm:grid-cols-3">
|
||||
<%= f.field :date, as: :date, required: true %>
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
<%= javascript_importmap_tags %>
|
||||
</head>
|
||||
|
||||
<body class="min-h-full text-slate-800">
|
||||
<body class="min-h-full text-slate-800" data-controller="offline-sync">
|
||||
<div class="hidden space-y-1 bg-amber-50 px-4 py-2 text-center text-sm text-amber-900 ring-1 ring-amber-200" data-offline-sync-target="banner" role="status"></div>
|
||||
<header class="bg-header text-white">
|
||||
<div class="mx-auto flex max-w-7xl items-center justify-between gap-4 px-4 py-3">
|
||||
<%= link_to root_path, class: "flex items-center gap-2 font-semibold tracking-tight whitespace-nowrap" do %>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
// Makes the app installable and gives it an offline page. Only public, static
|
||||
// files are cached (the offline page, icons and fingerprinted /assets/), never
|
||||
// aircraft pages, so no private data is stored on the device.
|
||||
const VERSION = "v1"
|
||||
// Makes the app installable and gives it an offline page. Cached: public,
|
||||
// static files (the offline page, icons, fingerprinted /assets/) and, so a
|
||||
// flight can be logged without signal, the Log Flight forms you've opened
|
||||
// (tail number and meter readings only). Other aircraft pages are never cached.
|
||||
const VERSION = "v2"
|
||||
const FORM_CACHE = "sam-flight-forms"
|
||||
const FLIGHT_FORM = /^\/aircraft\/\d+\/flight_logs\/new$/
|
||||
const SHELL_CACHE = `sam-shell-${VERSION}`
|
||||
const ASSET_CACHE = "sam-assets"
|
||||
const MAX_ASSETS = 80
|
||||
const SHELL = [ "/offline.html", "/icon.svg", "/icon-192.png" ]
|
||||
const SHELL = [ "/offline.html", "/offline.js", "/icon.svg", "/icon-192.png" ]
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting()))
|
||||
@@ -25,7 +28,9 @@ self.addEventListener("fetch", (event) => {
|
||||
const url = new URL(request.url)
|
||||
if (url.origin !== self.location.origin) return
|
||||
|
||||
if (request.mode === "navigate") {
|
||||
if (request.mode === "navigate" && FLIGHT_FORM.test(url.pathname)) {
|
||||
event.respondWith(flightForm(request))
|
||||
} else if (request.mode === "navigate") {
|
||||
// Always go to the network for pages; show the offline page if it's unreachable.
|
||||
event.respondWith(fetch(request).catch(() => caches.match("/offline.html")))
|
||||
} else if (url.pathname.startsWith("/assets/")) {
|
||||
@@ -51,3 +56,16 @@ async function trim(cache) {
|
||||
const keys = await cache.keys()
|
||||
await Promise.all(keys.slice(0, Math.max(0, keys.length - MAX_ASSETS)).map((k) => cache.delete(k)))
|
||||
}
|
||||
|
||||
// The Log Flight form: fresh from the network when there is one (and kept),
|
||||
// the kept copy when there isn't.
|
||||
async function flightForm(request) {
|
||||
const cache = await caches.open(FORM_CACHE)
|
||||
try {
|
||||
const response = await fetch(request)
|
||||
if (response.ok && !response.redirected) await cache.put(request.url.split("?")[0], response.clone())
|
||||
return response
|
||||
} catch (_) {
|
||||
return (await cache.match(request.url.split("?")[0])) || caches.match("/offline.html")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user