Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

shinsenter/defer.php

Repository files navigation

defer.php

πŸš€ A PHP library that focuses on minimizing payload size of HTML document and optimizing processing on the browser when rendering the web page.

GitHub GitHub Release Date

CodeFactor Libraries.io dependency status for GitHub repo Post an issue GitHub issues

πŸ”Œ defer.js is super tiny, native performant library for lazy-loading JS, CSS, images, iframes...

Defer almost anything, easily speed up your website. Easily speed up your website! Hope you guys like it.

How to use

Install with composer

composer require shinsenter/defer.php

Basic usage

// Include the library
require_once __DIR__ . '/vendor/shinsenter/defer.php/defer.php';

// Create a Defer object
$defer = new \shinsenter\Defer();

// Read HTML source from file
$html_source = file_get_contents('mypage.html');

// Then get the optimized output
$result = $defer->fromHtml($html_source)->toHtml($html);
var_dump($result);

// Load another HTML without creating new object
// You can write all methods in one line like this
$result = $defer->fromHtml(file_get_contents('otherpage.html'))->toHtml();
var_dump($result);

Optimize output HTML for your website

You can capture the output that is generated by PHP, and optimize the output with defer.php, like this:

// Include the library
require_once __DIR__ . '/vendor/shinsenter/defer.php/defer.php';

// Create a function for output optimization
function deferjs_optimization($html) {
    // Create a Defer object
    $defer = new \shinsenter\Defer();

    // Then get the optimized output
    return $defer->fromHtml($html)->toHtml();
}

// Turn output buffering on with ob_start
// Run this before HTML rendering
ob_start('deferjs_optimization');

// .......... (other PHP stuffs here)

// And call this to flush optimized output HTML
// right before you send the HTML to browser
ob_end_flush();

Library's options

// Include the library
require_once __DIR__ . '/vendor/shinsenter/defer.php/defer.php';

// Create a Defer object
$defer = new \shinsenter\Defer();

// Library injection
$defer->append_defer_js     = false;
$defer->default_defer_time  = 10;

// Add custom splash screen
$defer->custom_splash_screen = '<div id="splash">Loading</div>';

// Page optimizations
$defer->enable_preloading   = true;
$defer->enable_dns_prefetch = true;
$defer->fix_render_blocking = true;
$defer->minify_output_html  = true;

// Tag optimizations
$defer->enable_defer_css        = true;
$defer->enable_defer_scripts    = true;
$defer->enable_defer_images     = true;
$defer->enable_defer_iframes    = true;
$defer->enable_defer_background = true;
$defer->enable_defer_fallback   = true;

// Web-font optimizations
$defer->defer_web_fonts = true;

// Image and iframe placeholders
$defer->empty_gif               = '';
$defer->empty_src               = '';
$defer->use_color_placeholder   = true;
$defer->use_css_fadein_effects  = true; // true or 'grey'

// Blacklist
$defer->do_not_optimize = [
    'document\.write\s*\(',
    '(jquery([-_][\d\.]+)?(\.min)?\.js|jquery-core)',
];

// Then get the optimized output
$result = $defer->fromHtml(file_get_contents('mypage.html'))->toHtml();
var_dump($result);

Manually add defer.js library script tag

In some regions, you may want to serve defer.js library locally due to The General Data Protection Regulation (EU).

// Include the library
require_once __DIR__ . '/vendor/shinsenter/defer.php/defer.php';

// Create a Defer object
$defer = new \shinsenter\Defer();
$defer->manually_add_deferjs = true;

// Then get the optimized output
$result = $defer->fromHtml(file_get_contents('mypage.html'))->toHtml();
var_dump($result);

WordPress remains one of the most popular CMS platform until now. This is a WordPress plugin. Hope you guys like it.

Keep in touch

Donate via Paypal

Become a sponsor

Follow my defer.js project:

https://github.com/shinsenter/defer.js/releases

https://github.com/shinsenter/defer.js/stargazers


Released under the MIT license. https://appseeds.net/defer.php/LICENSE

Copyright (c) 2019 Mai Nhut Tan <[email protected]>