-
Notifications
You must be signed in to change notification settings - Fork 441
Open
Description
Cache then network example.
var networkDataReceived = false;
startSpinner();
// fetch fresh data
var networkUpdate = fetch('/data.json').then(function(response) {
return response.json();
}).then(function(data) {
networkDataReceived = true;
updatePage(data);
});
// fetch cached data
caches.match('/data.json').then(function(response) {
if (!response) throw Error("No data");
return response.json();
}).then(function(data) {
// don't overwrite newer network data
if (!networkDataReceived) {
updatePage(data);
}
})
.catch(function() {
// we didn't get cached data, the network is our last hope:
return networkUpdate;
})
.catch(showErrorMessage) // never called, because previous catch swallows thrown error
.then(stopSpinner()); // stopSpinner called immediately, should be `then(stopSpinner)`.
See comments for last 2 lines of above snippet
If there is better place to report this problem, please let me know. I'll move it.
Metadata
Metadata
Assignees
Labels
No labels