Custom layout based on URL
Answered
Alligator mississippiensis posted this in #help-forum
Alligator mississippiensisOP
I am trying to implement a dashboard in nextjs where the RootLayout will be different based on whether the user is visiting a route that starts with
Is there any better way of doing this?
/dashboard or not. In order to do this I would be needing a useRouter hook. But using this inside the RootLayout is problematic as I have to convert the RootLayout as a client component and loose the ability of using metadata API and SEO in general.Is there any better way of doing this?
Answered by chisto
you can play with the folders
instead of having the first 3 on /app/
you can make something like
app/(common)/
app/dashboard/
and put one layout on each?
instead of having the first 3 on /app/
you can make something like
app/(common)/
app/dashboard/
and put one layout on each?
33 Replies
which version of next are you using?
Alligator mississippiensisOP
14.2.13
why you need the useRouter?
Alligator mississippiensisOP
In order to see which URL I want to generate the layout for
I want to serve different layouts depending on whether the user is on
/dashboard or any other locationOkay here is my problem explained.
I have a website that has a landing page situated at
But when the user successfully logs in they are redirected to
I have a website that has a landing page situated at
/ there i want to show a navbar with the logo and 2 other items that navigate to /features and /auth. All 3 routes have a same layout of a navbar at top a footer at bottom and a section in between.But when the user successfully logs in they are redirected to
/dashboard now this dashboard and all nested routes have a different layout, they have a sidenavigation menu and a main section.In order to solve this I have created 2 separate layouts
CommonLayout & DashboardLayout and in my app/layout.tsx inside my Rootlayout I want to render any one of these based on the url. How should I achieve this?you can play with the folders
instead of having the first 3 on /app/
you can make something like
app/(common)/
app/dashboard/
and put one layout on each?
instead of having the first 3 on /app/
you can make something like
app/(common)/
app/dashboard/
and put one layout on each?
Answer
Alligator mississippiensisOP
But i still need to define the RootLayout right?
Also CommonLayout is what will be used for
/features, /community, /authyeah RootLayout can be like empty and just pass children
and the others be like
app
- (common)
features
page.tsx
community
page.tsx
auth
page.tsx
CommonLayout
- dashboard
DashboardLayout
pages etc
and the others be like
app
- (common)
features
page.tsx
community
page.tsx
auth
page.tsx
CommonLayout
- dashboard
DashboardLayout
pages etc
Alligator mississippiensisOP
Let me try this
Wait hold on
the layout is the same for
/ page, how do i define that?As in the
/ page use the Common Layoutyou make a folder on (common) called (home) and that would be the /
(common)
(home)
page.tsx
(common)
(home)
page.tsx
Alligator mississippiensisOP
My current directory structure is
app/
page.tsx
layout.tsx
auth/
layout.tsx
page.tsx
community/
layout.tsx
page.tsx
features/
layout.tsx
page.tsxMy
RootLayout will exist in app/layout.tsx and the correspondng page in app/page.tsxyou can move the app/page.tsx to app/(common)/(home)/page.tsx
and it will be / too
and it will be / too
Alligator mississippiensisOP
Just to be clear If i do a
that would work fine?
cp app/page.tsx app/common/home/page.tsxthat would work fine?
the group routes should have parenthesis, im not sure if that would work
Alligator mississippiensisOP
ahhh ok, so basically if i do
It should work?
cp app/page.tsx app/(common)/(home)/page.tsxIt should work?
you have to move it, not just copy it, cause you will have 2 routes /
Alligator mississippiensisOP
Agreed i was just confirming the file paths
also reading the route groups doc, this solves my problem exactly.
I guess i missed this part of the docs
let me try it
route groups are just to share layouts and the text on parenthesis are not shown on the url
should look something like this
solved?
Alligator mississippiensisOP
Yes this was the correct solution
can you mark it as solved?