Accessing indexedDB in ServiceWorker. Race condition

前端 未结 3 1607
猫巷女王i
猫巷女王i 2021-01-02 08:36

There aren\'t many examples demonstrating indexedDB in a ServiceWorker yet, but the ones I saw were all structured like this:

const request = indexedDB.open(         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 09:02

    Opening the IDB every time the ServiceWorker starts up is unlikely to be optimal, you'll end up opening it even when it isn't used. Instead, open the db when you need it. A singleton is really useful here (see https://github.com/jakearchibald/svgomg/blob/master/src/js/utils/storage.js#L5), so you don't need to open IDB twice if it's used twice in its lifetime.

    The "activate" event is a great place to open IDB and let any "onupdateneeded" events run, as the old version of ServiceWorker is out of the way.

提交回复
热议问题