On our farm, I'm deploying some JavaScript and CSS files under the _layouts
folder.
I'm then referencing them, where it's required using a <SharePoint:ScriptLink>
or a <SharePoint:CssRegistration>
control :
<SharePoint:CssRegistration runat="server" Name="/_layouts/styles/company/custom.css" /> <SharePoint:ScriptLink runat="server" Name="/_layouts/scripts/company/custom.js" Localizable="false" />
This is working fine.
However, these assets are cached in the client browser. When I deploy minor updates, I have to ask users to press Ctrl+F5 to reload without the cache, but it's not an acceptable workaround.
Is there a way to ensure the latest version is loaded, without disabling the cache?
By now, I can see two possible solutions:
- version the assets in the file name (
custom.1.0.css
,custom.1.1.css
, ...): I don't like this solution because it requires to update every code that reference the asset Add a "revision number" to the url. I can trick using this code :
<SharePoint:CssRegistration runat="server" Name="/_layouts/styles/company/custom.css" id="customCss" /> <SharePoint:ScriptLink runat="server" Name="/_layouts/scripts/company/custom.js" Localizable="false" id="customJs" />
And in the codebehind :
private static readonly long g_AssemblyTimeStamp = File.GetCreationTime(System.Reflection.Assembly.GetExecutingAssembly().Location).Ticks; protected override void OnInit(EventArgs e) { customJs.Name += "?rev=" + g_AssemblyTimeStamp; customCss.Name += "?rev=" + g_AssemblyTimeStamp; }
This adds to the script url something like
?rev=634946193703232026
. The browser then sees a new url and reload it, and still cache this file.This is working, but still requires some plumbing code to make it works.
Is there any clean solution to meet this requirement?