Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTML Output Minification in laravel 4
<?php
### --- Snip --- ###
App::after(function($request, $response)
{
// HTML Minification
if(App::Environment() != 'local')
{
if($response instanceof Illuminate\Http\Response)
{
$output = $response->getOriginalContent();
// Clean comments
$output = preg_replace('/<!--([^\[|(<!)].*)/', '', $output);
$output = preg_replace('/(?<!\S)\/\/\s*[^\r\n]*/', '', $output);
// Clean Whitespace
$output = preg_replace('/\s{2,}/', '', $output);
$output = preg_replace('/(\r?\n)/', '', $output);
$response->setContent($output);
}
}
});
### --- Snip --- ###
@garagesocial

This comment has been minimized.

Copy link

@garagesocial garagesocial commented Jul 23, 2013

Thanks for this Zach. We restructured it a bit on our fork https://gist.github.com/garagesocial/6059962

Also note the first filter /<!--([^\[|(<!)].*)/ breaks when using the HTML5 Boilerplate it will suck up line <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> @ https://github.com/h5bp/html5-boilerplate/blob/master/index.html#L5

@zmsaunders

This comment has been minimized.

Copy link
Owner Author

@zmsaunders zmsaunders commented Sep 5, 2013

Nice, I like the cleaned up version. I had a feeling that comment cleaning would come back to bite me at some point with IE's conditional Comments. I'll have to play around with it a bit and see if I can get it to behave.

@allaniftrue

This comment has been minimized.

Copy link

@allaniftrue allaniftrue commented Feb 20, 2014

Thanks @zach && @garagesocial, I added a substitute for HTML comment filter to exclude the IE conditions. https://gist.github.com/paparts/9113258

@viniciusmr3

This comment has been minimized.

Copy link

@viniciusmr3 viniciusmr3 commented Jul 1, 2014

Well done @zmsaunders! It helped a lot! Cheers!

@miguelbalboa

This comment has been minimized.

Copy link

@miguelbalboa miguelbalboa commented Oct 25, 2014

Verify that the response header is text/html, to avoid problems when returning an image for example.
https://gist.github.com/miguelbalboa/9224ecc1daeadff5ab49

@morilog

This comment has been minimized.

Copy link

@morilog morilog commented Dec 9, 2014

minifying only text/html output

// HTML Minification
    if(App::Environment() != 'local')
    {
        if($response instanceof Illuminate\Http\Response)
        {
            $output = $response->getOriginalContent();
            if($response->headers->get('content-type') == 'text/html; charset=UTF-8')
            {
                // Clean comments
                $output = preg_replace('/<!--([^\[|(<!)].*)/', '', $output);
                $output = preg_replace('/(?<!\S)\/\/\s*[^\r\n]*/', '', $output);
                // Clean Whitespace
                $output = preg_replace('/\s{2,}/', '', $output);
                $output = preg_replace('/(\r?\n)/', '', $output);
                $response->setContent($output);
            }
        }
    }
@rafelsanso

This comment has been minimized.

Copy link

@rafelsanso rafelsanso commented Jan 20, 2015

This code has an important error. Run the full content of the page twice. The first to generate the page, and the second to go through the :: after. That doubles the load times. I think it's worse than spending the HTML uncompressed. You can do the test by putting a log on a page and see that the refresh the log is printed twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.