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

woff2-feature-test

v1.0.3

Published

A simple feature test for the WOFF2 font format

Downloads

614

Readme

woff2-feature-test

A simple feature test for the WOFF2 font format.

Use Case

Given that we use loadCSS to asynchronously load a stylesheet containing all of our typefaces as data URIs, we need a way to programmatically determine if the WOFF2 format is supported in the browser.

var fontFile = "/url/to/woff.css";

// Use WOFF2 if supported
if( supportsWoff2 ) {
	fontFile = "/url/to/woff2.css";
}

loadCSS( fontFile );

Requirements

  1. Must not make an HTTP request to a remote server, anything using a remote test file was out.
  2. Prefer a synchronous approach to an asynchronous one, we want it to execute and the result to be available immediately.
  3. More than just an inference. It was suggested that we could infer based on the existence of the Font Loading API given that it currently matches support for WOFF2. But what happens if a browser implements the Font Loading API but doesn’t implement WOFF2? This scenario seemed entirely possible and we didn’t want any false positives.

Approach

We use the Font Loading API to load an empty WOFF2 data URI and see if the font set status is loading or not. If it attempts to load, the format is supported. If it does not, the format is unrecognized (see the intentionally failing test with an imaginary format).

Demo

If a browser eventually implements the WOFF2 format but does not implement the Font Loading API, this script will report a false negative (which is preferable to the false positive scenarios described above).

Limitations

Note that if you use a restrictive Content Security Policy header on font-src, that can cause issues with this feature test.

You’ll also need to make sure your Content Security Policy allows Data URIs.

For example, github.com (not github.io) uses Content-Security-Policy:default-src *; font-src assets-cdn.github.com; which denies our @font-face Data URI.

Addendum

A few previous revisions of this script can be found on a gist.