question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SWRInfinte bug]: repeated api calls for initial page index

See original GitHub issue

In useSWRInfinite, our colleagues have spotted this issue that initial page is always requested repeatedly with any next page request.

Made an infinite scroller and console logged pageIndex, and url from the fetcher function. If you see attached screenshots below (in order), you can easily spot the pattern.

For every incremental page , it is making double requests - one for page 1 [initial page], and another for the next page. Changing revalidation rules in options has no effect.

image

image

image

image

image

image

Here’s the custom hook used inside the scrolling div.

_

import { KeyLoader } from “swr”; import useSWRInfinite, { SWRInfiniteConfiguration } from “swr/infinite”; export const useFeedPagination = <T>( url: string, options?: SWRInfiniteConfiguration ) => { const PAGE_SIZE = 5; const getKey: KeyLoader = (pageIndex: number, previousPageData: any[]) => { console.log(“key index ****”, pageIndex); pageIndex = pageIndex > 0 ? parseInt(previousPageData[0].postId) + 1 : 1; if (previousPageData && !previousPageData.length) return null; // reached the end return ${url}/posts/${pageIndex}/comments; // SWR key };

const fetcher = (url: RequestInfo) => { console.log(“/////”, url.toString()); if (url.toString().includes(“/posts/1/”)) { // do } return fetch(url).then((res) => res.json()); };

const { data, size: page, setSize: setPage, error, isValidating, mutate, } = useSWRInfinite(getKey, fetcher, options);

const paginatedData: T[] = [].concat.apply([], data || []);

const isLoadingMore = data && typeof data[page - 1] === “undefined”;

const isReachedEnd = data && data[data.length - 1]?.length < PAGE_SIZE; return { mutate, error, paginatedData, page, setPage, isLoadingMore, isReachedEnd, isValidating, }; };

_

_Originally posted by @VewMet in https://github.com/vercel/swr/discussions/1612#discussioncomment-1648517_

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
shudingcommented, Dec 12, 2021

Since SWR 1.1.0, there is a new option revalidateFirstPage that you can use to turn this off. Related docs: https://github.com/vercel/swr/releases/tag/1.1.0

If you are interested, here are the full discussions of why we built this feature in the first place: https://github.com/vercel/swr/issues/1401#issuecomment-907764278, and why we now provide this new option to customize it.

Let me know if it helps you!

1reaction
patriqcommented, Nov 17, 2021

Yeah I ended up finding that PR and realizing it was intended. I was looking at the code for a workaround but I am glad to know that you are considering the first page revalidation optional. Thank you very much for the quick reply.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useSWRInfinite - getKey function always gets pageIndex as 1
The example from https://swr.vercel.app/docs/pagination#example-1-index-based-paginated-api. has the line. if (previousPageData && !
Read more >
Pagination
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
Read more >
How to fix SWR to work correctly with initialData or fallbackData
Problems with duplicate calls. I was working on an application that would send search requests to an api and return a set of...
Read more >
How to use SWR in Next JS - client-side data-fetching technique
The request key ( index ) is what SWR uses to know what data (page) to retrieve. The initial value of the request...
Read more >
Modern way of fetching data in React applications using SWR ...
Easy integration with custom fetcher and the fetch API ... In our pages/index.ts we gonna create our initial home page and fetch our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found