@newaitix

Как получить json файл?

Есть переменная version которая содержит номер версии воркера.
Но хотелось бы чтоб эта версия бралась из другого места, к примеру из json файла.
Как это сделать?
Пытался сделать fetch но получаю ошибку что время на регистрацию воркера уже истекло.

Код сервис воркера
var version='v1';
self.addEventListener('install',function(event){
  self.skipWaiting();
  event.waitUntil(caches.open(version).then(function(cache){
    if(location.protocol=='http:'||location.protocol=='https:'){
      return fetch('/resource.json').then(function(response){
        return response.json();
      }).then(function(files){
        return cache.addAll(files);
      });
    }
  }));
});
self.addEventListener('activate',function(event){
  event.waitUntil(caches.keys().then(function(keyList){
    return Promise.all(keyList.map(function(key){
      if(version!=key){
        return caches.delete(key);
      }
    }));
  }));
});
self.addEventListener('fetch',function(event){
  event.respondWith(caches.match(event.request).then(function(response){
    return response||fetch(event.request).then(function(response){
      var responseToCache=response.clone();
      caches.open(version).then(function(cache){
        if(event.request.url.split('://')[0]!='chrome-extension'&&event.request.method!='POST'){
          cache.put(event.request,responseToCache);
        }
      });
      return response;
    }).catch(function(){
      return caches.match('/offline.html');
    });
  }));
});
  • Вопрос задан
  • 110 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы