GithubHelp home page GithubHelp logo

freeozyl80 / fis3-postpackager-cacheresource Goto Github PK

View Code? Open in Web Editor NEW

This project forked from huangwenming/fis3-postpackager-cacheresource

0.0 1.0 0.0 9 KB

A fis3 plugin to optimize the page loading speed by localstorage

JavaScript 100.00%

fis3-postpackager-cacheresource's Introduction

A fis3 plugin to optimize the page loading speed by localstorage

fis3-postpackager-cacheresource ![NPM version][npm-image]

Description

1.This is a FIS3 plugin to optimize the page loading speed combined with fis3-postpackager-loader.

2.The key method to optimize the page loading speed:

The page will check whether there are corresponding resources in localstorage. If it exits, the page will load the resources from localstorage, otherwise, the page will send requests to the server to get resources. When the at resources are loaded, the page will push the resources to localstorage.

Installation

Run npm install fis3-postpackager-cacheresource

Usage

fis.match('::package', {
        postpackager: [fis.plugin('loader', {}),fis.plugin('replacescript', {})]
    });

Example

Pre processing

<script type="text/javascript" src="/games/newbattle_2463497.js"></script>
<script src="/games/static/libs/pano-webgl.js"></script> 

After processing

<script ls_id="/games/newbattle_2463497.js">
	if (localStorage) {
		var scriptFromCache = localStorage.getItem("/games/newbattle_2463497.js");
		var scriptDom = document.querySelector("[ls_id='/games/newbattle_2463497.js']");
		if (scriptFromCache) {
			scriptDom.text = eval(scriptFromCache);
		}else {
			AjaxPage("/games/newbattle_2463497.js", "/games/newbattle_2463497.js")
		}
	} else {
		scriptDom.defer = true;
		scriptDom.setAttribute("src","/games/newbattle_2463497.js");
	}
</script>
<script ls_id="/games/static/libs/pano-webgl.js">
	if (localStorage) {
		var scriptFromCache = localStorage.getItem("/games/static/libs/pano-webgl.js");
		var scriptDom = document.querySelector("[ls_id='/games/static/libs/pano-webgl.js']");
		if (scriptFromCache) {
			scriptDom.text = eval(scriptFromCache);
		}else {
			AjaxPage("/games/static/libs/pano-webgl.js", "/games/static/libs/pano-webgl.js")
		}
	} else {
		scriptDom.defer = true;
		scriptDom.setAttribute("src","/games/static/libs/pano-webgl.js");
	}
</script>

The AjaxPage function

 function GetHttpRequest(){
	if ( window.XMLHttpRequest )
		return new XMLHttpRequest();
	else if ( window.ActiveXObject )
		return new ActiveXObject("MsXml2.XmlHttp") ;
}
function AjaxPage(sId, url){
	var oXmlHttp = GetHttpRequest();
	oXmlHttp.onreadystatechange = function(){
	if ( oXmlHttp.readyState == 4 ){
		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ) {
			IncludeJS( sId, url, oXmlHttp.responseText );
		}
	}};
	oXmlHttp.open("GET", url, false);
	oXmlHttp.send(null);
}
function IncludeJS(sId, fileUrl, source) {
	if (( source != null ) && ( !document.getElementById(sId) )) {
		var oHead = document.getElementsByTagName("HEAD").item(0);
		var oScript = document.createElement("script");
		oScript.type = "text/javascript";
		oScript.id = sId;
		oScript.defer = true;
		oScript.text = source;
		localStorage.setItem(sId, source);
		oHead.appendChild(oScript);
	}
}

License

MIT © 2016 huangwenming ([email protected]) [npm-image]: https://badge.fury.io/js/fis3-preprocessor-px2rem.svg

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.