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

fetch-tracker

v1.0.1

Published

Adds preFetch and postFetch events for tracking fetch calls globally.

Downloads

2

Readme

Fetch Tracker

Wraps the fetch function so it can emit a few custom fetch events, useful for tracking analytics from fetch.

The events are non-destructive and will not change anything about the fetch itself (unlike the FetchEvent fetch event).

Installation

Include before you call fetch, and it will be wrapped automatically.

Important Note: If you are using a fetch polyfill, you should include this module after any polyfills or other fetch changing modules.

ES6

import 'fetch-tracker';

Common JS

require('fetch-tracker');

Browser

Minified:

<script href="path/to/fetch-tracker/dist/fetch-tracker.min.js"></script>

Unminified:

<script href="path/to/fetch-tracker/dist/fetch-tracker.js"></script>

There are also accompanying maps fetch-tracker.min.js.map and fetch-tracker.js.map.

Basic Usage

addEventListener('preFetch', event => {
  console.log(event.request);
});

addEventListener('postFetch', event => {
  console.log(event.request, event.response, event.duration);
});

fetch('http://example.com');

Two events are available, preFetch and postFetch. Add an event listener for them (at the top-level). They will be dispatched every time fetch is called (unless paused with fetch.pauseTracking()).

Properties

fetch.trackingPaused

Boolean. Indicates if tracking has been paused.

Methods

fetch.pauseTracking()

Calling this will pause any tracking of fetch, so it will not emit preFetch or postFetch events any more.

This is useful if you are going to make an especially large call that would perform poorly when cloning the Response object.

fetch.resumeTracking()

Resumes tracking after calling fetch.pauseTracking().

Events

preFetch

This event is fired before the normal fetch event.

It will receive a PreFetchEvent event, which includes the request.

The PreFetchEvent has a request property which is a clone of the original Request.

addEventListener('preFetch', event => {
  console.log(event.request.method);
});

postFetch

This event is fired once the fetch itself is complete, but before any then() functions are fired from the fetch.

It will receive a PostFetchEvent event, which includes the following properties:

  • request - A clone of the original Request object.
  • response - A clone of the Response object.
  • duration - The time (in milliseconds) between the initiation of the fetch and when it concludes.

Note: If there are multiple postFetch events, they will share the same response, so reading the body once will mean subsequent events can't be read. If you are doing this, you should use response.clone() in at least one of those so you can read it multiple times (or combine it into one event).

addEventListener('postFetch', event => {
  event.response.json().then(json => console.log(json));
});

fetch

This event isn't implemented by this library, but if you are using a library/environment which does use it, it occurs after preFetch but before postFetch. It is unaffected by this library. MDN FetchEvent Documentation