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
7 changes: 7 additions & 0 deletions .changeset/mutation-observer-resubscribe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/query-core': patch
---

fix(query-core): reconnect MutationObserver to the current mutation on resubscribe (StrictMode fix)

Previously, when a MutationObserver unsubscribed (e.g. React StrictMode's unmount cycle) while a mutation was in flight, it was removed from the mutation's observers list and never reattached on resubscribe, leaving `isPending` stuck as `true` indefinitely. `MutationObserver` now implements `onSubscribe()` to reattach to the current mutation — the same pattern `QueryObserver` already relies on.
32 changes: 32 additions & 0 deletions packages/query-core/src/__tests__/mutationObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ describe('mutationObserver', () => {
unsubscribe2()
})

it('should reconnect the observer to the current mutation on resubscribe', async () => {
const mutation = new MutationObserver(queryClient, {
mutationFn: (text: string) => sleep(20).then(() => text),
})

const subscriptionHandler = vi.fn()
const unsubscribe = mutation.subscribe(subscriptionHandler)

mutation.mutate('input')
expect(subscriptionHandler).toHaveBeenCalledTimes(1)
expect(mutation.getCurrentResult()).toMatchObject({ status: 'pending' })

// Simulates React StrictMode's unmount: the observer is removed from the mutation
unsubscribe()

// Simulates StrictMode's remount: the observer should reattach to the in-flight mutation
mutation.subscribe(subscriptionHandler)

await vi.advanceTimersByTimeAsync(20)

expect(subscriptionHandler).toHaveBeenLastCalledWith(
expect.objectContaining({
status: 'success',
data: 'input',
}),
)
expect(mutation.getCurrentResult()).toMatchObject({
status: 'success',
data: 'input',
})
})

it('unsubscribe should remove observer to trigger GC', async () => {
const mutation = new MutationObserver(queryClient, {
mutationFn: (text: string) => sleep(5).then(() => text),
Expand Down
8 changes: 8 additions & 0 deletions packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ export class MutationObserver<
}
}

protected onSubscribe(): void {
if (this.listeners.size === 1 && this.#currentMutation) {
this.#currentMutation.addObserver(this)
this.#updateResult()
this.#notify()
}
}

onMutationUpdate(
action: Action<TData, TError, TVariables, TOnMutateResult>,
): void {
Expand Down