diff --git a/src/use-swr.ts b/src/use-swr.ts index 4ed3eba35..ef4c606d1 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -36,6 +36,11 @@ import { const IS_SERVER = typeof window === 'undefined' +// polyfill for requestIdleCallback +const rIC = IS_SERVER + ? null + : window['requestIdleCallback'] || (f => setTimeout(f, 1)) + // React currently throws a warning when using useLayoutEffect on the server. // To get around it, we can conditionally useEffect on the server (no-op) and // useLayoutEffect in the browser. @@ -437,14 +442,10 @@ function useSWR( config.revalidateOnMount || (!config.initialData && config.revalidateOnMount === undefined) ) { - if ( - typeof latestKeyedData !== 'undefined' && - !IS_SERVER && - window['requestIdleCallback'] - ) { + if (typeof latestKeyedData !== 'undefined') { // delay revalidate if there's cache // to not block the rendering - window['requestIdleCallback'](softRevalidate) + rIC(softRevalidate) } else { softRevalidate() } diff --git a/test/use-swr.test.tsx b/test/use-swr.test.tsx index 7a185f434..4eed92fe9 100644 --- a/test/use-swr.test.tsx +++ b/test/use-swr.test.tsx @@ -106,7 +106,8 @@ describe('useSWR', () => { return
hello, {data}
} - render() + const { container } = render() + await waitForDomChange({ container }) expect(fetch).toHaveBeenCalled() })