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
5 changes: 5 additions & 0 deletions .changeset/tall-moons-repeat.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@tanstack/preact-query': patch
---

fix(preact-query): do not go into optimistic fetching state when not subscribed
28 changes: 28 additions & 0 deletions packages/preact-query/src/__tests__/useQueries.test.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,6 +64,34 @@ describe('useQueries', () => {
expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }])
})

it('should not optimistically show fetching when unsubscribed', () => {
const key = queryKey()
const queryFn = vi.fn(() => Promise.resolve('data'))

function Page() {
const [query] = useQueries({
queries: [{ queryKey: key, queryFn }],
subscribed: false,
})

return (
<div>
<span>isFetching: {String(query.isFetching)}</span>
<span>fetchStatus: {query.fetchStatus}</span>
</div>
)
}

const rendered = renderWithClient(queryClient, <Page />)

expect(queryFn).not.toHaveBeenCalled()
expect(
queryClient.getQueryCache().find({ queryKey: key })!.observers.length,
).toBe(0)
rendered.getByText('isFetching: false')
rendered.getByText('fetchStatus: idle')
})

it('should track results', async () => {
const key1 = queryKey()
const results: Array<Array<UseQueryResult>> = []
Expand Down
29 changes: 29 additions & 0 deletions packages/preact-query/src/__tests__/useQuery.test.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -5895,6 +5895,35 @@ describe('useQuery', () => {
).toBe(1)
})

it('should not optimistically show fetching when subscribed is false', () => {
const key = queryKey()
const queryFn = vi.fn(() => Promise.resolve('data'))

function Page() {
const query = useQuery({
queryKey: key,
queryFn,
subscribed: false,
})

return (
<div>
<span>isFetching: {String(query.isFetching)}</span>
<span>fetchStatus: {query.fetchStatus}</span>
</div>
)
}

const rendered = renderWithClient(queryClient, <Page />)

expect(queryFn).not.toHaveBeenCalled()
expect(
queryClient.getQueryCache().find({ queryKey: key })!.observers.length,
).toBe(0)
rendered.getByText('isFetching: false')
rendered.getByText('fetchStatus: idle')
})

it('should not be attached to the query when subscribed is false', async () => {
const key = queryKey()
const queryFn = vi.fn(() => Promise.resolve('data'))
Expand Down
8 changes: 6 additions & 2 deletions packages/preact-query/src/useBaseQuery.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,10 +70,14 @@ export function useBaseQuery<
}
}

const subscribed = options.subscribed !== false

// Make sure results are optimistically set in fetching state before subscribing or updating options
defaultedOptions._optimisticResults = isRestoring
? 'isRestoring'
: 'optimistic'
: subscribed
? 'optimistic'
: undefined

ensureSuspenseTimers(defaultedOptions)
ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary, query)
Expand All@@ -91,7 +95,7 @@ export function useBaseQuery<
// note: this must be called before useSyncExternalStore
const result = observer.getOptimisticResult(defaultedOptions)

const shouldSubscribe = !isRestoring && options.subscribed !== false
const shouldSubscribe = !isRestoring && subscribed
useSyncExternalStore(
useCallback(
(onStoreChange) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/preact-query/src/useQueries.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -223,6 +223,7 @@ export function useQueries<
const client = useQueryClient(queryClient)
const isRestoring = useIsRestoring()
const errorResetBoundary = useQueryErrorResetBoundary()
const subscribed = options.subscribed !== false

const defaultedQueries = useMemo(
() =>
Expand All@@ -234,11 +235,13 @@ export function useQueries<
// Make sure the results are already in fetching state before subscribing or updating options
defaultedOptions._optimisticResults = isRestoring
? 'isRestoring'
: 'optimistic'
: subscribed
? 'optimistic'
: undefined

return defaultedOptions
}),
[queries, client, isRestoring],
[queries, client, isRestoring, subscribed],
)

defaultedQueries.forEach((queryOptions) => {
Expand All@@ -265,7 +268,7 @@ export function useQueries<
(options as QueriesObserverOptions<TCombinedResult>).combine,
)

const shouldSubscribe = !isRestoring && options.subscribed !== false
const shouldSubscribe = !isRestoring && subscribed
useSyncExternalStore(
useCallback(
(onStoreChange) =>
Expand Down