In this snippet, you will learn how to speed up Laravel applications by minify HTML on demand, remove comments, inline CSS, defer Javascript files, collapse whitespace, and more.
1 - Install the Package with Composer
The first step is to install the package using composer. By default this will install the latest version of the package.
composer require renatomarinho/laravel-page-speed
2 - Publish Configuration File
Do publish the configuration file to change the settings whenever necessary. It will be handly for later so run the command below.
php artisan vendor:publish --provider="RenatoMarinho\LaravelPageSpeed\ServiceProvider"
3 - Enable the Optimization
To enable the optimization you just have to include the middleware for the necessary part that you want to optimize.
<?php # app/Http/Kernel.php protected $middleware = [ # your other middleware here... \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, # Note: This middleware invokes "RemoveComments::class" before it runs. \RenatoMarinho\LaravelPageSpeed\Middleware\DeferJavascript::class, ]
To learn more about the middleware details you can just head over to the documentation on GitHub page (Middleware Details).
Leave a reply