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

preconnect

v1.0.1

Published

A nano-sized `preconnect` hint wrapper.

Downloads

18

Readme

preconnect

A nano-sized preconnect hint wrapper.


preconnect is a very small script that allows you to programmatically invoke a preconnect hint to any host to mask connection latency. The script is designed so that you can invoke this hint whenever is appropriate for your application.

Installing

Since preconnect is Just JavaScript™, you can install it with npm as a production dependency:

npm i preconnect --save

If you're not the npm type, grab one (or both) of the minified versions in this repo's dist folder. There are two minified versions:

  • preconnect.min.js is the Babel-fied ES5 build. It assigns a variable named preconnect on the window.
  • preconnect.min.mjs is the untransformed minified ES6 build. Its default export is a function eponymously named preconnect.

Usage

import preconnect from "preconnect";

const preconnecter = new preconnect({
  // Injects `dns-prefetch` hints in addition to `preconnect` hints
  getDns: true
});

// Preconnects to a link as the user hovers over it
document.getElementById("some-link").addEventListener("mouseover", event => {
  preconnecter.add(event.target.href);
}, {
  // Execute this event handler code only once avoid multiple injections of hints
  once: true
});

In the above example, an early connection is established to the host specified in an <a> element's href value. This could potentially speed up navigation to that host for the user, improving the perceived performance of the navigation.

Options

When you instantiate a new instance of preconnect, you can pass in an options object. There are two options:

  • getDns (default: false)Inject a dns-prefetch hint in addition to a preconnect hint. Some browsers don't support preconnect, but some of those browsers do support dns-prefetch. Enabling this ensures those browsers still receive some kind of benefit.
  • timeout (default: 0)This script can use requestIdleCallback to take advantage of idle browser time. This helps to reduce monopolization of the main thread so that the page is more responsive to user input. This option specifies, in milliseconds, the deadline by which requestIdleCallback must inject the resource hint elements into the document <head>. A value of 0 (the default) disables the use of requestIdleCallback entirely.

Contributing

If you'd like to contribute, please file an issue first so we can discuss. Because I'd like to keep this script very small and simple, new features aren't likely to be added unless the weight they add can be justified.

Author info

My name is Jeremy Wagner. I'm an independent web performance consultant. I write about web stuff, and sometimes I even get to talk about web stuff. I also ramble on Twitter @malchata and make web performance videos on YouTube.