-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(query-core): ensure query refetches on mount/retry when status is error (#9728) #9927
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
Merged
+197
−0
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d10cca3
fix(query-core): ensure query refetches on mount/retry when status is…
xiangnuans 4502d56
Merge branch 'main' into fix/9728-retry-after-error
TkDodo b5d5e73
fix: mark queries as invalidated on background errors instead of chec…
xiangnuans fd6c19b
Merge branch 'fix/9728-retry-after-error' of https://github.com/xiang…
xiangnuans 12e4897
Merge branch 'main' into fix/9728-retry-after-error
TkDodo d5caeb8
Merge branch 'main' into fix/9728-retry-after-error
TkDodo 5f316e3
refactor: simplify isInvalidated logic in error case
xiangnuans 233d48e
Merge branch 'fix/9728-retry-after-error' of https://github.com/xiang…
xiangnuans 7c4c681
Merge branch 'main' into fix/9728-retry-after-error
TkDodo 26035a4
Apply suggestion from @TkDodo
TkDodo c0cea84
Apply suggestions from code review
TkDodo e059010
Merge branch 'main' into fix/9728-retry-after-error
TkDodo 08a6f2b
fix: eslint
TkDodo 909a504
fix: disable retry in background error tests to prevent timeout
xiangnuans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/query-core': patch | ||
| --- | ||
|
|
||
| Fix: Always treat existing data as stale when query goes into error state. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // @vitest-environment jsdom | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { fireEvent, render } from '@testing-library/react' | ||
| import * as React from 'react' | ||
| import { ErrorBoundary } from 'react-error-boundary' | ||
| import { queryKey } from '@tanstack/query-test-utils' | ||
| import { | ||
| QueryClient, | ||
| QueryClientProvider, | ||
| QueryErrorResetBoundary, | ||
| useQuery, | ||
| } from '..' | ||
|
|
||
| describe('issue 9728', () => { | ||
| it('should refetch after error when staleTime is Infinity and previous data exists', async () => { | ||
| const key = queryKey() | ||
| const queryFn = vi.fn() | ||
| let count = 0 | ||
|
|
||
| queryFn.mockImplementation(async () => { | ||
| count++ | ||
| if (count === 2) { | ||
| throw new Error('Error ' + count) | ||
| } | ||
| return 'Success ' + count | ||
| }) | ||
|
|
||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| retry: false, | ||
| staleTime: Infinity, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| function Page() { | ||
| const [_, forceUpdate] = React.useState(0) | ||
|
|
||
| React.useEffect(() => { | ||
| forceUpdate(1) | ||
| }, []) | ||
|
|
||
| const { data, refetch } = useQuery({ | ||
| queryKey: key, | ||
| queryFn, | ||
| throwOnError: true, | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <div>Data: {data}</div> | ||
| <button onClick={() => refetch()}>Refetch</button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function App() { | ||
| return ( | ||
| <QueryErrorResetBoundary> | ||
| {({ reset }) => ( | ||
| <ErrorBoundary | ||
| onReset={reset} | ||
| fallbackRender={({ resetErrorBoundary }) => ( | ||
| <div> | ||
| <div>Status: error</div> | ||
| <button onClick={resetErrorBoundary}>Retry</button> | ||
| </div> | ||
| )} | ||
| > | ||
| <React.Suspense fallback={<div>Loading...</div>}> | ||
| <Page /> | ||
| </React.Suspense> | ||
| </ErrorBoundary> | ||
| )} | ||
| </QueryErrorResetBoundary> | ||
| ) | ||
| } | ||
|
|
||
| const { getByText, findByText } = render( | ||
| <React.StrictMode> | ||
| <QueryClientProvider client={queryClient}> | ||
| <App /> | ||
| </QueryClientProvider> | ||
| </React.StrictMode>, | ||
| ) | ||
|
|
||
| // 1. First mount -> Success | ||
| await findByText('Data: Success 1') | ||
| expect(queryFn).toHaveBeenCalledTimes(1) | ||
|
|
||
| // 2. Click Refetch -> Triggers fetch -> Fails (Error 2) -> ErrorBoundary | ||
| fireEvent.click(getByText('Refetch')) | ||
|
|
||
| // Wait for error UI | ||
| await findByText('Status: error') | ||
| expect(queryFn).toHaveBeenCalledTimes(2) | ||
|
|
||
| // 3. Click Retry -> Remounts | ||
| // Because staleTime is Infinity and we have Data from (1), | ||
| // AND we are in Error state. | ||
| fireEvent.click(getByText('Retry')) | ||
|
|
||
| // Should call queryFn again (3rd time) and succeed | ||
| await findByText('Data: Success 3') | ||
| expect(queryFn).toHaveBeenCalledTimes(3) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.