Created
May 17, 2016 07:07
-
-
Save itsmethemojo/d6f0dd6937b7f8bee9a2857f83ae9043 to your computer and use it in GitHub Desktop.
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 cacheName = 'v1', | |
checkinDataRegex = /applicable\?pnr=([a-zA-Z0-9]+)&lastname=([a-zA-Z]+)/ | |
ticketRegex = /image\/pnr\/([a-zA-Z0-9]+)\/lastname\/([a-zA-Z]+)\/ticket\/([0-9]+)/; | |
self.addEventListener('fetch', function(event) { | |
var request = event.request, | |
matchCheckin = checkinDataRegex.exec(request.url); | |
if (matchCheckin) { | |
// Use regex capturing to grab only the bit of the URL | |
// that we care about (in this case the checkinID) | |
var cacheRequest = new Request(match[1]); | |
event.respondWith( | |
caches.match(cacheRequest).then(function(response) { | |
return response || fetch(request).then(function(response) { | |
caches.open(cacheName).then(function(cache) { | |
cache.put(cacheRequest, response); | |
}) | |
return response; | |
}); | |
}) | |
); | |
} | |
if (ticketRegex) { | |
// disable the image (by replacing it) | |
[...] | |
} | |
[...] | |
}); | |
// communication between the service worker and the app.js | |
self.addEventListener("message", function(event) { | |
var data = event.data; | |
switch(data.command) { | |
case 'deleteCheckin': | |
// open current cache | |
caches.open(cacheName).then(function(cache) { | |
// remove the flight data (JSON) | |
cache.delete(data.checkinID).then(function(success) { | |
event.ports[0].postMessage({ | |
error: success ? null : 'Item was not found in the cache.' | |
}); | |
)}; | |
}) | |
break; | |
[...] | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment