defer-html
If you want to make your document smaller (just to remind you this is the most critical resource, nothing happens in the browser until it's loaded) and you have a bunch of hidden items or items that are not visible on the initial screen then you can defer it!
Here is an example
Install
npm i defer-html
Usage
hidden-posts.html
and place it in the same folder as index.html
Extract a bunch of html you are going to defer into a file hidden-posts.html
Post 1 Post 2 Post 3
index.html
file
Add a placeholder in
It could be p
, div
or any other DOM element, doesn't matter. Only data-defer-html makes sense over there.
deferHtml.js
and call loadHtml
method:
Include - via ES6 module
; ;
- via script tag
Options
loadHtml
takes an object which may contain the following parameters:
event
Document Whenever an html file is loaded defer-html:loaded
event is fired.
document;
ES5 implementation
./bin/deferHtml.es5.js
Another approach from WebComponents API: link rel="import"
Check out a gulp plugin to inject lazy html loading into your build system gulp-defer-html