Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if a write to localStorage is canceled?

Let's say I make a call to local storage like so:

window.localStorage.setItem("key", bigJsonObject);

And immediately afterwards, the user closes their web browser. What will be the result of

window.localStorage.getItem("key") 

Will the bigJsonObject be partially written? Or will the whole write fail? Is their any way to guarantee that there will be no partial writes?

like image 276
Jamie Avatar asked Jan 06 '15 19:01

Jamie


Video Answer


1 Answers

Refer to §4.1 of the "web storage" specification:

The setItem() and removeItem() methods must be atomic with respect to failure. In the case of failure, the method does nothing. That is, changes to the data storage area must either be successful, or the data storage area must not be changed at all.

However, there have (historically) been browser bugs in this regard, e.g. some time before Chrome 21 until some time before Chrome 29.

like image 192
Lightness Races in Orbit Avatar answered Sep 30 '22 17:09

Lightness Races in Orbit