tompazourek/RazorHtmlMinifier.Mvc5

RazorHtmlMinifier not minify my project views

ahmedabdellatiff opened this issue · 4 comments

we install RazorHtmlMinifier using Install-Package RazorHtmlMinifier.Mvc5
then replace Views/Web.config with
then rebuild application but result not minify please help?
we use VS2015 , MVC5

I'm not sure what your issue might be.

I'd recommend trying this first on a smaller application, e.g. just the ASP.NET Web Application project template included in Visual Studio. If it works there, try iterating on what's different to your application.

If you can isolate a sample and attach it, I can take a look at it.


  • Note that the minification is still not perfect and can leave some spaces. I try to improve on this (e.g. the last release 1.2.0 got a bit better at this), but it's still not the most minified version possible. The home page of the VS 2017 ASP.NET MVC template gets minified from 3226 B to 2639 B, that's still over 18% reduction in size, and during compile-time, not runtime.
  • Also please note that the minification happens when Razor is compiled, it only minifies the code in the Razor views. So any HTML that gets written out runtime (e.g. HTML content that's stored in the database and is written to output) will not get minified.

thanks for attention
i create mvc sample and set comment in index action but still not work

err1
testmin.zip

The library, unfortunately, doesn't remove HTML comments, it just reduces white-space.

In Razor, I don't recommend using HTML comments like:

<!--dddddddddddddd-->

But rather use Razor comments like:

@* dddddddddddddd *@

That way, you'll never expose your code comments to the client-side. Plus it also works nicely with Visual Studio, you can comment and uncomment by using keyboard shortcuts (CTRL+K and CTRL+C to comment, CTRL+K and CTRL+U to uncomment).

But still, I wouldn't say it doesn't work. In your provided sample, the HTML response has 2675 bytes. If you switch to the default MvcWebRazorHostFactory, you get 3303 bytes. That's still over 19% reduction of data with no added cost at runtime, all during compile-time.

RazorHtmlMinifier.Mvc5 isn't the most aggressive minifier that's possible. If you'd process the contents through something like https://www.willpeavy.com/minifier/, you get an even smaller response. In your example, you'd get 2565 bytes, which is even 3% smaller than the output from RazorHtmlMinifier.Mvc5, and it will actually remove your HTML comment as well.

The difficult issue is that the minification might not always be safe. Whitespace can sometimes be significant in HTML. Consider the following code, for example: <span>Hello</span> <span>World</span>. If it gets minified and the space between the HTML tags removed, you'd get <span>Hello</span><span>World</span> and it might get rendered as HelloWorld instead of Hello World in the browser. RazorHtmlMinifier.Mvc5 is designed to be rather safe with the minification in order to avoid issues like this.

The minification process is deliberately trivial and to understand, it doesn't do any advanced parsing and analysis of HTML nodes to figure out what to remove and what to keep, it just simply reduces the whitespace. That does most of the job.

OK thank you very mush ,hopefully to improve new features to increase compression and web performance
thanks again 👍