Guest User

Untitled

a guest
Oct 16th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. const PRECACHE = 'app-name';
  2. const RUNTIME = 'runtime';
  3.  
  4. // A list of local resources we always want to be cached.
  5. const PRECACHE_URLS = [
  6. '/css/file.css',
  7. '/js/file.js',
  8. '/images/logo.png',
  9. '/fonts/roboto/Roboto-Regular.woff2'
  10. ]
  11.  
  12. // The install handler takes care of precaching the resources we always need.
  13. self.addEventListener('install', event => {
  14. event.waitUntil(
  15. caches.open(PRECACHE)
  16. .then(cache => cache.addAll(PRECACHE_URLS))
  17. .then(self.skipWaiting())
  18. );
  19. });
  20.  
  21. // The activate handler takes care of cleaning up old caches.
  22. self.addEventListener('activate', event => {
  23. const currentCaches = [PRECACHE, RUNTIME];
  24. event.waitUntil(
  25. caches.keys().then(cacheNames => {
  26. return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
  27. }).then(cachesToDelete => {
  28. return Promise.all(cachesToDelete.map(cacheToDelete => {
  29. return caches.delete(cacheToDelete);
  30. }));
  31. }).then(() => self.clients.claim())
  32. );
  33. });
  34.  
  35. // The fetch handler serves responses for same-origin resources from a cache.
  36. // If no response is found, it populates the runtime cache with the response
  37. // from the network before returning it to the page.
  38. self.addEventListener('fetch', event => {
  39. // Skip cross-origin requests, like those for Google Analytics.
  40. // if (event.request.url.startsWith(self.location.origin)) {
  41. // event.respondWith(
  42. // caches.match(event.request).then(cachedResponse => {
  43. // if (cachedResponse) {
  44. // return cachedResponse;
  45. // }
  46.  
  47. // return caches.open(RUNTIME).then(cache => {
  48. // return fetch(event.request).then(response => {
  49. // // Put a copy of the response in the runtime cache.
  50. // return cache.put(event.request, response.clone()).then(() => {
  51. // return response;
  52. // });
  53. // });
  54. // });
  55. // })
  56. // );
  57. // }
  58. });
Add Comment
Please, Sign In to add comment