Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revalidateOnMount flag that disable automatic revalidation on mount #225

Merged
merged 10 commits into from
May 12, 2020
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)
- `suspense = false`: enable React Suspense mode [(details)](#suspense-mode)
- `fetcher = undefined`: the default fetcher function
- `initialData`: initial data to be returned (note: This is per-hook)
- `revalidateOnMount`: enable or disable automatic revalidation when component is mounted (by default revalidation occurs on mount when initialData is not set, use this flag to force behavior)
- `revalidateOnFocus = true`: auto revalidate when window gets focused
- `revalidateOnReconnect = true`: automatically revalidate when the browser regains a network connection (via `navigator.onLine`)
- `refreshInterval = 0`: polling interval (disabled by default)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ConfigInterface<
refreshWhenHidden?: boolean
refreshWhenOffline?: boolean
revalidateOnFocus?: boolean
revalidateOnMount?: boolean
revalidateOnReconnect?: boolean
shouldRetryOnError?: boolean
fetcher?: Fn
Expand Down
5 changes: 4 additions & 1 deletion src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ function useSWR<Data = any, Error = any>(
const softRevalidate = () => revalidate({ dedupe: true })

// trigger a revalidation
if (!config.initialData) {
if (
config.revalidateOnMount ||
(!config.initialData && config.revalidateOnMount === undefined)
) {
if (
typeof latestKeyedData !== 'undefined' &&
!IS_SERVER &&
Expand Down
29 changes: 29 additions & 0 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,35 @@ describe('useSWR', () => {
)
})

it('should not call fetch function when revalidateOnMount is false', async () => {
const fetch = jest.fn(() => 'SWR')

function Page() {
const { data } = useSWR('revalidateOnMount', fetch, {
revalidateOnMount: false
})
return <div>hello, {data}</div>
}

render(<Page />)
expect(fetch).not.toHaveBeenCalled()
})

it('should call fetch function when revalidateOnMount is true even if initialData is set', async () => {
const fetch = jest.fn(() => 'SWR')

function Page() {
const { data } = useSWR('revalidateOnMount', fetch, {
revalidateOnMount: true,
initialData: 'gab'
})
return <div>hello, {data}</div>
}

render(<Page />)
expect(fetch).toHaveBeenCalled()
})

it('should dedupe requests by default', async () => {
let count = 0
const fetch = () => {
Expand Down