-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Handle web storage DOMException #148
Handle web storage DOMException #148
Conversation
Hey! Thanks for opening this issue with a fix together!
I had this problem a long time ago, and I ended up forgetting about it. Thank you again!
Well, I went up to search about it and, appearently, there are many possible error messages for many browsers. This blog post suggests to, basically, just catch everything.
This might be true, but don't you think that it requires a really LOT of keys to freeze a browser for a second? Well, it'll require a benchmark to prove anything. But, what about adding an option ( Instead of just removing the expired ones (which includes verifying if the cache can be stale), it can be broken into multiple steps:
In case the |
Hey again, appreciate the comments..
clearOnError makes sense true. Maybe some people will want to handle this on their own indeed. They may indeed want to e.g. not store more results and that could be a case under certain use cases. Regarding browser freezes you are right, maybe I was a bit biased based on my use case on that, when I did experience browser freezes with firefox but still like you said performance checking would suggest the way to go. Now coming to your final point about introducing a set of steps in order to avoid totally clearing the cache. I am not that familiar with the library internal (so I may be wrong and the library already covers the following), This way having such a key value pair would (even if there was a list method in the storage API), allow the system to more quickly filter against the value stored in that key, to find the last or the expiring keys and clear them. Of course this means that on application load (typically visiting or refreshing the page) the app would need to load the value of that key in memory from storage, then on each key set method, update the in memory structure of the keys stored and persist that on the storage so that steps 1 - x can follow when needed in case the clearOnError preference is not true. I'll see to work on the clearOnError preference. |
You can use |
Np! I'm working on this PR too |
But that will return all the keys stored in object or sessionStorage wont it? among which possible other keys not "owned" by the cache interceptor, therefore it will also require scanning the content of the keys to make sure its "owned" or not... Even with checking the content what about clashes with other values with similar in nature structure? |
Yep, and to avoid this key conflict problem, there's the |
Yes just saw the code... you have already fixed most of the things you mentioned.. Great! |
Also, i thought for a while about the The default (and desired) behaviour is to handle quota errors. And makes no sense in the first error to just clear all other "healthy" keys because of the quota. So, if someone needs this "unique" behavior, they can just override the |
This is true.. Great |
Sorry, closed the pr accidentally instead of comment.. Saw your comment makes sense |
This gets me every time too 🤣🤣 |
Codecov Report
@@ Coverage Diff @@
## main #148 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 16 17 +1
Lines 428 493 +65
Branches 143 145 +2
=========================================
+ Hits 428 493 +65
Continue to review full report at Codecov.
|
Hey @ckopanos, everything ok or there's anything more to do? |
The only thing I can think of (unless your typesscript object declarations will guard against it, sorry not familiar with TS) is the case when prefix === ''.
allValues will return all keys ("owned" and not) in local or sessionStorage which then on will either cause issues with
Or |
The order of if checking will not cause issues with canStale though |
Just for clarification I am not referring to "not owned" as keys stored using a e.g. second buildWebStorage method to be used in other axios instances, but other possible plain keys in localStorage been used in other parts of the app not related to axios or the interceptor |
That's why But will it be a breaking change? |
Honestly yes.. it will be breaking true.. suddenly everyone will have their stored keys rendered unusable if a default prefix is introduced yes. Since you have already mentioned the need for a prefix in your documentation then I guess in case of errors other devs will figure out.. It depends on each case.. Would I value more existing keys been rendered unusable once a default prefix was introduced or my app breaking for because I had other keys stored not related to axios? Personally I would prefer my app to continue to work even if that meant that I would loose the speedup gain cause some data would need to be fetched again from the remote. But in the end its up to you.. You cal always just keep it as it is and highlight the possible case with your release notes. |
You have a very strong case, i'm going to change and add a note on the next release. |
Hi, Thanks for the handy library.
I came over an issue trying to use localStorage when the values stored exceeded the browser quota. Since there was no exception handling in the set item method, this made the application break with a DOMException, and consequent requests of the app to an http api would also misbehave.
The current pull request addresses handling exceptions in the set method of buildWebStorage when using sessionStorage or localStorage APIs.
In case the DOMException is a QuotaExceededError the storage is cleared to allow the application to store new data.
A possible better approach would be to just scan the keys and only delete the ones that have expired until storage quota is less than the browser quota, but considering the synchronous behavior of localStorage and sessionStorage it could slow down the app or even freeze the browser (depending on the number of keys stored), not to mention the need to address fetching the browser quota using the StoreManager API to determine when we are below quota and if the value or consequent values to store would not raise new issues.
Therefore just clearing the storage should be a good compromise for most applications.
Let me know if you have any concerns over this. Writing typescript for the first time so you may encounter a couple of things possibly.