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

dnstradamus

v1.3.1

Published

A prognosticating client-side DNS prefetcher for outbound links.

Downloads

41

Readme

dnstradamus

dnstradamus is a very small script that uses dns-prefetch to perform early DNS lookups links to external sites. It uses IntersectionObserver to monitor <a> elements as they are scrolled into the viewport, as well as requestIdleCallback to take advantage of idle browser time to minimize jank. dnstradamus is inspired by quicklink, which performs link prefetching rather than DNS lookups.

Bah, why bother with DNS prefetching?

DNS lookup times are undoubtedly reduced by redundant DNS caches in your browser, operating system, and many layers beyond. Public DNS servers such as Cloudflare's 1.1.1.1 and Google's public DNS also offer very fast resolution that help to make the web snappier for everyone everywhere. That said, sometimes DNS lookups can still take longer than we'd like.

dns-prefetch gives us a resilient and fault-tolerant way to mask the latency of DNS lookups. Unfortunately, it may not be practical to manually add tons of these hints in your page's <head> to cover every single external link on your site. This is especially true for sites with lots dynamic or user-generated content. This means we're leaving an opportunity on the table to improve perceived navigation performance for outbound links.

Why not just prefetch pages, though?

Page prefetching goes a long way toward improving perceived performance. Yet, link prefetching can be wasteful, which is not always acceptable for people on metered data plans. quicklink mitigates this somewhat by checking to see if users have data saver mode enabled on Chrome for Android, as well as checking the Network Information API's effective connection type. Despite these mitigations, however, these signals are only available in Chrome.

On the other hand, speculative DNS lookups are low risk because of the relative cheapness of them compared to prefetching documents.

How to use dnstradamus

If you're the npm sort, you can install it:

npm i dnstradamus --save

From there, it's not much trouble to get up and running:

import { dnstradamus } from "dnstradamus";

document.addEventListener("DOMContentLoaded", function () {
  // Read below for more options
  dnstradamus({
    observeChanges: true
  });
});

If you'd prefer not to install with npm, there are scripts in both ES6 and ES5 flavors in the dist folder available in this repo as dnstradamus.min.mjs and dnstradamus.min.js, respectively.

However you use dnstradamus, you need to be aware that it depends on IntersectionObserver to work. If used in a browser that doesn't support IntersectionObserver, you can polyfill it conditionally with this script:

<script defer src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>

If you don't load a polyfill for IntersectionObserver, dnstradamus will fail silently. This ensures that an unpolyfilled feature won't brick your website.

Options

dnstradamus has a few options:

context

Default: "body"

The context in which links are selected. This option accepts any valid CSS selector. It's useful for helping you to narrow down where dnstradamus looks for links. For example, let's say you only wanted to do DNS prefetching for external links within an element with an ID of content:

dnstradamus({
  context: "#content"
});

This context should point to a unique element. If you're not sure if you should specify anything here, leaving it undefined is okay.

include

Default: (anchor:HTMLAnchorElement, origin:string) => true

If you want to restrict what HTMLAnchorElements dnstradamus will perform DNS lookups on, include helps you to do that by providing a filter. This filter's interface includes the HTMLAnchorElementitself, as well as the origin it points to. From here, you can create your own filtering logic to decide what links should be observed. Returning any expression that evaluates totruewill include the link's origin for DNS prefetching. For example, let's say you wanted to exclude anchor elements with adata-nolookup` attribute from DNS prefetching:

dnstradamus({
  include: (anchor, origin) => !("nolookup" in anchor.dataset)
});

Or, let's say you only wanted to include links that match a regex for specific domains:

dnstradamus({
  include: (anchor, origin) => /^https?:\/\/(www\.)?(alistapart\.com|css-tricks\.com)/i.test(origin)
});

The interface include provides should be flexible enough to let you figure out what's best for your application.

observeChanges

Default: false

Flipping this to true creates a MutationObserver to look for new links added to the DOM after initialization. This is useful for single page applications where large swaths of the DOM can change. When in doubt—or in settings where client-side routing isn't used—don't set this to true.

Note: The context option also restricts the scope of the mutation observer!

Special thanks

Thanks to my pal Dave (@flipty] for the neat graphic. Cheers, friend.

Thank you to BrowserStack for graciously providing free cross-platform browser testing services! BrowserStack