I want to create a specific layout for the /api/auth/*
routes that do not include the CustomNavbar
and Footer
components defined in the main RootLayout. Despite my efforts, the main layout still appears on these routes.
Here's my project structure within the app directory:
app/
├── (auth)/
│ ├── api/
│ │ ├── auth/
| | | ├── [...nextauth]
│ │ │ ├── layout.tsx // Auth-specific layout
│ │ │ ├── signin/
│ │ │ │ └── page.tsx
├── layout.tsx // Main layout
├── page.tsx // Home page
Even with these setups, the CustomNavbar
and Footer
components are still rendered on the /api/auth
routes. How can I ensure the RootLayout
is not applied to these specific routes, and only the auth layout is used?
What is the correct way to isolate layouts in Next.js using the App Router to achieve independent layouts for specific routes like /api/auth/*
?
layout.tsx
within the (auth)/api/auth
directory.useIsAuth
hook to check if the pathname starts with /api/auth
and conditionally render components.useIsAuth
Hook:
"use client";
import { usePathname } from "next/navigation";
export const useIsAuth = () => {
const pathname = usePathname();
return pathname.startsWith("/api/auth");
};
RootLayout
:
https://gist.github.com/Kumala3/96f57abb72497163d611f9a561046a32
I came up with an idea on how to implement an independent layout and it's working just perfectly.
Moreover, my app directory is better-structured thanks to the route groups.
My solution is to remove Footer
and Navbar
components from the RootLayout and wrap all public pages into users
route group. After doing this, we can create a custom layout for a specific route group, create layout.tsx
inside /app/(users)/*
, and add needed components there (Footer
, NavBar
). I also created layout.tsx
for all Auth pages, so now I am independent of the RootLayout
. In addition, added needed components to my main page.
The project structure looks like this now:
app/
├── (auth)/
│ ├── api/
│ │ ├── auth/
| | | ├── [...nextauth]
│ │ │ ├── signin/
│ │ │ │ └── page.tsx
│ ├── layout.tsx // Auth Layout specific for these pages
├── (users)/
│ ├── layout.tsx // Users Layout for all public pages
│ ├── ... // Other pages located here
├── layout.tsx // Root layout
├── page.tsx // Home page