Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-05-23 19:55:46

thijs
Member
Registered: 2012-04-17
Posts: 39

adding a random string to img src? browser cache issue

I dunno if this can be done easily, or even if it’s a good idea, but I wondered if I can influence browser caching of the images on my site, by adding a random string at the end of of the img src, or some other method.
I occasionally need to change and reup images, and because textpattern changes the image names into 1.jpg, 2.jpg etc, a browser will occasionally display the wrong/old image because it has the same name. I want to be able to replace an image and be sure visitors see the right one. (Once I did a clean reinstall, and reupped all images in a different order, which meant mismatches aplenty.)

So my question is if there a way to prevent browser caching, if possible in a measured way, so that only replaced images are reloaded, and not every image each time.

Any help appreciated as usual!

Offline

#2 2013-05-24 07:17:34

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,395
Website

Re: adding a random string to img src? browser cache issue

You can use cache-control headers in a htaccess file in your images folder to do that, see e.g. this page. Set the time to ‘0’ (zero) in your case. That will apply to all content images on your site, though.

Manually, if you use Textile or plain HTML syntax to insert the images, you can add a query string. If you regularly modify those images, there is probably a way to add a more random string with a bit of txp:php magic (some of the forums php wizards will have to chime in though…).


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg

Offline

#3 2013-05-24 09:34:17

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: adding a random string to img src? browser cache issue

No good idea! Standard aim to boost performance is to cache all static (media, css, javascript) files as long as possible.

Instead update pictures and use new image (path/file name) instead of cached one.


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

#4 2013-05-24 14:47:13

etc
Developer
Registered: 2010-11-11
Posts: 5,396
Website GitHub

Re: adding a random string to img src? browser cache issue

thijs wrote:

I occasionally need to change and reup images, and because textpattern changes the image names into 1.jpg, 2.jpg etc, a browser will occasionally display the wrong/old image because it has the same name.

I wonder on what occasions this happens. When you update an image, Textpattern makes the server send appropriate headers, so browsers should see that the image has been modified and download the new one (except for history navigation). Appending a random hash to src is easy:

<txp:etc_query data='<txp:image id="17" />' query="img" replace="@src+=?hash={$uniqid()}" />

but, unless you uncheck “send lastmod” preference (which would be pity), this alone will not help, since browsers will still get a “Not Modified” header.

Offline

#5 2013-06-06 15:07:05

thijs
Member
Registered: 2012-04-17
Posts: 39

Re: adding a random string to img src? browser cache issue

thanks for the replies, sorry for not getting back earlier!

Markus, I understand that it’s not generally a good idea to prevent browsers from caching, but I’d be happy with an other method that doesn’t throw the baby out with the bathwater.

Phillippe and Oleg, thanks for your suggestions, I will look into them!

much appreciate your replies.

And if there’s someone who can cast light on the header issue, I’d be happy to hear that too.
cheers,
Thijs

Offline

#6 2013-06-06 15:31:25

towndock
Member
From: Oriental, NC USA
Registered: 2007-04-06
Posts: 333
Website

Re: adding a random string to img src? browser cache issue

We have a lot of dynamic content on our key site that I don’t want cached. Web cams that update every 10 minutes, weather images, etc.

I use several methods depending on the content, but one method is to use a the Txp plugin upm_datetime like this:

<img src=“imagename.jpg?x=<txp:upm_datetime format=”%s” />” />

It puts out the number of seconds since… well I forget… since Moses, or 1970, or the invention of spam. Whatever – it is a big number, and changes every time. It works.

Last edited by towndock (2013-06-06 15:32:57)

Offline

#7 2013-06-06 20:18:48

etc
Developer
Registered: 2010-11-11
Posts: 5,396
Website GitHub

Re: adding a random string to img src? browser cache issue

towndock wrote:

It works.

It works because your page always sends 200 OK header, making browsers redownload it on every request, so you don’t (fully) benefit from caching. I guess your txp site is not sending the Lastmod header, which is not necessary a bad thing. But adding random parameters makes the browser fill its cache with many copies of identical images, and that’s, well, suboptimal. This said, it seems to be no other choice on Textpattern level.

A proper solution would pass by Apache htaccess, as suggested by phiw13, targeting some particular directory/files. You could make browsers store some images for at most 10 minutes or so, or not at all. Google for image no cache, and you should be set.

Offline

Board footer

Powered by FluxBB