Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a bundle (Sytem.Web.Optimization) generate the build fingerprint for bundle-links?

In MVC-4, bundles for compressing CSS & Js files can be linked in a layout file with this Razor syntax: @Scripts.Render("~/JavaScripts")

This generates a link in the layout file that includes a fingerprint, which is re-generated on each app-build, to assist with cache-control.

So the generated link looks like:

<script src="/JavaScripts?v=dSMc_JTHMMP5GrWnILSYt_QBMw-g1pPlzknSorXpkyQ1"></script>

I'd like to know how the fingerprint is being generated (to use for similar purposes), but Sytem.Web.Optimization is not yet open-source.

like image 893
Faust Avatar asked May 09 '13 09:05

Faust


People also ask

What is bundle optimization?

Bundling and minification are the performance optimization techniques that can help to improve load time by reducing the number of requests to the server and reducing the size of requested assets (such as JavaScript and CSS.)

How does bundling work in MVC?

Bundling is one of the features of MVC. By implementing this, we can improve performance request load time. Minification is the process of removing unnecessary data without changing its functionality such as removing white spaces, comments, converting the large variable names to small, etc.

How does bundling use browser cache capability?

Busting Browser's Cache by Bundling Thus, when a Web page requests a resource, it checks in cache first. If the resource is found in cache, use cached copy rather than retrieving the resources from the Server. Hence, whenever you change the content of CSS and JS, files will not reflect on the Browser.

How do I create a bundling and minification in MVC 5?

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled. To enable bundling and minification, set the debug value to "false".


1 Answers

The fingerprint is generated using a sha256 hash of the bytes from the bundle and then encoded:

            byte[] hash = sha256.ComputeHash(Encoding.Unicode.GetBytes(bundle));
            return HttpServerUtility.UrlTokenEncode(hash);
like image 124
Hao Kung Avatar answered Jan 03 '23 17:01

Hao Kung