fix: non riuscivi ad acquistare i RIS al gruppo
- lista linkREG
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
/* global workbox */
|
||||
/* global cfgenv */
|
||||
|
||||
const VITE_APP_VERSION = "1.2.28";
|
||||
const VITE_APP_VERSION = "1.2.29";
|
||||
|
||||
const CACHE_NAME = 'pwa-cache-' + VITE_APP_VERSION; // Nome della cache
|
||||
|
||||
@@ -259,89 +259,72 @@ if (workbox) {
|
||||
const DYNAMIC_CACHE = 'dynamic-cache-v2';
|
||||
const ENABLE_DYNAMIC_CACHING = true;
|
||||
const baseUrl = self.location.origin;
|
||||
console.log('baseUrl', baseUrl);
|
||||
const APP_DOMAIN = extractDomain(baseUrl);
|
||||
let API_DOMAIN = '';
|
||||
if (ISTEST) {
|
||||
API_DOMAIN = 'testapi.' + removeTestPrefix(APP_DOMAIN);
|
||||
} else {
|
||||
if (APP_DOMAIN.includes('localhost')) {
|
||||
API_DOMAIN = 'localhost:3000';
|
||||
} else {
|
||||
API_DOMAIN = 'api.' + APP_DOMAIN;
|
||||
}
|
||||
}
|
||||
const API_DOMAIN = determineApiDomain(APP_DOMAIN);
|
||||
|
||||
console.log('API_DOMAIN', API_DOMAIN);
|
||||
|
||||
// Funzione per gestire specificamente le richieste API
|
||||
// Funzione per determinare il dominio API
|
||||
function determineApiDomain(appDomain) {
|
||||
if (ISTEST) {
|
||||
return 'testapi.' + removeTestPrefix(appDomain);
|
||||
}
|
||||
return appDomain.includes('localhost') ? 'localhost:3000' : 'api.' + appDomain;
|
||||
}
|
||||
|
||||
// Funzione per gestire richieste API
|
||||
async function handleApiRequest(request) {
|
||||
const modifiedRequest = new Request(request.url, {
|
||||
method: request.method,
|
||||
headers: {
|
||||
...Object.fromEntries(request.headers.entries()),
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
mode: 'cors',
|
||||
credentials: 'include', // Abilita le credenziali (cookie, token)
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(modifiedRequest);
|
||||
const response = await fetch(request);
|
||||
|
||||
// Se la risposta non è valida, restituisci un errore personalizzato
|
||||
if (!response.ok) {
|
||||
console.warn('[SW] API Response Error:', response.status, response.statusText);
|
||||
|
||||
// Prova a recuperare una risposta dalla cache
|
||||
const cache = await caches.open('api-cache');
|
||||
const cachedResponse = await cache.match(request);
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
|
||||
// Fallback a una risposta personalizzata
|
||||
return new Response(JSON.stringify({
|
||||
error: 'API error',
|
||||
message: `❌ Invalid response from API: ${response.status} ${response.statusText}`,
|
||||
}), {
|
||||
status: response.status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}), { status: response.status, headers: { 'Content-Type': 'application/json' } });
|
||||
}
|
||||
|
||||
// Se la risposta è valida, restituiscila
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('[Service Worker] API request error ❌:', error);
|
||||
|
||||
// Prova a recuperare una risposta dalla cache
|
||||
const cache = await caches.open('api-cache');
|
||||
const cachedResponse = await cache.match(request);
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
|
||||
// Restituisci una risposta di errore personalizzata
|
||||
return new Response(JSON.stringify({
|
||||
error: 'Network error',
|
||||
message: '❌ Unable to fetch from API: ' + error.message,
|
||||
}), {
|
||||
status: 503,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}), { status: 503, headers: { 'Content-Type': 'application/json' } });
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per effettuare una richiesta di rete e memorizzare nella cache
|
||||
async function fetchAndCache(request) {
|
||||
const cache = await caches.open(DYNAMIC_CACHE);
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
|
||||
// Clona e salva la risposta nella cache solo se valida
|
||||
if (response.ok) {
|
||||
const responseClone = response.clone();
|
||||
cache.put(request, responseClone);
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('[SW] Fetch and cache error ❌:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Strategia di caching: stale-while-revalidate
|
||||
async function cacheWithStaleWhileRevalidate(request, event) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const cache = await caches.open(DYNAMIC_CACHE);
|
||||
|
||||
// Prova a recuperare la risorsa dalla cache
|
||||
const cachedResponse = await cache.match(request);
|
||||
if (cachedResponse) {
|
||||
// Restituisci la risposta in cache mentre aggiorni in background
|
||||
// Aggiorna in background mentre restituisci la risposta in cache
|
||||
event.waitUntil(fetchAndCache(request).catch((error) => {
|
||||
console.error('[SW] Background fetch and cache error ❌:', error);
|
||||
}));
|
||||
@@ -350,8 +333,7 @@ if (workbox) {
|
||||
|
||||
// Se non è in cache, fai la richiesta di rete
|
||||
try {
|
||||
const response = await fetchAndCache(request);
|
||||
return response;
|
||||
return await fetchAndCache(request);
|
||||
} catch (error) {
|
||||
console.error('[SW] Cache miss and network error ❌:', error);
|
||||
|
||||
@@ -359,40 +341,41 @@ if (workbox) {
|
||||
return new Response(JSON.stringify({
|
||||
error: 'Network error',
|
||||
message: 'Unable to fetch resource from network or cache.',
|
||||
}), {
|
||||
status: 503,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}), { status: 503, headers: { 'Content-Type': 'application/json' } });
|
||||
}
|
||||
}
|
||||
|
||||
// Listener per gestire tutte le richieste
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const request = event.request;
|
||||
const { request } = event;
|
||||
const url = new URL(request.url);
|
||||
try {
|
||||
|
||||
// Ignora richieste non gestibili
|
||||
if (request.method !== 'GET' || url.protocol !== 'https:') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Gestione richieste API
|
||||
if (url.hostname === API_DOMAIN) {
|
||||
if (debug) {
|
||||
console.log('E\' una RICHIESTA API ! ')
|
||||
// Ignora richieste non gestibili
|
||||
if (request.method !== 'GET' || url.protocol !== 'https:') {
|
||||
return;
|
||||
}
|
||||
event.respondWith(handleApiRequest(request));
|
||||
return;
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
console.log('E\' una RICHIESTA statica...')
|
||||
// Ignora richieste per file di sviluppo (es. /src/)
|
||||
if (url.pathname.startsWith('/src/') || url.search.includes('vue&type')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Gestione richieste API
|
||||
if (url.hostname === API_DOMAIN) {
|
||||
if (debug) console.log('E\' una RICHIESTA API!');
|
||||
event.respondWith(handleApiRequest(request));
|
||||
return;
|
||||
}
|
||||
|
||||
// Gestione risorse statiche e altre richieste
|
||||
if (debug) console.log('E\' una RICHIESTA statica...');
|
||||
event.respondWith(cacheWithStaleWhileRevalidate(request, event));
|
||||
} catch (error) {
|
||||
console.error('[Service Worker] Fetch error ❌:', error);
|
||||
}
|
||||
// Gestione risorse statiche e altre richieste
|
||||
event.respondWith(cacheWithStaleWhileRevalidate(request, event));
|
||||
});
|
||||
|
||||
|
||||
// Gestione degli errori non catturati
|
||||
self.addEventListener('unhandledrejection', event => {
|
||||
console.error('[Service Worker] Unhandled rejection ❌:', event.reason);
|
||||
|
||||
Reference in New Issue
Block a user