簡體   English   中英

服務工作者未獲取緩存文件

[英]Cached file is not being fetched by the service worker

當我嘗試訪問http://localhost/visites/時,它應該獲取預緩存的文件visites/index.php 所以我想我必須在某個地方指出這個特定的路線與那個文件相匹配,你知道我該怎么做嗎?

我把我的 SW 代碼留在這里以防萬一:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM