Skip to content

[bug]: useInfiniteQuery with "next": "13.5.1" and above (using app dir and server actions), fetchStatus status is always fetching (after next fetchNextPage call) when navigating via next.js <Link /> to dynamic route, then coming back via browser back button #6145

@VladSez

Description

@VladSez

Describe the bug

Hello, thanks for a wonderful library

I am using useInfiniteQuery from @tanstack/react-query with next.js and app dir and server actions to implement infinite loading pattern.

Starting from "next": "13.5.1" I experience a bug, when navigating to dynamic route via next.js Link and coming back via browser back button

I found that after browser back navigation from dynamic route and second call to fetchNextPage while scrolling, fetchStatus is stuck in status fetching as well as isFetchingNextPage and isFetching are both stuck in true (please, check the attached video)

// in useEffect

// this was working before "next": "13.5.1" (in next: 13.4.19)  now, has a described above bug
if (inView && hasNextPage && !isFetchingNextPage) {
    console.log("fetching next page!!!!!!!!!!!!!!!!!!");
    void fetchNextPage();
}

// to make it work in "next": "13.5.1" and above we need to remove `isFetchingNextPage` check
// this will work, but this is not optimal
// if (inView && hasNextPage) {
//   console.log("fetching next page!!!!!!!!!!!!!!!!!!");
//   void fetchNextPage();
// }

I think the issue could be in next.js server actions or app dir, because seem's like root page is getting rerendered on the server after navigating back via browser button and then calling server action (check video and server console), so not sure if this @tanstack/react-query or next bug

If we downgrade next to 13.4.19 everything works as expected
Also if I just scroll the page without navigating to dynamic route via next js Link and not using browser back button to return back, everything works as expected

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/nextjs-bug-13-5-4-forked-r4zfnj?file=%2Fapp%2Fcharacters.tsx%3A1%2C1

Steps to reproduce

  1. Go to root page
  2. Click on any link
  3. Navigate back via browser button
  4. Scroll down to the end of the page (first call to fetchNextPage should be good)
  5. Scroll further
  6. Notice that page is stuck with "Fetching next page..." message

Also, please, check the attached video

Expected behavior

isFetchingNextPage prop should have a correct info

How often does this bug happen?

Every time

Screenshots or Videos

Screenshare.-.2023-10-13.12_27_16.AM.mp4

Platform

Operating System:
Platform: linux
Arch: x64
Version: #1 SMP PREEMPT_DYNAMIC Sun Aug 6 20:05:33 UTC 2023

Browser: Google Chrome Desktop Version 117.0.5938.152 (Official Build) (64-bit)

Tanstack Query adapter

react-query

TanStack Query version

v4.36.1

TypeScript version

5.2.2

Additional context

I found that if i am not using infinite loading pattern (use effect + intersection observer) and use a button with onClick to append next page data to the list, seems like the issue is gone, so could be that something is wrong with my code or useEffect in app dir

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions