Next.js Discord

Discord Forum

Migrating to next 14

Answered
Oriental Scops-Owl posted this in #help-forum
Open in Discord
Oriental Scops-OwlOP
Hi i am migrating to next 14. how can i get current URL pathname on server-side inside of layout.tsx ?

I need the layout to render conditionally based on which URL is being accessed
Answered by Ray
I think its better to use route group with it
app/layout.tsx <-- root layout

app/(protected)/layout.tsx <-- dashboard layout
app/(protected)/dashboard.tsx

app/(marketing)/layout.tsx <-- marketing layout
app/(marketing)/products/page.tsx
app/(marketing)/categories/page.tsx
View full answer

24 Replies

Oriental Scops-OwlOP
i know there is layout.ts in each app page folder, but the layouts get nested which is undesirable
@Oriental Scops-Owl Hi i am migrating to next 14. how can i get current URL pathname on server-side inside of layout.tsx ? I need the layout to render conditionally based on which URL is being accessed
I think its better to use route group with it
app/layout.tsx <-- root layout

app/(protected)/layout.tsx <-- dashboard layout
app/(protected)/dashboard.tsx

app/(marketing)/layout.tsx <-- marketing layout
app/(marketing)/products/page.tsx
app/(marketing)/categories/page.tsx
Answer
Oriental Scops-OwlOP
yeah.. the issue is the main layout.ts is a wrapped in all the children layout.ts
seems like every route i create i have to copy/paste similar code
for example, if i have app/specs and app/how-it-works page and they both use the same layout, i have to copy/paste the same layout.ts in each folder
(this is if i create a basic layout.ts file for the main app/ folder)
the root layout is shared on all the pages inside of app/ folder
Oriental Scops-OwlOP
if i put the main layout in app/layout.tsx, and all of my /dashboard routes use a completely different layout, it gets wrapped/nested under the main layout in app/layout.tsx
so i got the dashboard page loading 2 different layouts now since they are nested
the idea of nesting layouts is a poor design imo
Oriental Scops-OwlOP
gotcha
ty
I usually have a simple root layout then I have more control how the thing render
Oriental Scops-OwlOP
thanks. i got it working
do you have a URL to setting up meta data for each route?
i see this in the root layout file:

export const metadata: Metadata = {
  title: 'GrubStack',
  description: 'Food CRM and Order Processing',
}


but i need more control over each individual route
Oriental Scops-OwlOP
ty
im out for tonight. i got a good bit migrated. will be redoing my auth stuff tomorrow and just stuff it all into cookies instead of having a cookie and local storage object. thanks @Ray