我的服务人员有问题。我使用CAS登录到我的应用程序。当我关闭网页并尝试再次打开它时,出现以下错误:
The page isn't redirecting properly
An error occurred during a connection to start.oacloud.org.
This problem can sometimes be caused by disabling or refusing to accept cookies.
Here是显示重定向循环的屏幕截图。
这是我的服务人员:
cacheName = 'portal-cache';
const staticAssets = [
'portal_.php',
'css/portal.css',
'css/portalI.css',
'css/portalJ.css',
'css/portalE.css',
'js/jquery-ui.css',
'js/jquery.js',
'js/jquery-ui.js',
'js/lodash.js',
'portal.js',
'js/jquery.ui.touch-punch.js',
'js/jquery-ui.structure.css',
'js/jquery-ui.structure.min.css',
'js/jquery-ui.theme.css',
'js/jquery-ui.theme.min.css'
];
self.addEventListener('install', function(event)
{
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(staticAssets);
})
);
});
self.addEventListener('activate', function(e)
{
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
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('portal-dynamic')
.then(function(cache)
{
cache.put(event.request.url, res.clone());
return res;
})
}).catch(function(err)
{
return caches.open('error messages').then(function(cache)
{
return cache.match('portal_.php');
});
});
}
})
);
});
我到底在做什么错,导致此循环发生,我该如何解决?
答案 0 :(得分:0)
我设法先从互联网上获取内容,然后再从缓存中获取内容来解决此问题:
self.addEventListener('fetch', function(event) {
var requestURL = new URL(event.request.url);
if(requestURL.host =='your CAS server' || (requestURL.host =='page to redirect to' && requestURL.pathname == 'your server')){
console.log('IN: '+requestURL);
event.respondWith(fetch(event.request)
.catch(function() {
return new Response('{status: \'no internet connection\'}',
{headers: { 'Content-Type': 'text/html' }, status : 404}
);
}));
return;
}
console.log('The service worker is serving the asset.');
event.respondWith(
//check first if the request is for a static asset and serve it directly from cache
caches.open(cacheStatic).then(function(cache) {
return cache.match(event.request).then(function (response) {
return response ||
//if asset is not in static cache try to take it from the network
fetch(event.request).then(function(responseD) {
//save it in dynamic cache for when there is no network
return caches.open(cacheDynamic).then(function(cacheD) {
cacheD.put(event.request, responseD.clone());
return responseD;
});
}).catch(function(error) {
console.log('catch: '+requestURL);
//if unable to take it from the network, take it from dynamic cache
return caches.open(cacheDynamic).then(function(cacheD) {
return cacheD.match(event.request).then(function (response) {
return response || new Response('{status: \'no internet connection\'}',
{headers: { 'Content-Type': 'text/html' }, status : 404}
);
});
});
});
})
})
)
});