diff --git a/_internal/src/utils/mutate.ts b/_internal/src/utils/mutate.ts index 4667b1b7c..baee2b615 100644 --- a/_internal/src/utils/mutate.ts +++ b/_internal/src/utils/mutate.ts @@ -97,8 +97,8 @@ export async function internalMutate( cache ) as GlobalState - const revalidators = EVENT_REVALIDATORS[key] const startRevalidate = () => { + const revalidators = EVENT_REVALIDATORS[key] if (revalidate) { // Invalidate the key by deleting the concurrent request markers so new // requests will not be deduped. diff --git a/test/use-swr-remote-mutation.test.tsx b/test/use-swr-remote-mutation.test.tsx index ef4dc9e2f..514366418 100644 --- a/test/use-swr-remote-mutation.test.tsx +++ b/test/use-swr-remote-mutation.test.tsx @@ -537,6 +537,51 @@ describe('useSWR - remote mutation', () => { expect(logger).not.toHaveBeenCalledWith('bar') }) + it('should revalidate the SWR request that starts during the mutation', async () => { + const key = createKey() + + function Page() { + const [triggered, setTriggered] = React.useState(false) + const { data } = useSWR( + triggered ? key : null, + async () => { + await sleep(10) + return 'foo' + }, + { revalidateOnMount: false } + ) + const { trigger } = useSWRMutation(key, async () => { + await sleep(20) + return 'bar' + }) + + return ( +
+ +
data:{data || 'none'}
+
+ ) + } + + render() + + // mount + await screen.findByText('data:none') + + fireEvent.click(screen.getByText('trigger')) + await act(() => sleep(50)) + + // The SWR request that starts during the mutation should be revalidated. + await screen.findByText('data:foo') + }) + it('should revalidate after populating the cache', async () => { const key = createKey() const logger = jest.fn()