Created
February 4, 2020 14:59
-
-
Save leon0707/393bbdc6a73b9a298b7d928bea228f20 to your computer and use it in GitHub Desktop.
cache worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var CACHE_NAME = 'my-site-cache-v1'; | |
| var urlsToCache = [ | |
| '/', | |
| '/styles/main.css', | |
| '/script/main.js' | |
| ]; | |
| self.addEventListener('install', function(event) { | |
| // Perform install steps | |
| event.waitUntil( | |
| caches.open(CACHE_NAME) | |
| .then(function(cache) { | |
| console.log('Opened cache'); | |
| return cache.addAll(urlsToCache); | |
| }); | |
| ); | |
| }); | |
| self.addEventListener('fetch', function(event) { | |
| event.respondWith( | |
| caches.match(event.request) | |
| .then(function(response) { | |
| // Cache hit - return response | |
| if (response) { | |
| return response; | |
| } | |
| return fetch(event.request); | |
| } | |
| ) | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment