I have a route /chat/[id], this route awaits data in when loaded in and also handles some logic if the chat is new or not. The logic inside the If searchParams block is causing too much delay before anything displays
Here is my file (cut some unnecessary code for post length):
type props = {
params:{
id: string
},
searchParams: { [key: string]: string | undefined }
}
export default async function Page(props: props) {
const messages = await drizzle query
if(props.searchParams["new"] && props.searchParams["message"]){
const messageId = props.searchParams["message"];
const req = await fetch("http://localhost:3000/api/message", {
method: "POST",
});
const res = await req.json();
if(res.success){
revalidatePath(`/chat/${props.params.id}`);
revalidatePath("/")
redirect(`/chat/${props.params.id}`);
}
}
async function sendMessage(formData: FormData) {
"use server"
const message = formData.get("message");
if (message) {
const req = await fetch("http://localhost:3000/api/message", {
method: "POST",
}
const data = await req.json();
if(data.success){
revalidatePath(`/chat/${props.params.id}`);
}
}
}
return(
<Messages messages={messages}/>
<form action={sendMessage}/>
)
}