Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing indexedDB in ServiceWorker. Race condition

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( 'myDB', 1 );
var db;

request.onupgradeneeded = ...

request.onsuccess = function() {
    db = this.result; // Average 8ms
};


self.onfetch = function(e)
{
    const requestURL = new URL( e.request.url ),
    path = requestURL.pathname;

    if( path === '/test' )
    {
        const response = new Promise( function( resolve )
        {
            console.log( performance.now(), typeof db ); // Average 15ms

            db.transaction( 'cache' ).objectStore( 'cache' ).get( 'test' ).onsuccess = function()
            {
                resolve( new Response( this.result, { headers: { 'content-type':'text/plain' } } ) );
            }
        });

        e.respondWith( response );
    }
}

Is this likely to fail when the ServiceWorker starts up, and if so what is a robust way of accessing indexedDB in a ServiceWorker?

like image 946
Adria Avatar asked Mar 23 '15 09:03

Adria


People also ask

Can service workers access Indexeddb?

Note: IndexedDB can be used inside a service worker for data storage if you require it.

Can service workers access session storage?

Instead, service workers can be used to manage user sessions for server side consumption. This works because of the following: Service workers have access to the current Firebase Auth state. The current user ID token can be retrieved from the service worker.

Can service workers access cache?

# Access to a JavaScript-driven caching APIThe Cache interface can be accessed within the service worker scope and within the scope of the main thread.

What is Indexeddb in HTML5?

The indexeddb is a new HTML5 concept to store the data inside user's browser. indexeddb is more powerful than local storage and useful for applications that require to store a large amounts of the data. These applications can run more efficiency and load faster.


1 Answers

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.

like image 134
JaffaTheCake Avatar answered Sep 27 '22 22:09

JaffaTheCake