Can you make a CSS file expire?

Can you make a CSS file expire?

by Jan Dierckx -
Number of replies: 8

Trying to learn CSS, I made some mistakes in the theme of our schoolsite. Unfortunately I didn't found out in my test install, so I had already uploaded the new theme to our server.

I have fixed the issue, but now all of the schoolcomputers keep showing the cached faulty theme. mixed
Is there anyway to add an expiry date or something like that to the CSS file. I don't want to go around and have all the students in every computerlab hit CTRL-F5 ... also because using the words theme, cache and CTRL-F5 makes students look at you as if you were some kind of lunatic wink

Average of ratings: -
In reply to Jan Dierckx

Re: Can you make a CSS file expire?

by Andrew Walker -
Hi,

One possible method you could try would be to append a query string to the end of the filename for your stylesheet. This should force the browser to download the latest version. To do this go into the header.html file for your theme, you should see the line

<?php echo $meta ?>

just after the opening head tag. Replace this with,

<?php echo str_replace('/theme/your-theme-name-here/styles.php', '/theme/your-theme-name-here/styles.php?revision=1', $meta); ?>

Now if you view the source from any of your moodle pages you should hopefully see ?revision=1 has been added to the end of the filename of your stylesheet.

If you ever needed to force another revision you could modify the query string again, e.g. ?revision=2
In reply to Andrew Walker

Re: Can you make a CSS file expire?

by Andrew Walker -
Sorry the blanks where the code should have been are,

<?php echo $meta ?>

which would be replaced by,

<?php echo str_replace('/theme/your-theme-here/styles.php', '/theme/your-theme-here/styles.php?revision=1', $meta); ?>
In reply to Jan Dierckx

Re: Can you make a CSS file expire?

by Chris Lamb -

You can console yourself with the knowledge that it should eventually expire from the browser's cache anyway, probably by tomorrow.

Keep your head down, and if anyone mentions it look surprised and say you'll look into the problem, and by the weekend the problem will have just gone away and everyone will think you're wonderful for fixing it!  big grin

Chris 

In reply to Jan Dierckx

Re: Can you make a CSS file expire?

by David Scotson -

The easiest way is probably to duplicate your theme, give it a new name, then upload and select that theme.

This achieves the same effect as Andrew's suggestion (basically creating a new URL for the CSS) but with slightly less effort, or at least a different kind of effort.

FYI, the Moodle CSS does have an expiry date (something surprisingly short like 30 minutes) but browsers and proxies can be a bit odd about actually following these directions.

In reply to David Scotson

Re: Can you make a CSS file expire?

by Jan Dierckx -

Thanks Andrew, Chris, David,

I like the psychological approach Chris provided. Maybe I'll combine all three solutions...

  • tell people I will look into it next weekend

  • then change the name of the css file

  • if people asked how I fixed it, talk about changing meta strings and point them to Andrew's post big grin

In reply to Jan Dierckx

Re: Can you make a CSS file expire?

by Matt Gibson -
I found this advice the other day and have followed it succesfully with my apache2 setup. I now have faster page refreshes as it forces the browser to cache the graphics that come with the theme. you can alter the css expiry times as you like by fiddling with .htaccess, but be aware that you need to be able to tell apache to load mod_expires and mod_headers.

Matt
In reply to Matt Gibson

Re: Can you make a CSS file expire?

by David Scotson -

Did you follow the testing procedure outlined in that article and see images being downloaded each time? I'm surprised that a browser would fetch an image on each page. If anything I have the opposite problem and often have to force refreshes to get images to update. And if the browser does try to fetch an image (or any other resource) a second time then, if all is going according to plan, then the server should say "no need, the copy you have is still fresh".

I honestly don't know that much about this area, but what I read in that article surprises me.

In reply to David Scotson

Re: Can you make a CSS file expire?

by Matt Gibson -
Yeah, I was getting full page reloadsevery time, images and all. sometimes, pages were taking around 25 seconds to load. I've noticed that moodle.org has similar load times sometimes. I've now got VASTLY faster performance, with all images and javascript running from the cache every time. To get it to refresh from scratch seems to only need the browser's 'refresh' button to be pressed and then the whole lot laboriously reloads. Maybe this was a quirk with my school's proxy setup or something (we use external hosting) but it's certainly a useful fix, especially with the HTML editor. Do you find it takes time for the editor to load each time? It could be that you already have this feature enabled on your webserver.