Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 服务工作者未提取缓存文件_Javascript_Service Worker_Service Worker Config - Fatal编程技术网

Javascript 服务工作者未提取缓存文件

Javascript 服务工作者未提取缓存文件,javascript,service-worker,service-worker-config,Javascript,Service Worker,Service Worker Config,当我尝试访问http://localhost/visites/它应该获取预缓存文件visites/index.php。所以我想我必须指出某个地方,这个特定的路径匹配那个文件,你知道我怎么做吗 我将我的软件代码留在这里,以防万一: const cacheName = 'v1'; const cacheAssets = [ 'accueil.php', 'js/accueil.js', 'visites/index.php', 'js/visites.js',

当我尝试访问
http://localhost/visites/
它应该获取预缓存文件
visites/index.php
。所以我想我必须指出某个地方,这个特定的路径匹配那个文件,你知道我怎么做吗

我将我的软件代码留在这里,以防万一:

const cacheName = 'v1';

const cacheAssets = [
    'accueil.php',
    'js/accueil.js',
    'visites/index.php',
    'js/visites.js',
    'js/global.js',
    'css/styles.css',
    'charte/PICTOS/BTN-Visites.png',
    'charte/STRUCTURE/GESTEL-Logo.png',
    'charte/PICTOS/BTN-Animaux.png',
    'charte/PICTOS/BTN-Deconnexion.png',
    'charte/PICTOS/BTN-Fermes.png',

];

// Call Install Event
self.addEventListener('install', e => {
  console.log('Service Worker: Installed');

  e.waitUntil(
    caches
      .open(cacheName)
      .then(cache => {
        console.log('Service Worker: Caching Files');
        cache.addAll(cacheAssets);
      })
      .then(() => self.skipWaiting())
  );
});

// Call Activate Event
self.addEventListener('activate', e => {
  console.log('Service Worker: Activated');
  // Remove unwanted caches
  e.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(
        cacheNames.map(cache => {
          if (cache !== cacheName) {
            console.log('Service Worker: Clearing Old Cache');
            return caches.delete(cache);
          }
        })
      );
    })
  );
});

// Call Fetch Event
self.addEventListener('fetch', e => {
  console.log('Service Worker: Fetching');
  e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
})

您可以在
fetch
处理程序中包含一些逻辑,用于说明此路由信息:

self.addEventListener('fetch', e => {
  // Use a URL object to simplify checking the path.
  const url = new URL(e.request.url);

  // Alternatively, check e.request.mode === 'navigate' if
  // you want to match a navigation to any URL on your site.
  if (url.pathname === '/visites/') {
    e.respondWith(caches.match('visites/index.php'));
    // Return after responding, so that the existing
    // logic doesn't get triggered.
    return;
  }

  e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
});