Open littledino2112 opened 1 month ago
Someone also mentioned they run into this issue in here.
Facing the same issue right now and trying to create a work around
This sounds like an interesting issue.
Since saveChat is an async function, router.refresh() might not wait for the completion of the Promise.
Creating an API seems to be a good option.
For example:
Creating an api router in Nextjs as api/v1/
useEffect (() => {
function handleBeforeUnload {
<your code>
}
if (navigator){
navigator.sendBeacon("/api/v1/<name>", data);
}
window.addEventListener("beforeunload", handleBeforeUnload);
return () => {
window.removeEventListener("beforeunload", handleBeforeUnload);
};
},[your dependencies])
in prod, my user
sometimes can't be found, whereas it works ok on local. weird...
facing the same issue, still trying to find a workaround
@theitaliandev instead of using onSetAIState
, simply add your saveToDB()
logic right after the aiState.done(...)
i commented away the onSetAIState
all together. worked for me.
@terrytjw thank you!
Description
I'm following the AI chatbot example to develop my own and run into this issue. I'm using supabase's database and hosted the project on vercel. I noticed that the saveChat call sometimes would finish AFTER the new message is updated in aistate. For the chatbot example, It would make the chat_id page refresh and redirect to /chat since when router.refresh is called, the chat message isn't saved to db yet. I notice this happens a lot when I set connection_limit to 2 or 3 in my connection string to supabase.
Code example
This is the code in the Chat component where it refreshes the page when messagesLength equals 2
Additional context
Link to vercel ai chatbot example: https://github.com/vercel/ai-chatbot