So - the solution we sought was to ensure caching when we wanted it, and to avoid it when we didn't!
More specifically, in our development environment, we never want to cache the site's supporting files (as they're likely to be changing quite frequently). In our production environment, we want to cache these files all of the time (for improved performance) - until we make a new release at which point we want to force the clients to pick up the new files.
As with all the best things in life (of which this probably isn't one), the concept is simple. All we need to do to force the browser to re-load the file from the server is to give it a different file name. Changing the file name each time we build the site would be a bit of a wrench. Instead, we can add a simple query string to the URL of the file with a new number in it every time we build the site.
As far as the browser is concerned, /scripts/site.js?v=123 is completely different to /scripts/site.js?v=456 - but of course, we know better!
Once this is in place, our page simply needs to call the function with the path to the script file as follows:
Once this is implemented, all you need to do is hook your build process up to your version control system to automatically increment the assembly's revision number every time you build, and you're done!
It's pretty clear to see where this is going, but for the sake of completeness, here's the function we use for Stylesheets...
Public Sub RegisterClientStyleSheetFile(ByVal StyleSheetFilePath As String, Optional ByVal Media As String = "all") Dim LinkTag As HtmlGenericControl LinkTag = New HtmlGenericControl("link") LinkTag.Attributes.Add("rel", "stylesheet") LinkTag.Attributes.Add("media", Media) #If DEBUG Then LinkTag.Attributes.Add("href", StyleSheetFilePath & "?v=" & Now.Ticks) #Else Dim Revision as Integer Revision = System.Reflection.Assembly.GetExecutingAssembly().GetName.Version.Revision LinkTag.Attributes.Add("href", StyleSheetFilePath & "?v=" & Revision) #End If Page.Header.Controls.AddAt(0, LinkTag) End Sub
I'm Chris Roberts, a software developer turned business owner. Join me as I grow a great software business in Cheltenham, England ...
Receive updates automatically using your RSS reader.