<?xml version="1.0" encoding="ISO-8859-1" ?>

<?xml-stylesheet type="text/xsl" href="/dev/devedge/lib/xsl/devedge-1.00/article_en.xsl"?>
<nde:article 
  xmlns:nde="http://devedge.netscape.com/2002/de"
  url="/viewsource/2003/link-prefetching/"
  xmlns="http://www.w3.org/1999/xhtml"  
  xml:lang="en">

  <nde:header>
    <nde:title>Prefetching Content for Increased Performance</nde:title>
    <nde:category>Article</nde:category>
    <nde:authlist>
      <nde:author>
        <nde:authname>Doron Rosenberg</nde:authname>
        <nde:authaffil>Netscape Communications</nde:authaffil>
        <nde:email href="/community/feedback/">Feedback</nde:email>
      </nde:author>
    </nde:authlist>
    <nde:pubdate year="2003" month="02" day="28"/>

    <nde:channels>
      <nde:channel id="viewsource" />
      <nde:channel id="html" />
      <nde:channel id="gecko" />
      <nde:channel id="ns-7" />
    </nde:channels>
  
    <nde:summary>
      Learn how to optimize download speeds of your webpages using link prefetching.
    </nde:summary>  
  
    <nde:abstract>
      This article shows how to speed up web page loading times by prefetching content for sites 
      that a user is highly likely to visit.      
    </nde:abstract>    
  </nde:header>

 <nde:head>
   <style type="text/css">
     .sourceCode {padding-left: 5px; padding-top:5px;}        
     
     .sourceView {margin: 5px; padding: 5px; border: 1px dashed grey; font-family: Verdana,sans-serif;}
     .sourceInfo {padding-bottom: 5px; font-size: 0.9em; padding-right: 5px; border-bottom: 1px dashed gray; 
                  border-right: 1px dashed gray;}
      #content h2 {margin-top: 1em}
    </style>
</nde:head>

<nde:content>

  <h2>Introduction</h2>
     <p>
       Link prefetching is a browser feature that allows an HTML page to retrieve web content when an end user's browser connection
       is idle.  The prefetched content gets stored into the browser's cache, and will therefore appear quickly once the user goes to 
       the page containing the prefetched content.  It's important to note that prefetching will be stopped when the user clicks on a 
       link, loads a new page, or makes the browser do any sort of networking activity.  Partially prefetched content is still stored 
       in the user's cache.
     </p>
     
     <p>
       The prefetching mechanism described in this article is the one implemented in Netscape Gecko-based browsers.
     </p>

  <h2>How to Set Up Prefetching</h2>
     
     <p>     
       <div class="sourceView">
         <span class="sourceInfo">Figure 1 : How to prefetch content in an HTML document</span>
    
         <p class="sourceCode">
           <strong>Using the HTML link element:</strong><br />
            &lt;head><br />
            &#160;&#160;&lt;link rel="prefetch" href="/images/big.jpg"><br />
            &lt;/head>
            
            <br /><br /><strong>Using a meta header</strong><br />
            &lt;meta HTTP-EQUIV="Link" CONTENT="&lt;/images/big.jpg&gt;; rel=prefetch">
         </p>         
       </div>        
     </p>
     
     <p>
       As shown in Figure 1, a web page can specify what content may be prefetched via the <code>link</code> tag with the 
       <code>rel</code> attribute set to <code>prefetch</code> or <code>next</code> and the <code>href</code> attribute pointing to the 
       content that should be prefetched.  The <code>link</code> tag has to be inside the <code>head</code> tag to make
       prefetching work. Is is also possible to do the same with a meta tag in the HTML document.  An HTML document can 
       contain multiple such prefetching "hints." Each hint is processed serially (one at a time), not in parallel.  
       Prefetching is broken off when the browser needs to connect to the network, such as loading new content.
     </p>
     
     <p>
       Whenever Netscape 7.x prefetches content, it sends a <code>X-moz: prefetch</code> header with that request.  This allows the server
       to distinguish a prefetch request from a normal HTTP request by the browser.
     </p>
           
  <h2>What Can Be Prefetched?</h2>
  <p>
    HTML documents can be prefetched simply by pointing to the html file (<code>&lt;link rel="prefetch" href="document.html"></code> 
    for example), however none of the linked media such as images and external stylesheets/javascript files are prefetched.  Since a 
    document can contain multiple prefetching commands, it's easily possible to prefetch external media as well by adding more hints.
  </p>
  
  <h2>What Cannot Be Prefetched?</h2>
  <p>
    There are some important restrictions as to what can be prefetched.  
	<ul>
	<li>Secure sites (https://) are not prefetched for security reasons.</li>
	<li>URLs that contain query strings (www.netscape.com/search.psp<strong>?query=foo</strong> for example) are ignored 
    since caching such pages usually adds no benefit.</li>  
	<li>Non-HTTP protocols such as FTP are also ignored since they are not cachable.</li> 
	</ul>
  </p>
      
  <h2>Example</h2>  
  <p>
    An example of prefetching is the DevEdge Wallpaper Gallery, where the user can navigate several pages with
    DevEdge-themed wallpapers.  Each image is about 120k, so a end user will usually have to wait a bit before the image is retrieved
    and displayed.  Two versions of the Gallery exist for comparison - one with prefetching hints and one without prefetching.  Even on 
    a broadband connection the images load much faster out of the cache than freshly from the network.  When testing, be sure to clear 
    your cache each time you run the example.  You can clear your Netscape 7.x cache by going to the Edit menu, choosing Preferences,
    expanding the Advanced item from the left-hand tree and clicking on Cache.  Then click the "Clear Cache" button.    
  </p>
  
  <p>
    <a href="example-withprefetch/">Example with prefetching</a><br />
    <a href="example-noprefetch/">Example without prefetching</a>    
  </p>
  
  <p>
    There is also a narrowband-testable version where the images are under 20k in size:<br />

    <a href="example-withprefetch-narrowband/">Example with prefetching (for narrowband users)</a><br />
    <a href="example-noprefetch-narrowband/">Example without prefetching (for narrowband users)</a>    
  </p>  
  
  <p>
    One real-life example of prefetching is on <a href="http://search.aol.com/">AOL Search</a>.  The top two search results
    are prefetched, as there is a high likelihood that the user will visit those pages.
  </p>
      
  <h2>Supported Browsers</h2>
  <p>
    Netscape 7.01 and above as well as Mozilla 1.2+ support link prefetching. Opera 7 will prefetch the content referenced by the
    <code>link</code> tag with an <code>rel</code> attribute set to "next" only if the user presses "ctrl-x". 
  </p>
  <p>Browsers which do not support link prefetching will simply ignore the prefetching code.</p>

    <h2>Recommendations</h2>    
     <p>
       Prefetching makes sense when it's very likely that the user will visit the prefetched content.  An example would be the top story
       on a news site, the top search results of a search query, or, in case of a image gallery, the next image to be shown.
     </p>

  <h2>Resources</h2>    
     <ul>
       <li><a href="http://www.mozilla.org/projects/netlib/Link_Prefetching_FAQ.html">Link Prefetching FAQ</a></li>
       <li><a href="http://hotwired.lycos.com/webmonkey/98/26/index2a.html">Site Optimization Tutorial</a> 
           at <a href="http://hotwired.lycos.com/webmonkey/index.html">Webmonkey</a></li>
     </ul>    
  </nde:content>

  <nde:related area="nde">
    <nde:item url="/central/html">HTML Central</nde:item>
 <nde:item url="/library/releases/netscape-7/">Other Articles about Netscape 7</nde:item>
  </nde:related> 

  <nde:related area="ext">
    <nde:item url="http://www.mozilla.org/projects/netlib/Link_Prefetching_FAQ.html">Link Prefetching FAQ</nde:item>
    <nde:item url="http://hotwired.lycos.com/webmonkey/98/26/index2a.html">Site Optimization Tutorial</nde:item>
  </nde:related>
  
</nde:article>
