How to optimize website speed and performance in Laravel?

By Hardik Savani November 5, 2023 Category : PHP Laravel

It is very important for every website to quick load that means your website should load in few seconds like 4 or 5. We are always fetching issues about page speed like how to increase website speed in laravel, how to reduce loading time of website in laravel, is it possible speed up php execution time of my website, how to optimize php laravel script to increase speed, how to speed up web browsing in laravel. So basically you have several question regarding to improve your website performance in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10.

However, There are several composer packages available for increase website page speed in php laravel. So here i found "laravel-page-speed" composer package for 35%+ optimization of your website.

laravel-page-speed package will minify HTML output and it can be optimization your web page. laravel-page-speed package will delete unused attributes in HTML tags, delete unused quotes in HTML tags, delete unused prefixes from URLs, delete unused whitespace in HTML, delete HTML comments. Also there are several feature. They also minify css and minify js files. There are several things done with this page and i am sure it make better performance at it was before.

Here, i will give you very small example with screen shot. I created simple bootstrap file and with simple table. If i see in "CTRL + u" and see source code then i can see too many white space and if anything unused attribute etc. After used this package you can see on source all white space remove and clean with quick run code.

So here i attach both small and simple example screen shot, so you can understand how it can help you. After that you can see how it works.

Before:

After:

As you can see how it optimization and help you. So just follow few step and get simple example:

Step 1: Install laravel-page-speed Composer Package

first thing is that we have to add laravel-page-speed package for improve page speed so one your cmd or terminal and fire bellow command, Make sure laravel-page-speed package support laravel 5.3, laravel 5.4, laravel 5.5 and laravel 5.5 + version of laravel.

composer require renatomarinho/laravel-page-speed

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [

....

RenatoMarinho\LaravelPageSpeed\ServiceProvider::class

]

.....

You can publish the default configuration file by following command:

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

After that you have to register your middleware on Kernel.php file so, do like as bellow:

app/Http/Kernel.php

<?php


namespace App\Http;


use Illuminate\Foundation\Http\Kernel as HttpKernel;


class Kernel extends HttpKernel

{

......

/**

* The application's route middleware groups.

*

* @var array

*/

protected $middlewareGroups = [

'web' => [

.....

\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,

],


.....

];

.....

}

Step 2: Make Route

In this is step we need to create route display simple layout file. so open your routes/web.php file and add following route.

routes/web.php

Route::get('mySource', function () {

return view('mySource');

});

Step 3: Create Blade File

In Last step, let's create mySource.blade.php(resources/views/mySource.blade.php) for layout and we will write design code here and put following code:

resources/views/mySource.blade.php

<!DOCTYPE html>

<html>

<head>

<title>My Source</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />

</head>

<body>


<div class="container">

<h1>My Source</h1>

<table class="table table-bordered sortable">

<thead>

<tr>

<th>No</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

<tbody>

<tr>

<td>1</td>

<td>Haresh</td>

<td>haresh@gmail.com</td>

</tr>

<tr>

<td>2</td>

<td>Vimal</td>

<td>vimal@gmail.com</td>

</tr>

<tr>

<td>3</td>

<td>Harshad</td>

<td>harshad@gmail.com</td>

</tr>

<tr>

<td>4</td>

<td>Raju</td>

<td>raju@gmail.com</td>

</tr>

<tr>

<td>5</td>

<td>Paresh</td>

<td>paresh@gmail.com</td>

</tr>

</tbody>

</table>

</div>


</body>

</html>

Now, you are ready to run example.

You can get more information about laravel-page-speed plugin from here : laravel-page-speed

I hope it can help you....

Shares