add one ui theme
This commit is contained in:
1 parent
7a39f96256
commit
e19147dc73
18 files changed
+5299
-785
No files matched your search
@@ -1,45 +1,77 @@
|
||||
// Service Worker — Web Camera Category PWA
|
||||
// Caches app shell for offline use
|
||||
// Service Worker — CamCat 2.0
|
||||
// กลยุทธ์: navigation = network-first (ได้เวอร์ชันใหม่ก่อน) · asset = stale-while-revalidate
|
||||
const CACHE = "camcat-v2.1.1";
|
||||
|
||||
const CACHE_NAME = "camcat-v1";
|
||||
const APP_SHELL = [
|
||||
"/",
|
||||
"/index.html",
|
||||
"/manifest.json",
|
||||
"/icon-192.png",
|
||||
"/icon-512.png",
|
||||
"https://cdn.jsdelivr.net/npm/[email protected]/dist/jszip.min.js",
|
||||
const ASSETS = [
|
||||
"./",
|
||||
"./index.html",
|
||||
"./manifest.json",
|
||||
"./css/app.css",
|
||||
"./js/config.js",
|
||||
"./js/naming.js",
|
||||
"./js/db.js",
|
||||
"./js/exif.js",
|
||||
"./js/device.js",
|
||||
"./js/camera.js",
|
||||
"./js/archive.js",
|
||||
"./js/ui.js",
|
||||
"./js/main.js",
|
||||
"./vendor/jszip.min.js",
|
||||
"./vendor/piexif.js",
|
||||
"./vendor/qrcode.min.js",
|
||||
"./icon-192.png",
|
||||
"./icon-512.png",
|
||||
];
|
||||
|
||||
// ── Install: cache app shell ──
|
||||
self.addEventListener("install", (e) => {
|
||||
e.waitUntil(
|
||||
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)).then(() => self.skipWaiting())
|
||||
caches.open(CACHE)
|
||||
.then((c) => Promise.allSettled(ASSETS.map((u) => c.add(new Request(u, { cache: "reload" })))))
|
||||
.then(() => self.skipWaiting())
|
||||
);
|
||||
});
|
||||
|
||||
// ── Activate: clean old caches ──
|
||||
self.addEventListener("activate", (e) => {
|
||||
e.waitUntil(
|
||||
caches.keys().then((keys) =>
|
||||
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
|
||||
).then(() => self.clients.claim())
|
||||
caches.keys()
|
||||
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
||||
.then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
// ── Fetch: cache-first for app shell, network-first for photos ──
|
||||
self.addEventListener("fetch", (e) => {
|
||||
const url = new URL(e.request.url);
|
||||
// Don't intercept non-GET
|
||||
if (e.request.method !== "GET") return;
|
||||
// Cache-first for same-origin
|
||||
if (url.origin === self.location.origin) {
|
||||
const req = e.request;
|
||||
if (req.method !== "GET") return;
|
||||
const url = new URL(req.url);
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
// หน้าเว็บ: เอาจากเครือข่ายก่อน ถ้าออฟไลน์ค่อยใช้แคช
|
||||
if (req.mode === "navigate") {
|
||||
e.respondWith(
|
||||
caches.match(e.request).then((cached) => cached || fetch(e.request).then((resp) => {
|
||||
const clone = resp.clone();
|
||||
caches.open(CACHE_NAME).then((c) => c.put(e.request, clone));
|
||||
return resp;
|
||||
}).catch(() => caches.match("/index.html")))
|
||||
fetch(req)
|
||||
.then((resp) => {
|
||||
const clone = resp.clone();
|
||||
caches.open(CACHE).then((c) => c.put(req, clone));
|
||||
return resp;
|
||||
})
|
||||
.catch(() => caches.match(req).then((r) => r || caches.match("./index.html")))
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// ไฟล์อื่น: คืนแคชทันที + อัปเดตเบื้องหลัง
|
||||
e.respondWith(
|
||||
caches.match(req).then((cached) => {
|
||||
const network = fetch(req)
|
||||
.then((resp) => {
|
||||
if (resp && resp.ok) {
|
||||
const clone = resp.clone();
|
||||
caches.open(CACHE).then((c) => c.put(req, clone));
|
||||
}
|
||||
return resp;
|
||||
})
|
||||
.catch(() => cached);
|
||||
return cached || network;
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user