StorageArea for service worker

59 views
Skip to first unread message

Nathan Peterson

unread,
Dec 3, 2021, 10:22:05 AM12/3/21
to Chromium Extensions
The MV3 docs announce "In-memory storage: a new StorageArea on the Storage API that can be used to store values in memory across service worker restarts."

We are working on completing our migration to MV3 and this feature (especially if it operates synchronously) could be extremely useful to us. Any update on when this is expected?

peter....@gmail.com

unread,
Dec 3, 2021, 10:40:09 AM12/3/21
to Chromium Extensions, natp...@gmail.com
I am in the works of migrating some older extensions with 100's of localStorage.* code all over the place, one after another after another, all synchronously and all separate objects. It's a complete mess, but forcing me to update this to one Object structure is actually a good thing compared to the current way the code is written. However, migrating from synchronous to asynchronous is frustrating to say the least.

I don't know anything about StorageArea but I did find these, I hope it helps:

hrg...@gmail.com

unread,
Dec 3, 2021, 1:10:01 PM12/3/21
to Chromium Extensions, peter....@gmail.com, natp...@gmail.com
There's an automatic way of turning synchronous code into asynchronous. You have to prepend the keyword ASYNC to ALL function statements and function expressions in your code base. Then prepend the AWAIT operator to ALL function calls that were made async.
This can be automated with a Babel plugin.

Then you can replace (also automatically) all calls to localStorage.* with await chrome.storage.local.*(...)

Alex S

unread,
Dec 3, 2021, 2:20:44 PM12/3/21
to Chromium Extensions, hrg...@gmail.com, peter....@gmail.com, natp...@gmail.com
Hey,

Not a chrome dev, but I think I can help here.

Chrome 96 (now stable) introduces the chrome.storage.session api for in-memory storage that persists across service worker restarts - only available to MV3 extensions.
The docs haven't been updated for chrome 96 yet to include it, but the interface is
interface SessionApi {
clear(): Promise<void>;
get<T>(key: string): Promise<T | undefined>;
set(keyValue: any): Promise<void>;
remove(key: string): Promise<void>;
} There's a thread here discussing it https://groups.google.com/a/chromium.org/g/chromium-extensions/c/18V6_-5Hwbw/m/h-2KKJU_BgAJ Hope that helps, Alex S

Nathan Peterson

unread,
Dec 3, 2021, 3:06:50 PM12/3/21
to Chromium Extensions, Alex S, hrg...@gmail.com, peter....@gmail.com, Nathan Peterson
Alex, thanks for pointing me to the previous thread on this topic. Not sure that SessionApi will handle our needs but it's good to know that it exists as an option.

hrg...@gmail.com

unread,
Dec 3, 2021, 3:45:54 PM12/3/21
to Chromium Extensions, natp...@gmail.com, Alex S, hrg...@gmail.com, peter....@gmail.com
The session storage that Alex is referring to is precisely the new in-memory storage  that is announced in the page you linked above.
There won't be any synchronous API similar to window.localStorage accessible from a service worker.
Reply all
Reply to author
Forward
0 new messages