[Moin-user] I want a "changing logo" - browser cache problem

Matthew Nuzum newz at bearfruit.org
Thu Jun 21 15:30:27 EDT 2007


On 6/19/07, Sebastian Haase <sebastian.haase at mdc-berlin.de> wrote:
> Hi,
> I have a cron-job that regularly overwrites my wiki-logo
> /wiki/mylogo.jpg
> with a different image.
>
> But it turns out that I would have to explicitly press my browser's
> "reload"-button to see the current logo-image. Even changing to a
> different wiki page still shows the old image !!
>
> This is somewhat off-topic here -- but would anyone know a way to have
> the browser automatically load the current image as I change to a
> different wiki page !?

Option 1:
Serialize the name of the logo. Therefore,
/wiki/mylogo-2007-06-21-3.jpg

You don't have to keep the logos around, but you might want to keep
the last few, just in case someone pulls a cached copy of your page
and doesn't have the original logo.

Option 2:
You could write a little bit of javascript to make the logo name
unique and defeat any caching. This works without needing to serialize
your logos. All it does is add a little query string to the end of
your image name witch will be ignored by the webserver.

<script type="text/javascript">
d = new Date().getTime();
document.write('<img src="/wiki/mylogo.jpg?d='+d+'" alt="logo">');
</script>
<noscript><img src="/wiki/mylogo.jpg" alt="logo"></noscript>

The problem with option 2 is that it completely ruins caching. You
could enhance it to enable caching of the image for up to 1 hour with:
// current date
dt = new Date();
// current js epoch
dt = (dt.getTime() - dt.getMilliseconds())
// truncate to min
d = (dt - dt % 60) / 60
// truncate to hour
d = (d - d % 60) / 60
document.write('<img src="/wiki/mylogo.jpg?d='+d+'" alt="logo">');

I think you get the picture... because the url to mylogo.jpg changes
either with each request or at the top of each hour the browser's
cache is invalidated. People without js get the logo as specified by
the noscript.

-- 
Matthew Nuzum
newz2000 on freenode




More information about the Moin-user mailing list