Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip route from being minified #3

Closed
ghost opened this issue Sep 11, 2017 · 2 comments · Fixed by #26
Closed

Skip route from being minified #3

ghost opened this issue Sep 11, 2017 · 2 comments · Fixed by #26

Comments

@ghost
Copy link

ghost commented Sep 11, 2017

Is there a way to skip a route or a part of the code from being minified? Would be very helpful.

@ahmadarif
Copy link

Yes, I'm getting error when show pdf file.

@MrJmpl3
Copy link

MrJmpl3 commented Sep 14, 2017

For the moment:

  • Create new Middleware and extends \RenatoMarinho\LaravelBladeMinify\Middleware\Minify
  • Override the html function with:
public function html($response)
    {
        $buffer = $response->getContent();

        $replace = [
            '/<!--[^\[](.*?)[^\]]-->/s' => '',
            "/<\?php/" => '<?php ',
            "/\n([\S])/" => '$1',
            "/\r/" => '',
            "/\n/" => '',
            "/\t/" => '',
            "/ +/" => ' ',
            "/> +</" => '><',
        ];

        if(strpos($buffer,'<pre>') !== false) {
            $replace = [
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/" => '<?php ',
                "/\r/" => '',
                "/>\n</" => '><',
                "/>\s+\n</" => '><',
                "/>\n\s+</" => '><',
            ];
        }

        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);

        if(!($response instanceof BinaryFileResponse)) {
            $response->setContent($buffer);
        }

        return $response;
    }

For example:

I created new Middleware called MinifyHTML:

<?php

namespace App\Http\Middleware;

use Symfony\Component\HttpFoundation\BinaryFileResponse;

class MinifyHTML extends \RenatoMarinho\LaravelBladeMinify\Middleware\Minify
{
    /**
     * @param $response
     * @return mixed
     */
    public function html($response)
    {
        $buffer = $response->getContent();

        $replace = [
            '/<!--[^\[](.*?)[^\]]-->/s' => '',
            "/<\?php/" => '<?php ',
            "/\n([\S])/" => '$1',
            "/\r/" => '',
            "/\n/" => '',
            "/\t/" => '',
            "/ +/" => ' ',
            "/> +</" => '><',
        ];

        if (strpos($buffer, '<pre>') !== false) {
            $replace = [
                '/<!--[^\[](.*?)[^\]]-->/s' => '',
                "/<\?php/" => '<?php ',
                "/\r/" => '',
                "/>\n</" => '><',
                "/>\s+\n</" => '><',
                "/>\n\s+</" => '><',
            ];
        }

        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);

        if (!($response instanceof BinaryFileResponse)) {
            $response->setContent($buffer);
        }

        return $response;
    }
}

And replace in Kernel.php \RenatoMarinho\LaravelBladeMinify\Middleware\Minify::class to MinifyHTML::class

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
        MinifyHTML::class,
    ];

joaorobertopb added a commit to joaorobertopb/laravel-page-speed that referenced this issue Oct 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants