Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Created July 18, 2014 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakearchibald/766e0bffb87c87baeba3 to your computer and use it in GitHub Desktop.
Save jakearchibald/766e0bffb87c87baeba3 to your computer and use it in GitHub Desktop.
var networkDataReceived = false;
startSpinner();
// fetch fresh data
var networkUpdate = fetch('/data.json').then(function(response) {
return response.body.asJSON();
}).then(function(data) {
networkDataReceived = true;
updatePage();
});
// fetch cached data
caches.match('/data.json').then(function(response) {
return response.body.asJSON();
}).then(function(data) {
if (!networkDataReceived) {
updatePage(data);
}
}).catch(function() {
// we didn't get cached data, the network is our last hope:
return networkUpdate;
}).catch(showErrorMessage).then(stopSpinner);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment