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

[2.x] Fix bugs and code improvements #111

Merged

Conversation

joaorobertopb
Copy link
Collaborator

@joaorobertopb joaorobertopb commented Sep 12, 2019

Description

This big PR will:

  • Drop support for Laravel <5.5;
  • Bump PHP minimun version to 7.1.3+;
  • Update dependency constraints in composer.json;
  • Recfator all tests:
    • Remove deprecated functions e prepare code to next phpunit 9.0 version;
    • Improvements tests coverage;
  • Fix RemoveComments middleware, now it remove JS and CSS comments;
  • Fix CollapseWhitespace, now dependent on 'RemoveComments' middleware;
  • Update the old phpunit.xml configuration file;
  • Update the .travis.yml file and run the tests against in PHP 7.3 / PHP 7.4;
  • Update README.md;

Motivation and context

Why is this change required? What problem does it solve?

It fixes: #30 #46 #55 #57 #77 #82 #107 #108

The CollapseWriteSpace::class filter was producing a bug when the HTML contains inline JS comments.... All JS code after the inline comment was also commented!

For example:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <meta name="description" content="">
    </head>
    <body>
        <script>
            // Alert Hello!!
            alert('Hello!!');

            // Log Hello!!
            console.log('Hello!!');
        </script>
    </body>
</html>

After filter apply:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><title></title><meta name="description" content=""></head><body><script> // Alert Hello!! alert('Hello!!'); // Log Hello!! console.log('Hello!!'); </script></body></html>

What is the solution?

We have to remove all inline JS comments before the 'CollapseWriteSpace' filter is executed.

🤔 ... Okay, we need to make sure RemoveComments::class filter always runs before CollapseWriteSpace::class filter!

All right, it's done:

class CollapseWhitespace extends PageSpeed
{
public function apply($buffer)
{
$replace = [
"/\n([\S])/" => '$1',
"/\r/" => '',
"/\n/" => '',
"/\t/" => '',
"/ +/" => ' ',
"/> +</" => '><',
];
return $this->replace($replace, $this->removeComments($buffer));
}
protected function removeComments($buffer)
{
return (new RemoveComments)->apply($buffer);
}
}

But...

Oooh Nooo! The RemoveComments::class filter was only removing HTML comments 🤦‍♂️...

Not problem ... RemoveComments::class has been updated! Now, it removes HTML, JS and CSS comments:

class RemoveComments extends PageSpeed
{
public function apply($buffer)
{
$replace = [
'/<!--[^]><!\[](.*?)[^\]]-->/s' => '',
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/' => '',
];
return $this->replace($replace, $buffer);
}
}

How has this been tested?

Refactoring and improve test coverage.

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bugfix / Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have read the CONTRIBUTING document.
  • My pull request addresses exactly one patch/feature.
  • I have created a branch for this patch/feature.
  • Each individual commit in the pull request is meaningful.
  • I have added tests to cover my changes.
  • If my change requires a change to the documentation, I have updated it accordingly.

I did a lot of testing and it seems to be working okay...

Can someone help me to test this PR, please? 😄

Ping @renatomarinho

@muarachmann
Copy link

@joaorobertopb thanks will test and get back to you

@muarachmann
Copy link

Also, i noticed that the Urls were truncated when the application is served
for example instead of https://www.google.com it shows //www.google.com but still loads the library.

@joaorobertopb
Copy link
Collaborator Author

@muarachmann yes man, it is correct.

See:

\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class,

The TrimUrls::class filter trims URLs by resolving them by making them relative to the base URL for the page.

Warning: TrimUrls::class is considered medium risk. It can cause problems if it uses the wrong base URL. This can happen, for example, if you serve HTML that will be pasted verbatim into other HTML pages. If URLs are trimmed on the first page, they will be incorrect for the page they are inserted into. In this case, just disable the middleware.

@HDVinnie
Copy link

@joaorobertopb beautiful PR. Very clean and documented. Tested and cannot appear to find any issues yet. Will report back if do find something this weekend.

@muarachmann
Copy link

Hi @joaorobertopb tested on a live laravel (5.8) application and it seems to be breaking on the <img src=. I removed this and it breaks on the <script src= tag. Here is a sample of the page source
Screen Shot 2019-09-15 at 10 12 25 PM

Screen Shot 2019-09-15 at 10 12 55 PM

For example here is a sample page and it seems to break on line 9 https://gist.github.com/muarachmann/a020549549e332c8304a5cecce270c14 as seen below.
Screen Shot 2019-09-15 at 10 26 25 PM
This is when i include all the stuffs in the Kernel.php removing them one by one to see the pb
I see it is with the \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class

	     // Laravel Page Speed
	    \RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class,
	    \RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class,
	    \RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class,
	  //  \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class,
	    \RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class,
	    \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class,
	    \RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class,

Not sure of what is happening as i instead suspected the TrimUrls to behave as such.
If not removing that everything else works fine :)

@muarachmann
Copy link

And also just noticed my laravel debug screen shows blank without the usual laravel debug screen if an error occurs. It just returns a 500 error

@joaorobertopb
Copy link
Collaborator Author

Hi @muarachmann, thanks for reporting this.

I'll check!

@vineetha-123
Copy link

how to integrate above new code of RemoveComments into my project

@joaorobertopb
Copy link
Collaborator Author

@vineetha-123 Would have to wait for this PR to be merged.

OR

You can configure composer.json to require my fork repository. This is not very recommended, but if you are in a hurry ...

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/joaorobertopb/laravel-page-speed"
        }
    ],
    "require": {
        "renatomarinho/laravel-page-speed": "dev-fix-bugs-and-code-improvements"
    }
}

See more details: https://stackoverflow.com/questions/13498519/how-to-require-a-fork-with-composer

@vineetha-123
Copy link

@vineetha-123 Would have to wait for this PR to be merged.

OR

You can configure composer.json to require my fork repository. This is not very recommended, but if you are in a hurry ...

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/joaorobertopb/laravel-page-speed"
        }
    ],
    "require": {
        "renatomarinho/laravel-page-speed": "dev-fix-bugs-and-code-improvements"
    }
}

See more details: https://stackoverflow.com/questions/13498519/how-to-require-a-fork-with-composer

thanks

@Cannonb4ll
Copy link

Is there any ETA when this will be merged and released?

@joaorobertopb
Copy link
Collaborator Author

Sorry for the delay 😅 ! It will be merged later this year...

@deekumar24
Copy link

I don't know if I'm 100% right here,
if you're using TrimUrls, it'll trim the Laravel's assets URL and the regex is taking that line as comment.
I tried to use it in double quotes. but its still appearing that way.
See the screenshot below.
Screenshot from 2019-12-26 11-25-32
Screenshot from 2019-12-26 11-22-44

@joaorobertopb
Copy link
Collaborator Author

@rudee24 thanks for reporting this.

I'll check!

@huyvl102
Copy link

2020 coming :D , Did u updated 💃

Copy link
Collaborator

@lucasMesquitaBorges lucasMesquitaBorges left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @joaorobertopb
I have tested this PR a lot and I just found one problem with RemoveComments middleware.
It's breaking html code for "//" strings.

src/Middleware/RemoveComments.php Show resolved Hide resolved
@muarachmann
Copy link

muarachmann commented Feb 13, 2020 via email

@gagandeep995
Copy link

any updates about the release?

@lucasMesquitaBorges
Copy link
Collaborator

Yes @gagandeep995! We're currently working on the milestone 2.0.0 that will address this release.

@joaorobertopb joaorobertopb changed the title Fix bugs and code improvements [2.x] Fix bugs and code improvements May 16, 2020
@joaorobertopb joaorobertopb changed the base branch from master to 2.x May 16, 2020 14:11
@joaorobertopb joaorobertopb force-pushed the fix-bugs-and-code-improvements branch from 8dc9125 to 8794aaf Compare May 16, 2020 15:33
@joaorobertopb
Copy link
Collaborator Author

Merging this PR in branch 2.x

@joaorobertopb joaorobertopb merged commit 8bae9d0 into renatomarinho:2.x May 16, 2020
@joaorobertopb joaorobertopb deleted the fix-bugs-and-code-improvements branch May 16, 2020 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment