в моем PWA поддерживает автономный режим, в то время как в автономном режиме пользователи могут сделать фотографию, написать ее описание и сохранить оба файла в indexeddb, пока они не вернутся в оперативный режим. После этого данные должны быть взяты из indexeddb и отправлены онлайн.
Все отлично работает на android и P C (chrome), но фотография не загружается, когда я снова подключаюсь онлайн на iphone.
Служебный работник:
self.addEventListener('message', (event) => {
// upload cached data when user comes back online
if (event.data == 'is_online') {
submitRequests();
}
});
// submit past failed requests from indexed db
function submitRequests() {
console.log( 'From submit' );
localforage.length().then(function(numberOfKeys) {
if (numberOfKeys > 1) {
localforage.iterate(function(value, key, iterationNumber) {
if (key != 'user' && key != 'photo') {
console.log( [key, value] );
var requestUrl = value.url;
var formData = new FormData();
for ( var index in value ) {
formData.append(index, value[index]);
}
var payload = formData;
var method = 'POST';
// var headers = {
// };
fetch(requestUrl, {
// headers: headers,
method: method,
body: payload
}).then(function (response) {
// delete from indexdb if successful
console.log( 'Response' );
console.log( response );
if (response.status == 200) {
localforage.removeItem(key).then(function() {
// Run this code once the key has been removed.
console.log(requestUrl + ' is cleared!');
})
}
}).catch(function (error) {
console.error('Send to Server failed:', error)
throw error
})
}
}).then(function() {
console.log('Iteration has completed');
}).catch(function(err) {
// This code runs if there were any errors
console.log(err);
});
}
}).catch(function(err) {
// This code runs if there were any errors
console.log(err);
});
}
Обычный javascript:
window.addEventListener('online', () => {
swRegistration.active.postMessage('is_online');
});