img-1

Installing RabbitLoader on a Laravel Website

Laravel is a PHP-based open-source framework that follows an MVC architectural pattern and modular packaging system. RabbitLoader can be used to improve PageSpeed Insights and core web vitals for the Laravel websites.

Video Guide

Installing the Package

The RabbitLoader Laravel Package is available via Composer and it is pretty easy to install. Navigate to the root directory of the website first (where you already have the website’s composer.json file. To install the package, run the below command-

composer require rabbit-loader/laravel

After successful installation of the package, activate the package using the below command-

php artisan vendor:publish --provider='RabbitLoader\Laravel\RLServiceProvider'

Update Middleware

In the app/Http/Kernel.php file, add the RabbitLoader middleware.

//app/Http/Kernel.php

protected $middleware = [
    ...
    \RabbitLoader\Laravel\Process::class
    ...
]

Adding the License Key

The package requires a license key to function. The license key for the website can be obtained when you add a website.

Navigate to the “My Websites” section on the RabbitLoader console and click on the “Add New Website” button. Please refer to the below screenshot for reference –

add-php-website-license

Please note that if you lose the license key, you can not get it again as this is a one-time process. To issue a license key again, you need to first delete the website from the main menu Action->Delete.

The license key should be saved in the ‘.env’ file of the Laravel app. The .env file should be at the root of the website (at the same place where the composer.json) file is available. Append this towards the end of the .env file and replace the license value with the one you got above.

RABBIT_LOADER_LICENSE_KEY="license key goes here"

Excluding Pages from RabbitLader

There are a few ways page optimization can be controlled.

Using Page Rules

Page rules control how different sections of the websites will be optimized and cached. For example, you would want to enable JavaScript optimization for one section, while keeping it disabled for another section. If you are new to this, please go through the Page rule starter guide and use cases.

Using the Package Config

The package also has multiple ways to exclude specific pages from being optimized and cached by RabbitLoader. After installing the package, go to the config file located at /config/rabbitloader.php and adjust these values to meet your needs-

//skip caching if path starts with admin
'skipPaths' => ['/admin*'],

//skip caching if a cookie is found with name cookie1 
'skipCookies' => ['cookie1'],

Points to Take Care of When Updating Content

For Main Content

When an existing page’s content (HTML or other dynamic contnet) is updated from the admin section or by other means, a purging of the cached content is required to reflect the updated changes.

You can create a hook and attach it to any API or the admin section of the website that triggers a cache invalidation when content is modified.

#in the admin section

$liceseKey = config('rabbitloader.licenseKey', '');
$cacheDir = config('rabbitloader.cacheDir', '');
$rlSDK = new RabbitLoader\SDK\RabbitLoader($licenseKey, $cacheDir);

$urlModified = 'https://mywebsite.com/modified-page-slug/';
$rlSDK->onContentChange($urlModified);

#if home page needs to be purged too, then-
$rlSDK->onContentChange('https://mywebsite.com/');

For Static Files

When updating an existing CSS/JS file or an image, make sure that you use some kind of versioning in the resource path. Since the existing files are already cached on the edge servers, modifying the file on the hosting server may introduce a delay in reflecting those for the website visitors.

Below is an example where you see '?v=1.2' appended as a GET parameter to a CSS file. When you update this file, simply change the version to '?v=1.3' or anything of that sort that ignores the previous version of the CSS file when serving the page to visitors.

<link rel="stylesheet" href="https://mywebsite.com/assets/css/theme.css?v=1.2" />

The same concept applies to all other static files like JavaScript and images.

Purging All Cache

For any reason, when you want to purge or discard the entire cache generated by RabbitLoader, you can empty the ‘cacheDir‘ path set in the Laravel config. The default path is /tmp/rabbitloader

You can use the File Browsing application provided by your hosting or run the bash command to empty the directory.

rm command /tmp/rabbitloader/*

If you have changed the config to use a different location for cache storage, use that path instead.

Note: Purging all cache after you change any page rule from the RabbitLoader console is important.