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

Commit 8b7207d

Browse files
committed
Minimized script and css
1 parent d29436a commit 8b7207d

File tree

13 files changed

+53
-95
lines changed

13 files changed

+53
-95
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
🚀 A PHP library that aims to help you concentrate on web performance optimization.
44

55
- **Package**: [@shinsenter/defer.php](https://www.npmjs.com/package/@shinsenter/defer.php)
6-
- **Version**: 2.0
6+
- **Version**: 2.3.0
77
- **Author**: Mai Nhut Tan <[email protected]>
88
- **Copyright**: 2021 AppSeeds <https://code.shin.company/>
99
- **License**: [MIT](https://raw.githubusercontent.com/shinsenter/defer.php/master/LICENSE)
@@ -125,7 +125,7 @@ $options = [
125125
'manually_add_deferjs' => false,
126126

127127
// URL to defer.js javascript file.
128-
// Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.3.0/dist/defer_plus.min.js
128+
// Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.4.0/dist/defer_plus.min.js
129129
'deferjs_src' => \AppSeeds\DeferConstant::SRC_DEFERJS_CDN,
130130

131131
// URL to javascript contains fixes.

assets/helpers.js

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,11 @@
3737
|--------------------------------------------------------------------------
3838
*/
3939

40-
// Backup jQuery.ready
41-
var _jqueryReady;
42-
4340
// Common texts
4441
var _dataLayer = 'dataLayer';
45-
var _deferClass = 'deferjs';
46-
var _deferPrefix = 'defer-';
47-
var _lazied = 'lazied';
48-
var _dataPrefix = 'data-';
49-
var _media = 'media';
50-
51-
// Common class names
52-
var _classLazied = _deferPrefix + _lazied;
53-
var _classLoaded = _deferPrefix + 'loaded';
54-
var _classLoading = _deferPrefix + 'loading';
55-
56-
// Common attributes
57-
var _attrClassName = 'className';
58-
var _attrDataIgnore = 'data-ignore';
5942

6043
// Common CSS selectors
61-
var _queryTarget = '.' + _classLoading + ':not([' + _attrDataIgnore + ']):not([lazied])';
44+
var _queryTarget = '.defer-loading:not([data-ignore]):not([lazied])';
6245

6346
/*
6447
|--------------------------------------------------------------------------
@@ -77,49 +60,10 @@
7760
*/
7861

7962
function _replaceClass(node, find, replace) {
80-
node[_attrClassName] = (' ' + node[_attrClassName] + ' ').
81-
replace(' ' + find + ' ', ' ' + replace + ' ').trim();
82-
}
83-
84-
function _lazyload() {
85-
defer.dom(_queryTarget, 0, _classLazied, function (node) {
86-
_replaceClass(node, _classLoading, _classLoaded);
87-
}, _options);
88-
89-
[].slice.call(document.querySelectorAll('style[defer]')).
90-
forEach(function(node) {
91-
node[_media] = node.getAttribute(_dataPrefix + _media) || 'all';
92-
});
63+
node.className = ((' ' + node.className + ' ').
64+
replace(' ' + find + ' ', ' ') + replace).trim();
9365
}
9466

95-
function _copyright(_copyText) {
96-
if (console.log) {
97-
console.log(_copyText || [
98-
'Optimized by defer.php',
99-
'(c) 2021 AppSeeds',
100-
'Github: https://code.shin.company/defer.php'
101-
].join('\n'));
102-
}
103-
}
104-
105-
function _boot() {
106-
defer(_lazyload, _delay);
107-
_replaceClass(document.documentElement, 'no-' + _deferClass, _deferClass);
108-
_copyright();
109-
}
110-
111-
/*
112-
|--------------------------------------------------------------------------
113-
| Define helper object
114-
|--------------------------------------------------------------------------
115-
*/
116-
117-
// Check if missing defer feature
118-
if (!defer) {return;}
119-
120-
// Fallback for older versions
121-
window.defer_helper = {'defermedia': _lazyload};
122-
12367
/*
12468
|--------------------------------------------------------------------------
12569
| Fallback for external libraries
@@ -131,29 +75,47 @@
13175
window.ga = window.ga || function () {(window.ga.q = window.ga.q || []).push(arguments)}; window.ga.l = Number(Date());
13276
window[_dataLayer] = window[_dataLayer] || [];
13377

134-
// Fake jQuery.ready, if jQuery loaded
135-
defer(function (jquery) {
136-
if (_jqueryReady) {
137-
return;
138-
}
78+
/*
79+
|--------------------------------------------------------------------------
80+
| Define helper object
81+
|--------------------------------------------------------------------------
82+
*/
13983

140-
jquery = window.jQuery;
84+
_replaceClass(
85+
document.documentElement,
86+
'no-deferjs',
87+
defer ? 'deferjs' : ''
88+
);
14189

142-
if (jquery && jquery.fn) {
143-
_jqueryReady = jquery.fn.ready;
144-
jquery.fn.ready = function (callback) {
145-
defer(function () {_jqueryReady(callback)}, _delay);
146-
}
147-
}
148-
});
90+
// Check if missing defer feature
91+
if (!defer) {
92+
return;
93+
}
14994

15095
/*
15196
|--------------------------------------------------------------------------
15297
| Main
15398
|--------------------------------------------------------------------------
15499
*/
155100

156-
157-
_boot();
101+
// Lazyload all style tags
102+
defer(function() {
103+
[].slice.call(document.querySelectorAll('style[defer]')).
104+
forEach(defer.reveal);
105+
}, _delay);
106+
107+
// Lazyload all media
108+
defer.dom(_queryTarget, _delay, 0, function (node) {
109+
_replaceClass(node, 'defer-loading', 'defer-loaded');
110+
}, _options);
111+
112+
// Copyright
113+
if (console.log) {
114+
console.log([
115+
'Optimized by defer.php',
116+
'(c) 2021 AppSeeds',
117+
'Github: https://code.shin.company/defer.php'
118+
].join('\n'));
119+
}
158120

159121
})(this, document, console);

assets/styles.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ embed {
2020
}
2121

2222
.defer-faded [lazied]{
23-
transition: opacity 0.3s;
23+
transition: opacity 0.2s;
2424
}
2525

26-
.defer-faded .defer-loading:not([data-ignore]) {
27-
opacity: 0.1!important;
26+
.defer-faded .defer-loading {
27+
opacity: 0.5!important;
2828
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": "Mai Nhut Tan <[email protected]>",
44
"license": "MIT",
55
"dependencies": {
6-
"@shinsenter/defer.js": "^2.3.0"
6+
"@shinsenter/defer.js": "^2.4.0"
77
},
88
"scripts": {
99
"cleanup": "rm -rf ./node_modules package-lock.json",

public/helpers.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/styles.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Helpers/DeferConstant.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class DeferConstant
152152
// -------------------------------------------------------------------------
153153

154154
// Source files
155-
const SRC_DEFERJS_CDN = 'https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.3.0/dist/defer_plus.min.js';
155+
const SRC_DEFERJS_CDN = 'https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.4.0/dist/defer_plus.min.js';
156156
const SRC_POLYFILL_CDN = 'https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver';
157157
const SRC_DEFERJS_FALLBACK = DEFER_PHP_ROOT . '/public/lib/defer_plus.min.js';
158158
const SCR_DEFERJS_CACHE = DEFER_PHP_ROOT . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;

src/Helpers/DeferOptimizer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,6 @@ public static function optimizeElement(ElementNode &$node, DeferOptions &$option
238238
$lazied = $resolver->lazyload();
239239

240240
if ($lazied) {
241-
$node->addClass(DeferConstant::CLASS_DEFER_LOADING);
242-
243241
if (!empty($fallback)) {
244242
$fallback->detach();
245243
$node->follow($fallback);

src/Helpers/DeferOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function getWellKnown3rd($useCache = true)
241241
* 'manually_add_deferjs' => false,
242242
*
243243
* // URL to defer.js javascript file.
244-
* // Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.3.0/dist/defer_plus.min.js
244+
* // Default: https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@2.4.0/dist/defer_plus.min.js
245245
* 'deferjs_src' => \AppSeeds\DeferConstant::SRC_DEFERJS_CDN,
246246
*
247247
* // URL to javascript contains fixes.

src/Resolvers/DeferResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ public function createDataAttr($attr, $placeholder = '')
359359
if ($placeholder != $value) {
360360
$this->node->setAttribute($attr, $placeholder);
361361
$this->node->setAttribute($data_attr, $value);
362+
$this->node->addClass(DeferConstant::CLASS_DEFER_LOADING);
362363
}
363364

364365
return $this;

src/Resolvers/IframeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function normalize()
4848
}
4949

5050
if (empty($this->node->getAttribute('title'))) {
51-
$this->node->setAttribute('title', basename($src ?: ''));
51+
$this->node->setAttribute('title', basename($src ?: 'blank'));
5252
}
5353

5454
// Browser-level image lazy-loading for the web

src/Resolvers/StyleResolver.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ public function lazyload()
119119
// Only defer when it is a CSS node
120120
// and "onload" attribute is not provided
121121
$media = $this->node->getAttribute('media');
122-
123-
if (!empty($media)) {
124-
$this->node->setAttribute('data-media', $media);
125-
}
122+
$this->node->setAttribute('data-media', $media ?: 'all');
126123

127124
// Lazyload the style
128125
$this->node->setAttribute('media', DeferConstant::TEMPLATE_LAZY_MEDIA_ATTR);

tests/v2/test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
require_once BASE . DS . 'helpers.php';
2929

3030
// Test request arguments
31-
$_REQUEST['nodefer'] = 0;
32-
$_REQUEST['debug'] = 0;
33-
$_REQUEST['debug_time'] = 1;
34-
$_REQUEST['minify_output_html'] = 1;
31+
$_REQUEST['nodefer'] = 0;
32+
$_REQUEST['debug'] = 0;
33+
$_REQUEST['debug_time'] = 1;
3534

3635
// New instance
3736
$defer = new AppSeeds\Defer([
@@ -79,6 +78,7 @@
7978
'.header_top_icon_list img',
8079
'.header_logo img',
8180
'.banner img',
81+
'.logo',
8282
],
8383
]);
8484

0 commit comments

Comments
 (0)