How to Optimize Website Speed and Performance in Laravel

How to improve website speed and performance in laravel? In this example, I will explain to you How to Optimize Website Speed and Performance in Laravel. In this tutorial, we will use laravel 5.6. We will use renatomarinho/laravel-page-speed package for increase website speed and performance in laravel. This package optimizes your website speed up to 35%.

This package has several things to improve speed like minify HTML output and it can be optimization your web page. This package will delete unused attributes in HTML tags, delete unused prefixes from URLs, delete unused quotes in HTML tags, delete HTML comments,delete unused whitespace in HTML.They also minify css and minify js files.

If your website has a low speed then definitely you will see the down SERP result in google. Google always prefer high-speed website priority. The website speed major effect in SEO. It is very important for every website to quick load that means your website should load in a few seconds like 4 or 5. If you want to more information about this package, then move to Github.

How to Optimize Website Speed and Performance in Laravel

We are going to Configure Laravel Project.

#1: Install Laravel Project

Install Laravel 5.6 Project by the typing following command.

$ composer create-project --prefer-dist laravel/laravel optimizewebsitespeed

#2: Create a View File

You can create a file in resources/views/data.blade.php and put this following code in it.

<!-- data.blade.php -->
<!DOCTYPE html>
<html>
<head>
	<title>How to Optimize Website Speed and Performance in Laravel</title>
	<link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
	<h1>Optimize Website Speed and Performance in Laravel</h1>
	<table class="table table-bordered">
	    <thead>
	      <tr>
	        <th>Id</th>
	        <th>Name</th>
	        <th>Email</th>
	      </tr>
	    </thead>
	    <tbody>
	      <tr>
	        <td>1</td>
	        <td>Ronaldo</td>
	        <td>ronaldo@gmail.com</td>
	      </tr>
	      <tr>
	        <td>2</td>
	        <td>Messi</td>
	        <td>messi@gmail.com</td>
	      </tr>
	      <tr>
	        <td>3</td>
	        <td>Mbappe</td>
	        <td>mbappe@gmail.com</td>
	      </tr>
	      <tr>
	        <td>4</td>
	        <td>Bale</td>
	        <td>bale@gmail.com</td>
	      </tr>
	      <tr>
	        <td>5</td>
	        <td>Neymar</td>
	        <td>neymar@gmail.com</td>
	      </tr>
	    </tbody>
	</table>
</div>
</body>
</html>

#3: Define Route

We register route in a web.php file.

Route::get('data', function () {
    return view('data');
});

You can see the output in the below screenshot.

Laravel Optimize Website Speed and Performance Example

#4: Install laravel-page-speed Package

Now we will install renatomarinho/laravel-page-speed composer package using by following command in our project.

$ composer require renatomarinho/laravel-page-speed

#5: Change Configuration

Laravel 5.5 and up

You don’t have to do anything else, this package uses the Package Auto-Discovery feature, and should be ready as soon as you install it via Composer.

Laravel 5.4 or 5.3

You can add the Service Provider to your config/app.php.

'providers' => [
	          RenatoMarinho\LaravelPageSpeed\ServiceProvider::class
               ]

You can publish the default configuration file by the following command(laravel 5.5+ follow below steps):

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

You can register middleware in the app/Http/Kernal.php file.

protected $middleware = [
    ...
    \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,
]

#6: Testing

php artisan serve

You can see output in below screenshot and compare to screenshot 1.

Laravel Increase Website Speed and Performance Example

What is happening? Because laravel speed package improves speed and performance.

At last, our How to Optimize Website Speed and Performance in Laravel is over.

Post Your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.