0

I am using an HttpHandler to modify some CSS (only simple colours) on the fly, based on a technique I read about on SO.

Everything works just fine expect on the page where I am giving the user the option to specify the colours they want. Ideally as soon as the user saves his new colours and the page refreshes I want the new colours to be displayed. However they only come through when I explicitly press the browser reload or F5 key.

I appreciate that something somewhere (IIS or the browser) is doing some helpful caching of my stylesheet which 999 times in 1000 is exactly what I want, however on this particular page event I want to be able to force a reload and cause the HttpHandler to fire.

Anyone understand how this works and what I can do?

Things I have tried:

    Response.Clear();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Expires = -1;
    Response.Cache.SetExpires(DateTime.Now.AddDays(-1));

Because I am also using ASP.NET themes adding a querystring the stylesheet link isn't really a simple option.

Thoughts anyone?

bigtv
  • 2,611
  • 5
  • 29
  • 42
  • The control that you save user preferences makes a good old-fashioned post right? – Yiğit Yener Jul 08 '11 at 17:53
  • Im not an ASP.net guy, but is there a way you can change the name of the stylesheet? This should stop caching on a proxy level and browser level. – Eddie Jul 08 '11 at 18:04

1 Answers1

0

This can be solved with technique that I use on my sites to cause reloads of assets once they have changed, such as after a deploy.

Append ?value to the end of your CSS url, where value corresponds to the version, or some unique value the browser hasn't seen yet. In my case I use the file modification time, however in your case since the CSS is dynamic on almost every pageload, I suggest generating some unique value.

Since the URL is always different, the browser will always reload it and it will never get put into its cache.

kcbanner
  • 4,050
  • 1
  • 25
  • 18
  • "Because I am also using ASP.NET themes adding a querystring the stylesheet link isn't really a simple option." – Eddie Jul 08 '11 at 18:04
  • Ah, I see. I'm not familiar with ASP.NET themes; however, can you verify that the server/client clocks are in sync? If you set the expires one day in the past and the client's clock is wrong, that may not work. – kcbanner Jul 08 '11 at 18:07
  • As the question states I am looking for a solution that doesn't require modifying the querystring of the stylesheet... – bigtv Jul 11 '11 at 07:33
  • Ok, then you will need to make sure that all your clients respect the expires headers that you set. Changing the URL is guaranteed to cause a new request. – kcbanner Jul 11 '11 at 19:35
  • How do you ensure the client respects the expires header? Isn't that down to the browser and out of my control on the server side? – bigtv Aug 23 '11 at 10:39
  • Yep, the expires header is for the client to use, you can't force it to respect it. That is why we give an entirely new URL, so the browser will always reload it. – kcbanner Aug 29 '11 at 17:20