I use a service worker on a WordPress site and will mix up redirects from https://example.com/page to https://example.com/page/ .
After the first download, by going to the URL without a slash, Blink browsers say, "This site cannot be reached," and Firefox says, "Damaged content error."
Based on my reading https://medium.com/@boopathi/service-workers-gotchas-44bec65eab3f#.hf3r4pbcs and How to change request headers? I think I need to determine when the response is 3xx and set the redirection mode to manual.
However, none of what I tried based on my research worked. How to fix it?
Current service work file:
var cacheName = 'v14';
var urlsToCache = [
];
self.addEventListener('install', event => {
function onInstall(event) {
return caches.open(cacheName)
.then(cache => cache.addAll(urlsToCache));
}
event.waitUntil(
onInstall(event)
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', event => {
function onActivate (event) {
return caches.keys()
.then(cacheKeys => {
var oldCacheKeys = cacheKeys.filter(key => key.indexOf(cacheName) !== 0);
var deletePromises = oldCacheKeys.map(oldKey => caches.delete(oldKey));
return Promise.all(deletePromises);
})
}
event.waitUntil(
onActivate(event)
.then(() => self.clients.claim ())
);
});
self.addEventListener('fetch', event => {
function onFetch (event) {
if (event.request.url.match(/wp-admin/) || event.request.url.match(/wp-login/) || event.request.url.match(/preview=true/) || event.request.url.match(/wp-includes/) || event.request.url.match(/plugins/) || event.request.url.match(/google-analytics/) || event.request.url.match(/gravatar\.com/) || event.request.url.match(/login/) || event.request.url.match(/admin/) || event.request.method !== 'GET') {
return;
}
var request = event.request,
acceptHeader = request.headers.get('Accept'),
resourceType = 'static';
if(acceptHeader.indexOf('text/html') !== -1) {
resourceType = 'content';
} else if(acceptHeader.indexOf('image') !== -1) {
resourceType = 'image';
}
if(resourceType === 'content') {
event.respondWith(fetch(request.url, {
method: request.method,
headers: request.headers,
mode: 'same-origin',
credentials: request.credentials,
redirect: 'manual'
})
.then(response => addToCache(request, response))
.catch(() => fetchFromCache(event))
.catch(() => offlineResponse(resourceType))
)
}
else if(resourceType === 'static' || resourceType === 'image') {
event.respondWith(fetchFromCache(event)
.catch(() => fetch(request))
.then(response => addToCache(request, response))
.catch(() => offlineResponse(resourceType))
)
}
}
onFetch(event);
});
function addToCache(request, response) {
if(response.ok) {
var copy = response.clone();
caches.open(cacheName)
.then(cache => {
cache.put(request, copy);
});
return response;
}
}
function fetchFromCache (event) {
return caches.match(event.request)
.then(response => {
if(!response) {
throw Error('${event.request.url} not found in cache');
}
return response;
});
}
function offlineResponse (resourceType) {
if(resourceType === 'content') {
return caches.match('/offline/');
}
return undefined;
}
source
share