Quote from vercel's SWR for react:
The name “SWR” is derived from stale-while-revalidate, a HTTP cache invalidation strategy popularized by HTTP RFC 5861. SWR is a strategy to first return the data from cache (stale), then send the fetch request (revalidate), and finally come with the up-to-date data.
With SWR, components will get a stream of data updates constantly and automatically. And the UI will be always fast and reactive.
revalidate().mutate().You can use npm or yarn to install it.
npm i vswr
yarn add vswr
<template>
{{ post.title }}
</template>
<script>
import { useSWR } from 'vswr'
export default {
setup() {
// Call the `useSWR` and pass the key you want to use. It will be pased
// to the fetcher function. The fetcher function can be configured globally
// or passed as one of the options to this function.
const { data: posts } = useSWR('https://jsonplaceholder.typicode.com/posts')
// Pass the data to the component.
return { posts }
},
}
</script>
This is a simple example that will use SWR as the strategy to fetch the data. In this particular case, all the default options are used (or the ones specified in the global config) and it will fetch the data using the default or global fetcher and update the DOM when the request is done.
All hook calls can have their respective configuration options applied to it. Nevertheless you can also configure global options to avoid passing them to each hook call.
function useSWR(key, options): SWRResponse
// Can be destructured to get the response as such:
const { data, error, mutate, revalidate, clear, stop, isLoading, isValid } = useSWR(key, options)
key: A resolved or non-resolved key. Can either be a string, or a function. A function can be used for dependent fetching as seen below.options: An optional object with optional properties such as:fetcher: (key) => Promise<any> = (url) => fetch(url).then((res) => res.json()): Determines the fetcher function to use.
This will be called to get the data.initialData: D = undefined: Represents the initial data to use instead of undefined. Keep in mind the component will still attempt to re-validate unless revalidateOnMount is set false.loadInitialCache: boolean = true: Determines if we should attempt to load the initial data from the cache in case initialData is undefined.revalidateOnStart: boolean = true: Determines if SWR should perform a revalidation when it's called.dedupingInterval: number = 2000: Determines the deduping interval. This interval represents the time SWR will avoid to perform a request if
the last one was made before dedupingInterval ago.revalidateOnFocus: boolean = true: Revalidates the data when the window re-gains focus.focusThrottleInterval: number = 5000: Interval throttle for the focus event. This will ignore focus re-validation if it
happened last time focusThrottleInterval ago.revalidateOnReconnect: boolean = true: Revalidates the data when a network connect change is detected (basically the browser / app comes back online).data: Ref<D | undefined>: Stores the data of the HTTP response after the fetcher has proceesed it or undefined in case the HTTP request hasn't finished or there was an error.error: Ref<E | undefined>: Determines error of the HTTP response in case it happened or undefined if there was no error or the HTTP request is not completed yet.mutate: (value, options) => void: Mutate alias for the global mutate function without the need to append the key to it.revalidate: (options) => void: Revalidation alias for the global revalidate function without the need to append the key to it.clear: (options) => void: Clears the current key data from the cache.stop: () => void: Stops the execution of the watcher. This means the data will unsubscribe from the cache and error changes as well as all the event listeners.isLoading: ComputedRef<boolean>: Determines if the request is still on its way and therefore, it's still loading.isValid: ComputedRef<boolean>: Determines if the data is valid. This means that there is no error associated with the data. This exists because errors do not wipe the data value and can still be used.You can configure the options globally by creating a SWR instance and using it in your vue application. This step is not mandatory but it's recommened for most apps.
You can either choose to manually create a SWR instance and import it when needed or replace the default SWR instance used by all exported APIs. The second is recommended if only one instance will be needed for your application.
function createSWR(options): VSWR
function createDefaultSWR(options): VSWR
options: SWROptions: Parameters of the options that will be passed to all components. They are the same as the ones on each useSWR function call.A SWR instance that can be used to access all the API.
import { createApp } from 'vue'
import { createDefaultSWR } from 'vswr'
import App from './App.vue'
import axios from 'axios'
// Set the defaut SWR instance that will
// be used by all the exported APIs.
createDefaultSWR({
// Configure a global fetcher for all SWR hooks. This
// can be replaced with anything that returns a promise
// with the data inside, for example: axios.
fetcher: (key) => axios.get(key).then((res) => res.data),
})
createApp(App).mount('#app')
<template>
{{ post.title }}
{{ user.name }}
</template>
<script>
import { useSWR } from 'vswr'
export default {
setup() {
const { data: post } = useSWR('https://jsonplaceholder.typicode.com/posts/1')
// We need to pass a function as the key. Function will throw an error when post is undefined
// but we catch that and wait till it re-validates into a valid key to populate the user variable.
const { data: user } = useSWR(() => `https://jsonplaceholder.typicode.com/users/${post.value.userId}`)
return { post, user }
},
}
</script>
You can re-validate specific keys by importing the revalidate function.
import { revalidate } from 'vswr'
You can call this method anywhere in your application by giving it the key, and the options.
function revalidate(key, options): void
key: Determines the key that is going to be re-validated. This must be a resolved key, meaning it must be a string or undefined.
If undefined, the function will do nothing.options: A partial object (meaning all props can be optional / undefined) that accepts the following options:force: boolean = false: Determines if the re-validation should be forced. When a re-validation is forced, the dedupingInterval will be ignored and a fetch will be performed.dedupingInterval: number = 2000: Determines the dedupling interval. This interval represents the time SWR will avoid to perform a request if the last one was made before dedupingInterval ago.fetcher: SWRFetcher<D>: Determines the fetcher function to use.You can re-validate specific keys by grabing the revalidate function of the useSWR call.
This function will allow you to perform a re-validation of the data on demand. There's no need
to provide the key for this function since it's already bound to the hook key. It only accepts the options.
function revalidate(options): void
options: Same as global revalidate (check above).<template>
{{ post.title }}
<button @click="() => revalidate()">Revalidate</button>
</template>
<script>
import { useSWR } from 'vswr'
export default {
setup() {
const { data: post, revalidate } = useSWR('https://jsonplaceholder.typicode.com/posts/1')
return { post, revalidate }
},
}
</script>
You can mutate specific keys by importing the mutate function.
import { mutate } from 'vswr'
You can call this method anywhere in your application by giving it the key, the value and the options.
function mutate(key, value, options): void
key: Determines the key that is going to be mutated. This must be a resolved key, meaning it must be a string or undefined.
If undefined, the function will do nothing.value: D | ((D) => D): Determines the new value to set. This can either be the value itself or a function that receives the current state and returns the new one.options: A partial object (meaning all props can be optional / undefined) that accepts the following options:revalidate: boolean = true: Determines if the mutation should attempt to revalidate the data afterwards.revalidateOptions: Partial<SWRRevalidateOptions> = { ...defaultRevalidateOptions }: Determines the revalidation options passed to revalidate in case the parameter revalidate is set to true.You can mutate specific keys by grabing the mutate function of the useSWR call.
This function will allow you to perform a mutation of the data on demand. There's no need
to provide the key for this function since it's already bound to the hook key. It only accepts the value and the options.
function mutate(value, options): void
value: Same as global mutate (check above).options: Same as global mutate (check above).Keep in mind we set revalidate to false to avoid it performing a HTTP request for this example, since this would just over-write the static data with the server data again.
<template>
{{ post.title }}
<button @click="() => mutate((state) => ({ ...state, title: 'Sample' }), { revalidate: false })">
Mutate only title
</button>
<button @click="() => mutate({ title: 'Sample' }, { revalidate: false })">Leave only title</button>
</template>
<script>
import { useSWR } from 'vswr'
export default {
setup() {
const { data: post, mutate } = useSWR('https://jsonplaceholder.typicode.com/posts/1')
return { post, mutate }
},
}
</script>
You can manually subscribe to data or error changes by using the subscribe and subscribeErrors functions.
import { subscribe, subscribeErrors } from 'vswr'
const key = 'example/key'
subscribe(key, (data) => {
console.log(`${key} changed to ${data}`)
})
subscribeErrors(key, (error) => {
console.log(`${key} error: ${error}`)
})
You can also manually get values from the cache without the need to subscribe for changes or
use the built-in hook (that does more things under the hood like subscribing as well). You can use
the get and getOrWait functions to get the current values from the cache.
```js import { get, getOrWait } from 'vswr'
const key = 'example/key'
// Gets the current cached data of the given key. // This does not trigger any revalidation nor mutation of the data. // - If the data has never been validated (there is no cache) it will return undefined. // - If the item is pending to resolve (there is a request pending to resolve) it will return undefined. const currentValue = get(key)
// Gets an element from the cache. The difference with the get is that // this method r