简体   繁体   English

绕过Service-Worker缓存

[英]Bypass Service-Worker caching

I have a progressive web-app, which speaks to an API. 我有一个渐进式网络应用程序,它可与API通讯。 The calls to this api get cached by a service worker, which works great. 服务人员将对此API的调用进行缓存,效果很好。

But now, I want to add a reload-button, which ideally forces the service worker to try to bypass the cache and update it if successful, also it should not return the cached result if a connection could not be made. 但是现在,我想添加一个重新加载按钮,理想情况下,该按钮迫使服务工作者尝试绕过缓存并在成功后对其进行更新,如果无法建立连接,它也不应返回缓存的结果。

I am a bit unsure how to solve this. 我有点不确定如何解决这个问题。 I am using the sw-toolbox . 我正在使用sw-toolbox

Based on your description, you are using the application cache. 根据您的描述,您正在使用应用程序缓存。 It can be accessed from the app fronted independent of the sw-tool box. 可以从独立于sw-tool box的前端应用访问该文件。

  function onReloadButtonClicked(event) {
      //Check for browser cache support
      if ('caches' in window) {
        //Update cache if network query is successful
        caches.open('your_cache_name')
          .then(function(cache) {
            cache.add('your_url');
          }).catch(function(err) {
            // Do something with the error
          });
      }
    }

All requests go through the fetch callback which receives a request object . 所有请求都通过接收请求对象的提取回调。 Thus, before returning a cached response you can look for an additional header parameter (you need to include it into your request to API) to skip the logic returning cached response . 因此,在返回缓存的响应之前,您可以查找其他标头参数(您需要将其包含在对API的请求中)以跳过返回缓存的响应逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM