Skip to content

Commit

Permalink
skip immediate revalidation when initalData is provided (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
addstar34 authored and shuding committed Dec 22, 2019
1 parent d4db9c4 commit 5b96586
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,18 @@ function useSWR<Data = any, Error = any>(
const softRevalidate = () => revalidate({ dedupe: true })

// trigger a revalidation
if (
typeof latestKeyedData !== 'undefined' &&
!IS_SERVER &&
window['requestIdleCallback']
) {
// delay revalidate if there's cache
// to not block the rendering
window['requestIdleCallback'](softRevalidate)
} else {
softRevalidate()
if (!config.initialData) {
if (
typeof latestKeyedData !== 'undefined' &&
!IS_SERVER &&
window['requestIdleCallback']
) {
// delay revalidate if there's cache
// to not block the rendering
window['requestIdleCallback'](softRevalidate)
} else {
softRevalidate()
}
}

// whenever the window gets focused, revalidate
Expand Down
9 changes: 4 additions & 5 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,21 @@ describe('useSWR', () => {
})

it('should accept initial data', async () => {
const fetcher = jest.fn(() => 'SWR')

function Page() {
const { data } = useSWR('initial-data-1', () => 'SWR', {
const { data } = useSWR('initial-data-1', fetcher, {
initialData: 'Initial'
})
return <div>hello, {data}</div>
}

const { container } = render(<Page />)

expect(fetcher).not.toBeCalled()
expect(container.firstChild.textContent).toMatchInlineSnapshot(
`"hello, Initial"`
)
await waitForDomChange({ container }) // mount
expect(container.firstChild.textContent).toMatchInlineSnapshot(
`"hello, SWR"`
)
})

it('should set config as second parameter', async () => {
Expand Down

0 comments on commit 5b96586

Please sign in to comment.