首页 > Web开发 > 详细

[PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline

时间:2018-12-28 21:45:51      阅读:275      评论:0      收藏:0      [点我收藏+]

We can view the PWA offline because we are caching the static and CDN assets for the app - but the list of todo items won‘t display, because those API calls are not being cached. We‘ll add another route to the service worker, to store the result of any .json API call from our server. Then, we‘ll be able to view the app with the entire list of items offline.

 

When we want to cache json file from our server:

workbox.skipWaiting();
workbox.clientsClaim();

workbox.routing.registerRoute(
    new RegExp(‘https:.*min\.(css|js)‘),
    workbox.strategies.staleWhileRevalidate({
        cacheName: ‘cdn-cache‘
    })
  )

// Cache the json files from our server
// for both production and dev ‘/‘
  workbox.routing.registerRoute(
    new RegExp(‘/.*:4567.*\.json‘),
    workbox.strategies.networkFirst()
  )

workbox.precaching.precacheAndRoute(self.__precacheManifest || [])

 

[PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline

原文:https://www.cnblogs.com/Answer1215/p/10192902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!