2017-10-16 3 views
0

내 웹 사이트 서비스 종사자 설정에 문제가 있습니다.서비스 종사자와 start_url 캐싱 방지

CSS/js/font 및 일부 이미지/svg 만 캐시하고 싶습니다. 매분마다 모든 정보가 업데이트되므로 HTML을 캐시하고 싶지는 않습니다.

다소 효과가 있지만 내 스마트 폰에서 이미 내가 추가 한 경우에도 "홈 화면에 추가"알림이 계속 표시됩니다. Chrome Dev 앱에서는 추가 버튼이 표시되지 않습니다.

또한 등대와 나는 다음과 같은 오류를 얻을 :

"사용자가 웹 응용 프로그램을 설치하라는 메시지가 표시되지 않습니다 , 실패 "는 오프라인 200 때 응답하지 않습니다 "매니페스트 START_URL가 아닌 서비스 근로자가 캐시합니다. "

지금 내 sw.js는 다음과 같습니다. 보시다시피 나는 HTML을 캐싱하고 쿠키가 작동하지 않았기 때문에 가져 오기 부분에 주석을 달았습니다.

간단한 서비스 근로자 "템플릿"을 사용할 수 있습니까?

const PRECACHE = 'app-name'; 
const RUNTIME = 'runtime'; 

// A list of local resources we always want to be cached. 
const PRECACHE_URLS = [ 
'/css/file.css', 
'/js/file.js', 
'/images/logo.png', 
'/fonts/roboto/Roboto-Regular.woff2' 
] 

// The install handler takes care of precaching the resources we always need. 
self.addEventListener('install', event => { 
    event.waitUntil(
    caches.open(PRECACHE) 
     .then(cache => cache.addAll(PRECACHE_URLS)) 
     .then(self.skipWaiting()) 
); 
}); 

// The activate handler takes care of cleaning up old caches. 
self.addEventListener('activate', event => { 
    const currentCaches = [PRECACHE, RUNTIME]; 
    event.waitUntil(
    caches.keys().then(cacheNames => { 
     return cacheNames.filter(cacheName => !currentCaches.includes(cacheName)); 
    }).then(cachesToDelete => { 
     return Promise.all(cachesToDelete.map(cacheToDelete => { 
     return caches.delete(cacheToDelete); 
     })); 
    }).then(() => self.clients.claim()) 
); 
}); 

// The fetch handler serves responses for same-origin resources from a cache. 
// If no response is found, it populates the runtime cache with the response 
// from the network before returning it to the page. 
self.addEventListener('fetch', event => { 
    // Skip cross-origin requests, like those for Google Analytics. 
    // if (event.request.url.startsWith(self.location.origin)) { 
    // event.respondWith(
    //  caches.match(event.request).then(cachedResponse => { 
    //  if (cachedResponse) { 
    //   return cachedResponse; 
    //  } 

    //  return caches.open(RUNTIME).then(cache => { 
    //   return fetch(event.request).then(response => { 
    //   // Put a copy of the response in the runtime cache. 
    //   return cache.put(event.request, response.clone()).then(() => { 
    //    return response; 
    //   }); 
    //   }); 
    //  }); 
    //  }) 
    // ); 
    // } 
});  

답변

0

가 나는 배너가 나타나기는하지만 등대에 의해 주어진 두 가지 오류가 propably index.html을 매우 START_URL의 누락 캐싱에 관련된 설치 이유를 모르겠어요. 따라서 Lighthouse는 여기에 설명 된 캐싱 전략을 따르는 경우 항상 그러한 사실을 알려줍니다.

Workbox 및 런타임 캐싱을 시도해 볼 것을 제안합니다. 런타임 캐싱은 다음과 같이 작동합니다. * .svg, * .css 등의 URL을 지정하면 클라이언트가 먼저 요청하면 SW가 캐시합니다. 앞으로 파일이 이미 캐시 될 때 SW는 캐시에서 클라이언트로 파일을 제공합니다. 근본적으로 당신은을 만났을 때 이것과 그 종류의 URL을 에 캐시하도록 SW에게 말합니다.

런타임 캐싱은 파일 집합을 캐시하기 위해 미리 캐시 (Workbox에서도 찾을 수 있음)를 수반 할 수 있습니다.

은 여기를 체크 아웃 : https://workboxjs.org

그들은 빌드 도구에 대한 예제와 플러그인의 몇 가지있다.