Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 localStorage for assets (stylesheets, JavaScript, images etc)

To save both client download time and network usage, is it possible to use the localStorage feature of HTML5 to store contents of linked stylesheets, javascript files and binary data (e.g. images), rather than hitting the server each time?

Would that just complicate things - since I assume you would have to add the linked asset via JavaScript rather than just a script or link element, breaking the page for those without JavaScript enabled)? Unless you can parse the content (using HEAD requested to check last modified date and other headers) before the browser downloads it.

Or best just to stick with 304 Not Modified and eTag headers?

like image 514
SamWM Avatar asked Jan 26 '10 11:01

SamWM


People also ask

What is localStorage in HTML5?

The localStorage read-only property of the window interface allows you to access a Storage object for the Document 's origin; the stored data is saved across browser sessions.

How is data stored in HTML5 storage?

The Web Storage is one of the great features of HTML5. With the Web Storage feature, web applications can locally store data within the browser on the client side. It stores data in the form of key/value pair on the browser. Web Storage sometimes also known as DOM storage.

Can we use localStorage in HTML?

localStorage as a type of web storage is an HTML5 specification. It is supported by major browsers including IE8. To be sure the browser supports localStorage , you can check using the following snippet: if (typeof(Storage) !==


2 Answers

I think in this case you should consider offline caching:

  • https://developer.mozilla.org/en/Offline_resources_in_Firefox#Specifying_a_cache_manifest

You can also store user input data in localStorage or sessionStorage:

  • https://developer.mozilla.org/en/DOM/Storage#sessionStorage
  • https://developer.mozilla.org/en/DOM/Storage#localStorage

Don't use globalStorage (not a standard).

I've written an article about offline, see: http://hacks.mozilla.org/2010/01/offline-web-applications/

It's about offline, but such mechanism can be used to boost your web app.

like image 53
Paul Rouget Avatar answered Jan 17 '23 16:01

Paul Rouget


You could base64 encode your image/binary data and store it as a string in localStorage, using base64 urls doesn't work in a few browsers so it's not a perfect solution.

CSS and js would be fine, you could write them into the page, or use a base64 url also.

I wouldn't worry about doing this breaking the site for non JS users, as is JS is disabled you cant access localStorage anyway.

like image 34
garrow Avatar answered Jan 17 '23 16:01

garrow