Dynamic CSS - caching problem?

北城以北 提交于 2019-12-13 21:01:27

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6628227/dynamic-css-caching-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!