Blog Detail


Optimize Your Site Automatically with Laravel Page Speed cover image

arrow_back Optimize Your Site Automatically with Laravel Page Speed

Laravel Page Speed is a simple package to minify HTML output on demand which results in a 35%+ optimization.



  • PHP 7.2.5+
  • Laravel 6.0+

You can install the package via composer:

composer require renatomarinho/laravel-page-speed

This package supports Laravel Package Discovery.

Publish configuration file

php artisan vendor:publish --provider="RenatoMarinho\LaravelPageSpeed\ServiceProvider"

Do not forget to register middlewares

Next, the \RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class and other middleware must be registered in the kernel, for example:


protected $middleware = [
    \RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class, // Note: This middleware invokes "RemoveComments::class" before it runs.

Middlewares Details


The RemoveComments::class filter eliminates HTML, JS and CSS comments. 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.


The CollapseWhitespace::class filter reduces bytes transmitted in an HTML file by removing unnecessary whitespace. This middleware invokes the RemoveComments::class filter before execution.


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).

Quote removal produces a modest savings in byte count on most pages.


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.


The InsertDNSPrefetch::class filter Injects tags in the HEAD to enable the browser to do DNS prefetching.

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 toward 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.

Skip routes

You would probably like to configure the package to skip some routes.


//You can use * as wildcard.
'skip' => [
    '*.pdf', //Ignore all routes with final .pdf
    '*/downloads/*',//Ignore all routes that contain 'downloads'
    'assets/*', // Ignore all routes with the 'assets' prefix

By default this field comes configured with some options, so feel free to configure according to your needs.

For more details, you can visit its complete documentation on Github.

Published at : 22-06-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project