├── LICENSE.md ├── README.md ├── art └── logo.png ├── composer.json ├── config └── laravel-page-speed.php └── src ├── Entities └── HtmlSpecs.php ├── Middleware ├── CollapseWhitespace.php ├── DeferJavascript.php ├── ElideAttributes.php ├── InlineCss.php ├── InsertDNSPrefetch.php ├── PageSpeed.php ├── RemoveComments.php ├── RemoveQuotes.php └── TrimUrls.php └── ServiceProvider.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Renato Marinho 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Laravel Page Speed logo 3 |

4 | 5 |

6 | Build Status 7 | Latest Stable Version 8 | Total Downloads 9 | License 10 |

11 | 12 | # Laravel Page Speed 13 | 14 | Simple package to minify HTML output on demand which results in a 35%+ optimization. Laravel Page Speed was created by [Renato Marinho][link-author], and currently maintained by [João Roberto P. Borges][link-maintainer], [Lucas Mesquita Borges][link-maintainer-2] and [Renato Marinho][link-author]. 15 | 16 | ## Installation 17 | 18 | > **Requires:** 19 | - **[PHP 7.2.5+](https://php.net/releases/)** 20 | - **[Laravel 6.0+](https://github.com/laravel/laravel)** 21 | 22 | You can install the package via composer: 23 | 24 | ```sh 25 | composer require renatomarinho/laravel-page-speed 26 | ``` 27 | 28 | This package supports Laravel [Package Discovery][link-package-discovery]. 29 | 30 | ### Publish configuration file 31 | 32 | `php artisan vendor:publish --provider="RenatoMarinho\LaravelPageSpeed\ServiceProvider"` 33 | 34 | ## Do not forget to register middlewares 35 | 36 | Next, the `\RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class` and other middleware must be registered in the kernel, for example: 37 | 38 | ```php 39 | //app/Http/Kernel.php 40 | 41 | protected $middleware = [ 42 | ... 43 | \RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class, 44 | \RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class, 45 | \RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class, 46 | \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class, 47 | //\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class, 48 | //\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class, 49 | \RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class, // Note: This middleware invokes "RemoveComments::class" before it runs. 50 | \RenatoMarinho\LaravelPageSpeed\Middleware\DeferJavascript::class, 51 | ] 52 | ``` 53 | 54 | ## Middlewares Details 55 | 56 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class 57 | 58 | The **RemoveComments::class** filter eliminates HTML, JS and CSS comments. 59 | The filter reduces the transfer size of HTML files by removing the comments. Depending on the HTML file, this filter can significantly reduce the number of bytes transmitted on the network. 60 | 61 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class 62 | 63 | The **CollapseWhitespace::class** filter reduces bytes transmitted in an HTML file by removing unnecessary whitespace. 64 | This middleware invoke **RemoveComments::class** filter before executation. 65 | 66 | > **Note**: Do not register the "RemoveComments::class" filter with it. Because it will be called automatically by "CollapseWhitespace::class" 67 | 68 | #### Before 69 | 70 | ![Before of Laravel Page Speed][link-before] 71 | 72 | #### After 73 | 74 | ![After of Laravel Page Speed][link-after] 75 | 76 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class 77 | 78 | The **RemoveQuotes::class** filter eliminates unnecessary quotation marks from HTML attributes. While required by the various HTML specifications, browsers permit their omission when the value of an attribute is composed of a certain subset of characters (alphanumerics and some punctuation characters). 79 | 80 | Quote removal produces a modest savings in byte count on most pages. 81 | 82 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class 83 | 84 | The **ElideAttributes::class** filter reduces the transfer size of HTML files by removing attributes from tags when the specified value is equal to the default value for that attribute. This can save a modest number of bytes, and may make the document more compressible by canonicalizing the affected tags. 85 | 86 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class 87 | 88 | The **InsertDNSPrefetch::class** filter Injects tags in the HEAD to enable the browser to do DNS prefetching. 89 | 90 | DNS resolution time varies from <1ms for locally cached results, to hundreds of milliseconds due to the cascading nature of DNS. This can contribute significantly towards total page load time. This filter reduces DNS lookup time by providing hints to the browser at the beginning of the HTML, which allows the browser to pre-resolve DNS for resources on the page. 91 | 92 | ### ⚠️ \RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class, 93 | 94 | The **TrimUrls::class** filter trims URLs by resolving them by making them relative to the base URL for the page. 95 | 96 | > **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. 97 | 98 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class 99 | 100 | The **InlineCss::class** filter transforms the inline "style" attribute of tags into classes by moving the CSS to the header. 101 | 102 | ### \RenatoMarinho\LaravelPageSpeed\Middleware\DeferJavascript::class 103 | 104 | Defers the execution of javascript in the HTML. 105 | 106 | > If necessary cancel deferring in some script, use `data-pagespeed-no-defer` as script attribute to cancel deferring. 107 | 108 |
109 | 110 | ## Configuration 111 | 112 | After installing package, you may need to configure some options. 113 | 114 | ### Disable Service 115 | 116 | You would probably like to set up the local environment to get a readable output. 117 | 118 | ```php 119 | //config/laravel-page-speed.php 120 | 121 | //Set this field to false to disable the laravel page speed service. 122 | 'enable' => env('LARAVEL_PAGE_SPEED_ENABLE', true), 123 | ``` 124 | ### Skip routes 125 | 126 | You would probably like to configure the package to skip some routes. 127 | 128 | ```php 129 | //config/laravel-page-speed.php 130 | 131 | //You can use * as wildcard. 132 | 'skip' => [ 133 | '*.pdf', //Ignore all routes with final .pdf 134 | '*/downloads/*',//Ignore all routes that contain 'downloads' 135 | 'assets/*', // Ignore all routes with the 'assets' prefix 136 | ]; 137 | ``` 138 | 139 | By default this field comes configured with some options, so feel free to configure according to your needs... 140 | 141 | > *Notice*: This package skip automatically 'binary' and 'streamed' responses. See [File Downloads][link-file-download]. 142 | 143 | ## Testing 144 | 145 | ```sh 146 | $ composer test 147 | ``` 148 | 149 | ## Contributing 150 | 151 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 152 | 153 | ## Contributors 154 | 155 | - [Caneco](https://twitter.com/caneco) (for the logo) 156 | - [All Contributors][link-contributors] 157 | 158 | ## Inspiration 159 | 160 | #### Mod Page Speed (https://www.modpagespeed.com/) 161 | 162 | ## License 163 | 164 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 165 | 166 | [link-before]: https://i.imgur.com/cN3MWYh.png 167 | [link-after]: https://i.imgur.com/IKWKLkL.png 168 | [link-author]: https://github.com/renatomarinho 169 | [link-maintainer]: https://github.com/joaorobertopb 170 | [link-maintainer-2]: https://github.com/lucasMesquitaBorges 171 | [link-contributors]: ../../contributors 172 | [link-file-download]: https://laravel.com/docs/6.0/responses#file-downloads 173 | [link-package-discovery]: https://laravel.com/docs/6.0/packages#package-discovery 174 | -------------------------------------------------------------------------------- /art/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renatomarinho/laravel-page-speed/b16c0185ae6816b47c8534b4ecfbce73e6c11cf8/art/logo.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "renatomarinho/laravel-page-speed", 3 | "description": "Laravel Page Speed", 4 | "keywords": [ 5 | "laravel", 6 | "page speed", 7 | "minify", 8 | "optimize", 9 | "html" 10 | ], 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Renato Marinho", 15 | "email": "renato.marinho@s2move.com" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^8.5 || ^9.0", 24 | "orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0", 25 | "squizlabs/php_codesniffer": "^3.6", 26 | "mockery/mockery": "^1.4" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "RenatoMarinho\\LaravelPageSpeed\\": "src/" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "RenatoMarinho\\LaravelPageSpeed\\Test\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "test": "phpunit", 40 | "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", 41 | "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" 42 | }, 43 | "minimum-stability": "dev", 44 | "prefer-stable": true, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "RenatoMarinho\\LaravelPageSpeed\\ServiceProvider" 49 | ] 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /config/laravel-page-speed.php: -------------------------------------------------------------------------------- 1 | env('LARAVEL_PAGE_SPEED_ENABLE', true), 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | Skip Routes 19 | |-------------------------------------------------------------------------- 20 | | 21 | | Skip Routes paths to exclude. 22 | | You can use * as wildcard. 23 | | 24 | */ 25 | 'skip' => [ 26 | '*.xml', 27 | '*.less', 28 | '*.pdf', 29 | '*.doc', 30 | '*.txt', 31 | '*.ico', 32 | '*.rss', 33 | '*.zip', 34 | '*.mp3', 35 | '*.rar', 36 | '*.exe', 37 | '*.wmv', 38 | '*.doc', 39 | '*.avi', 40 | '*.ppt', 41 | '*.mpg', 42 | '*.mpeg', 43 | '*.tif', 44 | '*.wav', 45 | '*.mov', 46 | '*.psd', 47 | '*.ai', 48 | '*.xls', 49 | '*.mp4', 50 | '*.m4a', 51 | '*.swf', 52 | '*.dat', 53 | '*.dmg', 54 | '*.iso', 55 | '*.flv', 56 | '*.m4v', 57 | '*.torrent' 58 | ], 59 | ]; 60 | -------------------------------------------------------------------------------- /src/Entities/HtmlSpecs.php: -------------------------------------------------------------------------------- 1 | '$1', 11 | "/\r/" => '', 12 | "/\n/" => '', 13 | "/\t/" => '', 14 | "/ +/" => ' ', 15 | "/> + '><', 16 | ]; 17 | 18 | return $this->replace($replace, $this->removeComments($buffer)); 19 | } 20 | 21 | protected function removeComments($buffer) 22 | { 23 | return (new RemoveComments)->apply($buffer); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Middleware/DeferJavascript.php: -------------------------------------------------------------------------------- 1 | ]+src[^>]+)((?![^>]+defer|data-pagespeed-no-defer[^>]+)[^>]+)/i' => '