mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
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>
This commit is contained in:
1 parent
0767545143
commit
21ef577653
13 files changed
+158
-47
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 @@
|
||||
{
|
||||
"name": "SimpleAircraftManager",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/icon.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512"
|
||||
},
|
||||
{
|
||||
"src": "/icon.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512",
|
||||
"purpose": "maskable"
|
||||
}
|
||||
],
|
||||
"id": "/",
|
||||
"name": "Simple Aircraft Manager",
|
||||
"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)))
|
||||
}
|
||||
Reference in new issue
Block a user