Code Monkey home page Code Monkey logo

swr-vue's Introduction

Vue Swr

Introduction

swr-vue is a Vue composables library for data fetching.

The name โ€œSWRโ€ is derived from stale-while-revalidate, a cache invalidation strategy popularized by HTTP RFC 5861. SWR first returns the data from cache (stale), then sends the request (revalidate), and finally comes with the up-to-date data again.

Features

  • Fast and reusable data fetching
  • Transport and protocol agnostic
  • Built-in cache and request dedulication
  • Revalidation on focus
  • Revalidation on network recovery
  • Polling
  • Local mutation (Optimistic UI)
  • Type safe
  • Configurable

For a more details you can visit the documentation

If you are looking to contribute, please check CONTRIBUTING.md

Quick Start

<script setup lang="ts">
import { useSWR } from 'swr-vue';

const fetcher = async (url) => {
  const res = await fetch(url);
  
  return res.json();
}

const { data, error } = useSWR('/api/user', fetcher);
</script>

<template>
  <div v-if="error">failed to load</div>
  <div v-else-if="!data">loading...</div>
  <div v-else>hello {{ data.name }}!</div>
</template>

In this example, the composable useSWR accepts a key and a fetcher function. The key is a unique identifier of the request, normally the URL of the API. And the fetcher accepts key as its parameter and returns the data asynchronously.

useSWR returns 2 values: data and error. When the request is not yet finished, data and error will be undefined. And when we get a response, it sets data and error based on the result of fetcher.

fetcher can be any asynchronous function, you can use your favourite data-fetching library to handle that part.


Thanks

swr-vue is inspired by these great works:

License

The MIT License.

swr-vue's People

Contributors

edumudu avatar semantic-release-bot avatar siimaoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

swr-vue's Issues

Unable to install on Node 18 LTS

The package.json specifies the Node engine to require version 16. With the release of NodeJS 18 LTS, this blocks installing of the package.

Is the engine intentionally locked down to 16, or would it be possible to change it to either include 18, or alternatively use 16 and up?

Add global configuration

Allow the user to set a default configuration for the project.

The local configuration should be more relevant than the global one

Implement configuration using the context(inject/provide). This way is possible to use multiple global configurations in the same app in the same way that Vue 3 allows you to create multiple apps

Add auto revalidations behaviors

Behavios should be controlled by these options

revalidateOnFocus

Should revalidade when the window get focused

Default: true

  • revalidateIfStale
    automatically revalidate even if there is stale data

Default: true

revalidateOnReconnect

Should automatically revalidate when the browser regains a network connection (via 'navigator.online')

Default: true

Key as array

Allow receiving an array as a key to offer an option of combining multiple pieces of information to form a unique key

Add Deduplication

add an option dedupingInterval with the default value as 2000 ms

this interval is to prevent send multiple requests for the same key when components are mounted inside this interval.

The first call to a key should send the request, and the subsequent calls inside this interval should not trigger a new request, and should resolve the same value as the first

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.