RTK Query: don't refetch on subscribe after error #1349
-
I have a pretty basic endpoint for getting the current logged-in user that looks like this: getUser: builder.query<User, void>({
query: () => 'users/me',
providesTags: ['User'],
keepUnusedDataFor: 9999999, // Keep the user in the cache until sign out
}), If the user is unauthenticated, this will return 403. When a component subscribes to this query, if it is in this 403 state, then it will refetch on mount. If it responds with 200 and some data, then RTK Query is satisfied and will not refetch the data on mount (I have refetchOnMountOrArgChange: false). Is there any way to not refetch this on mount if there's been an error? I only want to refetch it when the 'User' tag is invalidated. I know I could probably define a custom queryFn to not error on 403, and instead return success and some placeholder user data. But I wanted to ask if I was doing this correctly. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
I worked a bit more and think I understand this better now. RTK Query isn't really designed to consider an error response from the server as indicating "no data"; rather it interprets it as "could not access the resource" and attempts to load it again on a new subscription. I've decided to handle auth logic (fetching the authenticated user, sign in, etc.) just using regular Redux actions, and save RTK Query for all the stuff after. Thanks for making this library, it's been enjoyable to use :) |
Beta Was this translation helpful? Give feedback.
-
Sorry for the late response - I actually had looked at the issues and not found this one again 😅 Honestly, I think I just missed this scenario conceptually when writing that part of the lib. Now the question is what the best behaviour would be. 🤔 |
Beta Was this translation helpful? Give feedback.
-
@phryneas Do you have a suggestion on how to handle things like 404s? I am experiencing an infinite refetch on an error that the only way I've found to address is by returning Let's say I have a component that fetches a post and an author, if the post returns a 404 I see it keep attempting to refetch. Maybe I am doing something wrong, but I've exhausted my investigation so far and could use some help. function Post({ postId }) {
const { data: post, isLoading: postIsLoading, error: postError } = useFetchPost({ postId });
const { data: author, isLoading: authorIsLoading } = useFetchAuthorByPost({ postId });
if (postIsLoading || authorIsLoading) {
return <div>Loading...</div>;
}
if (postError) {
return <div>Failed to load post</div>;
}
return <h1>{post.title} - {author.name}</h1>
} |
Beta Was this translation helpful? Give feedback.
Sorry for the late response - I actually had looked at the issues and not found this one again 😅
Honestly, I think I just missed this scenario conceptually when writing that part of the lib. Now the question is what the best behaviour would be. 🤔
I think the current behaviour makes sense, but we will probably need a way to opt out of those refresh retries - probably a
refetchOnError
option on hook level, endpoint level (and maybe even api level) that defaults to true but can be changed.