sw.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // use a cacheName for cache versioning
  2. var cacheName = 'v1:static';
  3. // during the install phase you usually want to cache static assets
  4. self.addEventListener('install', function(e) {
  5. // once the SW is installed, go ahead and fetch the resources to make this work offline
  6. e.waitUntil(
  7. caches.open(cacheName).then(function(cache) {
  8. return cache.addAll([
  9. './index.html',
  10. './offline.html',
  11. './resources/logo.png'
  12. ]).then(function() {
  13. self.skipWaiting();
  14. });
  15. })
  16. );
  17. });
  18. // when the browser fetches a url
  19. self.addEventListener('fetch', function(event) {
  20. // either respond with the cached object or go ahead and fetch the actual url
  21. event.respondWith(
  22. caches.match(event.request).then(function(response) {
  23. if (response) {
  24. // retrieve from cache
  25. return response;
  26. }
  27. // fetch as normal
  28. return fetch(event.request);
  29. })
  30. );
  31. });