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 🙏

© 2026 – Pkg Stats / Ryan Hefner

leaflet-prefetching

v0.1.2

Published

Tile prefetching and instant layer switching for Leaflet maps

Readme

leaflet-prefetching

Tile prefetching and instant layer switching for Leaflet maps.

Features

  • InstantLayerSwitcher - keeps multiple tile layers ready for instant switching. The active layer is loaded first with full bandwidth priority; hidden layers are deferred until it finishes.
  • PrefetchingManager - prefetch tiles for a known next location across all layers, ordered by a fixed per-layer priority.

Installation

npm install leaflet-prefetching

Leaflet is a peer dependency - install it separately if you haven't already:

npm install leaflet

Usage

import L from "leaflet";
import {
  InstantLayerSwitcher,
  PrefetchingManager,
} from "leaflet-prefetching";

const map = L.map("map").setView([51.505, -0.09], 12);
const switcher = new InstantLayerSwitcher(map);

const cartoLayer = L.tileLayer(
  "https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
  { subdomains: ["a", "b", "c", "d"] }
);
const satLayer = L.tileLayer(
  "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
);

// Active layer loads first; hidden layers are deferred until it finishes.
switcher.register("carto",     cartoLayer, true); // true = active
switcher.register("satellite", satLayer);

const manager = new PrefetchingManager(map, switcher, [
  { key: "carto",     layer: cartoLayer, priority: 0 },
  { key: "satellite", layer: satLayer,   priority: 1 },
], {
  onQueueEmpty() {
    console.log("All prefetching complete");
  },
});

// Prefetch tiles for a future location (all layers, by priority order)
manager.prefetchNextLocation(L.latLng(48.8566, 2.3522));

// Navigate - instant if tiles are already cached
map.setView(L.latLng(48.8566, 2.3522), map.getZoom());

// Switch layers - instant if the layer's tiles are loaded
switcher.switchTo("satellite");

API

InstantLayerSwitcher

Manages multiple tile layers on a single map. The active layer is added to the map immediately and gets full browser bandwidth. Hidden layers are deferred until the active layer finishes loading, then added at opacity 0 for instant switching later.

On every pan/zoom, hidden layers are temporarily disconnected from map events so the active layer gets 100% of browser connections. Once it finishes, hidden layers are reconnected one at a time in priority order - no DOM elements are destroyed or recreated, so already-loaded tiles stay in place.

const switcher = new InstantLayerSwitcher(map);

switcher.register(key, layer, makeActive?)  // register a layer (deferred unless active)
switcher.switchTo(key)                      // returns true if switch was instant
switcher.isActive(key)                      // boolean
switcher.isLoaded(key)                      // true once layer's 'load' event fired
switcher.activeKey                          // string | null

PrefetchingManager

Prefetches tiles into the browser cache using Image() requests, independent of Leaflet's own tile loading. Primarily useful for pre-loading tiles at a future location so that navigation + layer switches there are instant.

Note: If you are using InstantLayerSwitcher, you do not need to call prefetchHiddenLayers() for the current viewport - the switcher already handles loading all registered layers at the current view. Use the manager for next-location prefetching.

const manager = new PrefetchingManager(map, switcher, layerConfigs, options?);

manager.prefetchNextLocation(latLng)        // prefetch all layers at a future location (by priority)
manager.prefetchHiddenLayers()              // prefetch hidden layers at current viewport (not needed with InstantLayerSwitcher)
manager.clearQueue()                        // cancel pending (not in-flight) requests
manager.resetCache()                        // allow tiles to be re-fetched
manager.getStats()                          // { queued, inFlight, prefetched }
manager.isLayerPrefetched(key)              // true once all tiles for a layer are done

LayerConfig

{ key: string; layer: L.TileLayer; priority?: number; prefetch?: boolean }
  • priority - lower number = prefetched first. All layers are prefetched in this fixed order. Default: 10.
  • prefetch - set to false to exclude a layer from prefetching entirely. Default: true.

PrefetchManagerOptions

| Option | Type | Default | Description | |---|---|---|---| | concurrency | number | 6 | Max simultaneous prefetch requests | | nextLocationZoomOffsets | number[] | [0] | Extra zoom levels to also prefetch, e.g. [-1, 0, 1] | | onTilePrefetched | fn | - | Called on each successful prefetch | | onTileError | fn | - | Called on each failed prefetch | | onQueueEmpty | fn | - | Called when the queue is fully drained and no requests are in flight |

Development

npm install
npm run dev       # watch mode
npm test          # run tests
npm run typecheck # TypeScript check
npm run build     # production build → dist/

License

MIT