Files
nis2-agile/public/sw.js
T
DevEnv nis2-agileandClaude Opus 4.8 69873e21be [FEAT] Epic C / C5 — chiusura open-items: email gated + fonti + provenienza 92/87
- #7 (go-live ready): send() aggancia l'invio email del questionario/firma-lettura via
  EmailService, GATED dal kill-switch EMAIL_SENDING_ENABLED (oggi=false → ZERO invii;
  pronto al go-live). Magic-link assoluto via APP_URL; invio solo ai target con email
  valida; i link restano sempre disponibili per la condivisione manuale. Smoke prod:
  email_sent=0, email_enabled=false (nessun invio).
- #2 (prevale Simon): nis2_sources.php — aggiunta NOTA DI PROVENIENZA sul 92 vs 87
  (prodotto usa 92 = file referente; ACN ufficiale 87; divergenza nota/accettata).
  Numero NON modificato per decisione confermata.

Note operative (host, fuori repo):
- #8: EMAIL_MS_URL spostato sul path interno http://172.21.0.1:8081/api/emails
  (verificato 400 con X-Internal-Key = funzionante; pre edge-strip). Rollback: URL pubblico.
- #9: pre-equip PHP-CA del DB CONFERMATO (CA presente, app connessa in TLS 1.3); enforce
  ALTER USER nis2_user REQUIRE SSL resta azione DB coordinata con VIGILE (utente @%).

v1.21.3. Additivo.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 23:32:27 +02:00

97 lines
3.4 KiB
JavaScript

/*
* NIS2 Agile — Service Worker (PWA)
* -----------------------------------------------------------------------------
* Strategia (SaaS autenticato multi-tenant — sicurezza prima di tutto):
* - /api/* -> NETWORK-ONLY, MAI in cache. Nessun dato autenticato
* viene cacheato => zero leak cross-utente su un
* dispositivo condiviso. La sessione resta gestita da
* JWT / auth-gate.js, fuori dal service worker.
* - navigazioni HTML -> NETWORK-FIRST, fallback alla cache e infine /offline.html.
* - asset statici -> STALE-WHILE-REVALIDATE (risposta immediata dalla cache,
* aggiornamento in background).
*
* Il nome cache include la release: va BUMPATO ad ogni rilascio (vedi version.json),
* cosi' activate elimina automaticamente la shell vecchia.
*/
const CACHE = 'nis2-shell-v1.21.3';
const PRECACHE = [
'/offline.html',
'/manifest.webmanifest',
'/css/style.css',
'/js/common.js',
'/js/common-bi.js',
'/js/api.js',
'/js/i18n.js',
'/js/pwa.js',
'/vendor/bootstrap-italia/dist/css/bootstrap-italia.min.css',
'/vendor/bootstrap-italia/dist/js/bootstrap-italia.bundle.min.js',
'/vendor/bootstrap-italia/dist/svg/sprites.svg',
'/assets/icons/icon-192.png',
'/assets/icons/icon-512.png'
];
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE);
// add uno-a-uno: un singolo asset mancante non deve far fallire l'install
await Promise.all(PRECACHE.map((url) =>
cache.add(new Request(url, { cache: 'reload' })).catch((err) =>
console.warn('[sw] precache skip', url, err && err.message))
));
await self.skipWaiting();
})());
});
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
const keys = await caches.keys();
await Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)));
await self.clients.claim();
})());
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return; // non-GET: rete normale
const url = new URL(req.url);
if (url.origin !== self.location.origin) return; // cross-origin: rete normale
// /api/* -> network-only, mai in cache
if (url.pathname.startsWith('/api/')) {
event.respondWith(
fetch(req).catch(() => new Response(
JSON.stringify({ error: 'offline', message: 'Connessione di rete assente' }),
{ status: 503, headers: { 'Content-Type': 'application/json' } }
))
);
return;
}
// navigazioni HTML -> network-first, fallback cache -> offline
if (req.mode === 'navigate') {
event.respondWith((async () => {
try {
return await fetch(req);
} catch (e) {
const cached = await caches.match(req);
return cached || (await caches.match('/offline.html'));
}
})());
return;
}
// asset statici stesso-origin -> stale-while-revalidate
event.respondWith((async () => {
const cache = await caches.open(CACHE);
const cached = await cache.match(req);
const network = fetch(req).then((res) => {
if (res && res.status === 200 && res.type === 'basic') {
cache.put(req, res.clone());
}
return res;
}).catch(() => null);
return cached || (await network) || (await caches.match('/offline.html'));
})());
});