Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/guard-infinite-undefined-pages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@tanstack/query-core': patch
---

fix(query-core): guard against malformed `InfiniteData` whose `pages` is `undefined`

`hasNextPage`/`hasPreviousPage` only checked for a missing `data`, so a malformed
`InfiniteData` (where `data.pages` is `undefined`) reaching `getNextPageParam`
threw `TypeError: Cannot read properties of undefined (reading 'length')`. This
can happen at runtime via SSR hydration or an external `setQueryData`. Guard
`pages` before reading `.length` so these helpers return `false`/`undefined`
instead of crashing.
19 changes: 18 additions & 1 deletion packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { CancelledError, InfiniteQueryObserver, QueryClient } from '..'
import { infiniteQueryBehavior } from '../infiniteQueryBehavior'
import {
hasNextPage,
hasPreviousPage,
infiniteQueryBehavior,
} from '../infiniteQueryBehavior'
import type { InfiniteData, InfiniteQueryObserverResult, QueryCache } from '..'

describe('InfiniteQueryBehavior', () => {
Expand Down Expand Up @@ -530,4 +534,17 @@ describe('InfiniteQueryBehavior', () => {

unsubscribe()
})

it('should not throw when data has no pages array (malformed InfiniteData)', () => {
const options = {
initialPageParam: 1,
getNextPageParam: () => 1,
getPreviousPageParam: () => 1,
}
const malformed = {} as InfiniteData<unknown>

// Should return false instead of throwing on `pages.length`.
expect(hasNextPage(options, malformed)).toBe(false)
expect(hasPreviousPage(options, malformed)).toBe(false)
})
})
30 changes: 17 additions & 13 deletions packages/query-core/src/infiniteQueryBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,30 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(

function getNextPageParam(
options: InfiniteQueryPageParamsOptions<any>,
{ pages, pageParams }: InfiniteData<unknown>,
{ pages, pageParams }: Partial<InfiniteData<unknown>>,
): unknown | undefined {
// `pages` can be undefined at runtime (hydration, `setQueryData`).
if (!pages?.length) return undefined
const lastIndex = pages.length - 1
return pages.length > 0
? options.getNextPageParam(
pages[lastIndex],
pages,
pageParams[lastIndex],
pageParams,
)
: undefined
return options.getNextPageParam(
pages[lastIndex],
pages,
pageParams?.[lastIndex],
pageParams ?? [],
)
}

function getPreviousPageParam(
options: InfiniteQueryPageParamsOptions<any>,
{ pages, pageParams }: InfiniteData<unknown>,
{ pages, pageParams }: Partial<InfiniteData<unknown>>,
): unknown | undefined {
return pages.length > 0
? options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams)
: undefined
if (!pages?.length) return undefined
return options.getPreviousPageParam?.(
pages[0],
pages,
pageParams?.[0],
pageParams ?? [],
)
}

/**
Expand Down