Merge pull request #10436 from GeoscienceAustralia/fix-service_worker_api-bug

Fix service_worker_api bug
This commit is contained in:
Nathan Shively-Sanders
2016-08-09 15:05:21 -07:00
committed by GitHub
2 changed files with 16 additions and 13 deletions

View File

@@ -19,13 +19,11 @@ self.addEventListener('fetch', function(event: FetchEvent) {
});
self.caches.open('v1').then(function(cache: Cache) {
cache.matchAll('/images/').then(function(response: Array<Request>) {
cache.matchAll('/images/').then(function(response: Array<Response>) {
response.forEach(function(element, index, array) {
cache.delete(element);
cache.delete(element.url);
});
});
});
self.addEventListener('install', function(event: InstallEvent) {
@@ -56,7 +54,11 @@ self.addEventListener('install', function(event: InstallEvent) {
});
self.addEventListener('fetch', function(event: FetchEvent) {
var cachedResponse = self.caches.match(event.request).catch(function() {
var cachedResponse = self.caches.match(event.request).then(function(response: Response) {
if (response) {
return response;
}
}).catch(function() {
return self.fetch(event.request).then(function(response: Response) {
return self.caches.open('v1').then(function(cache) {
cache.put(event.request, response.clone());
@@ -71,8 +73,8 @@ self.addEventListener('fetch', function(event: FetchEvent) {
});
self.caches.open('v1').then(function(cache) {
cache.match('/images/image.png').then(function(response) {
cache.delete(response);
cache.match('/images/image.png').then(function(response: Response) {
cache.delete(response.url);
});
});
@@ -185,4 +187,4 @@ self.addEventListener('notificationclick', function(event: NotificationEvent) {
if (self.clients.openWindow)
return self.clients.openWindow('/');
}));
});
});

View File

@@ -3,7 +3,8 @@
// Definitions by: Tristan Caron <https://github.com/tristancaron>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../es6-promise/es6-promise.d.ts" />
// <reference path="../es6-promise/es6-promise.d.ts" /> // REMOVED third "/" so this doesn't fire. Problem with duplicate Promises
// between es6 and typescript - https://github.com/DefinitelyTyped/DefinitelyTyped/issues/5015
/**
* Provides methods relating to the body of the response/request, allowing you
@@ -279,16 +280,16 @@ interface Cache {
* @param request The Request you are attempting to find in the Cache.
* @param {CacheOptions} options
*/
match(request: Request | string, options?: CacheOptions): Promise<Request>;
match(request: Request | string, options?: CacheOptions): Promise<Response>;
/**
* Returns a Promise that resolves to an array of all matching requests in
* Returns a Promise that resolves to an array of all matching responses in
* the Cache object.
*
* @param request The Request you are attempting to find in the Cache.
* @param {CacheOptions} options
*/
matchAll(request: Request | string, options?: CacheOptions): Promise<Array<Request>>;
matchAll(request: Request | string, options?: CacheOptions): Promise<Array<Response>>;
/**
* Returns a Promise that resolves to a new Cache entry whose key
@@ -893,4 +894,4 @@ interface Window extends ServiceWorkerGlobalScope {
interface NotificationEvent extends Event, ExtendableEvent {
notification: any;
}
}