Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of storing JS and CSS in localstorage?

While reading Stoyan Stefanov's web performance daybook I have encountered the following:

Google and Bing store JavaScript and CSS in localStorage to improve their mobile site performance

and the link to the blog with a description. Nonetheless I read it (and have reasonable experience working with localstorage), I sill can not understand what is the point of such action. In my opinion the browser is already doing a job of caching CSS and JS and there is absolutely no point to store them in localstorage.

Can anyone explain in simple English what is the reason?

P.S. In the beginning I thought that may be it has something to do with the size of the cache of mobile browsers, but when I checked I found that they have more then 20Mb of cache which (in my opinion) is pretty enough to make sure that such popular sites like google and bing will have place to be cached.

like image 820
Salvador Dali Avatar asked Dec 06 '13 00:12

Salvador Dali


People also ask

Why do we use localStorage in JavaScript?

localStorage is a property that allows JavaScript sites and apps to save key-value pairs in a web browser with no expiration date. This means the data stored in the browser will persist even after the browser window is closed.

How do I store CSS in local storage?

Let's understand through this example (pseudo-code): var load_from_server = true; if (detect local storage) { if (cache of css, js found) { load the css and js from local storage cache load_from_server = false; } } if (load_from_server) { document. write('<script>...'); } $( window ).

Can we store JavaScript object in localStorage?

In summary, we can store JavaScript objects in localStorage by first converting them to strings with the JSON. stringify method, then back to objects with the JSON. parse method.

Is it safe to use local storage in JavaScript?

No. localStorage is accessible by any webpage, and if you have the key, you can change whatever data you want. That being said, if you can devise a way to safely encrypt the keys, it doesn't matter how you transfer the data, if you can contain the data within a closure, then the data is (somewhat) safe.


1 Answers

+1, Nice question. The only reason I can think of is for speed of load time, in those cases were it is no longer stored in the cache. Also, as the post you linked to pointed out, it makes their HTML files a lot smaller ---200kb to39kb is a big difference.

EDIT:

You asked in what conditions would it no longer be in the cache. From my understanding of how the cache works, I believe that the cache size is fixed, so after a while those files would get pushed out to make room for more files. Hope it helps

like image 141
tjons Avatar answered Nov 14 '22 23:11

tjons