Skip to content

Commit

Permalink
For v3.3.0 (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinsenter authored Sep 5, 2022
1 parent f380120 commit 16692e3
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .build/.minify
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"wrap_iife": true
},
"annotations": false,
"ie": false,
"ie": true,
"keep_fargs": false,
"keep_fnames": false,
"module": false,
Expand Down
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package @shinsenter/defer.js

🥇 A JavaScript library that helps you lazy load (almost) anything. It's zero-dependency, super-efficient, and Web Vitals friendly.
🥇 A JavaScript micro-library that helps you lazy load (almost) anything. It's zero-dependency, super-efficient, and Web Vitals friendly.

[![NPM](https://img.shields.io/npm/l/@shinsenter/defer.js)](https://code.shin.company/defer.js/blob/master/LICENSE)
[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](https://snyk.io/advisor/npm-package/@shinsenter/defer.js)
Expand All @@ -16,7 +16,7 @@
[![NPM](https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true)](https://www.npmjs.com/package/@shinsenter/defer.js)

- **Package**: [@shinsenter/defer.js](https://www.npmjs.com/package/@shinsenter/defer.js)
- **Version**: 3.2.0
- **Version**: 3.3.0
- **Author**: Mai Nhut Tan <[email protected]>
- **Copyright**: 2022 AppSeeds <https://code.shin.company/>
- **License**: [MIT](https://code.shin.company/defer.js/blob/master/LICENSE)
Expand All @@ -28,7 +28,7 @@

## Introduction

Big CSS files, slow javascript (third-party add-ons, etc.),
Big CSS files, slow JavaScript (third-party add-ons, etc.),
or media resources (photos, videos, iframes) on your website may cause
[Web Vitals](https://web.dev/vitals/) issues in real scenarios.

Expand Down Expand Up @@ -80,7 +80,7 @@ Just put a `<script>` tag pointing to the library URL just below the opening `<h
<title>My Awesome Page</title>

<!-- Put defer.min.js here -->
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.2.0/dist/defer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.3.0/dist/defer.min.js"></script>

<!-- ... -->
</head>
Expand All @@ -96,7 +96,7 @@ Because `defer.min.js` is optimized to a very tiny file size, you can even inlin
<title>My Awesome Page</title>

<!-- Copy the script from the below URL -->
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.2.0/dist/defer.min.js -->
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.3.0/dist/defer.min.js -->
<script>/* then replace this comment block with the content of defer.min.js */</script>

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

<!-- Put defer_plus.min.js here -->
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.2.0/dist/defer_plus.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.3.0/dist/defer_plus.min.js"></script>

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

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

*HINT*: Modern browsers support `IntersectionObserver` feature,
Expand Down Expand Up @@ -274,11 +274,14 @@ You can fully defer any script tag by setting its `type` attribute to `deferjs`.
This trick also works perfectly with `<script>` tags with an `src` attribute.

**Kind**: static method of [<code>Defer</code>](#Defer)
**Note**: Lazy loading behavior changed since v3.0
**Note**: (1) To avoid unexpected behavior when using
the `Defer.all()` function to delay the execution of script tags,
you should call run `Defer.all()` with a regular script tag.
**Note**: (2) Lazy loading behavior changed since v3.0
when you set `Defer.lazy=true` or `waitForInteraction=true`.
A `<script>` tags with `type="deferjs"` will not execute
unless the user starts interacting with your page.
**Note**: [Preload hints](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload) feature was added since v3.2
**Note**: (3) [Resource hints](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload) feature was added since v3.2
as it is recommended to prevent issues called "[Taming the Waterfall](https://blog.cloudflare.com/too-old-to-rocket-load-too-young-to-die/#quirksitamingthewaterfall)".
This feature is discussed at [#112](https://code.shin.company/defer.js/issues/112).
**Since**: 2.0
Expand All @@ -295,27 +298,32 @@ Using magic `type="deferjs"` attribute:
Before:
```html
<script type="text/javascript">
// your javascript is here
// your JavaScript is here
console.log('This script is a normal script tag.');
</script>
```

After:
```html
<script type="deferjs">
// your javascript will still be here,
// your JavaScript will still be here,
// but it will not run unless the user starts interacting with your page.
console.log('This script is lazy loaded with type="deferjs" attribute.');
</script>
```
**Example**
Using your value for the type attribute, such as `type="my-magic"`:

If you hate using the `type="deferjs"` attribute, you can even choose yours.
If you hate using the `type="deferjs"` attribute,
you can even choose yours by using the `Defer.all()` function.

Notice: To avoid unexpected behavior when using
the `Defer.all()` function to delay the execution of script tags,
you should call run `Defer.all()` with a regular script tag.

```html
<script type="my-magic">
// your javascript will still be here,
// your JavaScript will still be here,
// but it will not run unless the user starts interacting with your page.
console.log(
'This script is lazy loaded with type="my-magic" attribute ' +
Expand All @@ -339,6 +347,10 @@ The `waitForInteraction` argument (the fifth argument) is set to `true`,
the library will defer the load of the tippy.js library until the user starts
interacting, when the user moves his/her mouse on the button, a tooltip will show.

Notice: To avoid unexpected behavior when using
the `Defer.all()` function to delay the execution of script tags,
you should call run `Defer.all()` with a regular script tag.


```html
<button id="tooltip-button">My button</button>
Expand Down Expand Up @@ -753,14 +765,14 @@ No tag will be animated unless the user scrolls to its position.

### Defer.js(fileUrl, [id], [delay], [onload], [waitForInteraction]) ⇒ <code>void</code>
We use `Defer.js()` to defer a load of 3rd-party
javascript libraries, widgets, add-ons, etc. without blocking the page rendering.
JavaScript libraries, widgets, add-ons, etc. without blocking the page rendering.

**Kind**: static method of [<code>Defer</code>](#Defer)
**Note**: Because the download of a file using `Defer.js()` function is asynchronous,
**Note**: (1) Because the download of a file using `Defer.js()` function is asynchronous,
to avoid dependency error when lazy loading a third-party library using `Defer.js()`,
it is highly recommended that the `onload` callback function be used
to make sure that the library you needed is completely defined.
**Note**: Lazy loading behavior changed since v3.0
**Note**: (2) Lazy loading behavior changed since v3.0
when you set `Defer.lazy=true` or `waitForInteraction=true`.
The `fileUrl` will not be fetched unless the user starts interacting with your page.
**Since**: 2.0
Expand Down
18 changes: 9 additions & 9 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<main>
<header>
<h1><code>@shinsenter/defer.js</code></h1>
<p>🥇 A JavaScript library that helps you lazy load (almost) anything. It's zero-dependency, super-efficient, and Web Vitals friendly.</p>
<p>🥇 A JavaScript micro-library that helps you lazy load (almost) anything. It's zero-dependency, super-efficient, and Web Vitals friendly.</p>
<div class="demo-addthis addthis_inline_share_toolbox"></div>
</header>

Expand All @@ -42,18 +42,18 @@ <h3>Quick installation:</h3>
<div class="helper">
Just put a <code>&lt;script&gt;</code> tag pointing to
<a target="_blank" title="defer.min.js"
href="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.2.0/dist/defer.min.js">the library URL</a>
href="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.3.0/dist/defer.min.js">the library URL</a>
just below the opening <code>&lt;head&gt;</code> tag of your page.
Because <code>defer.min.js</code> is optimized to a very tiny file size,
you can even inline entire the library to save one HTTP request.
</div>
<div class="demo">
<!-- Copy the script from the below URL -->
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.2.0/dist/defer.min.js -->
<!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.3.0/dist/defer.min.js -->
<script>/* then replace this comment block with the content of defer.min.js here */</script>

<!-- If legacy browsers like Internet Explorer 9 still need to be supported -->
<script>'IntersectionObserver'in window||document.write('<script defer src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.2.0/dist/polyfill.min.js"><\/script>');</script>
<script>'IntersectionObserver'in window||document.write('<script defer src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.3.0/dist/polyfill.min.js"><\/script>');</script>
</div>
</div>
</section>
Expand Down Expand Up @@ -82,13 +82,13 @@ <h3>Using magic <mark><code>type="deferjs"</code></mark> attribute:</h3>
will not execute unless the user starts interacting with your page.
<br>
<strong>Note: </strong>
<a href="" target="_blank">Preload hints</a> feature was added since v3.2
<a href="" target="_blank">Resource hints</a> feature was added since v3.2
as it is recommended to prevent issues called <a href="https://blog.cloudflare.com/too-old-to-rocket-load-too-young-to-die/#quirksitamingthewaterfall" target="_blank">Taming the Waterfall</a>.
This feature is discussed at <a href="https://code.shin.company/defer.js/issues/112" target="_blank">#112</a>.
</div>
<div class="demo">
<script type="deferjs">
// your javascript will still be here,
// your JavaScript will still be here,
// but it will not run unless the user starts interacting with your page.
console.log('This script is lazy loaded with type="deferjs" attribute.');
</script>
Expand All @@ -110,13 +110,13 @@ <h3>Using your value for the type attribute, such as <mark><code>type="my-magic"
will not execute unless the user starts interacting with your page.
<br>
<strong>Note: </strong>
<a href="" target="_blank">Preload hints</a> feature was added since v3.2
<a href="" target="_blank">Resource hints</a> feature was added since v3.2
as it is recommended to prevent issues called <a href="https://blog.cloudflare.com/too-old-to-rocket-load-too-young-to-die/#quirksitamingthewaterfall" target="_blank">Taming the Waterfall</a>.
This feature is discussed at <a href="https://code.shin.company/defer.js/issues/112" target="_blank">#112</a>.
</div>
<div class="demo">
<script type="my-magic">
// your javascript will still be here,
// your JavaScript will still be here,
// but it will not run unless the user starts interacting with your page.
console.log(
'This script is lazy loaded with type="my-magic" attribute ' +
Expand Down Expand Up @@ -642,7 +642,7 @@ <h3>Lazy load animate.css library:</h3>
<h2><code>Defer.js(fileUrl, [id], [delay], [onload], [waitForInteraction])</code></h2>

<div class="helper">
We use <code>Defer.js</code> to defer a load of 3rd-party javascript libraries, add-ons etc. without blocking the page rendering.
We use <code>Defer.js</code> to defer a load of 3rd-party JavaScript libraries, add-ons etc. without blocking the page rendering.
<br>
<strong>Note 1: </strong>
Because the download of a file using <code>Defer.js</code> function is asynchronous,
Expand Down
4 changes: 2 additions & 2 deletions dist/defer.min.js

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

Loading

0 comments on commit 16692e3

Please sign in to comment.