Next.js Discord

Discord Forum

Sidebar Layout issue

Answered
Rex posted this in #help-forum
Open in Discord
RexOP
I have the following code root layout. The issue I am facing is for certain routes I don't want to show the sidebar and Aside component. To achieve thsi I manualy check if the url of the route matches with the one I don't to show the sidebar/aside on.
This approach somwhow works but I have issues for dynamic routes or nested routes
for example the I don't want to show Aside on the messages route if I /messages it works but if I created subroute eg /messages/[id] it doesn't work and for a big app it can get complex real quick
what can I do to have better structure?
const Sidebar = ({ className }: SidebarProps) => {
  const pathname = usePathname();
  const hideSidebarRoutes = ["/profile", "/verify", "/dashboard"];
  const shouldHideSidebar = hideSidebarRoutes.includes(pathname);
  if (shouldHideSidebar) return null;

    <div className=" flex flex-col items-center">
      <SiteHeader />
      <div
        className={cn(
          `min-h-[calc(100vh-0px)] pt-[60px] max-w-[1300px] w-full flex justify-center `
        )}
      >
        {user && <Sidebar />}
        <div className="flex-1 ">{children}</div>
        {user && <Aside />}
      </div>
    </div>
Answered by joulev
app/
  layout.tsx # remove Sidebar and Aside from here
  profile/page.tsx #/profile
  messages/page.tsx # /messages
  login/page.tsx # /login
  (main)/
    layout.tsx # add them to here
    page.tsx # /
View full answer

19 Replies

@joulev sh app/ layout.tsx # remove Sidebar and Aside from here (main)/ layout.tsx # add them to here ... # everything else login/ page.tsx # this page won't have Sidebar and Aside
RexOP
I have read about this but in my specific case how can I group them?
following routes
app/
  profile/ (don't want sidebar and aside)
    page.tsx
    layout.tsx
   messages/ (don't want aside)
     page.tsx
     layout.tsx 
   login/
      page.tsx
      layout.tsx
  page.tsx (should show sidebar and aside) (basically this page has twitter like feed) 
  layout.tsx
Answer
@Rex and what about the root `page.tsx`
remove it
RexOP
can I do that
damn
/ goes to app/(main)/page.tsx instead of app/page.tsx
RexOP
ayoo new information for me
I will try this and see how it works
Thanks juolev
yup ping me if it didn't work, i'll leave this thread for now
@joulev yup ping me if it didn't work, i'll leave this thread for now
RexOP
I have modified the structure like this
app/
  (main) 
     page.tsx
     layout.tsx
   profile/  (working as required) 
     page.tsx
     layout.tsx
   messages/ (only want the sidebar for messages)
    layout.tsx
    page.tsx 
layout.tsx

following is the (main) => layout.tsx code. If I move messages under the (main) then I get both sidebar and aside but I only want the sidebar. What do?

// layout component (main)
   <div className=" flex">
      {user && <Sidebar />}
        <div className="flex-1 ">{children}</div>
        {user && <Aside />}
   </div>

for profile and login it is working as expected
RexOP
YES I am able to solve the issue using this approach.