Files
boc/aamos-admin-upgrade/dist/sw.js
T
Bernt 6de2455917 v1.2.0: Add Global Markets footer, translated to 9 languages
- Added GLOBAL_MARKETS_TITLE to all translation files
- Updated footer with 12 markets (4 active + 8 upcoming)
- Translated market section to: zh-cn, zh-tw, ja, ko, th, vi, id, ms, hi
- Built and deployed to production
- CloudFront invalidation: I3RTMXVFDJXWLG3SYX208OP1CC
2026-07-08 19:56:03 +00:00

95 lines
3.4 KiB
JavaScript

'use strict';
const CACHE_NAME = 'aamos-admin-v1';
const OFFLINE_URL = '/admin/';
const SHELL = [
'/admin/',
'/admin/index.html',
'/admin/dashboard.html',
'/admin/modules.html',
'/admin/assets/admin.js',
'/admin/assets/api.js',
];
// ── Install: precache shell ───────────────────────────────────────────
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(SHELL))
.then(() => self.skipWaiting())
);
});
// ── Activate: purge stale caches ─────────────────────────────────────
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((keys) => Promise.all(
keys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key))
))
.then(() => self.clients.claim())
);
});
// ── Fetch ─────────────────────────────────────────────────────────────
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') return;
if (!request.url.startsWith('http')) return;
const { pathname } = new URL(request.url);
// API + auth: network-only, never cache tokens/responses
if (pathname.startsWith('/api/') || pathname.startsWith('/auth/')) return;
// Navigation: network-first → cached shell on offline
if (request.mode === 'navigate') {
event.respondWith(
fetch(request)
.catch(() =>
caches.match(request)
.then((cached) => cached || caches.match(OFFLINE_URL))
.then((r) => r || offlineResponse())
)
);
return;
}
// Static assets: cache-first → fetch + update cache on miss
event.respondWith(
caches.match(request).then((cached) => {
if (cached) return cached;
return fetch(request).then((response) => {
if (!response || response.status !== 200 || response.type === 'opaque') {
return response;
}
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
return response;
}).catch(() => caches.match(OFFLINE_URL).then((r) => r || offlineResponse()));
})
);
});
function offlineResponse() {
return new Response(
'<!DOCTYPE html><html lang="sv"><head><meta charset="UTF-8"><title>Offline</title>' +
'<meta name="viewport" content="width=device-width,initial-scale=1">' +
'<style>body{font-family:-apple-system,sans-serif;display:flex;align-items:center;' +
'justify-content:center;min-height:100dvh;margin:0;background:#F2F2F7;color:#000}' +
'.box{text-align:center;padding:32px}h1{font-size:22px;font-weight:700;margin-bottom:8px}' +
'p{font-size:15px;color:rgba(60,60,67,.6)}button{margin-top:20px;padding:12px 24px;' +
'background:#007AFF;color:#fff;border:none;border-radius:12px;font-size:16px;' +
'font-weight:600;cursor:pointer}</style></head>' +
'<body><div class="box"><h1>Ingen anslutning</h1>' +
'<p>Kontrollera nätverket och försök igen.</p>' +
'<button onclick="location.reload()">Försök igen</button></div></body></html>',
{ status: 503, headers: { 'Content-Type': 'text/html;charset=UTF-8' } }
);
}