Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Basket.js: A simple script loader that caches scripts with localStorage (addyosmani.github.com)
52 points by abraham on March 2, 2012 | hide | past | favorite | 12 comments


I feel like using localStorage to cache stuff that could be handled by http caching is probably a mistake. I'm in the process of caching a bunch of our application in localStorage because a quirk in our client-side architecture means I need to know if something is cached before making an asynch request for content, (which you can't do with the standard http cache). That means I'm reinventing a cache invalidation scheme -- which we all know is hard, right? Plus the space allocated for localStorage is pretty small, so I have to have a way to prioritize the cache and bump out old stuff to make way for new. It's all rather convoluted.

IMHO, edge cases aside, most people shouldn't do this.


Caching assets in localStorage definitely isn't a "best practice," and seems like something that should only be done when necessary.

One of the best uses for localStorage though, is caching client-side templates. If you're building an app that's got a ton of different models and templates for each one, it makes your application a lot more efficient to lazy-load them as needed and cache them in localStorage for later use with versioning and cache invalidation.


Be aware that localStorage is synchronous and browsers currently load the entire localStorage for a domain into memory to satisfy the first request. Shouldn't be a problem here but something to be aware of.


Hey guys - I wrote this. Just to point out: this project is really just a proof of concept. I haven't personally seen any figures to suggest that localStorage caching is more optimal than standard browser caching, but once these benchmarks are available (hoping to get them up on jsPerf), they'll be posted to the same page.


You're better off using a manifest file, which is part of the HTML5 spec. Not everything needs to be done in JavaScript =)


There's an extra HTTP request to fetch the manifest on every page load.

In the same vein, does the browser check if files specified in the manifest expired? If yes it would once again waste some time...


But it will be eventually be written in JavaScript.


Doesn't the using the application cache (e.g. the manifest file you speak of) cause a user prompt? That's a deal breaker in many situations.


I am using a manifest in my new app and never witnessed a user prompt. I am testing on Android 2.2 native browser, chrome and safari.


It is my understanding that desktop browsers do present the prompt, but mobile browsers do not. This feature was mostly designed for mobile, since the browser cache in mobile tends to be constrained. I have a feeling as things move along, that prompt will go away entirely.


Please don't use local storage. It's synchronous and will hang the entire browser or tab while the script is loading: http://paul.kinlan.me/we-need-to-kill-off-the-localstorage-a...





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: