Repository navigation
Milestone
Description
Feature Request
The storage APIs are very hard to use, so WXT should provide a standard storage API that's much simpler to use.
Here's an idea for the implementation using unstorage:
import { WatchCallback, createStorage, defineDriver } from "unstorage";
import type { Storage } from "webextension-polyfill";
const webExtensionDriver = defineDriver<{
storageArea: "sync" | "local" | "managed" | "session";
}>((opts) => {
if (browser.storage == null)
throw Error(
"You must request the 'storage' permission to use webExtensionDriver"
);
// Only ever create a single listener per storage area
const storageListener: (
changes: Storage.StorageAreaSyncOnChangedChangesType
) => void = (changes) => {
Object.entries(changes).forEach(([key, { newValue }]) => {
_listeners.forEach((callback) => {
callback(newValue ? "update" : "remove", key);
});
});
};
browser.storage[opts.storageArea].onChanged.addListener(storageListener);
const _listeners = new Set<WatchCallback>();
return {
name: "web-extension:" + opts.storageArea,
async hasItem(key) {
const res = await browser.storage[opts.storageArea].get(key);
return res[key] != null;
},
async getItem(key) {
const res = await browser.storage[opts.storageArea].get(key);
return res[key] ?? null;
},
async getItemRaw(key) {
const res = await browser.storage[opts.storageArea].get(key);
return res[key] ?? null;
},
async getItems(items) {
const res = await browser.storage[opts.storageArea].get(
items.map((item) => item.key)
);
return items.map((item) => ({
key: item.key,
value: res[item.key] ?? null,
}));
},
async setItem(key, value) {
await browser.storage[opts.storageArea].set({ [key]: value ?? null });
},
async setItemRaw(key, value) {
await browser.storage[opts.storageArea].set({ [key]: value ?? null });
},
async setItems(items) {
const map = items.reduce<Record<string, any>>((map, item) => {
map[item.key] = item.value ?? null;
return map;
}, {});
await browser.storage[opts.storageArea].set(map);
},
async removeItem(key) {
await browser.storage[opts.storageArea].remove(key);
},
async getKeys() {
const all = await browser.storage[opts.storageArea].get();
return Object.keys(all);
},
async clear() {
await browser.storage[opts.storageArea].clear();
},
watch(callback) {
_listeners.add(callback);
return () => void _listeners.delete(callback);
},
};
});
const storage = createStorage();
storage.mount("local", webExtensionDriver({ storageArea: "local" }));
storage.mount("session", webExtensionDriver({ storageArea: "session" }));
storage.mount("sync", webExtensionDriver({ storageArea: "sync" }));
storage.mount("managed", webExtensionDriver({ storageArea: "managed" }));
export default storage;I might try and submit webExtensionDriver to unstorage in a PR. I know the storage listener setup isn't optimized, but it works for a basic setup. I wouldn't add the listener until its needed in WXT's implementation.
Usage would looks something like this:
const installDate = await storage.getItem("local:install-date");
storage.watch("session:counter", (event, key) => {
console.log(`${key} ${event}d!`); // session:counter updated/removed!
});
await storage.setItem("session:counter", 123);Is your feature request related to a bug?
Related to #227
What are the alternatives?
Here are some alternative storage libraries:
- https://webext-core.aklinker1.io/guide/storage/ -
localStorage-esk APIs for chrome extensions - https://www.npmjs.com/package/webext-storage - This one is really interesting, it takes a unique approach, but is different from standard storage patterns. That doesn't make it bad though.
- https://www.npmjs.com/package/use-chrome-storage - A hook for react
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Improved Storage API[/-][+]Improved Storage API (looking for feedback)[/+]nnx0r commentedon Nov 13, 2023
I have nothing to add. I like the impl. idea and
unstorageseems to be amazing.The usage looks quite native, with the exception to
watchinstead oflistenfor example, butwatchsounds better, indeed...I'd simplify it even further:
I do not think there's need for
Itemsuffix. I'm looking forward to this feature!aklinker1 commentedon Nov 13, 2023
@dvlden If I'm using
unstorage, I'll be using theirStorageinterface and won't remove theItemsuffix from the method names.nnx0r commentedon Nov 13, 2023
Oh yea, makes sense. 🤣
aklinker1 commentedon Nov 15, 2023
Released in v0.10.0
nnx0r commentedon Nov 16, 2023
After using impl. of unstorage, I find it weird that in comparison of built-in storage, watcher doesn't return value(s), but only key.
Is it possible to hijack the implementation so that it return
k:vpair that changed?aklinker1 commentedon Nov 16, 2023
It's definitely not intended, and I didn't explore that. I think I can append an argument to the watch callback without breaking unstorage's interface. I'll add that, because I agree, the vue composable I shared is a little inefficient without it.
2 remaining items
nnx0r commentedon Nov 16, 2023
I understand. Was hoping it's possible, but at the moment on the watcher I have to get the current value with the key, so I guess it's not that big of a deal, was just hoping to avoid that extra step.
No clues why they decided to go with this specific approach on the watcher, but I guess it's for a good reason.
I tried to use the snippet you wrote for me in Vue, previously with two dependencies:
#227 (comment)
provide/injectuseAsyncStateBut I failed to make it work with new
wxt/storage. I'll need to play around more.At the moment, I'm using your
unstoragedriver as is, making a mess in my codebase... Testing if all puzzles fit, before I dig in deep into extension details.What can I say man, you did some amazing work in such short time, while I've been testing
wxtand I wish I knew this much to help with contributions, but some of the source code magic I simply don't understand.aklinker1 commentedon Nov 16, 2023
I'll share the entire example, maybe that will help.
nnx0r commentedon Nov 16, 2023
If you get time for it...
I made a contribution, I guess this will help
wxtin the future, if they approve it?unjs/unstorage#342
Not sure I did it right, but looks good to me 👍
aklinker1 commentedon Nov 16, 2023
@dvlden Here's a full example with a working composable:
https://github.com/wxt-dev/wxt-examples/blob/main/examples/vue-storage-composable/README.md
aklinker1 commentedon Dec 24, 2023
Was using the storage API yesterday, and realized unstorage saves things as strings (at least it saves numbers as strings).
So that's not acceptable... Will probably implement a custom storage API soon.
aklinker1 commentedon Dec 24, 2023
Here's what I'm thinking considering the feedback in this thread, my personal experience, and research into other libraries:
unstorage's API and patternsgetItemis verbose compared toget, it is fairly standard. It also allows for obvious plural forms of the functions, likegetItems. It also allows for other setters, likesetMeta, which could be useful in versioning fields.:separated keys are a nice approach, and using thelocal:/session:/sync:/managed:prefixes have worked wellStorageItemfromwebext-storage, but recognize that it's not something everyone would useWith all that in mind, here's the API I'm proposing for WXT 1.0:
Edit 1: I removed comments since the function names are pretty self-explanatory. I also added the
snapshotandrestoreSnapshotfunctions.Edit 2: Renamed
onChangetowatchandUnsubscribetoUnwatch.aklinker1 commentedon Dec 27, 2023
New storage API was released in
v0.13.0. Feel free to create new issues if you have problems or suggestions.nnx0r commentedon Dec 28, 2023
Oooh, very interesting. Dropping dependency sounds good, especially cause they did not care about our improvement. Plenty of dangling open PR's.
I just updated to v0.13.0 and going through refactor.
Got a question tho. I see everywhere literal examples of storing single primitive value per key in the storage. Would you advice to have a single storage item that stores an object instead?
Thinking about it, it's the way I used storage with my custom wrapper in the v2 of my extension. So now while developing v3, I do wonder why everyone is showing it as primitive values in storage only?
Since you have a lot of knowledge regarding development and extensions, please let me know. Would you advice me to use single source of truth basically or split it into multiple storage items?
I love the update!
––– UPDATE –––
Also, how performant is setting watcher per key vs single watcher and switch statement inside per key, as it was with unstorage?
aklinker1 commentedon Dec 28, 2023
@dvlden When working with storage, I never use a single key with an object containing multiple fields that need to be set independently for two reasons:
That's not to say you can't store objects. But you should only store objects that are updated as a single "value" all at once instead of updating individual parts of the object.
For example, if you have 5 different settings you need to store in your extension, I would store those 5 in separate keys, not a single "preferences" key, because they are updated one at a time. I don't have to get the latest value from storage before updating a single field of that object. I can just set the new value without worrying about that race condition
But if you are saving auth tokens for the logged in user, maybe a token and refresh token, those could be stored in a single "auth" key because they're never updated independently.
I decided to change this because I thought the old implementation might be a classic example of premature optimization. I've never measured the performance of the two approaches, so I don't have an answer for that.
Extension storage is implemented in native code in all browsers, so my thinking is that it's probably easier to let the native code manage the listeners.
If you are experiencing performance issues related to storage, I'll optimize it then.
KnightYoshi commentedon Dec 29, 2023
I'd love a Migration API. It's something that I had tried to bake into my extension, but didn't quite get it right — tbf I was new to extensions at the time and tried a quick implementation within a week while developing the rest of it.
aklinker1 commentedon Dec 29, 2023
@KnightYoshi Try it out in v0.13.0 and let me know what you think :)
Here are the docs for setting them up: https://wxt.dev/guide/storage.html#versioning-and-migrations