Declarative dynamic bundling/minification for MVC?

依然范特西╮ 提交于 2019-12-02 06:49:25

This is kind of a comment but I ran out of space.

This is neat but it seems like you end up with one bundle per (complete, rendered) page, which is pretty much the worst case scenario for a first-time visitor to the site. If you have several pages that use the same master page and don't add any additional content, you will end up with the same large file downloaded on each page with a different name. Instead of basing the name off the page name, try concatenating all of the filenames (in order) and calculating an MD5 hash to use as your bundle name - this serves as a fairly good uniqueness check, and should really cut down on your bandwidth usage. You can see an example here of how we do this in SquishIt - just remember that the value you calculated would be whats coming in as "key" at this point in the code. Another thing I would consider is defining bundles per physical view file instead of for the entire page, to maximize reusability.

I realize this sounds critical, but I do like the general direction you're going with this. I'm just not sure what the exact destination is. If you need any help, I try to watch this tag pretty closely and I'm pretty easy to find elsewhere.

As far as "auto-bundling" is concerned, I don't think there is anything out there that does what you're looking for - in large part because it requires such a nuanced approach. You can look at RequestReduce - it does a lot of optimization for you without intervention, but I don't believe it combines assets.

Please take a look at the Enfold project. It might be the solution that you are looking for.

Assuming you have the following views in the web project:

~/Views/Home/About.cshtml
~/Views/Home/Contact.cshtml
~/Views/Home/Index.cshtml

You can organize your Javascript files this way:

~/Scripts/Views/default.js
~/Scripts/Views/Home/default.js
~/Scripts/Views/Home/about.js
~/Scripts/Views/Home/contact.js
~/Scripts/Views/Home/index.js

With such a setup, the following bundles will be created:

~/bundles/home/about

~/scripts/views/default.js
~/scripts/views/home/default.js
~/scripts/views/home/about.js

~/bundles/home/contact

~/scripts/views/default.js
~/scripts/views/home/default.js
~/scripts/views/home/contact.js

~/bundles/home/index

~/scripts/views/default.js
~/scripts/views/home/default.js
~/scripts/views/home/index.js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!