服务工作者传播

时间:2017-11-02 07:58:06

标签: service-worker service-worker-events

我有一个用于缓存图像的服务工作者,这个服务工作者只在前端模板中注册,但它仍然会传播到我的管理模板中。

这会导致我的表单无法预测,因为验证令牌会受到影响。

使用一些console.log我认为安装事件在到达请求的页面之前被触发但我无法确定那里的当前/下一个URL。

如何阻止服务工作者传播到管理面板并干扰页面?我只想要缓存资产。

这是我的服务工作者,只要相关:

const PRECACHE = 'precache-v1.0.0';
const RUNTIME = 'runtime';

// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
    "public",
    "media",
    "unify",
];

importScripts('./cache-polyfill.js');

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', function(event) {
    console.log('installing resources');
    event.waitUntil(
        caches.open(PRECACHE)
            //.then(cache => cache.addAll(PRECACHE_URLS))
            .then(self.skipWaiting())
    );
});

// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', function(event) {
    const currentCaches = [PRECACHE, RUNTIME];
    event.waitUntil(
        caches.keys().then(cacheNames => {
            return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
        }).then(cachesToDelete => {
            return Promise.all(cachesToDelete.map(cacheToDelete => {
                return caches.delete(cacheToDelete);
            }));
        }).then(() => self.clients.claim())
    );
});

// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
    // Skip cross-origin requests, like those for Google Analytics.

    if (event.request.method === "GET") {
        if (event.request.url.indexOf(PRECACHE_URLS) > -1) {
            console.log("fetching " + event.request.url + " by the service worker");
            event.respondWith(
                caches.match(event.request).then(cachedResponse => {
                    if (cachedResponse) {
                        return cachedResponse;
                    }

                    return caches.open(RUNTIME).then(cache => {
                        return fetch(event.request).then(response => {
                            // Put a copy of the response in the runtime cache.
                            return cache.put(event.request, response.clone()).then(() => {
                                console.log('cached: ' + event.request.url);
                                return response;
                            });
                        });
                    });
                })
            );
        }
        else {
            console.log("fetching " + event.request.url + " by service worker blocked, it's not a resource");
        }
    }
    return fetch(event.request);
});

1 个答案:

答案 0 :(得分:1)

问题很可能是您的管理页面位于SW范围内。这意味着您的SW控件例如。 /和您的管理页面中的所有内容都位于/ admin /或其他内容中。

您可以通过检查SW正在拦截的抓取请求来阻止此行为。类似的东西:

if (event.request.url.match('^.*(\/admin\/).*$')) {
    return false;
}

这应该是SW的fetch监听器中的第一件事。它检查是否从管理页面收到了某些请求,然后取消了它是否确实如此。否则,它会继续正常。