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

be-preemptive

v0.0.22

Published

Load dependent resources eagerly, but not too eagerly.

Downloads

66

Readme

be-preemptive

be-preemptive loads dependent resources eagerly, but not too eagerly.

NPM version How big is this package in your project? Playwright Tests

<link rel=lazy id=package-name/theme.css as=script crossorigin=anonymous integrity=... fetchpriority=low be-preemptive=css href=https://some-cdn.com/[email protected]>

First use case (short-lived, hopefully) -- preemptive use of CSS Modules

Browser vendors and CDN's are taking their sweet time in providing support for CSS Modules. In the meantime, we can use this web component decorator / behavior to avoid that obstacle:

//this is a bit of a simplification, we are assuming the preemptive decorator has already attached to the link element.
//see be-loaded for more precise logic for how to do this.
const linkOrStylesheet = await self['package-name/theme.css'].beDecorated.preemptive.linkOrStylesheetPromise();

NB: Alternatives exist

In the case of Chromium-based browsers, you will be provided a Stylesheet, which can be adopted as needed. If not, you will probably want to append the returned link into your ShadowDOM root.

The value of rel can also be preload, which is really useful for non-lazy loaded web components. And because pre-emptive actually downloads the file, the browser won't complain about having a preload tag go unfulfilled.

rel can be anything, really.

Second use case (medium term, at most, hopefully?) -- passionately, but not aggressively, get needed resources ahead of time, without being chastised.

Beyond this short lived usage, be-preemptive will also preemptively perform a css module import, but (since the value of rel is lazy), after waiting for the document to finish parsing (at a minimum), so as not to slow anything else down.

However, calling the api above will start the download regardless.

This is meant for use cases where a web component lazy loads. Having css kept separate allows for multiple components to share the same css, and also web components can enable having the web component consumer design their own css without incurring any penalty from the provided (bundled) default css. A third benefit of maintaining separate css files is better fine-grained caching. Finally, tooling may be better when editing css separately (though tooling could also merge the files, so that is a rather weak consideration).

Having the css pre download ahead of time in this scenario is quite helpful, especially if streaming is used to stream in the HTML needed for the web component, w3c willing. (If using (declarative?) ShadowDOM for the entire component, streaming support may be far, far into the future -- or maybe not, wow!). This would allow the browser to render the web component progressively without suffering from FOUC.

The browser probably has a good reason for complaining about abusing link rel=preload, so this provides a way to achieve that goal, hopefully in a responsible way.

This functionality works well in combination with be-loaded and be-lazy.

Third use case

Lazy loading web components can rely on link type=modulepreload to be more quick to load when needed. But for unbundled components, this only downloads the top dependency. But with pre-emptive, we can preload much more, while not lowering lighthouse scores.

Specify not doing anything preemptively based on the user agent's network connectivity [TODO]

But remember, the moment anyone calls the api above, the download will begin regardless of any hesitation parameters specified.

Use of fetchpriority [TODO]

Support for media queries [TODO]

Support for integrity [TODO]

Not yet supported, apparently

Resource Types supported

  1. CSS
  2. JSON [TODO]
  3. XSLT [TODO]