diff --git a/src/use-swr.ts b/src/use-swr.ts index 9c9e510b1..d6d21fbc6 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -382,16 +382,18 @@ function useSWR( 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 diff --git a/test/use-swr.test.tsx b/test/use-swr.test.tsx index 64b964dcc..bdb7cbdba 100644 --- a/test/use-swr.test.tsx +++ b/test/use-swr.test.tsx @@ -243,8 +243,10 @@ 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
hello, {data}
@@ -252,13 +254,10 @@ describe('useSWR', () => { const { container } = render() + 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 () => {