Skip to content

Make sure mutate invalidates concurrent requests #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shuding opened this issue Jan 14, 2022 · 2 comments
Open

Make sure mutate invalidates concurrent requests #1786

shuding opened this issue Jan 14, 2022 · 2 comments
Assignees
Labels
area: mutation bug Something isn't working

Comments

@shuding
Copy link
Member

shuding commented Jan 14, 2022

X-ref: nandorojo/swr-react-native#18

We need to make sure that mutate() invalidates concurrent requests and deduplicates them. It’ll be similar to this test case, but without a target value (only revalidates):

it('should ignore in flight requests when mutating', async () => {
const key = createKey()
// set it to 1
act(() => {
globalMutate(key, 1)
})
function Section() {
const { data } = useSWR(key, () => createResponse(2, { delay: 150 }))
return <div>{data}</div>
}
renderWithGlobalCache(<Section />)
screen.getByText('1') // directly from cache
await act(() => sleep(100)) // still suspending
act(() => {
globalMutate(key, 3)
}) // set it to 3. this will drop the ongoing request
await screen.findByText('3')
await act(() => sleep(100))
screen.getByText('3')
})

@shuding shuding added bug Something isn't working reproduction needed labels Jan 14, 2022
@shuding shuding self-assigned this Jan 14, 2022
@shuding
Copy link
Member Author

shuding commented Jan 14, 2022

It turns to be introduced in #1498 which I think should be the correct behavior, that mutate() will not be deduplicated by default because it was triggered explicitly by the user. So if calling two mutate()s in a row:

mutate()
mutate()

It's expected to have 2 revalidation requests started.

However requests fired with useSWR(key, fetcher) should be deduplicated, if we do mutate(). This is working as expected.

I think the solution here is to have a way to manually revalidate with deduplication enabled. We'll have to come up with a new option for that...

@pke
Copy link

pke commented Sep 2, 2024

@shuding But what if you have 2 components that run on a timer and use mutate to trigger a revalidate? Then we will have more than one request running, which does not make much sense. Even when the revalidate is user triggered, it should deduplicate. Otherwise this is highly confusing and not (!) documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: mutation bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants