How do I compress HTML in laravel 5
Asked Answered
N

10

27

In Laravel 4.0, I use the code below to compress the HTML laravel response outputs to browser, however it doesn't work in laravel 5.

App::after(function($request, $response)
{
    if($response instanceof Illuminate\Http\Response)
    {
        $buffer = $response->getContent();
        if(strpos($buffer,'<pre>') !== false)
        {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/"                  => '<?php ',
                "/\r/"                      => '',
                "/>\n</"                    => '><',
                "/>\s+\n</"                 => '><',
                "/>\n\s+</"                 => '><',
            );
        }
        else
        {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/"                  => '<?php ',
                "/\n([\S])/"                => '$1',
                "/\r/"                      => '',
                "/\n/"                      => '',
                "/\t/"                      => '',
                "/ +/"                      => ' ',
            );
        }
        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);
        $response->setContent($buffer);
    }
});

Please how do i make this work in Laravel 5.

OR

Please provide a better way of compressing HTML in laravel 5 if any. Thanks in advance.

NB: I don't wish to use any laravel package for compressing html, just need a simple code that does the work without killing performance.

Natator answered 22/3, 2015 at 15:52 Comment(3)
I know you said you dont want a pacakge - but github.com/GrahamCampbell/Laravel-HTMLMin is the perfect solution. And it wont 'kill performance' any differently than if you did it yourself.Parlour
I would advise against what you are trying to do at all – a pre element is not the only thing where white space might matter, but also inside a textarea/input or basically in any element if it gets later on formatted via CSS (white-space). Just GZip the output before sending it to the client, that is much more effective than messing with the HTML code itself.Oleta
@cbroe how do I use GZip? any working exampleNatator
V
7

The recommended way to do this in Larvel 5 is to rewrite your function as middleware. As stated in the docs:

..this middleware would perform its task after the request is handled by the application:

<?php namespace App\Http\Middleware;

class AfterMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Perform action

        return $response;
    }
}
Vertebral answered 23/3, 2015 at 4:52 Comment(2)
Nice. How do I use it? Am running this on each request?Natator
Yes you'd run it on every http request. You can register it globally by adding your class in app/Http/Kernel.phpVertebral
U
25

Complete code is this (with custom GZip enabled) :

<?php

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $buffer = $response->getContent();
        if(strpos($buffer,'<pre>') !== false)
        {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/"                  => '<?php ',
                "/\r/"                      => '',
                "/>\n</"                    => '><',
                "/>\s+\n</"                 => '><',
                "/>\n\s+</"                 => '><',
            );
        }
        else
        {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/"                  => '<?php ',
                "/\n([\S])/"                => '$1',
                "/\r/"                      => '',
                "/\n/"                      => '',
                "/\t/"                      => '',
                "/ +/"                      => ' ',
            );
        }
        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);
        $response->setContent($buffer);
        ini_set('zlib.output_compression', 'On'); // If you like to enable GZip, too!
        return $response;
    }
}

Please check your browser network inspector for Content-Length header before/after implement this code.

enjoy it ... :).. .

Uncial answered 29/5, 2016 at 14:13 Comment(2)
(index):1 Uncaught SyntaxError: Unexpected end of input jquery not working after thatPendent
@Rahul Tathod This middleware will only compress HTML, and enable GZip. These two compresses doesn't effect on Jquery, or CSS, JS files. If there is any problem, you should find root cause.Uncial
W
22

It is not very good solution to minify html in middleware as you can spend a lot of CPU time on it and it runs on every request.

Instead it is better to use htmlmin package ( https://github.com/HTMLMin/Laravel-HTMLMin ):

composer require htmlmin/htmlmin
php artisan vendor:publish

Minifying HTML on blade template level and caching it in storage should be much more effective.

Whisenhunt answered 17/8, 2017 at 10:45 Comment(3)
will this still work if there are variables in your template that need to be accounted for on every request? Or even whole blocks that depend on conditions?Gawen
2021 update: this package seems to have a lot of bugs and doesn't seem to be maintained.Reenareenforce
does not work, lack of documentation for usage one of the worst packages i ever installed and uninstalled after 10minsBeyer
V
7

The recommended way to do this in Larvel 5 is to rewrite your function as middleware. As stated in the docs:

..this middleware would perform its task after the request is handled by the application:

<?php namespace App\Http\Middleware;

class AfterMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Perform action

        return $response;
    }
}
Vertebral answered 23/3, 2015 at 4:52 Comment(2)
Nice. How do I use it? Am running this on each request?Natator
Yes you'd run it on every http request. You can register it globally by adding your class in app/Http/Kernel.phpVertebral
Y
4

I have created a webpack plugin to solve same purpose.MinifyHtmlWebpackPlugin

Install the plugin with npm:

$ npm install minify-html-webpack-plugin --save-dev

For Laravel Mix Users

Paste below snippets into mix.js file.

    const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin');
    const mix = require('laravel-mix');

    mix.webpackConfig({
        plugins: [
            new MinifyHtmlWebpackPlugin({
                src: './storage/framework/views',
                ignoreFileNameRegex: /\.(gitignore)$/,
                rules: {
                    collapseWhitespace: true,
                    removeAttributeQuotes: true,
                    removeComments: true,
                    minifyJS: true,
                }
            })
        ]
    });

It will minify all view files during the Webpack build.

