Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compress HTML in laravel 5

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.

like image 603
Emeka Mbah Avatar asked Mar 22 '15 15:03

Emeka Mbah


People also ask

Can we compress HTML file?

HTML compression, meanwhile, shrinks file size by replacing redundant code instances with references attached to the original information that indicate the position of this duplicated data. Called “lossless compression”, this technique ensures just that: No data loss when file sizes are reduced.

How do I compress a website code?

The two most common compression methods on the web are Gzip and Deflate. The compression process locates similar strings within a document and replaces them with temporary strings with the same placeholder.

Can we use HTML in laravel?

In simple, Laravel doesn't render . html format, so simple change extension from . html to . blade.


2 Answers

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 ... :).. .


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.

like image 43
Jokerius Avatar answered Sep 18 '22 23:09

Jokerius