简体   繁体   English

服务工作者:不会缓存远程文件

[英]Service-Worker: Remote Files aren't getting cached

I am trying to cache static as well as remote files in my progressive web app. 我正在尝试在渐进式Web应用程序中缓存静态文件和远程文件。

The static files do get cached, but the remote file's aren't. 静态文件确实会被缓存,但远程文件却不会。

browser caching of static & remote files 浏览器缓存静态和远程文件

Here's the code: 这是代码:

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',
        ]))
    );
});

Thanks in advance 提前致谢

Here's the updated code 这是更新的代码

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);
    })
  );
});

I guess you are not using the same format as in this documentation . 我猜您使用的格式与本文档中的格式不同。 Here's a sample code from this thread : 这是此线程的示例代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM