GithubHelp home page GithubHelp logo

modulexcite / cached-webpgr.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from select/cached-webpgr.js

0.0 1.0 0.0 188 KB

cached-webpgr.js - simple localStorage based caching of JavaScript files

JavaScript 66.67% HTML 33.33%

cached-webpgr.js's Introduction

Synopsis

This tiny library uses the Web Storage (localStorage) to cache JavaScrip files. Cached files will be loaded much faster than requesting them from a server (even local ones).
My test showed:

Chrome FireFox
Loading jQuery from CDN 268ms 200ms
Loading jQuery from localStorage 47ms 14ms

The library is so simple that you can read and understand all of its code!

Code Example

  requireScript('jquery', '1.11.2', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', function(){
    requireScript('examplejs', '0.0.3', 'example.js');
  });

In this example I cache jQuery and a local file that is dependent on jQuery. On the first load it will be loaded from the provided URL and on the second load it will be loaded from the localStorage. Changing the version string will cause the cache to be cleared (1st load) and a new version to be loaded from the sever (2nd load). Since the example.js is dependen on jQuery I load it in the callback that is triggered after loading jQuery.

Motivation

My feeling was that caching in localStorage should be a simple and straight forward. However I could not find a simple library without depencies that fulfilled this so I created this mini project, for documentation and learning.

When I tried to use basket.js it unfortunately had some dependency, otherwise it is probabley more advanced (and larger) than this script.

Installation

Copy the code from chached-webpgr.min.js into your .html file (loading it from a server would make it slower!) and use the code from the Code Example to load your scripts. Be aware of depencies, and use the callbacks for dependent scripts like in the example.

This code will only work on a server, file:// is not supported.

Caching of scripts cross domain only works if the CORS header is set on the remote server.

API Reference

requireScript(name, version, url, callback)

provides the external API. The internal API consits of 3 functions.

_loadScript(url, name, version, callback)
_injectScript(content, name, version, callback)
_cacheScript(name, version, url)

Please read the code to learn how they work (they are very short!).

License

cached-webpgr.js is released under the MIT License.

cached-webpgr.js's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.