Can you store JavaScript and CSS files in localStorage to improve the performance of an online web app?

余生长醉 提交于 2019-12-02 23:32:37

It should be possible to get a browser to cache assets like javascript includes (although ultimitely these directives can be over-ridden by the user, probably not something to worry about). See here for a crash-course on caching. In particular see the HTTP header:

Cache-Control: public

which should force the browser to cache the asset. Not sure if this works across sessions, e.g if the browser is closed and re-opened, but at least only one request will be made when the user first hits your site, and won't be re-requested on subsequent page views.

Also make sure your javascript includes are minimised, and see here for general tips on speeding things up.

iOS will not cache resources beyond 25k. localStorage should work.

I've also been attempting to get this done without any luck. It seems the only way to do this is the follows:

  • Use AppCache only for images
  • Have all of your CSS and JS inline on the first page
  • Once everything is loaded, have a JS script running to pull each of the scripts into localStorage
  • You can version them through cookies to make sure the user has the most up-to-date copies

Have a look at http://sunpig.com/martin/archives/2011/05/21/considerations-for-caching-resources-in-localstorage.html for ideas on implementation!

I'm only halfway through but it's already working much better than depending on the appCache.

Edit: Keep in mind this is only useful for multi-page apps. If you're building a single page JS app, I don't think it's worth the effort to store back in localStorage. You might as well just inline everything and keep it there for best performance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!