服务工作者登录页面

Kyl*_*ker 2 php caching login service-worker

我在具有登录页面的Web应用程序上实现服务工作者缓存时遇到问题.

会发生什么,用户登录,服务工作者安装和激活,但是当用户点击链接时,即使他们已经登录,它也会将他们带回登录页面!

有人能指出我正确的方向来解决问题吗?

提前谢谢了

med*_*uz' 5

您可能错误的是在用户登录之前尝试缓存需要用户权限的页面.所以会发生的情况是:浏览器尝试检索路由,但服务器会提供登录页面,因为用户未登录.

你可能需要做什么:

  1. 您等待用户登录您的应用.
  2. 用户已登录,而不是您注册服务工作者.
  3. 缓存页面作为登录的用户,你必须发送的cookie信息,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…] */
Run Code Online (Sandbox Code Playgroud)

```