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

Commit ff9b9a3

Browse files
authored
Merge pull request #39 from shinsenter/develop
Fixed #30
2 parents a708237 + e8ec614 commit ff9b9a3

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ $defer->empty_src = '';
120120
$defer->use_color_placeholder = true;
121121
$defer->use_css_fadein_effects = true;
122122

123-
// Manually add deferjs library script tag
124-
// $defer->manually_add_deferjs = true;
125-
126123
// Blacklist
127124
$defer->do_not_optimize = [
128125
'document\.write\s*\(',
@@ -136,6 +133,25 @@ var_dump($result);
136133

137134

138135

136+
### Manually add defer.js library script tag
137+
138+
In some regions, you may want to serve defer.js library locally due to [The General Data Protection Regulation (EU)](https://en.wikipedia.org/wiki/General_Data_Protection_Regulation).
139+
140+
```php
141+
// Include the library
142+
require_once __DIR__ . '/vendor/shinsenter/defer.php/defer.php';
143+
144+
// Create a Defer object
145+
$defer = new \shinsenter\Defer();
146+
$defer->manually_add_deferjs = true;
147+
148+
// Then get the optimized output
149+
$result = $defer->fromHtml(file_get_contents('mypage.html'))->toHtml();
150+
var_dump($result);
151+
```
152+
153+
154+
139155
## [defer-wordpress](https://github.com/shinsenter/defer-wordpress/releases)
140156

141157
WordPress remains one of the most popular CMS platform until now. This is a WordPress plugin. Hope you guys like it.
@@ -144,6 +160,8 @@ WordPress remains one of the most popular CMS platform until now. This is a Word
144160

145161
## Keep in touch
146162

163+
[![Donate via Paypal](https://pics.paypal.com/00/s/NTQ4M2ZiN2YtZDg1My00ZmRiLWJiMDQtMTFlMjg2ODY2N2Uy/file.PNG)](https://www.paypal.me/shinsenter)
164+
147165
[![Become a sponsor](https://c5.patreon.com/external/logo/[email protected])](https://www.patreon.com/appseeds)
148166

149167

src/DeferOptimizer.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ protected function optimize()
6868
*/
6969
protected function initLoaderJs()
7070
{
71-
$cleanup = '//script[@id="defer-js" or @id="defer-script" or @id="polyfill-js"]|//style[@id="defer-css"]';
71+
if (!$this->manually_add_deferjs) {
72+
$cleanup = '//script[@id="defer-js" or @id="defer-script" or @id="polyfill-js"]|//style[@id="defer-css"]';
7273

73-
foreach ($this->xpath->query($cleanup) as $node) {
74-
$this->removeNode($node);
74+
foreach ($this->xpath->query($cleanup) as $node) {
75+
$this->removeNode($node);
76+
}
7577
}
7678

7779
$cache = $this->cache_manager;
@@ -140,19 +142,19 @@ protected function addDeferJs()
140142
static::ATTR_ID => 'defer-js',
141143
]);
142144

143-
$this->head->insertBefore($script_tag, $the_anchor);
145+
$this->head->appendChild($script_tag);
144146
$script_tag = null;
145147
}
146148

147149
$extra_scripts[] = static::$deferjs_script;
148150
$extra_scripts[] = '"IntersectionObserver"in window||deferscript("' . static::POLYFILL_URL . '","polyfill-js",1)';
149151
} else {
150152
$message = implode('\n', [
151-
'You should manually add the defer.js.\n\nFor example:',
152-
'<script id="defer-js" src="' . static::DEFERJS_URL . '"><\/script>',
153-
'<script id="polyfill-js" src="' . static::POLYFILL_URL . '"><\/script>',
153+
'You should manually add the defer.js.\n\nLike this:',
154+
'<script data-ignore id="defer-js" src="' . static::DEFERJS_URL . '"><\/script>',
155+
'<script data-ignore id="polyfill-js" src="' . static::POLYFILL_URL . '"><\/script>',
154156
]);
155-
$extra_scripts[] = "window.defer=setTimeout;defer(function(){console.info('${message}')});";
157+
$extra_scripts[] = "if(!'defer'in window){window.defer=setTimeout;defer(function(){console.info('${message}')})}";
156158
}
157159

158160
// Append helpers
@@ -165,7 +167,7 @@ protected function addDeferJs()
165167
if (!empty($script)) {
166168
$script_tag = $this->createNode(static::SCRIPT_TAG, trim($script), [static::ATTR_ID => 'defer-script']);
167169

168-
$this->head->insertBefore($script_tag, $the_anchor);
170+
$this->head->appendChild($script_tag);
169171
$script_tag = null;
170172
}
171173

test/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
$defer->use_color_placeholder = false;
5656

5757
// Manually add deferjs library script tag
58-
// $defer->manually_add_deferjs = true;
58+
// $defer->manually_add_deferjs = true;
5959

6060
$defer->clearCache();
6161

0 commit comments

Comments
 (0)