简体   繁体   English

Angular PWA 和缓存 API

[英]Angular PWA and caching API

I have made a PWA using Angular.我已经使用 Angular 制作了一个 PWA。 The PWA works fine, caching works. PWA 工作正常,缓存工作正常。 When going offline all the needed API calls are saved into the cache and accessed by the service worker.离线时,所有需要的 API 调用都会保存到缓存中并由服务工作者访问。

My question is simple, how can I access this data on command.我的问题很简单,如何按命令访问这些数据。

It's great that the service-worker does this for me when the PWA goes offline however, this data that is in cache is data that I want other components to use.当 PWA 离线时,service-worker 为我做这件事很棒,但是,缓存中的数据是我希望其他组件使用的数据。 Considering that it is allready in cache I don't want to have to make the API call again.考虑到它已经在缓存中,我不想再次进行 API 调用。

I was researching the different options and I couldn't really find anything conclussive.我正在研究不同的选择,但我真的找不到任何结论性的东西。 The caching API seemed interesting but I have no clue if that is how an Angular PWA saves its data.缓存 API 看起来很有趣,但我不知道这是否是 Angular PWA 保存数据的方式。

So, Anyone know how to access the cached data that a service-worker uses?那么,有人知道如何访问 service-worker 使用的缓存数据吗?

Inside my ngsw-config.json file, where I delcared my datagroups.在我的 ngsw-config.json 文件中,我在其中声明了我的数据组。 I gave the group a name, in this case 'mission-api'.我给这个组起了一个名字,在这个例子中是“mission-api”。

"dataGroups": [
  {
  "name": "mission-api",
  "urls": ["https://12goappapi.azurewebsites.net/api"],
  "cacheConfig": {
    "strategy": "freshness",
    "maxSize": 20,
    "maxAge": "1h",
    "timeout": "5s"
    }
  }
]

Using the cashing API, you are able to read the cached data.使用兑现 API,您可以读取缓存的数据。 The service worker actually uses those same calls. Service Worker 实际上使用了这些相同的调用。 To access the data that you save, all you need is the caching name.要访问您保存的数据,您只需要缓存名称。

Now this is were I was struggling, the name is not just the name you specify.现在这是我苦苦挣扎的地方,名称不仅仅是您指定的名称。 The ngsw-worker.js adds a bunch of pre-fix before the name in order to be sure that the name is unique. ngsw-worker.js 在名称前添加了一堆前缀,以确保名称是唯一的。

For me personally I had to open a browser and check to see what the name was.就我个人而言,我必须打开浏览器并查看名称。

浏览器缓存

When you find the corresponding name you can use the cache API to retrieve your data.当您找到相应的名称时,您可以使用缓存 API 来检索您的数据。 I feel like their should be a better way to find/determine this name.我觉得他们应该是找到/确定这个名字的更好方法。 If somebody finds it feel free to update my answer or provide one of your own!如果有人发现可以随时更新我的​​答案或提供您自己的答案!

for instance:例如:

if ('caches' in window) {
  console.log('CACH API ENABLED IN BROWSER');
  caches.open('ngsw:/:1:data:dynamic:mission-api:cache').then(cache => {
    cache.match('https://12goappapi.azurewebsites.net/api/missions').then(res => {
      res.json().then(result => {
        console.log(result);
        this.missions = result;
      });
    })
  });
}

I'm not exactly sure what yall are trying to do, but all you have to do is call the same api endpoint in any other component and the sw will intercept and retrieve that response from the cache if it's there, and never try to go over the network unless you have a "freshness" strategy.我不确定你们想要做什么,但你所要做的就是在任何其他组件中调用相同的 api 端点,如果它在那里,sw 将拦截并从缓存中检索该响应,并且永远不要尝试去通过网络,除非你有一个“新鲜”的策略。 Use "performance" strategy and it will do cache first.使用“性能”策略,它会先做缓存。 There's no need to manually try to grab the name of that specific cached data and that seems unreliable..无需手动尝试获取特定缓存数据的名称,这似乎不可靠。

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

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