@@ -150,36 +150,59 @@ export function useLiveInfiniteQuery<TContext extends Context>(
150150 const [ loadedPageCount , setLoadedPageCount ] = useState ( 1 )
151151 const [ isFetchingNextPage , setIsFetchingNextPage ] = useState ( false )
152152
153- // Track collection instance and whether we've validated it (only for pre-created collections)
154- const collectionRef = useRef ( isCollection ? queryFnOrCollection : null )
155- const hasValidatedCollectionRef = useRef ( false )
153+ // Track whether we've set initial window for current collection instance
154+ const hasSetInitialWindowRef = useRef ( false )
155+ const prevCollectionRef = useRef ( isCollection ? queryFnOrCollection : null )
156156
157157 // Track deps for query functions (stringify for comparison)
158158 const depsKey = JSON . stringify ( deps )
159159 const prevDepsKeyRef = useRef ( depsKey )
160160
161- // Reset pagination when inputs change
162- useEffect ( ( ) => {
163- let shouldReset = false
161+ // Validate pre-created collections have orderBy (required for infinite pagination)
162+ // and set initial window BEFORE useLiveQuery is called
163+ if ( isCollection ) {
164+ const utils = queryFnOrCollection . utils
165+ if ( ! isLiveQueryCollectionUtils ( utils ) ) {
166+ throw new Error (
167+ `useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. ` +
168+ `Please add .orderBy() to your createLiveQueryCollection query.`
169+ )
170+ }
164171
172+ // Check if this is a new collection instance
173+ const isNewCollection = prevCollectionRef . current !== queryFnOrCollection
174+ if ( isNewCollection ) {
175+ hasSetInitialWindowRef . current = false
176+ }
177+
178+ // Set initial window to override any pre-set limit from collection creation
179+ // This must happen BEFORE useLiveQuery is called below
180+ if ( ! hasSetInitialWindowRef . current ) {
181+ const initialLimit = pageSize + 1 // +1 for peek ahead
182+ console . log ( { initialLimit } )
183+ utils . setWindow ( {
184+ offset : 0 ,
185+ limit : initialLimit ,
186+ } )
187+ hasSetInitialWindowRef . current = true
188+ }
189+ }
190+
191+ // Reset page count when collection instance or deps change
192+ useEffect ( ( ) => {
165193 if ( isCollection ) {
166- // Reset if collection instance changed
167- if ( collectionRef . current !== queryFnOrCollection ) {
168- collectionRef . current = queryFnOrCollection
169- hasValidatedCollectionRef . current = false
170- shouldReset = true
194+ // Check if collection instance changed
195+ if ( prevCollectionRef . current !== queryFnOrCollection ) {
196+ prevCollectionRef . current = queryFnOrCollection
197+ setLoadedPageCount ( 1 )
171198 }
172199 } else {
173200 // Reset if deps changed (for query functions)
174201 if ( prevDepsKeyRef . current !== depsKey ) {
175202 prevDepsKeyRef . current = depsKey
176- shouldReset = true
203+ setLoadedPageCount ( 1 )
177204 }
178205 }
179-
180- if ( shouldReset ) {
181- setLoadedPageCount ( 1 )
182- }
183206 } , [ isCollection , queryFnOrCollection , depsKey ] )
184207
185208 // Create a live query with initial limit and offset
@@ -199,36 +222,15 @@ export function useLiveInfiniteQuery<TContext extends Context>(
199222
200223 // Check if collection has orderBy (required for setWindow)
201224 if ( ! isLiveQueryCollectionUtils ( utils ) ) {
202- // For pre-created collections, throw an error if no orderBy
203- if ( isCollection ) {
204- throw new Error (
205- `useLiveInfiniteQuery: Pre-created live query collection must have an orderBy clause for infinite pagination to work. ` +
206- `Please add .orderBy() to your createLiveQueryCollection query.`
207- )
208- }
209225 return
210226 }
211227
212- // For pre-created collections, validate window on first check
213- if ( isCollection && ! hasValidatedCollectionRef . current ) {
214- const currentWindow = utils . getWindow ( )
215- if (
216- currentWindow &&
217- ( currentWindow . offset !== expectedOffset ||
218- currentWindow . limit !== expectedLimit )
219- ) {
220- console . warn (
221- `useLiveInfiniteQuery: Pre-created collection has window {offset: ${ currentWindow . offset } , limit: ${ currentWindow . limit } } ` +
222- `but hook expects {offset: ${ expectedOffset } , limit: ${ expectedLimit } }. Adjusting window now.`
223- )
224- }
225- hasValidatedCollectionRef . current = true
226- }
227-
228228 // For query functions, wait until collection is ready
229229 if ( ! isCollection && ! queryResult . isReady ) return
230230
231- // Adjust the window
231+ // Adjust the window based on current page count
232+ // For pre-created collections, this handles pagination beyond the first page
233+ // For query functions, this handles all pagination including the first page
232234 const result = utils . setWindow ( {
233235 offset : expectedOffset ,
234236 limit : expectedLimit ,
0 commit comments