-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
{ | ||
"predef": [ | ||
"toolbox", | ||
"CACHE_PREFIX", | ||
"CACHE_VERSION", | ||
"Promise", | ||
"Request", | ||
"caches", | ||
"logDebug", | ||
"navigator", | ||
"self", | ||
"caches", | ||
"Promise" | ||
"toolbox" | ||
], | ||
"node": true | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
function getFallbackFromCache(request, values, options) { | ||
logDebug('Fetching from fallback url: '+ options.fallbackURL +'for url: '+request.url); | ||
var req = new Request(options.fallbackURL, request); | ||
return toolbox.cacheFirst(req, values, options).then(function(response) { | ||
if (response) { | ||
logDebug('Got fallback response from cache',response); | ||
return response; | ||
} | ||
}); | ||
} | ||
|
||
function fallbackResponse(request, values, options) { | ||
logDebug('Looking for fallback for:', request.url); | ||
return toolbox.networkFirst(request, values, options).then(function(response){ | ||
logDebug('Default request network got response:', request.url, response); | ||
if (!response) { | ||
logDebug('Fetching from fallback url: '+ options.fallbackURL +'for url: '+request.url); | ||
var req = new Request(options.fallbackURL, request); | ||
var originalResponse = response; | ||
return toolbox.cacheFirst(req, values, options).then(function(response) { | ||
if (response) { | ||
logDebug('Got response from cache',response); | ||
return response; | ||
} else { | ||
return originalResponse; | ||
} | ||
return new Promise(function(resolve, reject) { | ||
toolbox.networkFirst(request, values, options).then(function(response) { | ||
if (response) { | ||
resolve(response); | ||
} else { | ||
logDebug('Network first returned no response, calling fallback from cache.'); | ||
getFallbackFromCache(request, values, options).then(resolve).catch(function(err) { | ||
logDebug('Fallback failed with:', err); | ||
}); | ||
} | ||
}).catch(function(err){ | ||
logDebug('Network first returned err, calling fallback from cache:', err); | ||
getFallbackFromCache(request, values, options).then(resolve).catch(function(err) { | ||
logDebug('Fallback2 failed with:', err); | ||
}); | ||
} else { | ||
return response; | ||
} | ||
}); | ||
}); | ||
} |
This file contains 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
This file contains 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