Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Building Razor views in release mode should minify HTML #423

Closed
RehanSaeed opened this issue Jun 11, 2015 · 21 comments
Closed

Building Razor views in release mode should minify HTML #423

RehanSaeed opened this issue Jun 11, 2015 · 21 comments

Comments

@RehanSaeed
Copy link

Minifying HTML is difficult in ASP.NET MVC. Comments and white space in .cshtml files could be removed when building in release mode.

This project allows the minification of your .cshtml files using a post build event. This should be built into the tooling and not a separate product.

@NTaylorMullen
Copy link
Contributor

Hey @RehanSaeed are you suggesting to add Razor minification to the Visual Studio project templates?

@RehanSaeed
Copy link
Author

In my head I imagine minification of cshtml being just a check-box in the project properties. It could then be turned off for Debug/Development and on for Release/Staging/Production builds.

Adding this to the project as indicated in the blog is just one way of achieving this. I'm sure there are other ways which may be more integrated.

The benefits of minifying HTML are clearly shown by Mads Kristensen in his blog article. The best way of achieving minification is not during runtime as many frameworks do but during compile time where there is no performance hit to the application. Because of this fact, only Microsoft can reliably provide proper tooling to allow for this feature.

What are your thoughts?

@NTaylorMullen
Copy link
Contributor

Typically all minification occurs (in asp.net 5) via third party components that are configured to run post-build/restore etc. In my mind a .cshtml minifier would fall under the same bubble. With that mindset, if we were to add default .cshtml minification it would exist at the project template level.

@RehanSaeed
Copy link
Author

It seems then that the solution employed by Dean Hume in his blog article is very close to what you are suggesting.

This would be a very cool feature if implemented. Most developers will not go to the added trouble of adding this feature themselves. It would probably save terabytes of bandwidth when you add up how many developers use ASP.NET MVC 6 and start using this feature because it comes right out of the box.

@NTaylorMullen
Copy link
Contributor

@RehanSaeed thanks for the suggestion. Will discuss this with others to see if it has a place in our templates 😄

@RehanSaeed
Copy link
Author

Excellent. Thank You!

@glen-84
Copy link

glen-84 commented Jul 4, 2015

I completely disagree with the suggested implementation.

Why not do this as part of pre-compilation? It can be an option in the RazorViewEngineOptions class.

I think that this issue should be re-opened.

/cc @guardrex

@guardrex
Copy link

guardrex commented Jul 4, 2015

@glen-84 👍 I hate what I'm doing now, so that would be great.

However, @NTaylorMullen has there been any discussion on giving Razor directives line termination characters so that they wouldn't need to be on individual lines at the top of the markup and the Razor parser can grab them from minified markup? That's my main pain point with using Gulp to minify Razor views.

@NTaylorMullen
Copy link
Contributor

@guardrex no there hasn't. Not a horrible idea however, open an issue and we can discuss 😄

@RehanSaeed
Copy link
Author

I wrote a blog post about how much bandwidth this feature might have saved the internet here.

@ShamsulAmry
Copy link

ShamsulAmry commented Apr 15, 2016

May I know what's the status for this suggestion, is it denied? Suggestion by @glen-84 to have a precompiled minification would be really great.

Scenario: I have templates which I bought containing lots of HTML and Javascript comments. I want to leave it in .cshtml because it helps during development, but I don't want / need those comments to appear in production.

On-the-fly minification during runtime can be done, but since the HTML and Javascript portions in a .cshtml is pretty much static most of the time, it would be great if those static portions can be minified during precompilation and avoid unnecessary performance penalty for just-in-time runtime minification.

@guardrex
Copy link

@ShamsulAmry Backlogged at #444

@Sorrien
Copy link

Sorrien commented Jun 14, 2017

Just wanted to throw in my support for built-in minification either at compile time or run-time.

@ffocsi
Copy link

ffocsi commented Sep 6, 2017

Shouldn't this reopen?

I know for sure that knockout has a measurable performance increase (1 second boost in a real world project I have been working on) from html minification.

It's because when you have a:

[ul foreach]
    [li with stuff][/li]
[/ul foreach]

The dom has [ul][text][li][text][ul]. Knockout has to process many more extra dom nodes to find bindings etc.

Minified:

[ul foreach][li with stuff][/li][/ul foreach]

When minified it only sees [ul][li][ul] with no text nodes between the elements.
(I found this out on a blog but can't find the source. But after profiling I discovered it was true and significant)

I'm using WebMarkupMin but this is done at runtime which impacts performance on a high load site. Would be nice if this could be done when the razor views get built. Would also be good with an option to do it in debug mode as well as release.

@guardrex
Copy link

guardrex commented Sep 6, 2017

@ffocsi
Copy link

ffocsi commented Sep 6, 2017

Thanks.

Forgot to say we're still using asp.net 4 and mvc 5 for new projects.

I'm guessing we should upgrade.

@tompazourek
Copy link

For anybody interested in this, I built a tiny HTML minifier that can be used with MVC 5:

https://github.com/tompazourek/RazorHtmlMinifier.Mvc5

@lonix1
Copy link

lonix1 commented Jul 19, 2018

@guardrex Your minifier is cool, and I'm using it (thanks!), but is there no way to do this as compile-time?

Your minifier works "during compile-time during runtime" if I understand correctly - because it's part of the pipeline. But if you're doing a bin deploy, surely this minification should be specified in the .csproj or something like that?

@guardrex
Copy link

@lonix1 I'm glad to hear it still works. lol ... I haven't looked at this subject for quite a while now.

That code replaces the Razor Template Engine. Since it should run with view precompilation, it should be working at compile time. There was a time when precomp only worked for framework-dependent deployments, but AFAIK they fixed that. aspnet/MvcPrecompilation#102

It wouldn't be specified in the project file because that code interacts with the Razor build engine (as a replacement of the Razor Template Engine). It's a service registration to replace the OOB engine ...

.ConfigureServices(services =>
{
    services.AddMvc();
    services.AddSingleton<RazorTemplateEngine, CustomRazorTemplateEngine>();
})

@Sora2455
Copy link

Sora2455 commented Jul 19, 2018

Just FYI, @guardrex 's minifier needs to be modified to work with the most recent version of ASP Core 2.
Details here: #2422

@K3n4
Copy link

K3n4 commented Sep 5, 2018

Is this feature available on ASP Core 2.1 or you have denied it ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests