img-1

Setting up RabbitLoader on Custom-PHP Website

RabbitLoader can be used to improve PageSpeed Insights and Core Web Vitals for any custom website or in-house developed website that uses PHP. Though SDK supports v5.6 or above, it is recommended to use the latest version of PHP for security and performance reasons.

Installing the SDK

The RabbitLoader PHP SDK is available via Composer and it is pretty easy to install. Your website must be running a PHP version higher than 5.6. To install the SDK, run the below command-

composer require rabbit-loader/php-sdk

After the composer package is installed, we need to make some changes to the index.php (entry file for your website).

#1 make sure the composer autolaod file is loaded at the top
require_once("/vendor/autoload.php");


#2 integrate RabbitLoader
$licenseKey = 'YOUR_LICENSE_KEY'; //get your license key from environment variable
$storageDir = '/home/usr/cache-disk/rabbitloader'; //full path storage location where cached files will be stored
$rlSDK = new RabbitLoader\SDK\RabbitLoader($licenseKey, $storageDir);
$rlSDK->process();

#3 important headers
header('Content-Type: text/html; charset=utf-8');

#4 remaining code of the website goes after this ...
echo "<h1>Hello World!</h1>"

Before moving next, make sure after this code change, there are no errors. You should also ensure-

  • The Composer’s autoload.php is loaded before initializing RabbitLoader
  • The path provided in the $storageDir has the write access by the PHP script
  • The function $rlSDK->process() should be called before any echo or print statement
  • A valid Content-Type header is required for SDK to work

Adding the License Key

The SDK 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 “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.

It is recommended to store the license key in an environment file for secure retrieval and to prevent leakage.

Validate Image Optimization

RabbitLoader requires a full image path to optimize, convert, and serve images through CDN. Relative image paths are currently not supported. You should also validate an additional checklist for image optimization.

Advance Usage

This section covers some advanced usage examples including the integration with your website’s Admin Panel.

Purging the Cache

The cache can be purged either by deleting the content of the directory path provided via $storageDir variable when initializing SDK. But a more standard way will be to do it using the SDK itself. If you have an admin panel from where you control the content or other updates to the website, you can automate the purging of cache using the below example-

#admin.php
#load composer autoload.php and initialize PHP SDK.

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

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

Replace the URLs dynamically with your application flow.

Excluding Pages from RabbitLader

There are a few ways you can control the page optimization for different sections of the website. For example, completely excluding the admin and user accounts section from RabbitLoader. Apply CSS, JS, and Image optimizations differently to each section.

Using Page Rules to Control Optimization

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 SDK to Exclude Pages

The SDK also has multiple ways to exclude specific pages from being optimized and cached by RabbitLoader. Check the below code samples-

//skip caching if path starts with admin
$rlSDK->skipForPaths(['/admin*']);

//skip caching if a cookie is found with name cookie1 
$rlSDK->skipForCookies(['cookie1']);

//all the above options should come before the process() call
$rlSDK->process();