Skip to content

Preloading not working for on-demand queries #909

@Raj1v

Description

@Raj1v

minimal_repo.zip

  • I've validated the bug against the latest version of DB packages

Describe the bug

When using syncMode: "on-demand" with a query collection, calling:

  1. liveQuery.preload()
  2. liveQuery.stateWhenReady()

returns immediately with 0 items, even though the fetch has been triggered.

The stateWhenReady() promise resolves before the data actually arrives.


To Reproduce

  1. Create a query collection with syncMode: "on-demand"
  2. Create a live query collection from it
  3. Call await liveQuery.preload()
  4. Call await liveQuery.stateWhenReady()
  5. Check state.size — it's 0 even though queryFn was called

Minimal reproduction:

const postsCollection = createCollection(
  queryCollectionOptions({
    queryKey: ["posts"],
    queryFn: async () => {
      const res = await fetch("https://jsonplaceholder.typicode.com/posts");
      return res.json();
    },
    syncMode: "on-demand",
    queryClient,
    getKey: (item) => item.id,
  })
);

const posts = createLiveQueryCollection({
  query: (q) =>
    q
      .from({ post: postsCollection })
      .where(({ post }) => eq(post.userId, 1))
      .orderBy(({ post }) => post.id, "asc")
      .limit(10),
});

await posts.preload();

const state = await posts.stateWhenReady();
console.log(state.size); // ❌ 0 (expected: 10)

await sleep(2000);

const state2 = await posts.stateWhenReady();
console.log(state2.size); // ✅ 10 (data arrived after waiting)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions