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

laravel 10.x support and new line bug fix's #177

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^8.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0"
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0"
Copy link

Choose a reason for hiding this comment

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

lol

},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.0",
Expand Down
13 changes: 13 additions & 0 deletions config/laravel-page-speed.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@
'*.m4v',
'*.torrent'
],

/*
|--------------------------------------------------------------------------
| Skip Custom Domain
|--------------------------------------------------------------------------
|
| Skip Custom Domain to exclude.
|
*/

'domain' => [
// for eg: str_replace(['https://', 'http://'], ['', ''], env('FILAMENT_DOMAIN'))
],
];
15 changes: 13 additions & 2 deletions src/Middleware/CollapseWhitespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

namespace RenatoMarinho\LaravelPageSpeed\Middleware;

use Exception;

class CollapseWhitespace extends PageSpeed
{
public function apply($buffer)
{
if (str_contains($buffer, 'Error:')) {
return $buffer;
}

if (in_array(request()?->host(), config('laravel-page-speed.domain'), true)) {
return $this->replace([], $this->removeComments($buffer));
}

$replace = [
"/\n([\S])/" => '$1',
"/\r/" => '',
"/\n/" => '',
"/\t/" => '',
"/ +/" => ' ',
"/> +</" => '><',
'/ >/' => '>',
'/ +/' => ' ',
'/> +</' => '><',
];

return $this->replace($replace, $this->removeComments($buffer));
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/InlineCss.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function injectStyle()
$injectStyle = implode(' ', $this->inline);

$replace = [
'#</head>(.*?)#' => "\n<style>{$injectStyle}</style>\n</head>"
'#</head>(.*?)#' => "<style>{$injectStyle}</style></head>"
];

$this->html = $this->replace($replace, $this->html);
Expand Down
13 changes: 10 additions & 3 deletions src/Middleware/InsertDNSPrefetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ public function apply($buffer)
);

$dnsPrefetch = collect($matches[0])->map(function ($item) {
if (str_contains($item[0], 'debugbar')) {
return;
}

$domain = (new TrimUrls)->apply($item[0]);
$domain = explode(
'/',
str_replace('//', '', $domain)
);

return "<link rel=\"dns-prefetch\" href=\"//{$domain[0]}\">";
})->unique()->implode("\n");
if (str_contains(@$domain[0], 'www.schema.org')) {
$domain[0] = 'www.schema.org';
}

return "<link rel=\"preconnect\" href=\"//{$domain[0]}\" crossorigin><link rel=\"dns-prefetch\" href=\"//{$domain[0]}\">";
})->unique()->implode('');

$replace = [
'#<head>(.*?)#' => "<head>\n{$dnsPrefetch}"
'#<head>(.*?)#' => "<head>{$dnsPrefetch}",
];

return $this->replace($replace, $buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/RemoveComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class RemoveComments extends PageSpeed
{
const REGEX_MATCH_JS_AND_CSS_COMMENTS = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/';
const REGEX_MATCH_JS_AND_CSS_COMMENTS = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\"|[integrity])\/\/.*))/';
const REGEX_MATCH_HTML_COMMENTS = '/<!--[^]><!\[](.*?)[^\]]-->/s';

public function apply($buffer)
Expand Down