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-swr

v1.0.2

Published

A plugin to fetch data and cache it with a delay

Downloads

25

Readme

中文 | English

Fetch Swr

一个轻量级的npm包,它简化了从API获取数据的过程,同时提供了使用LocalForage在本地缓存结果的选项。

安装

要安装这个插件,只需在您的项目目录中运行以下命令:

npm install fetch-swr --save

使用方法

该插件提供了一种灵活的方式来获取和缓存数据。以下是如何使用它的方法:

import defineFetch from 'fetch-swr';

const fetchWithCache = defineFetch({
  cache_time: 120, // 缓存持续时间,单位为秒
  fetchAfterCache: true, // 在用户获取数据后,提供缓存数据并开始新的数据获取来更新数据
  fetchAfterCacheDelay: 10, // 提供缓存后,再次获取数据之前的延迟时间,单位为秒
});

fetchWithCache(
  { url: 'https://api.example.com/data'  },
  (data) => {
    console.log('获取到更新后的数据:', data);
    // 您可以在这里更新页面数据
  },
  false // 设置为true以绕过缓存
).then(data => {
  // 处理数据
}).catch(err => {
  // 处理任何错误
});

参数

  • cache_time: 缓存数据的持续时间,单位为秒。默认是60
  • fetchAfterCache: 一个布尔值,表示是否在提供缓存后再次获取数据。默认是false
  • fetchAfterCacheDelay: 在提供缓存后,再次获取数据之前的延迟时间,单位为秒。默认是0

fetchWithCache 函数

fetchWithCache 函数接受三个参数:

  1. params: 包含url的对象,用于获取数据。
  2. callback: 一个函数,将使用获取到的数据调用。
  3. noCache: 一个布尔值,表示是否绕过缓存。默认是false

贡献

欢迎贡献!请提交拉取请求或创建问题,以报告任何错误或建议。

许可

本项目采用MIT许可