How do I make the first page of my web site come up faster

How do I make the first page of my web site come up faster
Everyone knows that the all browsers have the ability to cache the files sent by you web site, so that the next time your visitor comes to your site, ideally all the files are served from the local file system / browser cache and there is no delay for the time it normally takes to send the files across the internet / network and for you hosting server to serve them up. So here is the ideal scenario, you set up you files such that they are essentially cached forever, but somehow magically when you do updates, the browser will pull the updated files from the server one time only and then cache the files forever until you do another update. If you want to read more about caching this site has a well written summary http://www.mnot.net/cache_docs/

There are a couple of caching parameters that are very important.

1) The browser settings regarding caching, in particular the user can change the default browser setting to never use the cache and always get the files from the server, this is usually done by developers, and thankfully not normally done by the average user. I say thankfully because if they turn caching off there is nothing we can do about it!

2) The browser sometimes checks the server just to see if the date on the file has changed from what it has in the cache, and then if they are different downloads the newer version into the cache. Most browsers behave that if you hit refresh, it well go and check the dates on all files, but if you access the site from a favorite, or type it in to the address bar it does not.

3) The length of time files are kept in the cache before invalidating the cached files is something the response from the server has control of in the “header” section of the html response served up. I will give you examples on how to set “cache forever” for PHP, Apache, and Nginx the three most common servers out there. If the hosting setup / provider you are using does not allow you to set the headers for all files served, then I can help you for a small fee decide the best move to make specific for the type of site / technology you are using.

Enough already you say, please get to the meat of the matter! Ok here is all you need to know, including how to handle Swf files which some browsers “hang on to differently” and require special handling. Here are the steps involved.

1) change your header to say essential “cache forever”, by changing your server

Example code snippet(s) can be found at http://deploymyapptothecloud.com

Here is the magical part call windows.location.reload(true) if you deploy updated code to the server

2) add version number into you main index.html file, and make and Ajax / XMLHttpRequest to the server to check if that version number matches what is on the server, I do this after a delay of 1.5 seconds to let my site stabilize before possible refresh to get new versions of files. Here is the Javascript and and build snippets to use windows.location.reload(true) to allow the browser to cache your files essentially “forever” while still allowing site updates to be pushed down to the browser as required

Example code snippet(s) can be found at http://deploymyapptothecloud.com

There is one gotcha / issue when your file set includes flex / flash swf files. the reload(true) does not get fresh versions of the swf files on some browsers, especially IE. So they require special handling. For these files we will make use of the fact the the key used to store files in the browser cache includes the query string, so we will make the loading of swf files have ?compileVersion=not-set as part of their query string. We use the ant build script to modify the “not-set” to the same build time stamp as above. Note the below technique will obliterate an values passed on the url that you want to access from Flex / Flash so if you need those values convert them to “flash_vars” so they can be accessed in Flex using Application.application.parameter in place of Query.parameter. Here is my example using

Example code snippet(s) can be found at http://deploymyapptothecloud.com

And there is one last gotcha for Flex applications that use ModuleLoader, the query string on the main application and the url for the module load must match the domain and the query or else you get a “sandbox security violation” and the module will not load. So make sure the compileVersion is either passed to Flex as a flex var or declared in you Flex main and modified by the Ant build script so that when you set the url for the module you are loading , you can append ?compileVersion=1234 to the module url. I highly recommend you replace the ModuleLoader with loading your modules via a byte array served from the back end. The reason for this is all modules can be decompiled, and yes there are obfuscators out there that can be used to prevent theft of you code, but it is even better if you serve your modules as a byte array and have your backend secured to only serve the modules to validated uses (after login). I cover this technique in the Flex code snippets section of this site.

Further details and syntax highlighted code snippets can be found athttp://deploymyapptothecloud.com or in Spanish at?http://desplegar-mi-aplicacion-a-la-nube.es

Processing your request, Please wait....