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

defer.php v2.0 #56

Merged
merged 22 commits into from
Mar 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated to defer.js v2
  • Loading branch information
shinsenter committed Feb 12, 2021
commit bb23d5702f8206f74f1add7c1b59581ce6206148
69 changes: 39 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,20 +2,26 @@

🚀 A PHP library that aims to help you concentrate on web performance optimization.

- **Package**: [@shinsenter/defer.php](https://www.npmjs.com/package/@shinsenter/defer.php)
- **Version**: 2.0
- **Author**: Mai Nhut Tan <[email protected]>
- **Copyright**: 2021 AppSeeds <https://code.shin.company/>
- **License**: [MIT](https://raw.githubusercontent.com/shinsenter/defer.php/master/LICENSE)

[![GitHub](https://img.shields.io/github/license/shinsenter/defer.php.svg)](https://github.com/shinsenter/defer.php)
[![GitHub Release Date](https://img.shields.io/github/release-date/shinsenter/defer.php.svg)](https://github.com/shinsenter/defer.php/releases)

[![CodeFactor](https://www.codefactor.io/repository/github/shinsenter/defer.php/badge)](https://www.codefactor.io/repository/github/shinsenter/defer.php)
[![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/shinsenter/defer.php.svg)](https://libraries.io/packagist/shinsenter%2Fdefer.php)
[![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/shinsenter/defer.php)](https://www.codefactor.io/repository/github/shinsenter/defer.php)
[![Post an issue](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/shinsenter/defer.php/issues)
[![GitHub issues](https://img.shields.io/github/issues-raw/shinsenter/defer.php.svg)](https://github.com/shinsenter/defer.php/issues/new)

* * *

🥇 Powered by [defer.js](https://github.com/shinsenter/defer.js) - A super small, super efficient library that helps you lazy load almost everything like images, video, audio, iframes as well as stylesheets, and JavaScript.

🔌 Powered by [defer.js](https://github.com/shinsenter/defer.js).
Defer almost anything, easily speed up your website. Easily speed up your website!
* * *


## TODO
## Features

- [x] Simplify library options
- [x] Embed defer.js library
@@ -39,19 +45,11 @@ Defer almost anything, easily speed up your website. Easily speed up your websit
- [x] Attribute to ignore lazyloading the element
- [x] Optimize AMP document
- [x] Minify HTML output
- [x] Update README.md


## Installation


### Backward compatible

This library recommends that the server is running PHP version 7.3 or greater for better performance and support.

Library's options of v2.x version are not backward compatible with previous release's options. Please read the library manual for more details.


### Install with composer

Install the latest version
@@ -79,6 +77,13 @@ require_once __DIR__ . '/vendor/autoload.php';
```


### Backward compatible

This library recommends that the server is running PHP version 7.3 or greater for better performance and support.

Library's options of v2.x version are not backward compatible with previous release's options. Please read the library manual for more details.


## Usages


@@ -127,7 +132,7 @@ $options = [
'manually_add_deferjs' => false,

// URL to defer.js javascript file.
// Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@1.1.15/dist/defer_plus.min.js
// Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.0.0/dist/defer_plus.min.js
'deferjs_src' => \AppSeeds\DeferConstant::SRC_DEFERJS_CDN,

// URL to javascript contains fixes.
@@ -350,31 +355,35 @@ Even when the library is turned off, above tags that contain `defer` attribute a
Only few options of this library are applicable to AMP pages (minifying HTML content for example).


## Plugin for WordPress
## My works

### Defer.js

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

🥇 A super small, super efficient library that helps you lazy load almost everything like images, video, audio, iframes as well as stylesheets, and JavaScript.

🚀 [defer-wordpress](https://wordpress.org/plugins/shins-pageload-magic/) is a WordPress plugin that focuses on minimizing payload size of HTML document and optimizing processing on the browser when rendering the WordPress page.

### Wordpress plugin

## Keep in touch
https://github.com/shinsenter/defer-wordpress/

- [Become a stargazer](https://github.com/shinsenter/defer.php/stargazers)
- [Report an issue](https://github.com/shinsenter/defer.php/issues/new)
- [Keep up-to-date with new releases](https://github.com/shinsenter/defer.php/releases)
⚡️ A native, blazing fast lazy loader. ✅ Legacy browsers support (IE9+). 💯 SEO friendly. 🧩 Lazy load almost anything.


[![Donate via Paypal](https://pics.paypal.com/00/s/NTQ4M2ZiN2YtZDg1My00ZmRiLWJiMDQtMTFlMjg2ODY2N2Uy/file.PNG)](https://www.paypal.me/shinsenter)
### Laravel package

[![Become a sponsor](https://c5.patreon.com/external/logo/[email protected])](https://www.patreon.com/appseeds)
Under development.


## Follow my defer.js project:
## Support my activities

- [https://github.com/shinsenter/defer.js/releases](https://github.com/shinsenter/defer.js/releases)
- [https://github.com/shinsenter/defer.js/stargazers](https://github.com/shinsenter/defer.js/stargazers)
[![Donate via Paypal](https://img.shields.io/badge/Donate-Paypal-blue)](https://www.paypal.me/shinsenter)
[![Become a sponsor](https://img.shields.io/badge/Donate-Patreon-orange)](https://www.patreon.com/appseeds)
[![Become a stargazer](https://img.shields.io/badge/Support-Stargazer-yellow)](https://github.com/shinsenter/defer.php/stargazers)
[![Report an issue](https://img.shields.io/badge/Support-Issues-red)](https://github.com/shinsenter/defer.php/issues/new)

---

Released under the MIT license.
[https://github.com/shinsenter/defer.php/blob/master/LICENSE](https://github.com/shinsenter/defer.php/blob/master/LICENSE)
* * *

Copyright (c) 2021 Mai Nhut Tan &lt;[[email protected]](mailto:[email protected])&gt;
From Vietnam 🇻🇳 with love.
9 changes: 4 additions & 5 deletions assets/helpers.js
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@
|--------------------------------------------------------------------------
*/

if(!window.defer) return;
if(!window.Defer) return;

// For defer.php
var PROJECT_NAME = "defer.php";
@@ -65,8 +65,7 @@
var FN_NOOP = Function();

var log = (console.log || FN_NOOP).bind(console);
var defer = window.defer;
var deferimg = window.deferimg || FN_NOOP;
var Defer = window.Defer;
var old_ready;

/*
@@ -167,7 +166,7 @@
*/

function lazyload(selector, options) {
deferimg(
Defer.dom(
selector,
helper.t,
helper.c,
@@ -259,7 +258,7 @@
old_ready = window[FN_JQUERY].fn.ready;

window[FN_JQUERY].fn.ready = function (fn) {
defer(function () {
Defer(function () {
old_ready(fn);
});

74 changes: 39 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
"author": "Mai Nhut Tan <[email protected]>",
"license": "MIT",
"dependencies": {
"@shinsenter/defer.js": "^1.1.15"
"@shinsenter/defer.js": "^2.0.0"
},
"devDependencies": {
"clean-css-cli": "latest",
13 changes: 13 additions & 0 deletions src/Helpers/DeferAssetUtil.php
Original file line number Diff line number Diff line change
@@ -106,13 +106,26 @@ public static function normalizeUrlOrigin($url)
*/
public static function isThirdParty($url, $lookup)
{
$host = '';

if (!empty(($_SERVER['HTTP_HOST']))) {
$host = '//' . $_SERVER['HTTP_HOST'];
}

if ($lookup instanceof DeferOptions) {
$lookup = $lookup->getWellKnown3rd();
} elseif (empty($lookup) || !is_array($lookup)) {
$lookup = DeferConstant::WELL_KNOWN_THIRDPARTY;
}

foreach ($lookup as $pattern) {
// If server origin esists in
// third-party list, always returns false
if (strstr($host, $pattern) !== false) {
return false;
}

// Else check the URL if it is a third-party
if (strstr($url, $pattern) !== false) {
return true;
}
11 changes: 6 additions & 5 deletions src/Helpers/DeferConstant.php
Original file line number Diff line number Diff line change
@@ -76,6 +76,7 @@ class DeferConstant
'data-lazy',
'data-lazy-src',
'data-original',
'data-img-url',
];

// Unify the srcset attribute from other related attributes
@@ -131,7 +132,7 @@ class DeferConstant
// -------------------------------------------------------------------------

// Source files
const SRC_DEFERJS_CDN = 'https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@1.1.15/dist/defer_plus.min.js';
const SRC_DEFERJS_CDN = 'https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.0.0/dist/defer_plus.min.js';
const SRC_POLYFILL_CDN = 'https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver';
const SRC_DEFERJS_FALLBACK = DEFER_PHP_ROOT . '/node_modules/@shinsenter/defer.js/dist/defer_plus.min.js';
const SCR_DEFERJS_CACHE = DEFER_PHP_ROOT . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
@@ -153,8 +154,11 @@ class DeferConstant
// Some well-known third-party patterns
// See: https://gist.github.com/lukecav/9931c3f6e402e23f58065d6b2665ef5b
const WELL_KNOWN_THIRDPARTY = [
'.addthis.com',
'.ampproject.org',
'.bootstrapcdn.com',
'.disqus.com',
'.doubleclick.net',
'.fontawesome.com',
'.google-analytics.com',
'.googlesyndication.com',
@@ -166,7 +170,6 @@ class DeferConstant
'.twimg.com',
'.wp.com',
'.youtube.com',
'//adservice.google.',
'//api.pinterest.com',
'//apis.google.com',
'//cdnjs.cloudflare.com',
@@ -183,10 +186,8 @@ class DeferConstant
'//platform.twitter.com',
'//s.w.org',
'//s.yimg.',
'//s7.addthis.com',
'//securepubads.g.doubleclick.net',
'//sitename.disqus.com',
'//syndication.twitter.com',
'//youtube.com',
'adservice.google.',
];
}
2 changes: 1 addition & 1 deletion src/Helpers/DeferOptions.php
Original file line number Diff line number Diff line change
@@ -214,7 +214,7 @@ public function getWellKnown3rd($useCache = true)
* 'manually_add_deferjs' => false,
*
* // URL to defer.js javascript file.
* // Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@1.1.15/dist/defer_plus.min.js
* // Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.0.0/dist/defer_plus.min.js
* 'deferjs_src' => \AppSeeds\DeferConstant::SRC_DEFERJS_CDN,
*
* // URL to javascript contains fixes.