perf(ui): a next-page fetch joins the one in flight instead of sending the cursor again
react-query's fetchNextPage cancels a next page still loading and sends it again by default.
Our requests ignore the abort signal, so the server ran both: the feed's read-ahead effect
re-runs on scroll frames before isFetchingNextPage reaches it, and on prod every feed page
went out 2-3 times (a 0.8 s title sort queued to 2.3 s). Every call now passes NEXT_PAGE
({cancelRefetch: false}), lint-enforced, with a contract test.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// A next-page call while that page is still loading joins it instead of sending it again (E31 S4e):
|
||||
// on prod every feed page went out 2–3 times with the same cursor.
|
||||
import { InfiniteQueryObserver } from "@tanstack/react-query";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { NEXT_PAGE } from "./nextPage";
|
||||
import { createQueryClient } from "./queryClient";
|
||||
|
||||
describe("NEXT_PAGE", () => {
|
||||
it("sends each page once when the read-ahead calls twice in a row", async () => {
|
||||
const sent: (string | null)[] = [];
|
||||
const observer = new InfiniteQueryObserver(createQueryClient(), {
|
||||
queryKey: ["probe"],
|
||||
initialPageParam: null as string | null,
|
||||
queryFn: async ({ pageParam }) => {
|
||||
sent.push(pageParam);
|
||||
await new Promise((r) => setTimeout(r, 5));
|
||||
return { next: `c${sent.length}` };
|
||||
},
|
||||
getNextPageParam: (last: { next: string }) => last.next,
|
||||
});
|
||||
const unsubscribe = observer.subscribe(() => {});
|
||||
await observer.refetch();
|
||||
|
||||
// Two scroll frames before `isFetchingNextPage` reaches the component.
|
||||
void observer.fetchNextPage(NEXT_PAGE);
|
||||
await observer.fetchNextPage(NEXT_PAGE);
|
||||
|
||||
expect(sent).toEqual([null, "c1"]);
|
||||
expect(observer.getCurrentResult().data?.pages).toHaveLength(2);
|
||||
unsubscribe();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user