Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ function useSWR<Data = any, Error = any>(

CONCURRENT_PROMISES_TS[key] = startAt = Date.now()

newData = await CONCURRENT_PROMISES[key]

setTimeout(() => {
delete CONCURRENT_PROMISES[key]
delete CONCURRENT_PROMISES_TS[key]
}, config.dedupingInterval)

newData = await CONCURRENT_PROMISES[key]

// trigger the success event,
// only do this for the original request.
config.onSuccess(newData, key, config)
Expand Down Expand Up @@ -470,7 +470,10 @@ function useSWR<Data = any, Error = any>(

// set up reconnecting when the browser regains network connection
let reconnect = null
if (typeof addEventListener !== 'undefined' && config.revalidateOnReconnect) {
if (
typeof addEventListener !== 'undefined' &&
config.revalidateOnReconnect
) {
reconnect = addEventListener('online', softRevalidate)
}

Expand Down
22 changes: 22 additions & 0 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,28 @@ describe('useSWR - local mutation', () => {
await act(() => new Promise(res => setTimeout(res, 110)))
expect(container.textContent).toMatchInlineSnapshot(`"data: 999"`)
})

it('should ignore in flight requests when mutating', async () => {
// set it to 1
mutate('mutate-2', 1)

function Section() {
const { data } = useSWR(
'mutate-2',
() => new Promise(res => setTimeout(() => res(2), 200))
)
return <div>{data}</div>
}

const { container } = render(<Section />)

expect(container.textContent).toMatchInlineSnapshot(`"1"`) // directly from cache
await act(() => new Promise(res => setTimeout(res, 150))) // still suspending
mutate('mutate-2', 3) // set it to 3. this will drop the ongoing request
expect(container.textContent).toMatchInlineSnapshot(`"3"`)
await act(() => new Promise(res => setTimeout(res, 100)))
expect(container.textContent).toMatchInlineSnapshot(`"3"`)
})
})

describe('useSWR - context configs', () => {
Expand Down