chrome.storage.local content lost on upgrade

197 views
Skip to first unread message

Stéphane SOPPERA

unread,
May 4, 2023, 5:12:42 AM5/4/23
to Chromium Extensions
Hi,

In my Chrome extension I am using chrome.storage.local to store some user configuration: the MIDI events to react to.

I am intentionally not using storage.sync as this configuration depends on local devices plugged to the computer. Thus synchronizing it across devices would be bad.

The issue I have is that each time Chrome is updated, via Ubuntu's APT system, the configuration is lost.

Is there a way to not lose this configuration on updates?

Thanks,
Stéphane

wOxxOm

unread,
May 4, 2023, 10:12:21 AM5/4/23
to Chromium Extensions, Stéphane SOPPERA
You're overwriting the data in your onInstalled event listener, which is triggered both for installs and updates because an update is a subtype of installation.

The solution is to check the event's `reason` parameter and only write the storage if it's 'install'. BTW you can even remove the background script and just specify the defaults via the "destructuring assignment" directly instead.

Stéphane SOPPERA

unread,
May 15, 2023, 9:56:04 AM5/15/23
to Chromium Extensions, wOxxOm, Stéphane SOPPERA
Sorry for not having answered earlier but I got not time to work on my extension.

Thanks a lot for your answer! I somehow overlooked this `reason` parameter and missed the 'chorme_update' part.
I tested the fix today and it seems to work as intended now.

> BTW you can even remove the background script and just specify the defaults via the "destructuring assignment" directly instead.

I guess in my use-case, since the storage data are objects with sub-fields, I wanted to make sure that updating the extension would properly reset the current settings as I did not want to write complex code that could deal with data from another version. I guess I could write code that ignores invalid data but that would require more care than the current solution that reset setting on extension update or installation (I rarely make any update so that is good enough for now I guess).

I may change my mind later but for now the simple fix of ignoring 'chrome_update' and 'shared_module_update' will do.

Thanks again for you help and having had a look at my code!
Reply all
Reply to author
Forward
0 new messages