Kong / swrv

Stale-while-revalidate data fetching for Vue

Home Page:https://docs-swrv.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

revalidateOnMount

simowe opened this issue · 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 it does not exist and yes I would be interested in a PR!

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

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