Skip to content

Defer some of your html sections that are not critical for the initial screen

License

Notifications You must be signed in to change notification settings

AntonLapshin/gulp-defer-html

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gulp-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!

Install

npm i -D gulp-defer-html

Usage

Wrap your html code that you want to defer with

<defer-html data-name="my_html_code">
  This content will be loaded dynamically
</defer-html>

Use gulp-defer-html plugin in your gulpfile.js

const deferHtml = require("gulp-defer-html");

gulp
  .src("output/**/*.html")
  .pipe(deferHtml())
  .pipe(gulp.dest("output"));

In the output you will get

  • 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>

Options

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
})

Load html files dynamically

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!

About

Defer some of your html sections that are not critical for the initial screen

Resources

License

Stars

Watchers

Forks

Packages

No packages published