6

Firstly, whilst I'm aware that gzipping HTML would probably have as much (more?) of a performance gain as minifying HTML - I am certainly intrigued by whether the following technique could work - or if anyone has done something similar.

Having seen some of the work done by others on pre-compiling MVC Razor views, I wondered if it is possible to some-way inject a pre-build event to such a process so that HTML can be minifyed, before then being built into a single DLL?

Or is there another way to minify at build time?

I've seen several people come up with ways of minifying by way of run-time techniques, but to me that seems to be counter to the who purpose of shrinking HTML size in the first place (runtime speed).

3 Answers 3

8

Minifying Razor views at build time wouldn't make sense as they are not final markup. Whatever you might have minified at build time would be broken at runtime when all those helpers spit their HTML. So if you really want to lower your bandwidth usage the first and foremost step is to gzip. The benefit will be enormous. And if you are a maniac and want to scratch a few more bytes minify the HTML runtime. I would recommend you the Meleze.Web NuGet for this purpose.

But remember: what ever you do, perform extensive load tests of your application before shipping. This way you will know what works best for you.

Sign up to request clarification or add additional context in comments.

6 Comments

Meleze.Web is a good project but it can't minify sections on your views unfortunately - only the main body
If you want to minify also sections see github.com/Chebur9tina/HtmlOptimizerMvc4
Minifying at build time makes perfect sense. It would be nice to minify the pre-existing HTML markup in the page, because the excessive amount of whitespace that Razor pages generate can become a problem.
I've done some measurements (nothing published, unfortunately), and minification before gzipping can make responses about 11% smaller on average then gzipping alone. So I believe it's still worth the trouble. I built a very tiny and trivial Razor output minifier for ASP.NET MVC 5 that operates in compile-time and is very easy to set up and understand (no magic): github.com/tompazourek/RazorHtmlMinifier.Mvc5
Are you sure Meleze.Web minifies during runtime, not compile time? As far as I can tell, Meleze.Web and @TomPažourek 's minifier both use the same strategry, but RazorHtmlMinifier.Mvc5 runs during compile time...
|
1

Minifyng HTML code is much less effective than minifying JavaScript - very few elements to reduce. That is why it rarely done.

2 Comments

This depends on the structure of your site. If you are JS-heavy or HTML-heavy it does matter.
Of couse it matter if such is the case, but as Boris Yankov says, it generally doesn't matter. In my case, I'd barely save 200 bytes of data
0

You can use RCompressor.

Disclaimer: I'm the author of the project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.