簡體   English   中英

如何在Visual Studio中自動將md5哈希的版本號添加到javascript和CSS文件中,或使用另一種方式強制瀏覽器緩存刷新?

[英]How to automatically add version numbers of md5 hash to javascript and css files in Visual Studio or use another way to force browser cache refresh?

每當我為任何Web應用程序發布新版本的CSS或javascript文件時,我都會在HTML開頭的URL中手動添加?v=# (#是版本號,例如3或10),確保用戶瀏覽器緩存不會妨礙更改。

但是某些應用程序的頭部具有多個CSS和多個JS文件,並且要跟蹤所有已更改的文件很繁瑣。

我們在IIS服務器上使用ASP.NET平台,因此使用答案是強制瀏覽器重新加載緩存的CSS / JS文件的一種優雅方法是什么? 是不可能的,但我正在尋找類似的東西。 最好所有的JavaScript。

我喜歡該帖子中使用md5哈希的建議,但似乎無法找到一種計算CSS或JS文件的校驗和的方法。

代碼看起來像這樣(使用jQuery):

$.each($("script[type='text\javascript']"),function(){
    $(this).attr("src",$(this).attr("src") + "?v=" + checkMD5Sum($(this).attr("src")) );
});

function checkMD5Sum(url){
    //get the hash or something to auto version the file
    //I could send the url or filename to a webmethod to get the MD5 check sum, but doing this after page load is not usefull, so what to do?
}

或者也許在Visual Studio中使用了postbuild流程,但是我從未做過,所以我不願意嘗試。


編輯

按照添加緩存配置文件在web.config中嘗試此操作

<configuration>
   <system.webServer>
      <caching enabled="true">
         <profiles>
            <add extension=".js" policy="CacheUntilChange" />
            <add extension=".css" policy="CacheUntilChange" />
         </profiles>
      </caching>
   </system.webServer>
</configuration>

您的解決方案似乎要求瀏覽器兩次提取文件,並且不能保證第一次提取的狀態。 因此它不起作用。

緩存由服務器控制。 無法決定何時在客戶端上提供新版本-除非您忽略緩存並始終獲取新副本。

有很多方法可以實現緩存策略。 具有版本號的指紋就是其中之一。

我更喜歡使用ETag 讓ETag成為filemtime,您就很好了。

  VersionNumber = System.Configuration.ConfigurationManager.AppSettings["Version"];

    //Caching Issues For JS
    public static string JSVersionUpdate(string JsFile)
    {
        StringBuilder str = new StringBuilder();
        str.Append(GetQualifiedPath(JsFile));
        str.Append("?Version=");
        str.Append(VersionNumber);
        return str.ToString();
    }

    public static void DiscardVersion()
    {
        VersionNumber = null;
    }

    //get Full Path for JS File
    private static string GetQualifiedPath(string path)
    {
        var httpRequestBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);

        path = path.Replace("~", string.Empty);

        string appPath = string.Empty;
        if (httpRequestBase != null)
        {
            //Formatting the fully qualified website url/name
            appPath = string.Format("{0}://{1}{2}{3}{4}",
                        httpRequestBase.Request.Url.Scheme,
                        httpRequestBase.Request.Url.Host,
                        httpRequestBase.Request.Url.Port == 80 ? string.Empty : ":" + httpRequestBase.Request.Url.Port,
                        httpRequestBase.Request.ApplicationPath,
                        path);
        }
        appPath = appPath.TrimEnd('/');

        return appPath;
    } 
}

在用戶界面頁面中: script src="JSVersionUpdate("~/Scripts/Application/Abc.js")"

O / p: ~/Scripts/Application/Abc.js/version=1.0

瀏覽器僅在版本不同的情況下才從服務器請求JS文件,否則它將被緩存出去。現在我不需要告訴別人在部署后一次又一次清除緩存。我要做的只是在Web Config中更改版本號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM