Skip to content
πŸš€ A PHP library that focuses on minimizing payload size of HTML document and optimizing page speed.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets Code refactoring Apr 18, 2019
cache Add defer.php library Mar 19, 2019
public Code refactoring Apr 18, 2019
src Fixed bug: defer function not found May 17, 2019
test Fixed #30 Apr 25, 2019
.gitattributes Add defer background images Mar 24, 2019
.gitignore Add defer.php library Mar 19, 2019
.php_cs Add defer.php library Mar 19, 2019
.uglifyjs Add defer background images Mar 24, 2019
LICENSE Initial commit Feb 18, 2019
README.md Fixed #30 Apr 25, 2019
composer.json Removed upper limit for symfony/http-foundation Apr 7, 2019
composer.lock Update library Apr 18, 2019
defer.php Fixed issues #30, #36, #36 Apr 23, 2019
sonar-project.properties Add defer.php library Mar 19, 2019

README.md

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;

// 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;

// 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);

defer-wordpress

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 <shin@shin.company>

You can’t perform that action at this time.