Guest User

Untitled

a guest
Dec 11th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. self.addEventListener('fetch', function(e) {
  2. console.log('[ServiceWorker] Fetch', e.request.url);
  3. if (cdnToCache.find((element)=>{return e.request.url.indexOf(element) === 0;})) {
  4. e.respondWith(
  5. caches.match(e.request.url).then(function(response) {
  6. if (response){
  7. return response
  8. }else{
  9. return fetch(e.request)
  10. .then(function(response) {
  11. return caches.open(cacheCdnName).then(function(cache) {
  12. cache.put(e.request.url, response.clone());
  13. console.log('[ServiceWorker] Fetched&Cached Data');
  14. return response;
  15. });
  16. })
  17. }
  18. })
  19. );
  20. } else {
  21. e.respondWith(
  22. caches.match(e.request).then(function(response) {
  23. return response || fetch(e.request);
  24. })
  25. );
  26. }
  27. });
Add Comment
Please, Sign In to add comment