Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,22 @@ describe('QueryErrorResetBoundary', () => {
consoleMock.mockRestore()
})

it('should not retry fetch if the reset error boundary has not been reset', async () => {
it('should retry fetch on remount even if the reset error boundary has not been reset', async () => {
const consoleMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)

const key = queryKey()

let succeed = false
let fetchCount = 0

function Page() {
const { data } = useQuery({
queryKey: key,
queryFn: () =>
sleep(10).then(() => {
fetchCount++
if (!succeed) throw new Error('Error')
return 'data'
}),
Expand Down Expand Up @@ -350,7 +352,8 @@ describe('QueryErrorResetBoundary', () => {

fireEvent.click(rendered.getByText('retry'))
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('error boundary')).toBeInTheDocument()
expect(rendered.getByText('data')).toBeInTheDocument()
expect(fetchCount).toBe(2)

consoleMock.mockRestore()
})
Expand Down Expand Up @@ -419,7 +422,7 @@ describe('QueryErrorResetBoundary', () => {
consoleMock.mockRestore()
})

it('should not retry fetch if the reset error boundary has not been reset after a previous reset', async () => {
it('should retry fetch on remount if the reset error boundary has not been reset after a previous reset', async () => {
const consoleMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
Expand Down Expand Up @@ -480,20 +483,14 @@ describe('QueryErrorResetBoundary', () => {
succeed = false
shouldReset = true

fireEvent.click(rendered.getByText('retry'))
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('error boundary')).toBeInTheDocument()
expect(rendered.getByText('retry')).toBeInTheDocument()

succeed = true
shouldReset = false

fireEvent.click(rendered.getByText('retry'))
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('error boundary')).toBeInTheDocument()

succeed = true
shouldReset = true

fireEvent.click(rendered.getByText('retry'))
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('data')).toBeInTheDocument()
Expand Down
9 changes: 7 additions & 2 deletions packages/react-query/src/errorBoundaryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ export const ensurePreventErrorBoundaryRetry = <
options.experimental_prefetchInRender ||
throwOnError
) {
// Prevent retrying failed query if the error boundary has not been reset yet
if (!errorResetBoundary.isReset()) {
// Prevent retrying failed query if the error boundary has not been reset yet.
// Allow retries on a fresh mount after the error boundary has unmounted the
// failed observer.
if (
!errorResetBoundary.isReset() &&
(options.suspense || query?.getObserversCount())
) {
options.retryOnMount = false
}
}
Expand Down