-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Support localStorage for storing settings #613
Support localStorage for storing settings #613
Conversation
Note on how this works:
The test only runs once on app launch, not every time a cookie method is used (you can see this in console.log). The reason we have to prefer EDIT: If a version of this PR is accepted, we must remember to update the Privacy Policy to indicate use of localStorage. |
In https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage, Mozilla says :
They suggest to use this https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage API instead, that should work in other browser extensions too. We have to keep in mind that, if we use these other storage APIs AND set them as default storage in some circumstances, we'll have to deal with data migration (move the existing cookie preferences to the other storage, then remove the cookies), and to deal with cases where some preferences would be set in different places (and decide which one we should use). Keeping cookies as the default preference storage would avoid these difficulties |
That's exactly the same scenario as cookies. If a user clears browsing data cookies will be cleared. In fact I think it's an advantage that localStorage would be cleared in the same circumstances, as it allows the user to "reset" all settings to default (say, where a particular setting is causing an issue due to an API change). |
I think we should respect a user's desire to clear their data for privacy reasons, and shouldn't try to circumvent that by using a more persistent API. |
PS Can easily add code to migrate settings to localStorage if supported and document.cookie is accessible. On the other hand, a user might not be too annoyed by having to choose two or three settings again in Config. Still, it's a few lines of code to migrate either way. Would you like me to add? |
OK, you're right. Our extension is more like an offline website with some preferences than an extension where we would need to keep settings even when the user clears the history/private data. |
Tested and working on Firefox OS. It uses |
|
I've just realized that if localStorage is preferred but it's not supported, there is no point checking for cookie support, because we don't have any other option, we have to fall back to something, and the most universal is likely to be cookie. Therefore logic dictates that we remove the test for cookie support and just assume it. If a cookie test were to fail, there'd be no other storage type to use anyway. In that hypothetical situation (no localStorage support and no cookie support), defaults will be used and the user will have to re-do settings between sessions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks right to me. But I've added a few remarks
This is ready for testing. I have added console log messages so we can see what exactly is migrated (it shows just the keys that are migrated). In the contexts I've tested quickly, the migration is one-off. However, in FFOS simulator, the following key is recreated in document.cookie every time the app starts:
This causes the migration to run on every startup of the app. Is this just an artefact of the simulator? |
Tests
Methodology: first load the app using master branch to ensure document.cookie is populated. Then switch to Use-localStorage branch and reload app with console open to see the migration. Reload again to see if migration is repeated. |
Now also tested in an Edge Chromium extension using developer mode. It uses localStorage and does not migrate settings because the extension loads with document.cookie empty. Settings are remembered between tab loads. I think this is ready for your testing and review, @mossroy . |
Yes, I was thinking about that, as I'd seen some non-Kiwix settings being migrated too. I guess we can add a The only way I can think of to be absolutely sure we don't migrate spurious keys would be to supply a list of known keys to migrate... |
As you seem to intend at the end of your last comment, we know which cookies kiwix-js uses, so we could look for and migrate only these specific cookies, and do not touch the other ones. No need to add |
OK, I've added the list of keys to migrate. I could only find seven in our app by searching for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good to me.
Apart from your own review comments, what is missing is the prefix added to setting keys (only in localStorage mode)
Ah, OK, I had misunderstood about the prefix (I thought we no longer needed it as we were specifying which keys to migrate). But I agree that it makes sense just in case we get key collisions with code inside the ZIMs that might also use localStorage. Will do. |
Done, and tested so far only on Firefox 75. Methodology for testing:
We'll need to test this in several different browsers/contexts before merging in case of any incompatibilities with the format of key names (I'm thinking of the hyphen and/or length -- shouldn't be a problem, but you never know). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully tested on my Firefox OS device, Chromium 80 and Firefox 75
Did not test inside extensions but you already did.
You might introduce a constant variable in settingsStore.js for kiwixjs-
string, to avoid repeating it many times. https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/const seems to be handled by all the browsers we support
Interesting. I always associate Happy to experiment with that. |
I've implemented that and have tested on Firefox 75 with file:// protocol, and on IE11 from localhost. It works fine on the latter, i.e. I'll do tests on the other platforms before squashing/merging (but not today). EDIT 14/4: Tested and working on FFOS simulator. |
Summary of final tests conducted: FFOS simulator: working So I'll squash and merge. |
This is a possible solution for #612 in contexts where the
localStorage
API is supported. It works in Firefox running from the file:// URL. I haven't tested in extension context. It should also enable Chromium to store settings from file:// context when the flag allowing file access is used.This solution has been implemented in Kiwix JS Windows for a year or so, to solve some other issue that I forget, so I simply copied it over here to test.
How can I test this in an extension on Firefox?