Developer forum

Forum » Ecommerce - Standard features » Minify HTML Setting

Minify HTML Setting

Andrew Rushworth
Reply

The "minify HTML" setting in DW is breaking the code.

I've noticed a number of places in the Rapido template code where line terminations in the js are not present.  There are also comments in the code "//".

This minify HTML settings claim to fame is that it "Removes spaces and line breaks (^\n|$\s+)".  So what is happenign is that everything after the first comment is "commented out", and also line terminations become important in the js code. If these are missing, then the js is corrupt.


Replies

 
Nicolai Pedersen
Reply

Hi Andrew

Thank you for your report. We will have a look at it - sounds like it is mostly related to Rapidos code - as it is dependent on valid code in order to not cause issues.

I would say that this settings should not be used as it does not provide anything. All responses are already gzipped and takes away the overhead of spaces and line breaks in code, so there is absolutely no reason to run with that setting enabled.

BR Nicolai

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

The Minify option actually removes a lot of the empty spaces and comments and it is pretty useful

We have already been asked by one of our customers to handle the minification of the HTML and we have avoided it because of this breaking change.

It would be good to have it solved.

Thank you,
Adrian

 
Andrew Rushworth
Reply

Comments:
It doesn't seem to remove the comments; so unless all comments are either removed or changed to not be in-line comments but rather block comments, it will break: (i.e instead of:
//my comment
use
/*my comment*/
) .
If its meant to keep comments, then change the minify logic that if a line starts with // to replace with "/*" and the linefeed to "*/".

Linefeeds:
The more important one is the missing semi-colons.  Javascript doesn't seem to mind if there is a linefeed, but the logic removes this and thus breaks the js
 

 
Mikkel Hornbech Nielsen
Reply

Is there a soloution for this incident in the pipeline or?

Right now we have a soloution where it interfers with the SEO results, so would be nice to get this fixed asap.

 
Nicolai Pedersen
Reply

Hi Mikkel

We consider this feature deprecated as it does not much useful. It is made as minify html and not inline js, so it will not work with js comments. Move js + css to a file if needed. 

But it is best to just disable the feature and gzip will take care of most of the minimizing the response. There is little or nothing saved in performance minifying the html - on the contrary as it takes time to minify. If we need to take inline css, js, comments etc into consideration, the complexity of the minify will become so high that the performance gain would be outweighed by the cost.

On standard Rapido demo, the difference between minified and not minified response is 9911 bytes vs. 10732 bytes running gzip. Practically nothing.

The "remove empty lines" setting makes sense when viewing source coming from razor files that can leave a lot of blank lines behind if not done 'right'.

BR Nicolai

 
Nicolai Pedersen
Reply

Hi Mikkel

A follow up. Someone from the community was kind enough to email me a possible solution to this that seems to perform ok.

That code will only minify lines that are pure html and not css and js lines.

Looks like this:

It is not as compact as the current version and not 100%, but will work with inline css and js code and other stuff.

This version also handles code like this:

<small>
text
</small>

to

<small>text
</small>,

And not

<small>text</small>
 
Mikkel Hornbech Nielsen
Reply

Hi Nicolai.

What code exactly???

 
Nicolai Pedersen
Reply

You want the minifying method?

Here goes:

private string RemoveEmptyLines(string originalString, bool removeLineBreaksAndSpaces)
        {
            string outputString = string.Empty;
            using (var reader = new StringReader(originalString))
            {
                using (var writer = new StringWriter())
                {
                    var newLineChars = System.Environment.NewLine.ToCharArray();
                    string line = reader.ReadLine();
                    while (line is object)
                    {
                        string lineTrimmed = line.Trim();
                        if (lineTrimmed.Length > 0)
                        {
                            if (removeLineBreaksAndSpaces && lineTrimmed.StartsWith("<") && lineTrimmed.EndsWith(">"))
                            {
                                writer.Write(lineTrimmed);
                            }
                            else if (removeLineBreaksAndSpaces)
                            {
                                writer.WriteLine(lineTrimmed);
                            }
                            else
                            {
                                writer.WriteLine(line);
                            }
                        }

                        line = reader.ReadLine();
                    }

                    outputString = writer.ToString();
                }
            }

            return outputString;
        }
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Will this code become part of the standard DW or we have to manually use it from now on when we need it?

Thank you,


Adrian

 
Nicolai Pedersen
Reply

I have implemented it on 9.10 branch so it will be out with the next Dynamicweb nuget package.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Thank you :)

Adrian

 
Adrian Lesik
Reply

Hello Nicolai,

I just want to ask if this method is implemented in 9.10.8 version? Because it looks that we have the same problem.

 
Nicolai Pedersen
Reply

9.10.8 was released 6 months ago, and this change was made 2 months ago, so no...

So go with 9.10.26 or later.

 
Adrian Lesik
Reply

Thanks for answer :) have a nice day :)

 

You must be logged in to post in the forum