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
| self.addEventListener('fetch', event => { | |
| console.log('[Service Worker] - fetch event') | |
| event.respondWith( | |
| self | |
| .caches | |
| .match(event.request) | |
| .then(result => { | |
| if (result == null) { | |
| return fetch(event.request) |
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
| document | |
| .querySelector('.cache-article') | |
| .addEventListener('click', (event) => { | |
| event.preventDefault() | |
| const id = this.dataset.articleId | |
| caches | |
| .open('mysite-article-' + id) | |
| .then((cache) => { |
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
| self.addEventListener('fetch', function(event) { | |
| event.respondWith( | |
| self | |
| .caches | |
| .open('mysite-dynamic') | |
| .then(function(cache) { | |
| return cache | |
| .match(event.request) | |
| .then(function (response) { | |
| if (response != null) { |
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
| self.addEventListener('fetch', function(event) { | |
| event.respondWith(caches.match(event.request)) | |
| }) |
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
| self.addEventListener('fetch', function(event) { | |
| event.respondWith(fetch(event.request)) | |
| }) |
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
| self.addEventListener('fetch', function(event) { | |
| event.respondWith( | |
| caches.match(event.request).then(function(response) { | |
| return response || fetch(event.request) | |
| }) | |
| ) | |
| }) |
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
| self.addEventListener('fetch', function(event) { | |
| event.respondWith( | |
| fetch(event.request).catch(function() { | |
| return caches.match(event.request) | |
| }) | |
| ) | |
| }) |
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
| // Here is the code in the app.js: | |
| var networkDataReceived = false | |
| startSpinner() | |
| // fetch fresh data | |
| var networkUpdate = fetch('/data.json') | |
| .then(function(response) { | |
| return response.json() | |
| }) |
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
| // Here is the code in the sw.js: | |
| self.addEventListener('fetch', function(event) { | |
| event.respondWith( | |
| caches | |
| .open('mysite-dynamic') | |
| .then(function(cache) { | |
| return fetch(event.request) | |
| .then(function(response) { | |
| cache.put(event.request, response.clone()) |
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
| self.addEventListener('fetch', function(event) { | |
| event.respondWith( | |
| // Try the cache | |
| caches | |
| .match(event.request) | |
| .then(function(response) { | |
| // Fall back to network | |
| return response || fetch(event.request) | |
| }) | |
| .catch(function() { |