Make the app an installable PWA with an offline page
Manifest, service worker (offline fallback, cached static assets only, no aircraft pages), new plane icon in all PWA/Apple sizes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No files matched your search
@@ -19,3 +19,8 @@ styleCharts()
|
||||
// Charts can be created before this module runs, so restyle after each page load too.
|
||||
document.addEventListener("turbo:load", restyleCharts)
|
||||
window.addEventListener("theme:change", restyleCharts)
|
||||
|
||||
// Installable app (PWA): the service worker adds an offline page and caches static assets.
|
||||
if ("serviceWorker" in navigator) {
|
||||
window.addEventListener("load", () => navigator.serviceWorker.register("/service-worker.js", { scope: "/" }).catch(() => {}))
|
||||
}
|
||||
@@ -9,9 +9,13 @@
|
||||
<%= csrf_meta_tags %>
|
||||
<%= csp_meta_tag %>
|
||||
<%= yield :head %>
|
||||
<link rel="manifest" href="/manifest.json">
|
||||
<meta name="theme-color" content="#0f172a">
|
||||
<meta name="apple-mobile-web-app-title" content="SAM">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
||||
<link rel="icon" href="/icon.png" type="image/png">
|
||||
<link rel="icon" href="/icon.svg" type="image/svg+xml">
|
||||
<link rel="apple-touch-icon" href="/icon.png">
|
||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
||||
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
|
||||
<%= javascript_importmap_tags %>
|
||||
</head>
|
||||
|
||||
@@ -7,7 +7,13 @@
|
||||
<meta name="robots" content="noindex, nofollow">
|
||||
<%= csrf_meta_tags %>
|
||||
<%= csp_meta_tag %>
|
||||
<link rel="manifest" href="/manifest.json">
|
||||
<meta name="theme-color" content="#0f172a">
|
||||
<meta name="apple-mobile-web-app-title" content="SAM">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
||||
<link rel="icon" href="/icon.png" type="image/png">
|
||||
<link rel="icon" href="/icon.svg" type="image/svg+xml">
|
||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
||||
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
|
||||
<%= javascript_importmap_tags %>
|
||||
</head>
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
{
|
||||
"id": "/",
|
||||
"name": "Simple Aircraft Manager",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/icon.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512"
|
||||
},
|
||||
{
|
||||
"src": "/icon.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512",
|
||||
"purpose": "maskable"
|
||||
}
|
||||
],
|
||||
"short_name": "SAM",
|
||||
"description": "Maintenance, inspections, ADs, squawks and flight logs for your aircraft.",
|
||||
"start_url": "/",
|
||||
"display": "standalone",
|
||||
"scope": "/",
|
||||
"description": "SimpleAircraftManager.",
|
||||
"theme_color": "red",
|
||||
"background_color": "red"
|
||||
"display": "standalone",
|
||||
"orientation": "any",
|
||||
"theme_color": "#0f172a",
|
||||
"background_color": "#0f172a",
|
||||
"categories": ["productivity", "utilities"],
|
||||
"icons": [
|
||||
{ "src": "/icon-192.png", "type": "image/png", "sizes": "192x192", "purpose": "any" },
|
||||
{ "src": "/icon.png", "type": "image/png", "sizes": "512x512", "purpose": "any" },
|
||||
{ "src": "/icon-maskable.png", "type": "image/png", "sizes": "512x512", "purpose": "maskable" },
|
||||
{ "src": "/icon.svg", "type": "image/svg+xml", "sizes": "any", "purpose": "any" }
|
||||
]
|
||||
}
|
||||
@@ -1,26 +1,53 @@
|
||||
// Add a service worker for processing Web Push notifications:
|
||||
//
|
||||
// self.addEventListener("push", async (event) => {
|
||||
// const { title, options } = await event.data.json()
|
||||
// event.waitUntil(self.registration.showNotification(title, options))
|
||||
// })
|
||||
//
|
||||
// self.addEventListener("notificationclick", function(event) {
|
||||
// event.notification.close()
|
||||
// event.waitUntil(
|
||||
// clients.matchAll({ type: "window" }).then((clientList) => {
|
||||
// for (let i = 0; i < clientList.length; i++) {
|
||||
// let client = clientList[i]
|
||||
// let clientPath = (new URL(client.url)).pathname
|
||||
//
|
||||
// if (clientPath == event.notification.data.path && "focus" in client) {
|
||||
// return client.focus()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (clients.openWindow) {
|
||||
// return clients.openWindow(event.notification.data.path)
|
||||
// }
|
||||
// })
|
||||
// )
|
||||
// })
|
||||
// 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"
|
||||
const SHELL_CACHE = `sam-shell-${VERSION}`
|
||||
const ASSET_CACHE = "sam-assets"
|
||||
const MAX_ASSETS = 80
|
||||
const SHELL = [ "/offline.html", "/icon.svg", "/icon-192.png" ]
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting()))
|
||||
})
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys()
|
||||
.then((keys) => Promise.all(keys.filter((k) => k.startsWith("sam-shell-") && k !== SHELL_CACHE).map((k) => caches.delete(k))))
|
||||
.then(() => self.clients.claim())
|
||||
)
|
||||
})
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const request = event.request
|
||||
if (request.method !== "GET") return
|
||||
const url = new URL(request.url)
|
||||
if (url.origin !== self.location.origin) return
|
||||
|
||||
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/")) {
|
||||
event.respondWith(cachedAsset(request))
|
||||
}
|
||||
})
|
||||
|
||||
// Fingerprinted assets never change, so serve them from cache once fetched.
|
||||
async function cachedAsset(request) {
|
||||
const cache = await caches.open(ASSET_CACHE)
|
||||
const hit = await cache.match(request)
|
||||
if (hit) return hit
|
||||
const response = await fetch(request)
|
||||
if (response.ok) {
|
||||
await cache.put(request, response.clone())
|
||||
trim(cache)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
// Old deploys' assets pile up; keep only the most recent entries.
|
||||
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)))
|
||||
}
|
||||
@@ -72,5 +72,7 @@ Rails.application.routes.draw do
|
||||
end
|
||||
|
||||
get "up" => "rails/health#show", as: :rails_health_check
|
||||
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
|
||||
get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
|
||||
root "aircraft#index"
|
||||
end
|
||||
|
After Width: | Height: | Size: 5.8 KiB |
|
After Width: | Height: | Size: 7.4 KiB |
|
After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 23 KiB |
@@ -1,3 +1,5 @@
|
||||
<svg width="512" height="512" xmlns="http://www.w3.org/2000/svg">
|
||||
<circle cx="256" cy="256" r="256" fill="red"/>
|
||||
<svg width="512" height="512" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">
|
||||
<rect width="512" height="512" rx="112" fill="#0f172a"/>
|
||||
<circle cx="256" cy="256" r="196" fill="none" stroke="#0369a1" stroke-width="14"/>
|
||||
<path fill="#fff" d="M256 92C270 92 278 108 278 128L278 214L408 270L408 300L278 280L276 370L326 400L326 424L256 410L186 424L186 400L236 370L234 280L104 300L104 270L234 214L234 128C234 108 242 92 256 92Z"/>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 122 B After Width: | Height: | Size: 447 B |
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<meta name="theme-color" content="#0f172a">
|
||||
<title>Offline · Simple Aircraft Manager</title>
|
||||
<link rel="icon" href="/icon.svg" type="image/svg+xml">
|
||||
<style>
|
||||
:root { --page: #f1f5f9; --surface: #fff; --text: #1e293b; --muted: #64748b; --accent: #0369a1; }
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root { --page: #0b1220; --surface: #0f172a; --text: #e2e8f0; --muted: #94a3b8; }
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; min-height: 100vh; display: grid; place-items: center; padding: 16px;
|
||||
background: var(--page); color: var(--text); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
|
||||
main { max-width: 26rem; width: 100%; background: var(--surface); border-radius: 12px; padding: 32px 24px; text-align: center;
|
||||
box-shadow: 0 1px 3px rgb(0 0 0 / 0.1); }
|
||||
img { width: 72px; height: 72px; }
|
||||
h1 { font-size: 1.25rem; margin: 16px 0 8px; }
|
||||
p { margin: 0 0 24px; color: var(--muted); }
|
||||
button { font: inherit; font-weight: 600; color: #fff; background: var(--accent); border: 0; border-radius: 6px; padding: 10px 20px; cursor: pointer; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<img src="/icon.svg" alt="">
|
||||
<h1>You're offline</h1>
|
||||
<p>Simple Aircraft Manager needs a connection to load your aircraft. Check your signal and try again.</p>
|
||||
<button type="button" onclick="location.reload()">Try again</button>
|
||||
</main>
|
||||
<script>window.addEventListener("online", function () { location.reload() })</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,34 @@
|
||||
require "test_helper"
|
||||
|
||||
class PwaTest < ActionDispatch::IntegrationTest
|
||||
test "manifest is public and points at icons that exist" do
|
||||
get pwa_manifest_path(format: :json)
|
||||
assert_response :success
|
||||
manifest = JSON.parse(response.body)
|
||||
assert_equal "Simple Aircraft Manager", manifest["name"]
|
||||
assert_equal "standalone", manifest["display"]
|
||||
assert_includes manifest["icons"].map { _1["purpose"] }, "maskable"
|
||||
manifest["icons"].each { |icon| assert Rails.public_path.join(icon["src"].delete_prefix("/")).exist?, icon["src"] }
|
||||
end
|
||||
|
||||
test "service worker is public and precaches files that exist" do
|
||||
get pwa_service_worker_path(format: :js)
|
||||
assert_response :success
|
||||
assert_match "javascript", response.media_type
|
||||
shell = response.body[/const SHELL = \[(.*?)\]/, 1].scan(/"([^"]+)"/).flatten
|
||||
assert_includes shell, "/offline.html"
|
||||
shell.each { |path| assert Rails.public_path.join(path.delete_prefix("/")).exist?, path }
|
||||
end
|
||||
|
||||
test "signed-in and shared pages link the manifest and register the worker" do
|
||||
sign_in create_user
|
||||
get root_path
|
||||
assert_select "link[rel=manifest][href='/manifest.json']"
|
||||
assert_select "meta[name=theme-color]"
|
||||
assert_select "link[rel=apple-touch-icon][href='/apple-touch-icon.png']"
|
||||
|
||||
token = create_aircraft(owner: create_user("o2@example.com")).share_tokens.create!(privilege: "status")
|
||||
get shared_path(token.token)
|
||||
assert_select "link[rel=manifest]"
|
||||
end
|
||||
end
|
||||