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!
npm i -D gulp-defer-html
<defer-html data-name="my_html_code">
This content will be loaded dynamically
</defer-html>
const deferHtml = require("gulp-defer-html");
gulp
.src("output/**/*.html")
.pipe(deferHtml())
.pipe(gulp.dest("output"));
- extracted
.html
files with the content you want to load dynamically - placeholders in the main
.html
files, for example:
<div data-defer-html="my_html_code.html"></div>
deferHtml
takes an object which may contain the following parameters:
deferHtml({
tag: 'p', // 'div' by default,
attr: 'my-custom-attr' // 'data-defer-html' by default
})
There are different ways how to import html files dynamically, for example link rel="import" from WebComponents API, but here is another npm module with the full browsers support: https://www.npmjs.com/package/defer-html Check it out!