swrv icon indicating copy to clipboard operation
swrv copied to clipboard

revalidateOnMount

Open simowe opened this issue 3 years ago • 1 comments

Is it possible to disable revalidation when the component mounts like the "revalidateOnMount" option in swr https://swr.vercel.app/docs/options?

Sometimes you might want to permanently cache data that is expensive to fetch, or you know it will never change.

If it does not exist, would you be interested in a pull request that adds this feature?

simowe avatar Jun 15 '21 12:06 simowe

@simowe it does not exist and yes I would be interested in a PR!

darrenjennings avatar Jun 29 '21 02:06 darrenjennings

I tried to control the value of the key to determine whether to send the request at initialization time, and it seemed to work.

so that we can avoid sending duplicate requests when we click through swrv.

export function useUserSubmit() {
    const key = '/api/user/submit'

    const needSubmit = ref(false)
    const { data, error } = useSWRV(() => needSubmit.value ? key : null, submitInfo)

    function doSubmit() {
        needSubmit.value = true
        // will trigger submitInfo request
    }

    return {
        doSubmit,
        data,
        error
    }
}

tangxiangmin avatar Aug 14 '23 14:08 tangxiangmin

This looks like the desired implementation; essentially forcing swrv to utilize the cache.

adamdehaven avatar Aug 14 '23 15:08 adamdehaven