How to force the browser to reload cached CSS/JS files?

后端 未结 30 4276
青春惊慌失措
青春惊慌失措 2020-11-21 05:49

I have noticed that some browsers (in particular, Firefox and Opera) are very zealous in using cached copies of .css and .js files, even be

30条回答
  •  温柔的废话
    2020-11-21 06:02

    Just use server side code to add the date of the file... that way it WILL be cached and only reloaded when the file changes

    In ASP.NET

    
    
        
    

    This can be simplified to:

    
    

    By adding an extension method to your project to extend Page:

    public static class Extension_Methods
    {
        public static string ResolveClientUrlUnique(this System.Web.UI.Page oPg, string sRelPath)
        {
            string sFilePath = oPg.Server.MapPath(sRelPath);
            string sLastDate = System.IO.File.GetLastWriteTime(sFilePath).ToString();
            string sDateHashed = System.Text.RegularExpressions.Regex.Replace(sLastDate, "[^0-9]", "");
    
            return oPg.ResolveClientUrl(sRelPath) + "?d=" + sDateHashed;
        }
    }
    

提交回复
热议问题