Skip to content

Commit 2ea00c4

Browse files
authored
Update docs (#110)
1 parent 567b8c1 commit 2ea00c4

11 files changed

+231
-60
lines changed

README.md

+44-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![NPM](https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true)](https://www.npmjs.com/package/@shinsenter/defer.js)
1717

1818
- **Package**: [@shinsenter/defer.js](https://www.npmjs.com/package/@shinsenter/defer.js)
19-
- **Version**: 3.0.0
19+
- **Version**: 3.1.0
2020
- **Author**: Mai Nhut Tan <[email protected]>
2121
- **Copyright**: 2022 AppSeeds <https://code.shin.company/>
2222
- **License**: [MIT](https://code.shin.company/defer.js/blob/master/LICENSE)
@@ -80,7 +80,7 @@ Just put a `<script>` tag pointing to the library URL just below the opening `<h
8080
<title>My Awesome Page</title>
8181

8282
<!-- Put defer.min.js here -->
83-
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/defer.min.js"></script>
83+
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/defer.min.js"></script>
8484

8585
<!-- ... -->
8686
</head>
@@ -96,7 +96,7 @@ Because `defer.min.js` is optimized to very tiny file size, you can even inline
9696
<title>My Awesome Page</title>
9797

9898
<!-- Copy the script from below URL -->
99-
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/defer.min.js -->
99+
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/defer.min.js -->
100100
<script>/* then replace this comment block with the content of defer.min.js */</script>
101101

102102
<!-- ... -->
@@ -114,7 +114,7 @@ just use `defer_plus.min.js` instead of `defer.min.js`.
114114
<title>My Awesome Page</title>
115115

116116
<!-- Put defer_plus.min.js here -->
117-
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/defer_plus.min.js"></script>
117+
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/defer_plus.min.js"></script>
118118

119119
<!-- ... -->
120120
</head>
@@ -132,7 +132,7 @@ right after the `defer.min.js` script tag as following example:
132132

133133
<!-- If legacy browsers like Internet Explorer 9 still need to be supported -->
134134
<!-- Please put IntersectionObserver polyfill right after defer.js script tag -->
135-
<script>'IntersectionObserver'in window||document.write('<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/polyfill.min.js"><\/script>');</script>
135+
<script>'IntersectionObserver'in window||document.write('<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/polyfill.min.js"><\/script>');</script>
136136
```
137137

138138
*HINT*: Modern browsers support `IntersectionObserver` feature,
@@ -195,7 +195,7 @@ as soon as the page finished loading.
195195
</script>
196196
```
197197
**Example**
198-
Sometimes, you would like your code not to run unless there is user activity.
198+
Sometimes, you would like your code not to run unless there is a user activity.
199199

200200
The third argument tells the `Defer()` to delay the execution of the function
201201
and wait until the user starts interacting with your page.
@@ -236,7 +236,7 @@ and wait until the user starts interacting with your page.
236236
### Defer.lazy : <code>boolean</code>
237237
The `Defer.lazy` variable was added since v3.0.
238238

239-
Setting `Defer.lazy = true` tells the library to delay the execution
239+
Setting `Defer.lazy=true` tells the library to delay the execution
240240
of deferred scripts until the user starts interacting with the page
241241
regardless of the page load event.
242242

@@ -274,7 +274,8 @@ You can fully defer any script tag by setting its `type` attribute to `deferjs`.
274274
This trick also works perfectly with `<script>` tags with a `src` attribute.
275275

276276
**Kind**: static method of [<code>Defer</code>](#Defer)
277-
**Note**: Lazy loading behavior changed since v3.0 when you set `Defer.lazy=true`.
277+
**Note**: Lazy loading behavior changed since v3.0
278+
when you set `Defer.lazy=true` or `waitForInteraction=true`.
278279
A `<script>` tags with `type="deferjs"` will not execute
279280
unless the user starts interacting with your page.
280281
**Since**: 2.0
@@ -325,6 +326,31 @@ If you hate using the `type="deferjs"` attribute, you can even choose your own o
325326
Defer.all('script[type="my-magic"]', 5000);
326327
</script>
327328
```
329+
**Example**
330+
Using `Defer.all()` with script tags with `src` attribute:
331+
332+
Your scripts will work perfectly when you mix inline scripts
333+
and script tags with a src attribute, like the below example.
334+
335+
The `waitForInteraction` argument (the fifth argument) is set to `true`,
336+
the library will defer the load of the tippy.js library until user starts
337+
interacting, when user moves his/her mouse on the button, a tooltip wil show.
338+
339+
340+
```html
341+
<button id="tooltip-button">My button</button>
342+
343+
<script type="myscript" src="https://unpkg.com/@popperjs/core@2"></script>
344+
<script type="myscript" src="https://unpkg.com/tippy.js@6"></script>
345+
346+
<script type="myscript">
347+
tippy('#tooltip-button', { content: 'Hello from Defer.js!' });
348+
</script>
349+
350+
<script>
351+
Defer.all('script[type="myscript"]', 0, true);
352+
</script>
353+
```
328354

329355
* * *
330356

@@ -652,7 +678,8 @@ We use the `Defer.css()` method to defer the load
652678
of external CSS files without blocking the page rendering.
653679

654680
**Kind**: static method of [<code>Defer</code>](#Defer)
655-
**Note**: Lazy loading behavior changed since v3.0 when you set `Defer.lazy=true` or `waitForInteraction=true`.
681+
**Note**: Lazy loading behavior changed since v3.0
682+
when you set `Defer.lazy=true` or `waitForInteraction=true`.
656683
The `fileUrl` will not be fetched unless the user starts interacting with your page.
657684
**Since**: 2.0
658685

@@ -726,7 +753,12 @@ We use `Defer.js()` to defer the load of 3rd-party
726753
javascript libraries, widgets, add-ons, etc. without blocking the page rendering.
727754

728755
**Kind**: static method of [<code>Defer</code>](#Defer)
729-
**Note**: Lazy loading behavior changed since v3.0 when you set `Defer.lazy=true` or `waitForInteraction=true`.
756+
**Note**: Because the download of file using `Defer.js()` function is asynchronous,
757+
to avoid dependency error when lazy loading a third-party library using `Defer.js()`,
758+
it is highly recommended that the `onload` callback function be used
759+
to make sure that the library you needed is completely defined.
760+
**Note**: Lazy loading behavior changed since v3.0
761+
when you set `Defer.lazy=true` or `waitForInteraction=true`.
730762
The `fileUrl` will not be fetched unless the user starts interacting with your page.
731763
**Since**: 2.0
732764

@@ -783,7 +815,8 @@ AddThis add-on will not be loaded until the user starts interacting with the pag
783815
Lazy load Prism.js library.
784816

785817
Using Defer.js to lazy load Prism.js library and its assets.
786-
The `<code>` blocks on the page will be rendered only when you scroll to their positions.
818+
The `<code>` blocks on the page will be rendered
819+
only when the user scrolls to any `code` block position.
787820

788821
```html
789822
<style>

demo/index.html

+42-8
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ <h3>Quick installation:</h3>
4242
<div class="helper">
4343
Just put a <code>&lt;script&gt;</code> tag pointing to
4444
<a target="_blank" title="defer.min.js"
45-
href="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/defer.min.js">the library URL</a>
45+
href="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/defer.min.js">the library URL</a>
4646
just below the opening <code>&lt;head&gt;</code> tag of your page.
4747
Because <code>defer.min.js</code> is optimized to very tiny file size,
4848
you can even inline entire the library to save one HTTP request.
4949
</div>
5050
<div class="demo">
5151
<!-- Copy the script from below URL -->
52-
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/defer.min.js -->
52+
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/defer.min.js -->
5353
<script>/* then replace this comment block with the content of defer.min.js here */</script>
5454

5555
<!-- If legacy browsers like Internet Explorer 9 still need to be supported -->
56-
<script>'IntersectionObserver'in window||document.write('<script defer src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0.0/dist/polyfill.min.js"><\/script>');</script>
56+
<script>'IntersectionObserver'in window||document.write('<script defer src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1.0/dist/polyfill.min.js"><\/script>');</script>
5757
</div>
5858
</div>
5959
</section>
@@ -96,8 +96,11 @@ <h3>Using your own type, such as <mark><code>type="my-magic"</code></mark>:</h3>
9696
If you hate using the <mark><code>type="deferjs"</code></mark> attribute,
9797
you can even choose your own one.
9898
<br>
99+
<code>Defer.all(cssSelector, [delay], [waitForInteraction])</code>
100+
<br>
99101
<strong>Note: </strong>
100-
Lazy loading behavior changed since v3.0 when you set <code>Defer.lazy=true</code>. <br>
102+
Lazy loading behavior changed since v3.0
103+
when you set <code>Defer.lazy=true</code> or <code>waitForInteraction=true</code>. <br>
101104
<code>&lt;script&gt;</code> tags deferred with this technique
102105
will not execute unless the user starts interacting with your page.
103106
</div>
@@ -115,6 +118,29 @@ <h3>Using your own type, such as <mark><code>type="my-magic"</code></mark>:</h3>
115118
<!-- The 2nd argument means those script tag will be delayed 5000ms -->
116119
<script>
117120
Defer.all('script[type="my-magic"]', 5000);
121+
</script>
122+
</div>
123+
</div>
124+
125+
<div class="example">
126+
<h3>Using <code>Defer.all</code> with script tags with <code>src</code> attribute:</h3>
127+
<div class="helper">
128+
Your scripts will work perfectly when mixing inline scripts
129+
and script tags with a <code>src</code> attribute,
130+
like the below example.
131+
<br>
132+
The library will defer the load of tippy.js until user starts interacting,
133+
when user moves his/her mouse on the button, a tooltip will show.
134+
</div>
135+
<div class="demo">
136+
<button id="tooltip-button">My button</button>
137+
<script type="myscript" src="https://unpkg.com/@popperjs/core@2"></script>
138+
<script type="myscript" src="https://unpkg.com/tippy.js@6"></script>
139+
<script type="myscript">
140+
tippy('#tooltip-button', { content: 'Hello from Defer.js!' });
141+
</script>
142+
<script>
143+
Defer.all('script[type="myscript"]', 0, true);
118144
</script>
119145
</div>
120146
</div>
@@ -191,7 +217,7 @@ <h3>Wait until the user starts interacting:</h3>
191217
<div class="example">
192218
<h3>The variable <code>Defer.lazy</code></h3>
193219
<div class="helper">
194-
Setting <code>Defer.lazy = true</code> tells the library
220+
Setting <code>Defer.lazy=true</code> tells the library
195221
to delay the execution of deferred scripts when the user
196222
starts interacting with the page regardless of the page load event.
197223
<br>
@@ -542,7 +568,8 @@ <h2><code>Defer.css(fileUrl, [id], [delay], [onload], [waitForInteraction])</cod
542568
We use <code>Defer.css</code> to defer the load of external CSS files without blocking the page rendering.
543569
<br>
544570
<strong>Note: </strong>
545-
Lazy loading behavior changed since v3.0 when you set <code>Defer.lazy=true</code> or <code>waitForInteraction=true</code>. <br>
571+
Lazy loading behavior changed since v3.0
572+
when you set <code>Defer.lazy=true</code> or <code>waitForInteraction=true</code>. <br>
546573
The <code>fileUrl</code> will not be fetched unless the user starts interacting with your page.
547574
</div>
548575

@@ -607,8 +634,15 @@ <h2><code>Defer.js(fileUrl, [id], [delay], [onload], [waitForInteraction])</code
607634
<div class="helper">
608635
We use <code>Defer.js</code> to defer the load of 3rd-party javascript libraries, add-ons etc. without blocking the page rendering.
609636
<br>
610-
<strong>Note: </strong>
611-
Lazy loading behavior changed since v3.0 when you set <code>Defer.lazy=true</code> or <code>waitForInteraction=true</code>. <br>
637+
<strong>Note 1: </strong>
638+
Because the download of file using <code>Defer.js</code> function is asynchronous,
639+
to avoid dependency error when lazy loading a library using <code>Defer.js</code>,
640+
it is highly recommended that the <code>onload</code> callback function be used to make sure that
641+
the third-party library you needed is completely defined.
642+
<br>
643+
<strong>Note 2: </strong>
644+
Lazy loading behavior changed since v3.0
645+
when you set <code>Defer.lazy=true</code> or <code>waitForInteraction=true</code>. <br>
612646
The <code>fileUrl</code> will not be fetched unless the user starts interacting with your page.
613647
</div>
614648

dist/defer.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/defer_plus.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)