Bla*_*ell 13 javascript minify visual-studio-2010 post-build
我有一些我想要实现的目标,但我不确定如何到达目的地:
从这篇文章:
http://encosia.com/automatically-minify-and-combine-javascript-in-visual-studio/
我已经添加了JSMin来使用如下命令来缩小我的文件:
"$(SolutionDir)Tools\jsmin.exe" < "$(ProjectDir)Scripts\myfile.js" > "$(ProjectDir)Scripts\myfile.min.js"
Run Code Online (Sandbox Code Playgroud)
我还将这个添加到我的网页中,以便在调试模式下保留非缩小文件:
<% if (HttpContext.Current.IsDebuggingEnabled) { %>
<script type="text/javascript" src="scripts\myfile.js"></script>
<% } else { %>
<script type="text/javascript" src="scripts/myfile.min.js"></script>
<% } %>
Run Code Online (Sandbox Code Playgroud)
所以我真的想知道如何防止myfile.min.js在更新时被Web浏览器视为静态内容.如果我的目标不是单击部署,我可以手动添加版本号,但这似乎不是一个可靠的方法.思考?
这个答案有点晚了,但我认为这是一种相当可靠的方法,可以确保缓存行为与文件同步,具体取决于文件本身的校验和.
<script type="text/javascript" src="/Scripts/Scripts.min.js?v=<%= Utils.GetFileHash("/Scripts/Scripts.min.js") %>"></script>
Run Code Online (Sandbox Code Playgroud)
在Utils类中,您可以使用生成文件校验和的方法
public static string GetFileHash(string path)
{
string hash = (string)HttpContext.Current.Cache["_hash_" + path];
if (hash == null)
{
// Get the physical path of the file
string file = HttpContext.Current.Server.MapPath(path);
// Code for MD5 hashing omitted for brevity
hash = Utils.GetMD5(file);
// Insert the hash into the Cache, with a dependency on the underlying file
HttpContext.Current.Cache.Insert("_hash_" + path, hash, new System.Web.Caching.CacheDependency(file));
}
return hash;
}
Run Code Online (Sandbox Code Playgroud)
这会缓存文件的哈希值,因此只需在文件更改时计算它.
此外,CacheDependency确保如果您要更改.js文件,它将确保重新生成哈希.
希望这会有所帮助,这就是我在我的一个生产网站中使用的内容.
if/else您可能会考虑实现一个即时缩小的工具,而不是把源代码到处乱扔IHttpFilter。像本文所做的那样应该可以很好地工作。
你的 JavaScript 应该是可缓存的,并且网络服务器上的压缩开销将是最小的。将其实现为还IHttpFilter意味着您不必添加路由或更改现有 URL,并且您可以IHttpFilter仅在发布配置中有条件地添加,以便您仍然可以在开发过程中调试 javascript。
至于版本控制,HTML 帮助程序(这里有一个包含更多信息的链接)可以通过将版本号附加为查询字符串参数来很好地解决问题。如果版本参数不存在,您甚至可以让您的调试版本抛出IHttpFilter异常,因此每当您添加新的 javascript 文件时,都会提醒您使用 HTTP 帮助程序。
| 归档时间: |
|
| 查看次数: |
3615 次 |
| 最近记录: |