views:

39

answers:

3

I am making a static site which is 'forced' to be cached via Cache-control, etc.

When a user visits my site, I want the browser to crawl my site, caching pages, so when the user navigates to a page, the load is almost instant.

(I do not need a recursive crawl, as that will probably happen as the user navigates between pages. I just need to crawl the links on the current page, and of course not re-caching a page which has already been cached.)

(Also, I am not changing pages using Ajax-like techniques. These are essentially normal flat HTML files with normal links.)

How can I do this pre-caching using Javascript? (I am using jQuery.)

A: 

Not only caching of the static content, lots of things need to be done for improving website performance look at Enhancing user browser experience

Amareswar
I understand that. I want to *pre*cache the pages, so it's already cached before the user visits.
strager
A: 

Look into the new HTML5 feature that is the Cache Manifest.

Jakub Hampl
I should have mentioned this needs to work reasonably with IE7/8, Safari, and Firefox.
strager
Well this is one of the things that won't be even noticed on browsers that don't support it, so I'd recommend doing it anyway.
Jakub Hampl
Otherwise you might be interested in this: https://developer.mozilla.org/en/Link_prefetching_FAQ
Jakub Hampl
+1  A: 
$.ajaxSetup({ cache : true, type : 'GET' });

$('a').each(
    function()
    {
        $.ajax({ url : $(this).attr('href') });
    }
);

But I am not sure your browser will cache the page for further use without using an XmlHttpRequest.

Boris Guéry
This was my approach. In my experience, the XmlHttpRequest object caches page requests unless told otherwise - and that cache is valid whether or not subsequent requests are "natural" browser requests or further AJAX calls. If you're forcing a cache header to the client, any AJAX calls to the link hrefs on the page should achieve the pre-cache behavior you are asking for. Of course, this is just behavior that I've experienced - I can't say if this is behavior that one should count on.
BradBrening
For some reason Firefox doesn't ALWAYS respect the caching I set up in Ajax requests. It works for most requests, though, and it always keeps the caches so page navigations are speedy. Thanks for the answer!
strager