#Agents

4 messages · Page 1 of 1 (latest)

pure cypressBOT
#

Hi @midnight ember. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Great questions! Here’s what the knowledge sources say about each:


1. Preloading chat messages on SSR and passing to useThreadMessages

Currently, preloading chat messages with preloadQuery on the server and then passing them to the useThreadMessages hook in a client component is not directly supported. According to a Discord support thread, the recommended approach is to use preloadQuery to load just the first page of messages on the server, but then let the client-side usePaginatedQuery (or useThreadMessages) load the first page again on the client for reactivity. There isn’t a built-in way to hydrate useThreadMessages with preloaded data from SSR and have it remain reactive—so you’ll see a double fetch for the first page, but this is the current best practice with Convex and Next.js agent chatDiscord: Preloading Next.js thread messages.


2. Optimistic IDs for new messages before backend processing

Yes, you can show a new message optimistically before the backend processes it. The recommended way is to use the optimisticallySendMessage helper from @convex-dev/agent/react with your mutation. This will insert an ephemeral message at the top of your message list, giving the user immediate feedback that their message was sent. The ephemeral message will be replaced by the real message once the backend mutation completes.

Here’s a code example from the docs:

import { optimisticallySendMessage } from "@convex-dev/agent/react";

const sendMessage = useMutation(
  api.streaming.streamStoryAsynchronously,
).withOptimisticUpdate(
  optimisticallySendMessage(api.streaming.listThreadMessages),
);

If your mutation arguments differ, you can use optimisticallySendMessage as a helper inside your own optimistic update function.

#

Regarding redirecting to the new chat page (/c/{chatId}): after you create the thread (e.g., via createThread), you can redirect immediately using the returned chatId (or threadId). The optimistic update will ensure the UI shows the new message right away, even before the backend confirms itOptimistic updates for sending messages.


Summary:

  • SSR preloading for useThreadMessages is not fully supported for hydration/reactivity—let the client fetch again for now.
  • Optimistic UI for new messages is supported via optimisticallySendMessage, and you can redirect to the new chat/thread immediately after creation.

Let me know if you need code samples for a specific setup!

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@midnight ember Kapa.ai is still learning and improving, please let me know how I did by reacting below.