I have a page and I have a service worker that works until I save the files in cache but when I put them offline I do not show the files in caches
index calling sw.js
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('sw.js').then(function(registration) {
// Si es exitoso
console.log('SW registrado correctamente');
}, function(err) {
// Si falla
console.log('SW fallo', err);
});
});
}
until it runs fine and now executes the sw
var nameapp = 'AppService_woker'
var versions = 'version_01'
var CACHE_NAME = nameapp + versions
var URLS = [
'./',
'./index.html',
'./sw.js',
'./manifiesto.json'
]
// Responder con recursos en caché
self.addEventListener('fetch', function (e) {
console.log('fetch request : ' + e.request.url)
e.respondWith(
caches.match(e.request).then(function (request) {
if (request) { // Si el caché está disponible, responda con caché
console.log('Respondiendo al Cache : ' + e.request.url)
return request
} else { // Si no hay caché, intente recuperar la solicitud
console.log('Los Archivos no se han guardado en cache : ' + e.request.url)
return fetch(e.request)
}
// Puede omitir si / else para console.log y poner una línea debajo también como esto.
// return request || fetch(e.request)
})
)
})
// Recursos de Cache
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
console.log('Instalando en Cache : ' + CACHE_NAME)
return cache.addAll(URLS)
})
)
})