动态 CSS - 缓存问题?
我正在使用 HttpHandler 来动态修改一些 CSS(仅简单的颜色),基于我在 SO 上读到的技术。
除了在页面上我为用户提供指定他们想要的颜色的选项之外,一切都工作得很好。理想情况下,一旦用户保存新颜色并且页面刷新,我希望显示新颜色。然而,只有当我明确按下浏览器重新加载或 F5 键时,它们才会出现。
我很欣赏某个地方(IIS 或浏览器)正在对我的样式表进行一些有用的缓存,其中 1000 次中的 999 次正是我想要的,但是在这个特定的页面事件上,我希望能够强制重新加载并导致 HttpHandler火。
有人了解这是如何工作的以及我能做什么吗?
我尝试过的事情:
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Expires = -1;
Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
因为我还使用 ASP.NET 主题添加查询字符串,所以样式表链接并不是一个简单的选择。
有人想吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可以通过我在网站上使用的技术来解决,该技术可以在资产发生更改(例如部署后)后重新加载资产。
将
?value
附加到 CSS url 的末尾,其中value
对应于版本,或者浏览器尚未看到的某个唯一值。在我的例子中,我使用文件修改时间,但是在你的例子中,由于 CSS 在几乎每个页面加载上都是动态的,我建议生成一些唯一的值。由于 URL 始终不同,因此浏览器将始终重新加载它,并且永远不会将其放入缓存中。
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, wherevalue
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.