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

Source: Internet
Author: User
Tags delete cache

So once you modify the code, service worker would auto create a new one and it won ' t take control over until the previous S Ervice Work Total die (close the tab or NAV to a new URL). The new service is in the waiting list.

Now, we want to do are delete the old caches and only keep the one which is latest installed.

So the-the-do-this, is in ' Activate ' state. So once anything changed, a new service worker is created and push into the waiting list. Then we want to delete old caches only keep the static one.

varStaticcachename ='wittr-demo-v13'; Self.addeventlistener ('Install', Function (Event) {  varUrlstocache = [    '/',    'Js/main.js',    'Css/main.css',    'Imgs/icon.png',    'Https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff',    'Https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff'  ]; Event. Waituntil (Caches.open (staticcachename). Then (cache)={cache.addall (Urlstocache); })  );}); Self.addeventlistener ('Activate', Function (Event){    Event. Waituntil (//Get all the cache nameCaches.keys (). Then (function (cachenames) {//wait all cache deleted            returnPromise.all (//filter out staticcachename (only keep Staticcachename)Cachenames.filter (function (name) {returnName.startswith ('wittr') && Name! =Staticcachename; }). map (function (name) {//Delete cache, return promise                    returncaches.delete (name); })            )        })    )})

So in the code, the static one is ' wittr-demo-v13 '.

We Use the APIs:

// return a promise with all the cache name  //  return a promise and delete the selected cache

Now only V13 are left.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.