Skip to main content

Pre-fetching CDN calls

Pre-fetching has been around for some time now and is often used to load images and CSS files.  While your mileage may vary, you can also pre-fetch your CDN calls (e.g. Jquery) and make your apps a bit faster by loading scripts in the <head> section to load JS calls before your <script> get resolved by browsers.

You can also CDN outage proof it (when it rarely happens) by having local JS libraries (for rainy days) using some quick jquery checks (based on code from +Hanselman)

Testing

Without pre-fetch:
Code Snippet
  1. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  2. <script>
  3.     if (typeof jQuery == 'undefined') {
  4.         document.write(unescape("%3Cscript src='[yourlocalbackupjqueryfile].js' type='text/javascript'%3E%3C/script%3E"));
  5.     }
  6.     var startTime = new Date();
  7.     jQuery.ready();
  8.     var endTime = new Date();
  9.     var difference = endTime - startTime;
  10.     alert("With out prefetch: " + difference + " milliseconds");
  11. </script>

Result:

With Pre-fetch:
Code Snippet
  1. <link rel="dns-prefetch" href="//ajax.googleapis.com" />
  2.     <link rel="dns-prefetch" href="//ajax.googleapis.com" />
  3.     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  4.     <script>
  5.         if (typeof jQuery == 'undefined') {
  6.             document.write(unescape("%3Cscript src='[yourlocalbackupjqueryfile].js' type='text/javascript'%3E%3C/script%3E"));
  7.         }
  8.         var startTime = new Date();
  9.         jQuery.ready();
  10.         var endTime = new Date();
  11.         var difference = endTime - startTime;
  12.         alert("With prefetch: " + difference + " milliseconds");
  13.     </script>

Result:



saved about 1 millisecond!

Comments

Popular posts from this blog

Processing ASP MVC Web API Requests in Multi-threaded JS Web Worker

Unlike an asynchronous Ajax call, HTML5 Web workers provide an opportunity to run a Multi-threaded JavaScript code in modern browsers that support them . Each worker spawns an isolated thread with dedicated JavaScript Event Loop, Stack and Heap memory. For example a regular Ajax Call to MVC Web API service call goes through the following asynchronous call cycle. The JavaScript Event Loop in this case could be interrupted by events that are being executed on the UI; for instance, a "window.alert", could possibly stop all scripts on the page from executing until a user responds. Replacing the Ajax Call with HTML5 web worker provides a great way to run long running scripts in separate threads so that asynchronous code execution is not interrupted by UI events. Here is the a JavaScript worker implementation of the same MVC Web API call using a JavaScript web worker. Despite the advantages of using a web worker, implementing one requires working with some constr...

Turning WCF Service into .asmx for debugging

Even though .asmx web services are becoming dinosaurs of the new .NET world of WCF. I missed the simplicity of debugging code right in visual studio without: Creating a client to consume WCF service Attaching w3p.exe process and Adding break points  One quick solution: Turn WCF service into .asmx service with few lines of code, debug your code with asmx, and turn .asmx off during deployment.  Detail steps: 1- First take your WCF class and add WebService attribute to it Code Snippet /// <summary> /// Dual mode with .ASMX and WCF /// </summary> [ WebService (Namespace = "http://www.yourdomain.com/" )] 2- Then add WebMethod attribute to a function you want to expose in .asmx Code Snippet [ WebMethod ] public List < PageController . Page > DualFunction() { 3- Take the .svc file from your solution - copy and rename the copied file [YourOriginalWCFFile.asmx]. Open up the copied file and rename "ServiceHost...

Simple and reusable directive for making list elements draggable.

I was working on Angular lists and needed a easy to implement and reusable solution to make list items draggable . I chose to implement the solution as an Angular directive so that I can pass scope references to the new helper controller and execute ng-repeat, as needed, to recreate Dom elements. After adding the helper controller, I was able to make ng-repeat lists in my application draggable by simply adding a custom attribute and passing the Angular collection name. Here is a Plunkr demo  and full GitHub source code .