mirror of
https://github.com/Nezumi-2711/onedrive-vercel-index.git
synced 2026-09-22 20:01:46 +00:00
32 lines
860 B
TypeScript
32 lines
860 B
TypeScript
import useSWR, { cache, Key } from 'swr'
|
|
import axios from 'axios'
|
|
|
|
/**
|
|
* Extract the current web page's base url
|
|
* @returns base url of the page
|
|
*/
|
|
export const getBaseUrl = () => {
|
|
if (typeof window !== 'undefined') {
|
|
return window.location.origin
|
|
}
|
|
return ''
|
|
}
|
|
|
|
// Common axios fetch function
|
|
const fetcher = (url: string) => axios.get(url).then(res => res.data)
|
|
/**
|
|
* Use stale SWR instead of revalidating on each request. Not ideal for this scenario but have to do
|
|
* if fetching serverside props from component instead of pages.
|
|
* @param dataKey request url
|
|
* @returns useSWR instance
|
|
*/
|
|
export const useStaleSWR = (dataKey: Key) => {
|
|
const revalidationOptions = {
|
|
revalidateOnMount: !cache.has(dataKey),
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
}
|
|
|
|
return useSWR(dataKey, fetcher, revalidationOptions)
|
|
}
|