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

@lucidclient/speculate

v0.2.1

Published

A lightweight library to handle speculate rules for prefetching and prerendering documents, with added support for data fetching on intent.

Downloads

185

Readme

Lucid Speculate

A simple library designed to enhance anchor elements by adding support for prefetching and prerendering. By default this uses the new experimental Speculation Rules API, but falls back to <link rel="prefetch"> or fetch when not supported. Additionally, the library features a PrefetchData class, enabling asynchronous data fetching based on user interactions with specified targets.

Features

  • Prefetch or prerender documents using Speculation Rules, <link rel="prefetch"> or fetch as a fallback.
  • Support for visible, immediate, eager, moderate and conservative triggers.
  • PrefetchData class for asynchronous data fetching based on user intent.

Getting Started

Installation

To install the Lucid Speculate library, run the following command:

npm install @lucidclient/speculate

Speculate Usage

Initialise the library by calling the speculateLinks function.

import { speculateLinks } from "@lucidclient/speculate";

speculateLinks();

Define the actions and triggers for your links using the rel attribute. For each action (prefetch or prerender) you can specify a trigger (visible, immediate, eager, moderate or conservative). For example:

<a href="/page-1" rel="prefetch:visible">I will prefetch when I enter the viewport</a>
<a href="/page-2" rel="prefetch:moderate">I will prefetch when a user interacts with me</a>

<a href="/page-3" rel="prerender:visible">I will prerender when I enter the viewport</a>
<a href="/page-4" rel="prerender:moderate">I will prerender when a user interacts with me</a>

Aside from the visible trigger, the others are based on the Speculation Rule Eagerness values and are used only when Speculation Rules are supported.

When Speculation Rules are not supported, the only valid triggers are visible and moderate. The moderate trigger being a fallback for the remaining triggers. The prerender action is ONLY supported with Speculation Rules and will fallback to the prefetch action when not supported.

Browser Support

Chrome

Chrome supports Speculation Rules, allowing for both prefetching and prerendering as intended. Chrome will always use the Speculation Rules API.

Firefox

Firefox does not support the Speculations API, but does support <link rel="prefetch">, however this requires cache headers (such as Cache-Control, Expires, or ETag) to function properly.

Safari

Safari doesn't support either the Speculation Rules API or <link rel="prefetch">. Instead, it uses a low-priority fetch, which requires cache headers (such as Cache-Control, Expires, or ETag) to function properly.

Prefetch Data Usage

To use the PrefetchData class, import the PrefetchData class and intialise a new instance of it per target.

import { PrefetchData } from "@lucidclient/speculate";

new PrefetchData({
    target: "#load-data",
    fetch: () => {
        return fetch("/api/users").then((res) => res.json());
    },
    onClick: (data) => {
        console.log(data);
    },
    staletime: 60000, // optional
});
<button 
    id="load-data"
    type="button"
>
    Load Data
</button>