From 9aa237328520dc018b57bc5d4356a662df35f2a2 Mon Sep 17 00:00:00 2001 From: sanjeev40 Date: Sun, 1 May 2022 15:34:52 +0530 Subject: [PATCH 1/6] fix: mutate args for useSWRInfinite hook --- infinite/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/infinite/index.ts b/infinite/index.ts index d8a4f500c..828f696a0 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -180,9 +180,13 @@ export const infinite = ((useSWRNext: SWRHook) => ] ) => { const data = args[0] + + // When passing as a boolean, it's explicitly used to disable/enable + // revalidation. + const options = typeof args[1] === 'boolean' ? {revalidate: args[1]} : args[1] || {} // Default to true. - const shouldRevalidate = args[1] !== false + const shouldRevalidate = options.revalidate !== false // It is possible that the key is still falsy. if (!infiniteKey) return From 96604858c5fe149e4c7d6a920d8b8cff0028ef82 Mon Sep 17 00:00:00 2001 From: sanjeev40 Date: Sun, 1 May 2022 17:13:06 +0530 Subject: [PATCH 2/6] Add swr infinite test for revalidation with arguments --- test/use-swr-infinite.test.tsx | 35 +++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test/use-swr-infinite.test.tsx b/test/use-swr-infinite.test.tsx index 05bb3603b..183909ccd 100644 --- a/test/use-swr-infinite.test.tsx +++ b/test/use-swr-infinite.test.tsx @@ -1181,7 +1181,7 @@ describe('useSWRInfinite', () => { await screen.findByText('data: B1,B2,B3') }) - it('should revalidate when the component is mounted and revalidateOnMount is enabled', async () => { + it('should revalidate the resource with bound mutate when arguments are passed', async () => { const key = createKey() let counter = 0 @@ -1220,4 +1220,37 @@ describe('useSWRInfinite', () => { fireEvent.click(screen.getByText('mutate')) await screen.findByText('data: 2') }) + + // https://github.com/vercel/swr/issues/1899 + it('should revalidate the resource with bound mutate when arguments are passed', async () => { + let t = 0 + const key = createKey() + const fetcher = jest.fn(async () => + createResponse(`foo-${t++}`, { delay: 10 }) + ) + const logger = [] + function Page() { + const { data, mutate } = useSWRInfinite(() => key, fetcher, { + dedupingInterval: 0 + }) + logger.push(data) + return ( + <> +
data: {String(data)}
+ + + ) + } + + renderWithConfig() + await screen.findByText('data: foo-0') + + fireEvent.click(screen.getByText('mutate')) + await screen.findByText('data: foo-1') + expect(fetcher).toBeCalledTimes(2) + + expect(logger).toEqual([undefined, ['foo-0'], ['foo-1']]) + }) }) From b6f47817246b1a9c435a45a5a9323ead763f78e7 Mon Sep 17 00:00:00 2001 From: sanjeev40 Date: Sun, 1 May 2022 17:32:55 +0530 Subject: [PATCH 3/6] Add swr infinite test for non-revalidation with arguments --- test/use-swr-infinite.test.tsx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/use-swr-infinite.test.tsx b/test/use-swr-infinite.test.tsx index 183909ccd..91d1db3cb 100644 --- a/test/use-swr-infinite.test.tsx +++ b/test/use-swr-infinite.test.tsx @@ -1253,4 +1253,36 @@ describe('useSWRInfinite', () => { expect(logger).toEqual([undefined, ['foo-0'], ['foo-1']]) }) + + // https://github.com/vercel/swr/issues/1899 + it('should not revalidate the resource with bound mutate when options is of Object type', async () => { + let t = 0 + const key = createKey() + const fetcher = jest.fn(async () => + createResponse(`foo-${t++}`, { delay: 10 }) + ) + const logger = [] + function Page() { + const { data, mutate } = useSWRInfinite(() => key, fetcher, { + dedupingInterval: 0 + }) + logger.push(data) + return ( + <> +
data: {String(data)}
+ + + ) + } + + renderWithConfig() + await screen.findByText('data: foo-0') + + fireEvent.click(screen.getByText('mutate')) + expect(fetcher).toBeCalledTimes(1) + + expect(logger).toEqual([undefined, ['foo-0']]) + }) }) From a3221302b3558a443f8cfa5d6e3dd27bb7c8f138 Mon Sep 17 00:00:00 2001 From: sanjeev40 Date: Tue, 3 May 2022 10:58:35 +0530 Subject: [PATCH 4/6] Rename swr infinite mutate test case --- test/use-swr-infinite.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/use-swr-infinite.test.tsx b/test/use-swr-infinite.test.tsx index 91d1db3cb..7bf2b6b20 100644 --- a/test/use-swr-infinite.test.tsx +++ b/test/use-swr-infinite.test.tsx @@ -1222,7 +1222,7 @@ describe('useSWRInfinite', () => { }) // https://github.com/vercel/swr/issues/1899 - it('should revalidate the resource with bound mutate when arguments are passed', async () => { + it('should revalidate the resource with bound mutate when options is of Object type ', async () => { let t = 0 const key = createKey() const fetcher = jest.fn(async () => From c0d8d7aabc80deedabf45eb47049d49ea00dd92b Mon Sep 17 00:00:00 2001 From: sanjeev40 Date: Sat, 14 May 2022 13:46:14 +0530 Subject: [PATCH 5/6] Update type for infite mutate args --- infinite/index.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/infinite/index.ts b/infinite/index.ts index 828f696a0..eae30aa40 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -7,7 +7,8 @@ import useSWR, { SWRHook, MutatorCallback, Middleware, - BareFetcher + BareFetcher, + MutatorOptions } from 'swr' import { useIsomorphicLayoutEffect } from '../src/utils/env' @@ -176,14 +177,15 @@ export const infinite = ((useSWRNext: SWRHook) => | [undefined | Data[] | Promise | MutatorCallback] | [ undefined | Data[] | Promise | MutatorCallback, - boolean + undefined | boolean | MutatorOptions ] ) => { const data = args[0] - + // When passing as a boolean, it's explicitly used to disable/enable // revalidation. - const options = typeof args[1] === 'boolean' ? {revalidate: args[1]} : args[1] || {} + const options = + typeof args[1] === 'boolean' ? { revalidate: args[1] } : args[1] || {} // Default to true. const shouldRevalidate = options.revalidate !== false From fd2d3db858e8327e040609460b1b2f923f81b34c Mon Sep 17 00:00:00 2001 From: sanjeev40 Date: Sat, 14 May 2022 14:15:12 +0530 Subject: [PATCH 6/6] Include 'MutatorOptions' import --- infinite/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/infinite/index.ts b/infinite/index.ts index 659dd80d2..1f8fd4a61 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -15,7 +15,8 @@ import { BareFetcher, useIsomorphicLayoutEffect, serialize, - withMiddleware + withMiddleware, + MutatorOptions } from 'swr/_internal' import type {