I have created a NextJs app router to achieve the routes as follows:
/chat/conversation_id
->/chat/new
or/chat
= Open in full main layout/chat/conversation_id/history
->/chat/new/history
or/chat/id/history
= Open in the – side drawer along with chat/chat/conversation_id/files
->/chat/new/files
or/chat/id/files
= Open in the side drawer along with chat/chat/conversation_id/<etc>
= Open such routes in the side drawer along with chat/settings
= Open in full main layout
In the app router added the chat section as a slot and history and files as routable through the children in layout. With this when navigated to /chat/conversation_id/history
the side drawer is opening. On soft navigating back to /chat/conversation_id
the side drawer still shows the content of history. but on the hard navigation history drawer closes.
The layout file code: chat/[conversationId]/layout.tsx
export default function ChatLayout({
children,
chatbot
}: Readonly<{
children: React.ReactNode
chatbot: React.ReactNode
}>) {
// console.log("children", children)
return (
<div className="flex h-full flex-row">
{children}
<div className="flex-1">{chatbot}</div>
</div>
)
}
Even have tried to hide children as chat/[conversationId]/default.tsx
return null
Questions:
- Can something like this be achievable in an app router, If achievable please share samples or do I have to do a side drawer in the chat page and toggle the side drawer with query params?
- Can new chat be routed to
/chat
instead of/chat/new
. This I was doing because I can`t have empty before the history path/chat/<empty>/history
? - At the start of the chat I am trying to replace the path with
/chat/id
but if did so the page redirects or renders. Can we do a soft change of the URL without redirecting or re-rendering?
2
Answers
I was able to get this working.
Answers:
chat/[conversationId]/layout.tsx
to check the pathname if history or files are not in the path name hiding the children. Code:/chat/new
and/chat/id
window.history.pushState
to do shallow routing. NextJS doc referenceyou can use (history) in the directory because then the app will not understand your router
=>
….
enter link description here