What's the recommended way to keep a consistent storage that the popup, background worker, and offscreen document each have access to?

554 views
Skip to first unread message

Chaka Bonzales

unread,
Jul 26, 2023, 3:13:18 AM7/26/23
to Chromium Extensions
What's the recommended way to keep a consistent storage that the popup, background worker, and offscreen document each have access to?

So I've run into a problem where I want to store auth tokens that can be used by the background script and offscreen doc, but localStorage isn't accessible in the background script, while chrome.storage.local isn't accessible by the offscreen document and continuously message passing short lived tokens b/w the two doesn't seem like an effective solution. Has anyone run into a similar issue?

wOxxOm

unread,
Jul 26, 2023, 9:45:39 AM7/26/23
to Chromium Extensions, Chaka Bonzales
Use IndexedDB.

Dustin Byers

unread,
Jul 26, 2023, 2:47:06 PM7/26/23
to Chromium Extensions, wOxxOm, Chaka Bonzales
Is indexDB preferable over storage api?

wOxxOm

unread,
Jul 26, 2023, 3:08:03 PM7/26/23
to Chromium Extensions, Dustin Byers, wOxxOm, Chaka Bonzales
Well, it's the only one available in all of the specified contexts. It's also much faster and more capable.

Oliver Dunk

unread,
Jul 26, 2023, 3:11:23 PM7/26/23
to wOxxOm, Chromium Extensions, Dustin Byers, Chaka Bonzales
Woxxom is right - as far as I know, IndexedDB is the only storage mechanism available across the board.

Depending on the specific use case I would definitely consider extension messaging between the service worker and offscreen document, even if you do have to do that fairly frequently. It should still be quite lightweight and avoids needing to poll IndexedDB for changes.
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/150b66cd-180e-4eae-8490-7c0a78908019n%40chromium.org.

Juraj M.

unread,
Jul 27, 2023, 3:27:38 AM7/27/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Dustin Byers, Chaka Bonzales, wOxxOm
There is no `storage.local` access in the offscreen document??? :D
Why?

Also, note that IndexedDB data won't be accessible in Incognito mode, right? 
And without some 3rd party library wrapper the IndexedDB is crazy unfriendly to use (as a simple storage replacement).
I would rather write a polyfill for `storage.local` that routes data through background script with messaging.

Oliver Dunk

unread,
Jul 27, 2023, 6:14:23 AM7/27/23
to Juraj M., Chromium Extensions, Dustin Byers, Chaka Bonzales, wOxxOm
There is no `storage.local` access in the offscreen document??? :D Why?

We very intentionally kept the APIs available in offscreen documents limited, as we want to encourage using them for fairly narrow tasks. I think for some basic APIs like that, it could be worth considering in the future, but that's why it wasn't added originally :)

Also, note that IndexedDB data won't be accessible in Incognito mode, right?

I'm not super familiar with things but that seems to be the case at first glance. I would expect that it might be ok for a spanning extension (the default) though since the offscreen document should be in the context of the main profile.
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB

Juraj M.

unread,
Jul 27, 2023, 7:23:16 AM7/27/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Dustin Byers, Chaka Bonzales, wOxxOm, Juraj M.
Thanks Oliver for the input! 
Although I can't say it makes sense, especially for such a basic feature such as storage.
I hope all Chrome internal extensions are built using these new technologies and that you evaluate how useful and easy to use they are :)

And I'll have to re-check why am I using this "spanning" mode in all my extensions because it seems to be source of many issues :)
Reply all
Reply to author
Forward
0 new messages