服务人员addAll()函数不起作用

时间:2018-10-12 18:06:44

标签: javascript php progressive-web-apps

我正在尝试构建一个渐进式Web应用程序,并且我想使用缓存API来缓存我的所有资产,但它无法正常工作。每当我检查缓存文件时,它始终为空。

这是我的代码:

self.addEventListener('install', function(event) {
  console.log('[Service Worker] Installing Service Worker ...', event);
  event.waitUntil(
    caches.open('static')
      .then(function(cache) {
        console.log('[Service Worker] Precaching App Shell');
        cache.addAll([
          '/',
          'src/css/index.html',
          'src/css/app1.js',
          'src/css/fatch1.js',
          'src/css/feed1.js',
          'src/css/promise1.js',
          'src/css/material.min1.js',
          'src/css/app.css',
          'src/css/feed.css',
          'https://fonts.googleapis.com/css?family=Roboto:400,700',
          'https://fonts.googleapis.com/icon?family=Material+Icons',
          'https://cdnjs.cloudflare.com/ajax/libs/material-design-lite/1.3.0/material.indigo-pink.min.css',
          'src/images/main-image.jpg'
          ]);
      })
  )
});

self.addEventListener('activate', function(event) {
  console.log('[Service Worker] Activating Service Worker ....', event);
  return self.clients.claim();
});

self.addEventListener('fetch', function(event) {
  console.log('[Service Worker] Fetching something ....', event);
  event.respondWith(
    caches.match(event.request).
    then(function(response){
            if (response) {
                return response;
            }else{
                return fetch(event.request);
            }
     })
    );
});

0 个答案:

没有答案