Replies: 3 comments 3 replies
-
@igoldny it('should dedupe infinite requests', async () => {
const requestLogs = [];
const key = createKey()
const Data = ({ prefix }: { prefix: string }) => {
const { data } = useSWRInfinite(
index => `data-${index}-${key}`,
infiniteKey => {
requestLogs.push(infiniteKey)
return createResponse(infiniteKey)
}
)
return <div>{prefix}:{data}</div>;
}
const Page = () => {
return (
<>
<Data prefix="a" />
<Data prefix="b" />
</>
)
}
renderWithConfig(<Page />)
await screen.findByText(`a:data-0-${key}`)
await screen.findByText(`b:data-0-${key}`)
expect(requestLogs).toEqual([`data-0-${key}`])
}) |
Beta Was this translation helpful? Give feedback.
-
Hi @koba04 it('should dedupe infinite requests', async () => {
const requestLogs = [];
const key = createKey()
const Data = ({ prefix }: { prefix: string }) => {
const { data, setSize, size } = useSWRInfinite(
index => `data-${index}-${key}`,
infiniteKey => {
requestLogs.push(infiniteKey)
return createResponse(infiniteKey)
}
)
useEffect(() => {
if (size < 2) {
setSize(size + 1);
}
}, []);
return <div>{prefix}:{data?.[1]}</div>;
}
const Page = () => {
return (
<>
<Data prefix="a" />
<Data prefix="b" />
</>
)
}
const { findByText } = renderWithConfig(<Page />)
await findByText(`a:data-1-${key}`)
await findByText(`b:data-1-${key}`)
expect(requestLogs).toEqual([`data-0-${key}`, `data-1-${key}`])
}) |
Beta Was this translation helpful? Give feedback.
-
@xu3u4 it('should dedupe infinite requests', async () => {
const requestLogs = [];
const key = createKey()
const Data = ({ prefix }: { prefix: string }) => {
const { data, setSize, size } = useSWRInfinite(
index => `data-${index}-${key}`,
infiniteKey => {
requestLogs.push(infiniteKey)
return createResponse(infiniteKey)
},
{
revalidateFirstPage: false
}
)
return <button onClick={() => setSize(size + 1)}>{prefix}:{data}</button>;
}
const Page = () => {
return (
<>
<Data prefix="a" />
<Data prefix="b" />
</>
)
}
renderWithConfig(<Page />)
await screen.findByText(`a:data-0-${key}`)
fireEvent.click(screen.getByText(`a:data-0-${key}`))
await screen.findByText(`a:data-0-${key}data-1-${key}`)
expect(requestLogs).toEqual([`data-0-${key}`, `data-1-${key}`])
})
Your example is tricky because it runs |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What I am doing and trying to achieve
I am using useSWRInfinite for the pagination feature and this hook is mounted multiple times by different components which request the exact same requests multiple times.
Expected behavior
I expect the same behavior as useSWR deduplication, the first request is called and the others reuse the same request to avoid duplicated requests for better performance.
Actual behavior
multiple components mounted with useSWRInfinite calling duplicated requests without reusing them, causing unnecessary requests that impact page performance.
I would really appreciate any help
Thank you :) 👍
Beta Was this translation helpful? Give feedback.
All reactions