Accessing indexedDB in ServiceWorker. Race condition

前端 未结 3 1590
猫巷女王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条回答
  •  -上瘾入骨i
    2021-01-02 08:44

    I don't know of anything special about accessing IndexedDB from the context of a service worker via accessing IndexedDB via a controlled page.

    Promises obviously makes your life much easier within a service worker, so I've found using something like, e.g., https://gist.github.com/inexorabletash/c8069c042b734519680c to be useful instead of the raw IndexedDB API. But it's not mandatory as long as you create and manage your own promises to reflect the state of the asynchronous IndexedDB operations.

    The main thing to keep in mind when writing a fetch event handler (and this isn't specific to using IndexedDB), is that if you call event.respondWith(), you need to pass in either a Response object or a promise that resolves with a Response object. As long as you're doing that, it shouldn't matter whether your Response is constructed from IndexedDB entries or the Cache API or elsewhere.

    Are you running into any actual problems with the code you posted, or was this more of a theoretical question?

提交回复
热议问题