-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f380120
commit 16692e3
Showing
13 changed files
with
203 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
@@ -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) | ||
|
@@ -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. | ||
|
||
|
@@ -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> | ||
|
@@ -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> | ||
|
||
<!-- ... --> | ||
|
@@ -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> | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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 ' + | ||
|
@@ -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> | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.