Reflect changes by not having to clear cookies or refresh pages on production after upgrade
-
We are getting complaints from the customer that they have to keep on refresh (Ctrl+F5) on the page or sometimes clear the cookies to reflect the changes done on upgrade. Especially, when we upload CSS/JS files or Images sometimes. To overcome this situation, I did some research and found one solution to append Query string with the name of image or CSS files. But it does not seem to be a coherent solution. Then I used NuGet packages for Bundle. It seems to be the solution but I have another issue like I am calling individual CSS file for each page (page specific) and to make a bundle for the single file is not the good idea. What should i follow as good approach to overcome this situation where changes can reflect on the server after uploading CSS/Images or JS files.
-
We are getting complaints from the customer that they have to keep on refresh (Ctrl+F5) on the page or sometimes clear the cookies to reflect the changes done on upgrade. Especially, when we upload CSS/JS files or Images sometimes. To overcome this situation, I did some research and found one solution to append Query string with the name of image or CSS files. But it does not seem to be a coherent solution. Then I used NuGet packages for Bundle. It seems to be the solution but I have another issue like I am calling individual CSS file for each page (page specific) and to make a bundle for the single file is not the good idea. What should i follow as good approach to overcome this situation where changes can reflect on the server after uploading CSS/Images or JS files.
You've already mentioned it, a querystring variable on the css\js paths.
-
We are getting complaints from the customer that they have to keep on refresh (Ctrl+F5) on the page or sometimes clear the cookies to reflect the changes done on upgrade. Especially, when we upload CSS/JS files or Images sometimes. To overcome this situation, I did some research and found one solution to append Query string with the name of image or CSS files. But it does not seem to be a coherent solution. Then I used NuGet packages for Bundle. It seems to be the solution but I have another issue like I am calling individual CSS file for each page (page specific) and to make a bundle for the single file is not the good idea. What should i follow as good approach to overcome this situation where changes can reflect on the server after uploading CSS/Images or JS files.
Mads Kristensen wrote a blog post explaining how to do this back in 2014: Cache busting in ASP.NET[^] However, I'd be inclined to use the query-string rather than a fake path. It's a fairly simple change:
using System;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;public class Fingerprint
{
public static string Tag(string rootRelativePath)
{
if (string.IsNullOrWhiteSpace(rootRelativePath)) return string.Empty;if (rootRelativePath\[0\] != '~') { rootRelativePath = "~" + rootRelativePath; } string result = HttpRuntime.Cache\[rootRelativePath\] as string; if (result == null) { string absolute = HostingEnvironment.MapPath(rootRelativePath); DateTime date = File.GetLastWriteTime(absolute); result = rootRelativePath + "?v=" + date.Ticks; HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); } return result; }
}
<link rel="stylesheet" href="@Fingerprint.Tag("~/content/file.css")" />
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You've already mentioned it, a querystring variable on the css\js paths.
-
Most people do.
-
Most people do.
-
Mads Kristensen wrote a blog post explaining how to do this back in 2014: Cache busting in ASP.NET[^] However, I'd be inclined to use the query-string rather than a fake path. It's a fairly simple change:
using System;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;public class Fingerprint
{
public static string Tag(string rootRelativePath)
{
if (string.IsNullOrWhiteSpace(rootRelativePath)) return string.Empty;if (rootRelativePath\[0\] != '~') { rootRelativePath = "~" + rootRelativePath; } string result = HttpRuntime.Cache\[rootRelativePath\] as string; if (result == null) { string absolute = HostingEnvironment.MapPath(rootRelativePath); DateTime date = File.GetLastWriteTime(absolute); result = rootRelativePath + "?v=" + date.Ticks; HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); } return result; }
}
<link rel="stylesheet" href="@Fingerprint.Tag("~/content/file.css")" />
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Only if you disable caching on the files, which will degrade the performance of your site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer