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

fache

v1.1.9

Published

Fetch and cache the response

Downloads

16

Readme

Introduction

Fache is a utility built on top of browser's Fetch API. It caches the result(Response object). When next time you call Fache API, it returns a promise with cached Response object from last call, until it expires.

Install

For adding directly to a web page, go to browser folder and download fache.min.js.

If you are developing a web application e.g. React, Angular,etc in a Node environment, then

npm i fache

Note that, as it uses Fetch API, make sure your application will run in a modern browser. Alternatively, look for a polyfill.

Use

In CommonJS

const fache = require('fache');

Or ES6 style

import fache from 'fache';

Difference between Fetch and Fache

It has same parameters as Fetch API, https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch. The difference is Fache adds more in the second parameter.

Example of fetch

fetch( '/api/data', { method: 'GET' } )

Example of fache

It caches the response for subsequent Fache calls for 10 seconds.

fache( '/api/data', { method: 'GET', seconds: 10 } )

Default seconds is 60.

Note that

  1. Time to invalidate the cache starts when the first Fache call's response is received.
  2. If a subsequent call has same Request or URL but different seconds value, e.g. 20, then it will NOT update the cache lifetime of the first call until the response of first call expires.
  3. When deciding if a response will be cached, it starts with checking fache's first parameter to see if they have the same value. Then it shallow compares values of the second parameter. If it returns true, then the response is cached for seconds time.

More settings of second parameter

| Settings | Type | Description | |---------------|----------------------|------------------------------------------------------------------------| | seconds | numberfunction | Time in seconds to invalidate cached response after response received. |

If seconds is a function, e.g. Cache for 10 seconds if response's status is 200.

{
    seconds: response => response.status === 200 ? 10 : 0
}

Methods of Fache object

| Method Name | Description | |-------------------------|------------------------------------------------------------------------| | clearAll() | Clear all cached responses. | | clear( urlOrRequest ) | Clear a cached response by URL or Request object. |

Clear previously sent request by URL.

fache.clear( '/api/data' );