npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazy-cf7-assets

v0.1.4

Published

Lazyloads Contact Form 7's JavaScript only if there is a form on the page. Use on a WordPress website with Contact Form 7 plugin active and its JavaScript manually dequeued.

Downloads

4

Readme

Lazy CF7 Assets

Improves performance by lazy loading Contact Form 7’s JavaScript files only when one of its forms exists on the page. Use this package on a WordPress website with Contact Form 7 plugin active and its JavaScript manually dequeued.

You can override any of the default options, although you'll likely only ever want to do this for gifPath.

Installation

npm:

npm install lazy-cf7-assets --save

Yarn:

yarn add lazy-cf7-assets

How to dequeue Contact Form 7’s JavaScript inclusion

// In your theme's functions.php file:
add_filter( 'wpcf7_load_js', '__return_false' );

// Optionally dequeue the CSS as well.
// Shown here for your reference only —
// the CSS won't be lazy loaded by this script!
add_filter( 'wpcf7_load_css', '__return_false' );

Usage Examples

After dequeueing Contact Form 7’s automatic JavaScript inclusion, you can now set its JavaScript files to load dynamically by importing Lazy CF7 Assets and initialising it from within your site’s JavaScript bundle, like so:

import lazyform from 'lazy-cf7-assets';

// Initialise once the DOM is ready, ie. at the end of the `body` tag.
lazyform.init()

Alternatively, if your scripts are included in the head tag rather than the end of the body tag, you should make sure the initialisation occurs after all the page elements have been rendered, thusly:

import lazyform from 'lazy-cf7-assets';

// ensures all page elements are loaded
// so the presence of CF7 forms can be detected
document.addEventListener('DOMContentLoaded', function() {
  lazyform.init()
})

You can override one or more of the default options by passing them into init via an object. You'll likely only ever need to do this for gifPath in the case where you’d prefer to use a custom loading animation gif rather than the default one included with Contact Form 7.

import lazyform from 'lazy-cf7-assets';

lazyform.init({
  // Set an absolute path to the alternate loading gif
  gifPath: '/wp-content/themes/my-theme-name/images/ajax-loader.gif'
});

Here is the full set of options:

{
  foundForm: document.querySelector('.wpcf7-form'),
  gifPath: '/wp-content/plugins/contact-form-7/images/ajax-loader.gif',
  jqueryPath: '/wp-includes/js/jquery/jquery.js',
  jqueryFormPath: '/wp-content/plugins/contact-form-7/includes/js/jquery.form.min.js',
  cf7ScriptPath: '/wp-content/plugins/contact-form-7/includes/js/scripts.js'
}

License

MIT