-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow to configure KEY_PREFIX #14
Comments
The plugin is a wrapper around localStorage, so if you have data already in localStorage you will need to migrate it by checking current data, add it as Capacitor data and remove you copy of localStorage, or keep using localStorage as you already do. The only problem with that is it won't use the native implementation on iOS and Android. Maybe we can add a migrate function for web implementation that does it for you, what do you thing @mlynch? |
@jcesarmobile In my opinion it should be possible to set a custom |
Changed the issue to be a feature request since it doesn't really break anything, it has worked like that since the beginning. Issues tagged with feature request are closed but tracked for reactions to gauge interest |
@jcesarmobile @dwieeb would you please explain that? I don't quite understand why it's not possible to configure a prefix for the storage key (remove prefix)? If capacitor is a good replacement for the old cordova project, then most likely cordova-plugin-nativestorage was used in the old project, which has no default prefix. With capacitor you should use the core storage plugin, which has NativeStorage.setItem('key1', 'value1'); // key1=value1
const res = await Plugins.Storage.get({ key: 'key1' });
console.log(res.value); // <== undefined |
This is indeed an unexpected behavior that I thought for sure would be configurable. I would be willing to create a PR for the feature if necessary. My current workaround is to just wrap the Capacitor Storage with a util class that removes the prefix from the
Usage:
But this just seems ridiculous to have to do. |
@yacut For example on iOS, NativeStorage uses a custom UserDefaults group called |
And I just found out the Android implementation doesn't even use the prefix to begin with, so this will be very difficult to implement in a cross-platform way. We may need to provide a way to migrate data. |
I am going to rewrite the iOS implementation to use a non-shared UserDefaults and rewrite the web implementation to use IndexedDB. This will remove the need for a prefix, and instead promote the idea of a "storage group". There will be a way to migrate data from the Capacitor 2 implementation. |
Okay, the latest for this is: If you use the storage group "NativeStorage", it will be backwards-compatible with On iOS and web, the Capacitor Storage plugin uses the storage group (default of "CapacitorStorage") as a prefix to separate key/values from other data. On Android, it uses the group as an entirely separate SharedPreferences file. Please take a look at this PR and let me know your thoughts! I'll update this issue when dev releases are made for Capacitor 3 and the new storage plugin. |
@dwieeb Thank you, I can't wait for Capacitor 3 👍 |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out. |
Hello,
I'm talking about this file line 9 and 58
https://github.com/ionic-team/capacitor/blob/master/core/src/web/storage.ts
We already have an existing application and the current Web version of Storage will not read the existing values from local storage due to this KEY_PREFIX.
I can see this is being used for
clear()
andkeys()
etc but right now it's breaking existing applications. I guess we can force KEY_PREFIX to an empty string for now but this should be improved in my opinion.Thank you!
The text was updated successfully, but these errors were encountered: