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

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[PWA] 19. Cache the avatars

[PWA] 17. Cache the photo

[PWA] 18. Clean the photo cache

[PWA] 7. First Cache when installed

[PWA] 11. Serve skeleton cache for root

[PWA] 8. Delete old cache and only keep one