chrome.storage.local.get failed?

114 views
Skip to first unread message

Jean Lomdes

unread,
Jan 31, 2024, 6:55:39 AM1/31/24
to Chromium Extensions
Hi,

Is it possible that reading a local stored value fails in this example ?
At install time, I am setting value in "somekey".
await chrome.storage.local.set({somekey: 'value'});

Then I use this value by getting it this way
let data = await chrome.storage.local.get(['somekey']);

data.somekey is sometimes undefined but I am pretty sure there is a value.
I did use try..catch around the get but nothing is thrown.

Is there a safer way to read this value?


Thank you,
Jean

wOxxOm

unread,
Jan 31, 2024, 9:00:39 AM1/31/24
to Chromium Extensions, Jean Lomdes
The usual culprits:
  1. some other code deletes the value e.g. onInstalled event listener which triggers on extension reload;
  2. the second code actually runs simultaneously with the first one e.g. setStorage(); useStorage(); instead of waiting e.g. setStorage().then(useStorage);
    note that the API doesn't guarantee the order in which the calls are processed!

Jean Lomdes

unread,
Jan 31, 2024, 11:22:28 AM1/31/24
to Chromium Extensions, wOxxOm, Jean Lomdes
Thank you for your response. 

Did you experience a loss of storage value? Or is it guarantee that local storage is never lost?


Thank you,
Jean

wOxxOm

unread,
Jan 31, 2024, 11:26:52 AM1/31/24
to Chromium Extensions, Jean Lomdes, wOxxOm
It is guaranteed, assuming the value does not exceed the limits, but bugs can happen. The default limit is 10MB as measured after JSON.stringify + UTF-8 encode, you can also specify "unlimitedStorage" permission to remove this limit, in which case it'll be limited by the internal API invocation message size limit, which is IIRC 64MB.
Reply all
Reply to author
Forward
0 new messages