2

我想緩存我的漸進網站應用程序中的靜態以及遠程文件。服務工作者:遠程文件沒有得到緩存

靜態文件確實被緩存,但遠程文件不是。

browser caching of static & remote files

下面的代碼:提前

self.addEventListener('install', e => { 
    e.waitUntil(
     caches.open('offline') 
     .then(cache => cache.addAll([ 
      '/', 
      'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', 
      'https://code.jquery.com/jquery-3.2.1.js', 
      'scripts/firebase.js', 
      'scripts/localForage.js', 
      'old-index.html', 
      'scripts/main.js', 
     ])) 
    ); 
}); 

感謝

下面是更新後的代碼

var filesToCache = [ 
    '.', 
    "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.css", 
    'https://code.jquery.com/jquery-3.2.1.js', 
    'scripts/firebase.js', 
    'scripts/localForage.js', 
    'cce.html', 
    'scripts/main.js', 
    'scripts/secondary.js', 
    'https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css' 
]; 

var staticCacheName = 'pages-cache-v1'; 

self.addEventListener('install', function(event) { 
    console.log('Attempting to install service worker and cache static assets'); 
    event.waitUntil(
    caches.open(staticCacheName) 
    .then(function(cache) { 
     return cache.addAll(filesToCache); 
    }) 
); 
}); 

回答

0

我猜你不使用的格式相同在這documentation。下面是這個thread一個示例代碼:

self.addEventListener('install', function(e) { 
    e.waitUntil(
    caches.open('airhorner').then(function(cache) { 
     return cache.addAll([ 
     'https://paul.kinlan.me/' 
     ]); 
    }) 
); 
}); 
+0

我試過的示例代碼,以及在一個文檔中,但問題仍然涉及。 – user3287355

+0

是否有任何其他問題,哪些代碼不起作用? – user3287355

相關問題