├── .gitignore
├── prettier.config.js
├── test
├── .eslintrc.json
├── karma.config.js
└── test.js
├── .travis.yml
├── .eslintrc.json
├── package.json
├── LICENSE
├── index.js
├── README.md
└── examples
└── index.html
/.gitignore:
--------------------------------------------------------------------------------
1 | dist
2 | node_modules
3 |
--------------------------------------------------------------------------------
/prettier.config.js:
--------------------------------------------------------------------------------
1 | /* @flow strict */
2 | module.exports = require('eslint-plugin-github/prettier.config')
3 |
--------------------------------------------------------------------------------
/test/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "mocha": true
4 | },
5 | "globals": {
6 | "assert": true
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | sudo: required
3 | node_js:
4 | - "node"
5 | addons:
6 | chrome: stable
7 | cache:
8 | directories:
9 | - node_modules
10 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "plugin:github/browser",
4 | "plugin:github/es6",
5 | "plugin:github/flow"
6 | ],
7 | "parser": "babel-eslint",
8 | "overrides": [
9 | {
10 | "files": "test/**/*.js",
11 | "rules": {
12 | "flowtype/require-valid-file-annotation": "off",
13 | "github/unescaped-html-literal": "off"
14 | }
15 | }
16 | ]
17 | }
18 |
--------------------------------------------------------------------------------
/test/karma.config.js:
--------------------------------------------------------------------------------
1 | module.exports = function(config) {
2 | config.set({
3 | frameworks: ['mocha', 'chai'],
4 | files: ['../dist/index.umd.js', 'test.js'],
5 | reporters: ['mocha'],
6 | port: 9876,
7 | colors: true,
8 | logLevel: config.LOG_INFO,
9 | browsers: ['ChromeHeadless'],
10 | autoWatch: false,
11 | singleRun: true,
12 | concurrency: Infinity
13 | })
14 | }
15 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lazy-loadable",
3 | "version": "0.1.3",
4 | "description": "Boilerplate for creating lazy-loadable images.",
5 | "type": "module",
6 | "main": "index.js",
7 | "module": "index.js",
8 | "license": "MIT",
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/manobi/lazy-loadable.git"
12 | },
13 | "files": [
14 | "index.js",
15 | "package.json",
16 | "README.md"
17 | ]
18 | }
19 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | describe('custom-element', function() {
2 | describe('element creation', function() {
3 | it('creates from document.createElement', function() {
4 | const el = document.createElement('custom-element')
5 | assert.equal('CUSTOM-ELEMENT', el.nodeName)
6 | })
7 |
8 | it('creates from constructor', function() {
9 | const el = new window.CustomElementElement()
10 | assert.equal('CUSTOM-ELEMENT', el.nodeName)
11 | })
12 | })
13 |
14 | describe('after tree insertion', function() {
15 | beforeEach(function() {
16 | document.body.innerHTML = '
24 |
28 | ```
29 |
30 | ## Attributes breakdown
31 |
32 | ***is="lazy-loadable"***
33 |
34 | Apply the lazy-loadable custom element pollyfill behaviour to your image tag.
35 | Usually people loop over all images on a page (querySelectorAll('img.lazy').forEach), to decide if it should lazy load, it can be very slow, you also have to wait until the DOM is ready or put your script at end of body, it would not work if a image appears in document after the document have loaded, like in infinity scrolling or lazy rendering. Using custom elements the browser handles all of it for you.
36 |
37 | ***loading="lazy"***
38 |
39 | The native way to tell the browser to delay the image loading, until it's in the screen. Only Google Chrome have [shipped it yet](https://caniuse.com/#feat=loading-lazy-attr).
40 |
41 | ***lazyload="1"***
42 |
43 | It seems [Microsoft](https://msdn.microsoft.com/en-us/ie/dn369270(v=vs.94)) implemented on IE 11 and Edge 12 a [unofficial attribute](https://caniuse.com/#feat=lazyload) called "lazyload".
44 | But it does not work the same way as "loading=lazy", it only tell the browser to decrese the loading priority of the resource. Actually it's pretty similar to what "importance=low" does.
45 |
46 | ***importance="low"***
47 |
48 | This is a [spec proposal](https://wicg.github.io/priority-hints/) to enable developers to signal the priority of each resource they need to download. In case a browser vendor never implement the "native lazy load", but for any reason ships the **priority hints** support, we would at least download the image without high priority.
49 |
50 | ***srcset***
51 | Since there is no ways yet to know [if the browser have native support](https://bugs.chromium.org/p/chromium/issues/detail?id=949365) before images starts loading we need to set a placeholder image.
52 |
53 | Our placeholder is the one responsible for the magic. I've chosen to use srcset as placeholder instead of a infamous "data-src". By placing the placeholder on "srcattr", we can hold the "src" loading, until we are sure if the browser handles lazy by default. In future if all browsers support native lazy-load, all you would have to do is remove this attribute from your html.
54 |
55 | The great advantage of using "srcattr" instead of "data-src" is that when you remove the placeholder, browsers already knows what to do, respecting the standard fallback src, picture source, media attributes and pixel density.
56 |
57 | ***width & height***
58 | Explicitally declare the image size to avoid page jumps.
59 |
60 | ## Safari custom elements pollyfill
61 |
62 | Apple have chosen [to not ship](https://github.com/w3c/webcomponents/issues/509) a complete implementation of custom elements V1, in order for it to work in Safari you may have to use the [ungap pollyfill](https://github.com/ungap/custom-elements-builtin) before your scripts:
63 |
64 | ```html
65 |
72 | ````
73 |
74 | ## Install
75 | If you prefer to bundle the pollyfill yourself or use it with a framework:
76 |
77 | ```shell
78 | npm install lazy-loadable -s
79 | ```
80 |
81 | ```javascript
82 | import LazyLoadable from 'lazy-loadable';
83 | customElements.define('lazy-loadable', LazyLoadable, { extends: "img" });
84 | ```
85 |
86 | ## License
87 |
88 | Distributed under the MIT license. See LICENSE for details.
89 |
--------------------------------------------------------------------------------
/examples/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |