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

spa-fetch

v1.0.3

Published

Global fetch for Single Page Applications

Downloads

8

Readme

[![npm version][npm-image]][npm-url] [![install size][install-size-image]][install-size-url]

spa-fetch documentation

Visit the full web site with the full source code as literate code.

Use as drop-in replacement of the native fetch()

The easiest and most basic use of spa-fetch is to simply import it

import { spaFetch } from '@spa-fetch'

...
...
await function () {
  response = await spaFetch('/users/10')
  data = await response.json()

  console.log(data)
}

Note that if you make the same HTTP GET call twice within 1 second, the server will only he hit once. However, the data will be available for each call:

import { spaFetch } from '@spa-fetch'

...
...
await function () {
  response1 = await spaFetch('/users/10')
  response2 = await spaFetch('/users/10')
  
  data1 = await response1.json()
  data2 = await response2.json()

  console.log(data1)
  console.log(data2)
}

Caching will happen regardless of how spaFetch() is called: using a text URL or a Request object as first parameter, and whether init is passed as its second parameter. If two HTTP GET calls are equivalent, they will be cached.

Configuring the module

spa-fetch esports a variable called spaFetchConfig that you can use to configure it.

The configuration properties are:

  • cacheDuration -- defaults to 1000ms (1 second). Determines how long items are cached for
  • fetcher -- defaults as null. If set, the passed function will be used to fetch, rather than window.fetch()

Setting a specific duration for the cache

import { spaFetch, spaFetchConfig } from '@spa-fetch'

spaFetchConfig.cacheDuration = 200

const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))

...
...
await function () {
  response1 = await spaFetch('/users/10')
  await sleep(300)
  response2 = await spaFetch('/users/10') // This will trigger a second server call
  
  data1 = await response1.json()
  data2 = await response2.json()

  console.log(data1)
  console.log(data2)
}

Unlike the previous case, in this case two calls will be made

Setting an alternative fetch function

If you want to run specific operations before and after the fetch call, you can define an alternative fetch() function:

import { spaFetch, spaFetchConfig } from '@spa-fetch'

spaFetchConfig.fetcher = (resource, init) => {
  console.log('About to fetch...', resource)
  const p = fetch(resource, init)

  p.then(
    (response) => {
      console.log('The response is:', response)
    },
    (error) => {
      console.log('The error is:', error)
    }  
  )
  return p
}

This will ensure that specific code is run both before the actual fetch() is called, and once the response is received.