Reputation: 31
I am struggling with making a simple service worker to work offline. I tried a lot of examples without success. First I thought it was because I am using a dynamic php based website or maybe it was my server or my htaccess. It is a mystery for me. Everything work on my localhost while fetching the index.html, but on a server the page can't load offline even if everything else is loading while analysing the chrome devtools network tab.
So I am trying to make this simple html offline PWA on my server and I create the same source on another server.
https://sw.punchunique.com
https://punchunique.neocities.org/test.html or
https://punchunique.neocities.org/
Maybe because I don't have a fallback.html but why would I need one if it's already loaded in the cache
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('mysite-static-v3').then(function(cache) {
return cache.addAll([
'index.html',
'punch-fixed.css',
'general.css',
'punch-homepage.css',
'thesansextralight_plain-webfont.woff',
'thesansextralight_plain-webfont.woff2',
'TweenMax.min.js',
'PLUGINS.js',
'punch.webmanifest',
'sw-demo.js',
'favicon.ico',
'favicon-16x16.png',
'favicon-32x32.png',
'favicon-194x194.png',
'apple-touch-icon.png',
'apple-touch-icon-72x72.png',
'apple-touch-icon-120x120.png',
'apple-touch-icon-144x144.png',
'apple-touch-icon-152x152.png',
'android-chrome-96x96.png',
'android-chrome-192x192.png',
'android-chrome-512x512.png',
'mstile-48x48.png',
'mstile-144x144.png',
'mstile-270x270.png',
'mstile-558x558.png',
'mstile-558x270.png',
'bcg-img-sect1.jpg',
'green-hook.png',
]);
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function(event) {
// Cache only
// If a match isn't found in the cache, the response
// will look like a connection error
// event.respondWith(caches.match(event.request));
// Network only
// event.respondWith(fetch(event.request));
// or simply don't call event.respondWith, which
// will result in default browser behaviour
// CACHE then NETWORK
event.respondWith(
caches.open('mysite-dynamic').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});
I have this when offline
An unknown error occurred when fetching the script. and
Uncaught (in promise) TypeError: Failed to fetch
but All the caches files are present on server and nothing is missing.
Upvotes: 1
Views: 2538
Reputation: 51
The HTML root files like index.html etc. are there, and you can process them, but the console.log(event.request.url) or self.console.log(event.request.url) do not output them.
Upvotes: 0
Reputation: 195
In fetch Event use match() function with {ignoreVary:true} as second parameter
caches.match(event.request,{ignoreVary:true})
.then(function(response) {....}
What it does is avoid matching the headers of the request. Due to header matching , your application will work on localhost and not in live environment.
Upvotes: 1
Reputation: 5260
Here is the perfect working example of service worker with static and dynamic caching
var CACHE_STATIC_NAME = 'static-v4';
var CACHE_DYNAMIC_NAME = 'dynamic-v2';
self.addEventListener('install', function(event) {
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache) {
console.log('[Service Worker] Precaching App Shell');
cache.addAll([
'/',
'/index.html',
'/src/js/app.js',
'/src/js/feed.js',
'/src/js/promise.js',
'/src/js/fetch.js',
'/src/js/material.min.js',
'/src/css/app.css',
'/src/css/feed.css',
'/src/images/main-image.jpg',
'https://fonts.googleapis.com/css?family=Roboto:400,700',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://cdnjs.cloudflare.com/ajax/libs/material-design-lite/1.3.0/material.indigo-pink.min.css'
]);
})
)
});
self.addEventListener('activate', function(event) {
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME) {
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
} else {
return fetch(event.request)
.then(function(res) {
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache) {
cache.put(event.request.url, res.clone());
return res;
})
})
.catch(function(err) {
});
}
})
);
});
Upvotes: 2