I have just spent half a day quietly going mad.
I\'m making changes to my classes in the Site.css file and they were not being reflected in the site being develope
My approach is using the “querystring changing” method to bypass caches (even in browser and proxy servers). Since I’m using Master Pages I maintain the link to CSS as usual like but adding an ID (named here as cssStyleSheet):
<head runat="server">
<link id="cssStyleSheet" href="~/Styles/Default.css" rel="stylesheet" type="text/css" />
Then at code behind I implemented at Page_Load this line of code, adding a quesrystring like “?t=5343423424234”.
Protected Sub Page_Load(…)
If IsNothing(Application("CSSTicks")) = True Then
Application("CSSTicks") = DateTime.Now.Ticks
End If
cssStyleSheet.Attributes("href") = cssStyleSheet.Attributes("href") & "?t=" & Application("CSSTicks")
End Sub
Why is that? At HTML code, some designer could change the CSS file as easy, no interfering at some “difficult” code. Using an Application variable I avoid spending bandwidth from my servers and also from customer perspective (like using mobiles).
If new application is deployed, the Application variable is reset automatically and a “new” version of CSS if downloaded to browser (even through proxies).
For Wordpress users, below is the code
<link rel="stylesheet" href="<?php echo get_bloginfo('stylesheet_url')."?d=".date( 'Ymd', time()); ?>" type="text/css" media="screen" />
Or better one
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen, projection" />
Cheers!
Press CTRL+F5 to hard-refresh everything on your webpage including scripts and stylesheets.
Additionally, you can incorporate the stylesheets to be served from a dynamic server page [php/asp.net] and the Response.Expires = -1 which will force the client to load the css on every HTTP-GET request explicitly. You can also do this in your webserver settings for CSS mime types.
Are you keeping your browser open between your changes? Often simply closing all browser windows between making changes to your CSS file will tell the browser to download a new copy.
To further Ian Kemp's answer which utilises the LastWriteTime
of the style sheet in question, I have written an MVC helper to output the <link>
tag with the cache-busting parameter built in.
The Code
public static class CssLinkHelper
{
public static IHtmlString StyleSheet(this HtmlHelper helper, string stylesheetname)
{
// define the virtual path to the css file (see note below)
var virtualpath = "~/" + stylesheetname;
// get the real path to the css file
var realpath = HostingEnvironment.MapPath(virtualpath);
// get the file info of the css file
var fileinfo = new FileInfo(realpath);
// create a full (virtual) path to the css file including a cache busting parameter (e.g. /main.css?12345678)
var outputpath = VirtualPathUtility.ToAbsolute(virtualpath) + "?" + fileinfo.LastWriteTime.ToFileTime();
// define the link tag for the style sheet
var tagdefinition = string.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", outputpath);
// return html string of the tag
return new HtmlString(tagdefinition);
}
}
Usage
@Html.StyleSheet("main.css")
Output
<link rel="stylesheet" type="text/css" href="/main.css?131393346850223541" />
Note
In case you're wondering about the var virtualpath = "/~" + ...
part and thinking, why not just pass it in as "~/main.css"
? I have implemented this function this way because all my css files are in a common folder (/assets) and the helper will prefix my output with the common folder name i.e. /assets/main.css?131393346850223541
This is a classic problem. You have a lot of solutions available: