views:

192

answers:

2

Hi,

Is there a way that I can force a page to reload, but only if it has been updated recently? I am working on a photography website so there are a lot of images, therefore it is best if it is cached.

However, if the content has been updated (e.g. more photographs added) then I want the browser to load a fresh copy, if not then use the version in the cache?

I was hoping that there was a meta tag that would take care of it. I've read about the expires tag but that wouldn't help as the page isn't updated regularly.

I'm only using CSS, HTML and Javascript, nothing server side. Any suggestions welcome.

+2  A: 

You can use an ETag.

HTTP 1.1 introduced a new kind of validator called the ETag. ETags are unique identifiers that are generated by the server and changed every time the representation does. Because the server controls how the ETag is generated, caches can be surer that if the ETag matches when they make a If-None-Match request, the representation really is the same.

Excellent information about caching can be found here.

Andreas Bonini
Just doesn't work if he can't set HTTP headers.
Felix Kling
That was edited in after I answered..
Andreas Bonini
+3  A: 

I think the best would be to use HTTP caching mechanisms. If you can’t use server side languages to send appropriate HTTP headers, try a built in solution like Apache’s mod_expire module. With that you can set a maximum age based on the request time:

ExpiresByType text/html "access plus 1 week"
Gumbo
I don't know when a page will expire because there are no regular updates, it's as and when there are new photos to add. For example, if I set a page to expire 1 month after I add it but don't update it for 2 months then the browser will unnecessarily take a fresh copy for the second month, or am I misunderstanding how this would work?
Fermin
@Fermin: The expiration affects only resources that are cached. An expired resource in a cache should not be used. Instead the resource should be requested again (either for revalidation or refetch). So in your case it doesn’t matter if you don’t know when a resource is expired. A low lifetime is just to ensure that the resource is requested often enough. A maximum age of one week means that your clients will only use variants that are not older than one week. And if you only set a low expiration for your HTML document, then only that documents will be requested that often and not the images.
Gumbo
So, to clarify the HTML source will be requested upon expiration but any images cached will be fetched from the cache? Excellent, thanks for clarifying that Gumbo.
Fermin
@Fermin: If you set the expiration for the images far in the future, they will not be refetched until then.
Gumbo