1

I am getting a QUOTA_BYTES_PER_ITEM quota exceeded error when trying to save an object to storage, but my precheck of the size passes. I am sure I am making some sort of basic mistake here (is this a valid way to check size of an object?). I have already compressed the item I want to save with LZString, but regardless, it seems much smaller than the quota.

var objToSave = {};   
objToSave[myKey] = compressedObj;  

console.log("Size of obj is: " + JSON.stringify(objToSave).length); //prints 3452
console.log(chrome.storage.sync.QUOTA_BYTES_PER_ITEM); //prints 8192
if (JSON.stringify(objToSave).length >= (chrome.storage.sync.QUOTA_BYTES_PER_ITEM)) { // this never triggers
      alert('objToSave is too large!');  
      return;   
}

chrome.storage.sync.set(objToSave, function() {    
  if (chrome.runtime.lastError) { // this error gets triggered.
    console.log("Error: " + chrome.runtime.lastError.message); // this error gets triggered. 
    return customAlert("Error!: " + chrome.runtime.lastError.message);    
  }   
});
Bob Aleena
  • 450
  • 1
  • 6
  • 16
  • is this a temporary or a persistant storage type – Nishant S Vispute May 16 '21 at 00:02
  • Here is a Similar thread i found with a answer https://stackoverflow.com/questions/13373187/can-i-increase-quota-bytes-per-item-in-chrome – Nishant S Vispute May 16 '21 at 00:04
  • It is persistent and using chrome.storage.sync as per Chrome APIs – Bob Aleena May 16 '21 at 00:05
  • In that case you might wanna call this ```requestQuota()``` inside of your precheck or a suitable place of your choice Here is a Link Explaining the Issue from developer.chrome site https://developer.chrome.com/docs/apps/offline_storage/#asking_more – Nishant S Vispute May 16 '21 at 00:09
  • Yes, it is my intent to do implement something like this if the object I want to store is larger than the quota length, but at this stage it is showing that the object I have is smaller than the available limit. – Bob Aleena May 16 '21 at 00:09
  • You mean bigger than then available limit.. :) – Nishant S Vispute May 16 '21 at 00:09
  • why are you saying bigger? that is my question. the console.log is saying I am trying to save 3452 while the limit is 8192. The precheck also succeeds. The alert is never getting triggered. – Bob Aleena May 16 '21 at 00:13
  • 1
    Well the Only logical reason for you to get **QUOTA_BYTES_PER_ITEM** is becasue you are trying to use a string which exceeds that 8k mark.. However there are 2 things that i feel might be going wronge here.. 1. double check the **key:val** pair 2. "variable byte length":: Since your are encoding the string there might be a slight chance that your string is full of 2 byte data [if the character code is bigger than 256 or (<= 0xFF) then the byte size is 2 ] Your string might be of 3452 but full of 2 bytes even then it would not cross 8k mark, however it might just be that blind spot. – Nishant S Vispute May 16 '21 at 01:16
  • 2
    To reiterate the above comment, if your value contains unicode characters e.g. Chinese, it will be encoded to UTF8 which may increase the size 2-3 times. Use `TextEncoder` to get the real size. – wOxxOm May 16 '21 at 03:23

2 Answers2

1

Thank you @nishant and @wOxxOm - that is exactly the error. I was checking the size incorrectly.

To correctly check the size, I am now using to get the byte size vs just the length of the string

var s = JSON.stringify(objToSave);
encodeURI(s).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length-1)

which is giving me a size of 10765 which I am now breaking down further using @wOxxOm's answer on https://stackoverflow.com/a/67429150/4797507

Bob Aleena
  • 450
  • 1
  • 6
  • 16
1

Well, the only logical reason for you to get QUOTA_BYTES_PER_ITEM is that you are trying to use a string that exceeds that 8k mark...

However there are 2 things that I feel might be going wrong here..

  1. Double-check the key:value pair
  2. "variable byte length" Since you are encoding the string there might be a slight chance that your string is full of 2-byte data [if the character code is bigger than 256 or (<= 0xFF) then the byte size is 2, Your string might be of 3452 but full of 2 bytes then it would cross 8k mark, it might just be that blind spot.
Nishant S Vispute
  • 713
  • 1
  • 7
  • 21