Yearlong answered 29/10, 2019 at 6:10 Comment(0)
T
2

This is almost a copy of Vahid's answer but it fixes two problems.

1) It checks if a response is a BinaryFileResponse as any attempt to modify this type of response will throw an Exception.

2) It retained newline characters as the complete elimination of newlines will lead to bad Javascript code on lines with single-line comment.

For example, the code below

 var a; //This is a variable
 var b; //This will be commented out

Will become

 var a; //This is a variable var b; //This will be commented out

Note: At the time of this answer I couldn't get my hands on a good regex to match single line comments without complications or rather, ignore newlines on only lines with a single-line comment, so I'm hoping for a better fix.

Here's the modified version.

<?php

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware {

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    $response = $next($request);
    if ($response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse) {
        return $response;
    } else {
        $buffer = $response->getContent();
        if (strpos($buffer, '<pre>') !== false) {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/" => '<?php ',
                "/\r/" => '',
                "/>\n</" => '><',
                "/>\s+\n</" => '><',
                "/>\n\s+</" => '><',
            );
        } else {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/" => '<?php ',
                "/\n([\S])/" => '$1',
                "/\r/" => '',
                "/\n+/" => "\n",
                "/\t/" => '',
                "/ +/" => ' ',
            );
        }
        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);
        $response->setContent($buffer);
        ini_set('zlib.output_compression', 'On'); //enable GZip, too!
        return $response;
    }
  }
}

Edit

Compressing output for every request using the middleware truly is really a bad idea, I recommend you check out this solution by Jokerius

Trossachs answered 6/12, 2016 at 23:56 Comment(0)
W
2

Just in case you are rendering the view manually:


echo view('example.site')->render(function($view, $content) { 
    return preg_replace(
            ['/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'],
            ['>', '<', '\\1'],
            $content
    ); }
);

Warbler answered 31/8, 2020 at 8:36 Comment(0)
P
0

This package is much better option in my opinion renatomarinho/laravel-page-speed

Picklock answered 10/12, 2019 at 4:56 Comment(0)
T
0

I did it with very simple code. Example: welcome.blade.php

Add the following code to the beginning of the page

<?php ob_start('compress_page');?>

Add the following code to the end of the page:

<?php   
ob_end_flush();
function compress_page($buffer) {
    $search = array("/>[[:space:]]+/", "/[[:space:]]+</");
    $replace = array(">","<");
    return preg_replace($search, $replace, $buffer);
}?>

Full page code example:

<?php ob_start('compress_page');?>
<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Raleway', sans-serif;
                font-weight: 100;
                height: 100vh;
                margin: 0;
            }
            .full-height {
                height: 100vh;
            }
            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }
            .position-ref {
                position: relative;
            }
            .top-right {
                position: absolute;
                right: 10px;
                top: 18px;
            }
            .content {
                text-align: center;
            }
            .title {
                font-size: 84px;
            }
            .links > a {
                color: #636b6f;
                padding: 0 25px;
                font-size: 12px;
                font-weight: 600;
                letter-spacing: .1rem;
                text-decoration: none;
                text-transform: uppercase;
            }
            .m-b-md {
                margin-bottom: 30px;
            }
        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                    @auth
                        <a href="{{ url('/home') }}">Home</a>
                    @else
                        <a href="{{ route('login') }}">Login</a>
                        <a href="{{ route('register') }}">Register</a>
                    @endauth
                </div>
            @endif

            <div class="content">
                <div class="title m-b-md">
                    Laravel
                </div>

                <div class="links">
                    <a href="https://laravel.com/docs">Documentation</a>
                    <a href="https://laracasts.com">Laracasts</a>
                    <a href="https://laravel-news.com">News</a>
                    <a href="https://forge.laravel.com">Forge</a>
                    <a href="https://github.com/laravel/laravel">GitHub</a>
                </div>
            </div>
        </div>
    </body>
</html>
<?php   
    ob_end_flush();
    function compress_page($buffer) {
        $search = array("/>[[:space:]]+/", "/[[:space:]]+</");
        $replace = array(">","<");
        return preg_replace($search, $replace, $buffer);
    }?>
Tannin answered 17/3, 2021 at 9:22 Comment(0)
S
-1

this is best way.. we don't need to use laravel packeges .Thanks..

<?php

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $buffer = $response->getContent();
        if(strpos($buffer,'<pre>') !== false)
        {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/"                  => '<?php ',
                "/\r/"                      => '',
                "/>\n</"                    => '><',
                "/>\s+\n</"                 => '><',
                "/>\n\s+</"                 => '><',
            );
        }
        else
        {
            $replace = array(
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/"                  => '<?php ',
                "/\n([\S])/"                => '$1',
                "/\r/"                      => '',
                "/\n/"                      => '',
                "/\t/"                      => '',
                "/ +/"                      => ' ',
            );
        }
        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);
        $response->setContent($buffer);
        ini_set('zlib.output_compression', 'On'); // If you like to enable GZip, too!
        return $response;
    }
}
Stoicism answered 4/1, 2017 at 18:2 Comment(1)
Code snippet doesn't seem to run.Gifford
R
-3

For easy compression, I build my own laravel module. This module will compress all the final html output before sending to the client (browser).

You can also target multiple environment at a time using .env file.

More details on how to install and configure will be found here

Romeyn answered 3/4, 2018 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.