Skip to content

Commit

Permalink
revalidateOnMount flag that disable automatic revalidation on mount (#…
Browse files Browse the repository at this point in the history
…225)

* Added autoLoad flag which defaults to true and disable automatic revalidation on mount

* Fixed config usage

* Added test coverage

* Update src/use-swr.ts

Co-Authored-By: Shu Ding <ds303077135@gmail.com>

* Update test/use-swr.test.tsx

Co-Authored-By: Shu Ding <ds303077135@gmail.com>

* Renamed option to revalidateInitialData, updated documentation README to explain usage

* Update src/use-swr.ts

Co-Authored-By: Sergio Xalambrí <hello@sergiodxa.com>

* Renamed config, fixed source, docs and tests

* Changed revalidateOnMount to a 3-state flag

* removed example from readme

Co-authored-by: Shu Ding <ds303077135@gmail.com>
Co-authored-by: Sergio Xalambrí <hello@sergiodxa.com>
  • Loading branch information
3 people committed May 12, 2020
1 parent 40914f1 commit cd095dd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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

0 comments on commit cd095dd

Please sign in to comment.