Forum Moderators: open

Message Too Old, No Replies

HTML and CSS Changes and Cache

         

montman

11:21 pm on Dec 7, 2009 (gmt 0)

10+ Year Member



I have a web page with an external CSS. It was working fine, except was not passing validation. So I changed the html and the stylesheet. My concern is that a visitor's browser might be using either the latest version of one of these, and the old version of the other, so things would not look right.

So will the visitors browser, if using the cache, be using both the old html and CSS files (if so things would still look okay). Or is there some process that would mean that if the visitor's browser is using one of the revised html or CSS files, it will use the other revised one?

tedster

2:15 am on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From my experience, the cache of the HTML and CSS will stay in sync - either both will be new or both will be the cached version.

montman

2:17 am on Dec 8, 2009 (gmt 0)

10+ Year Member



Thank you tedster. That is a relief.

jdMorgan

2:30 am on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer depends on what HTTP cache-control headers you sent with your HTML and CSS content, and the Expires time you've set for each object type. If you haven't set this up yourself and/or don't know how they're currently configured, then the answer is that there is no guarantee (or even any probability) that the user's cached objects will be "in sync." You will therefore have to assume the worst -- that users could get any 'mixture' of old/new html with old/new css.

The good news is that most people's reaction to a screwy-looking page that nevertheless looks interesting would be to 'try again' and reload the page.

Jim

montman

3:10 am on Dec 8, 2009 (gmt 0)

10+ Year Member



Now I am very nervous about this. I don't have any idea how to set up cache control headers. Can I set it to not use any cached items prior to a certain date and time? If so what is all the code that would go in the header for this?

jdMorgan

3:35 am on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You set the cache-controls on the objects you serve today -- that is, the page and css that will be fetched and cached today. If pages were fetched, served, and cached previously, you can't "reach into the user's browser cache and change them" now -- it's too late for that. Cache-control done on your objects now will control browser behavior later. If there's one thing that absolutely and unforgivingly demands planning ahead, cache-control is it.

Caching also isn't a subject that can be comprehensively-covered in a forum thread -- It's worth at least one dedicated chapter in a book on servers or Web technology.

Jim

montman

3:53 am on Dec 8, 2009 (gmt 0)

10+ Year Member



Thank you for your response.

I found this on a web site:

<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

Above META tags should be placed in between your <HEAD> and </HEAD> tags. Also note that the specified date should be a date in the past so that the browser will immediately discard the cached copy or not cache it at all.

Why use two tags you ask? Although "Expires" tag is properly handled by most browsers, some older browsers may still look for the "no-cache" tag. If you're not worried about users with old browsers, using the "Expires" tag alone is sufficient and will make it possible to specify the exact time to expire content.

Is there any potential problems with this?

swa66

10:49 am on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Easiest to fix this: add a version number to the name of the CSS file.
Update the SSI included link to the CSS file(s) [plural for conditional comments pointing to IE specific overrides]

That way you put up a new CSS, and then update all your pages in one go to point to the new CSS. Anything still cached still gets the old CSS, anything new gets the new CSS. No reason to fear any problems.

BTW: Turning off caching for static content is a bad idea IMHO.

jdMorgan

1:25 pm on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> add a version number

That's a much better approach than relying on <Meta>s, because caching proxies in the network (e.g. AOL, EarthLink) don't read the HTML content of pages, and so may 'keep' old copies despite the HTML meta-information.

That can provide immediate relief and give time to research and set up proper server cache-controls on a site-wide basis.

Jim

montman

11:40 pm on Dec 8, 2009 (gmt 0)

10+ Year Member



Thank you so much swa66 and jd. I will use the CSS file version number method for now.

penders

5:50 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Easiest to fix this: add a version number to the name of the CSS file.

Minor point... I don't think you need to actually change the name of the CSS file, but just append a querystring to the filename when you link to it...

<link href="mystyle.css?version#" ....

EDIT: Changed SCRIPT to LINK element! Oops!

[edited by: penders at 6:39 pm (utc) on Dec. 9, 2009]

montman

6:19 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



Thank you penders, but I don't understand. You show <script src= . . .) My link to the external file doesn't have this ("script"). It is: <link href="global.css" rel="stylesheet" type="text/css" /> Wouldn't it work to just go ahead and rename my latest CSS file to global2.css and then change my link to it to: <link href="global2.css" rel="stylesheet" type="text/css" />

penders

6:47 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, my mistake (less haste, more speed!). Yes, it should be the LINK element, as you have already (edited post).

I was just saying, that instead of actually changing the name of the file, you can just append a querystring to the filename wherever you link to it.

To follow your example...

<link href="global.css?2" rel="stylesheet" type="text/css" />

The file is still called "global.css", but the browser will look for "global.css?2" in its cache (which isn't there). The "?2" will otherwise be ignored (it is just a querystring).

montman

6:50 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



Okay. I see. Thank you for the suggestion.

swa66

2:43 am on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But if you use a query string that you ignore on the server, you open up yourself to a risk of mating old html with new CSS, which might not work together at all.

Moreover query strings might mean the caches and proxies aren't kicking in for all they're worth. I'd really put it in the filename.

montman

4:08 am on Dec 10, 2009 (gmt 0)

10+ Year Member



Thank you swa66. I think I will just put it in the filename, as you suggest.

That seems easy, and I believe I understand what to do: leave my original "global.css" file on the server. Name my revised CSS file that was global.css to global2.css and then change my link to it to: <link href="global2.css" rel="stylesheet" type="text/css" />. Then the next time make it global3.css, with the link pointing to global3.css, etc.

swa66

2:05 am on Dec 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, you got it right.

To not have to keep old revisions around: monitor the logs of you website. Once you see the requests for the old design die out you can start to think about removing the old file. It happens quite quickly unless spiders travel by fo it or you forget to switch over some html file (referrers really are good for finding those).

BTW: the same trick works for images you use in backgrounds, for logos etc.

montman

5:03 pm on Dec 13, 2009 (gmt 0)

10+ Year Member



Thanks so much swa66. You have been a great help.