how to cache ajax response by using serviceWorker

后端 未结 1 1739
日久生厌
日久生厌 2021-01-16 12:49

I am calling server data by using ajax in index.html. It is perfectly fetching those data. Now, i am working with serviceworker. I can cache all the static assets(images,js,

相关标签:
1条回答
  • 2021-01-16 13:36

    Your fetch event handler starts with

    if (event.request.mode === 'navigate') {
      // ...
    }
    

    That means the code inside of it will only execute if the incoming fetch event is for a navigation request. Only the initial request for an HTML document when first loading a page is a navigation request. Your AJAX requests for other subresources are not navigation requests.

    If you want to cache your requests for index.cfm?action=main.appcache in addition to your logic in place for navigation requests, you can add another if statement after your first one, and check for that URL:

    self.addEventListener('fetch', (event) => {
      if (event.request.mode === 'navigate') {
        // ...
      }
    
      if (event.request.url.endsWith('index.cfm?action=main.appcache')) {
        // Your caching logic goes here. See:
        // https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook#serving-suggestions
      }
    });
    
    0 讨论(0)
提交回复
热议问题