我在具有登录页面的 Web 应用上实现服务工作线程缓存时遇到问题。
发生的情况是,用户登录,Service Worker 安装并激活,但是当用户单击链接时,即使他们已经登录,它也会将他们带回登录页面!
有人能给我指出正确的解决方法吗?
提前非常感谢
最佳答案
您可能犯的错误是尝试在用户登录之前缓存需要用户权限的页面。因此会发生的情况是:浏览器尝试检索路由,但服务器给出登录页面,因为用户没有登录已登录。
您可能需要做什么:
- 您等待用户登录您的应用。
- 用户登录后,您就可以注册 Service Worker。
- 要以登录用户身份缓存页面,您必须发送 cookie 信息 with
credentials: include
。它将是这样的:
``` /* sw.js */
var cacheName = 'yourCacheName-vx.x.x';
// The route for which you need to be logged in. It's a Request Object.
var appRequest = new Request('/da-private-route', { credentials: 'include' });
var urlsToCache = [
appRequest, // you add it to your things to cache
'/manifest.json',
'/css/styles.css',
'/js/scripts.js',
// other resources…
];
// Installation.
self.addEventListener('install', e => {
e.waitUntil(
self.skipWaiting().then(function() {
caches.open(cacheName).then(function(cache) {
return cache.addAll(urlsToCache);
})
})
);
});
/* [continue your sw…] */
```
关于php - 服务人员登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666079/