invalidateQueries not triggering refetch after mutation in Next.js 14 with intercepting routes #8253
Unanswered
SergGrey1992
asked this question in
Q&A
Replies: 1 comment
-
The cache invalidation in React Query is not connected to the Next.js cache invalidation, which is likely causing the issue. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone,
I'm facing a perplexing issue in my application, and I hope someone can help me out.
Technologies Used:
Next.js 14 with App Routing
@tanstack/react-query: ^5.59.15
nuqs for query parameter handling
I'm using the following approach to manage my query keys:
At the URL localhost:3000/admin/documents, the following page component is rendered:
In the DocumentsList component, I fetch and display the data:
Up to this point, everything works as expected.
Problem:
When I attempt to delete an item or add a new one, I run into issues.
I handle deletion using the following mutation:
I open a modal window using @modal at the URL localhost:3000/admin/documents/remove/{id}.
After the mutation completes successfully, I call router.back() and expect the data to refetch because I invoked queryClient.invalidateQueries({ queryKey: opt.queryKey });.
Issue:
However, the refetch does not occur, which leaves me puzzled.
I tried simulating the functionality by adding a button that prepares the queryKey and calls queryClient.invalidateQueries({ queryKey: opt.queryKey });.
In this test, the refetch happens only once; on subsequent clicks, no additional requests are made.
What I've Tried:
Ensured that the queryKeys match exactly.
Set staleTime: 0 in the query options.
Used refetchOnMount: 'always' in the useQuery options.
Verified that the query is active when invalidateQueries is called.
Considered that the modal route might affect the active state of the query.
Question:
Can someone please help me figure out what's going on? I'm out of ideas at this point.
Additional Information:
The data-fetching component (DocumentsList) is rendered in the main page component.
The mutation occurs in a modal route, which may be affecting the query's active state.
I'm using Next.js 14's intercepting routes (@modal), which might be related to the issue.
Beta Was this translation helpful? Give feedback.
All reactions