StorageArea for service worker

Skip to first unread message

Nathan Peterson

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?

Dec 3, 2021, 10:40:09 AM12/3/21
to Chromium Extensions,
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:

Dec 3, 2021, 1:10:01 PM12/3/21
to Chromium Extensions,,
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*(...)

Alex S

Dec 3, 2021, 2:20:44 PM12/3/21
to Chromium Extensions,,,

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

Chrome 96 (now stable) introduces the 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 Hope that helps, Alex S

Nathan Peterson

Dec 3, 2021, 3:06:50 PM12/3/21
to Chromium Extensions, Alex S,,, 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.

Dec 3, 2021, 3:45:54 PM12/3/21
to Chromium Extensions,, Alex S,,
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
0 new messages