Building an AJAX Application: Design pattern and strategy - Cached HTML

(by Mark Qian on 9/14/2006)


Introduction

  In early days, web publish was static and simple: browsers fetched static html files from servers,
  displayed them, cached them and never fetched them again. The web world was changed after
  "dynamic" publish kicked in: we didn't cache pages any more. Just like enjoying vegetable and fruit, 
  we mostly got "fresh" pages from server containing the same static content again and again. 
  The result is that we suddently face heavy server load and much more bandwidth requirement. 
  
  Can we go back to the simple life: load once and never load again? The answer is Yes.
  
  Let's take a look at major pages on most popular commercial web site such as ebay.com and
  amazon.com. You may notice that a big percentage of content are "static" such as header area, 
  navigation area and footer. This static content is sent over and over during a single visit or across visits because 
  it is published dynamically and browser can not cache them. To verify, just download a free HttpWatch to see how those 
  jsp and php are sent back for every page load. 
  
  

Solution

The strategy of "Cached HTML" is to build these pages as static html pages and mark them as "never expired"(as fresh). A fresh representation will be available instantly from the cache. Then use Javascript (AJAX) to dynamically load the dynamic content. In this way, browser doesn't even send any validation request for the container file that consists of static content to the server (no 304). The static html pages simply always load these pages locally. And the Javascript resideing in the static content is launched to fetch the dynamic content.

Demo

Conclusion

For those pages with a lot of static content mixing with dynamic data, this approach could save huge amount of bandwidth and server cup time. Using HttpWatch to watch web pages, you will see most time browsers spend on server is to load dynamic content like jsp and php while files containing image and script are cached. Remote Scripting provides a "second chance" to load dynamic into static. One of the drawback of this approach is that it is not easy to update the static files with renaming since the files are containers. You may also want to see an opposite solution of mine at Building an AJAX Application: Design pattern and strategy - Client Side Include It is easier to update the static content in that approach.